|
from transformers import pipeline |
|
import gradio as gr |
|
|
|
|
|
generator = pipeline( |
|
"text-generation", |
|
model="thu-coai/CDial-GPT2_LCCC-base", |
|
tokenizer="thu-coai/CDial-GPT2_LCCC-base", |
|
device=-1 |
|
) |
|
|
|
|
|
def chat_fn(message, history): |
|
history = history or [] |
|
|
|
|
|
prompt = "" |
|
for user_msg, bot_msg in history: |
|
prompt += f"你說:{user_msg}\nAI說:{bot_msg}\n" |
|
prompt += f"你說:{message}\nAI說:" |
|
|
|
|
|
output = generator(prompt, max_new_tokens=80, pad_token_id=0)[0]["generated_text"] |
|
|
|
|
|
response = output.split("AI說:")[-1].split("你說:")[-1].strip() |
|
|
|
|
|
history.append((message, response)) |
|
return history, history |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## 🧠 中文聊天機器人(記住上下文)") |
|
|
|
chatbot = gr.Chatbot(label="GPT2 中文對話") |
|
msg = gr.Textbox(show_label=False, placeholder="請輸入訊息,Enter 送出") |
|
clear = gr.Button("🧹 清除對話") |
|
|
|
state = gr.State([]) |
|
|
|
msg.submit(chat_fn, inputs=[msg, state], outputs=[chatbot, state]) |
|
clear.click(lambda: ([], []), outputs=[chatbot, state]) |
|
|
|
demo.launch() |
|
|