Spaces:
Sleeping
Sleeping
File size: 1,193 Bytes
cf42bd0 1dacd0d 81050d1 1dacd0d 81050d1 e4b71d4 cf42bd0 1dacd0d 6c7392f 1dacd0d 16585c4 1dacd0d 16585c4 cf42bd0 6c7392f cf42bd0 6c7392f cf42bd0 6c7392f cf42bd0 6c7392f f4821ef 6c7392f f4821ef 1dacd0d c263170 e4b71d4 cf42bd0 c263170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# app_phogpt4b_chat.py
import gradio as gr
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
# 2️⃣ Hàm chat handler
def respond(message, history):
# Gom lịch sử chat + tin nhắn mới vào prompt
prompt = ""
for user_msg, bot_msg in history:
prompt += f"Bạn: {user_msg}\nBot: {bot_msg}\n"
prompt += f"Bạn: {message}\nBot:"
# Sinh văn bản
outputs = pipe(
prompt,
max_new_tokens=100, # khoảng 60–80 từ
temperature=0.7,
top_p=0.9,
do_sample=True
)
generated = outputs[0]["generated_text"]
# Tách phần Bot trả lời
answer = generated.replace(prompt, "").strip()
# Cập nhật lịch sử và trả về
history.append((message, answer))
return history
# 3️⃣ Giao diện Gradio
demo = gr.ChatInterface(
fn=respond,
# Không thêm message_format hay type → dùng default tuple format
)
if __name__ == "__main__":
demo.launch()
|