TronSpace / app.py
StronLu's picture
Create app.py
5754640
raw
history blame
No virus
1.14 kB
import gradio as gr
from transformers import Conversation, pipeline
# 创建聊天机器人pipeline,使用你的 api key
chatbot_pipeline = pipeline("conversational", model="Llama-2-70b-chat-hf", use_auth_token="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")
def chatbot_response(input_text, history=[]):
# 添加新的用户输入到对话历史
history.append(input_text)
# 使用 Hugging Face's Conversation API 创建对话
conversation = Conversation(input_text)
# 获取模型的回答
responses = chatbot_pipeline([conversation])
model_response = responses[-1].generated_responses[-1]
# 添加模型的回答到对话历史
history.append(model_response)
# 创建用于显示在 Gradio UI 的聊天历史
chat_log = [(u,b) for u,b in zip(history[::2], history[1::2])]
return chat_log, history
# 创建 Gradio 接口
iface = gr.Interface(
fn=chatbot_response,
inputs=gr.inputs.Textbox(lines=2, placeholder='Type a message...'),
outputs=[gr.outputs.Textbox(label="Chat History"), gr.outputs.Textbox(label="Chat Log")]
)
# 启动 Gradio 应用
iface.launch()