thanglekdi's picture
test xíu
81050d1
raw
history blame
1.19 kB
# 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()