bot-chat / app.py
Jenny991's picture
新增聊天機器人 app 與依賴
f987894
raw
history blame
1.21 kB
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
model_id = "MediaTek-Research/Breeze-7B-Instruct-v1_0" # Breeze7 模型名稱(請確認 Huggingface 上正確名稱)
# 載入 tokenizer 和模型
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, trust_remote_code=True)
def chat_fn(user_input, chat_history):
chat_history = chat_history or []
chat_history.append(f"你: {user_input}")
# 取最近幾句聊天作為 prompt,避免太長
prompt = "\n".join(chat_history) + "\nAI:"
response = generator(prompt, max_new_tokens=100, temperature=0.7)
reply = response[0]['generated_text'][len(prompt):].strip()
chat_history.append(f"AI: {reply}")
return reply, chat_history
with gr.Blocks() as demo:
chat_history = gr.State([])
chatbot = gr.Chatbot()
user_input = gr.Textbox(show_label=False, placeholder="說點什麼...")
user_input.submit(chat_fn, inputs=[user_input, chat_history], outputs=[chatbot, chat_history])
demo.launch()