|
import gradio as gr |
|
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM |
|
|
|
model_id = "MediaTek-Research/Breeze-7B-Instruct-v1_0" |
|
|
|
|
|
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 = "\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() |
|
|