StronLu commited on
Commit
5754640
·
1 Parent(s): 7a1deae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import Conversation, pipeline
3
+
4
+ # 创建聊天机器人pipeline,使用你的 api key
5
+ chatbot_pipeline = pipeline("conversational", model="Llama-2-70b-chat-hf", use_auth_token="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")
6
+
7
+ def chatbot_response(input_text, history=[]):
8
+ # 添加新的用户输入到对话历史
9
+ history.append(input_text)
10
+
11
+ # 使用 Hugging Face's Conversation API 创建对话
12
+ conversation = Conversation(input_text)
13
+
14
+ # 获取模型的回答
15
+ responses = chatbot_pipeline([conversation])
16
+ model_response = responses[-1].generated_responses[-1]
17
+
18
+ # 添加模型的回答到对话历史
19
+ history.append(model_response)
20
+
21
+ # 创建用于显示在 Gradio UI 的聊天历史
22
+ chat_log = [(u,b) for u,b in zip(history[::2], history[1::2])]
23
+
24
+ return chat_log, history
25
+
26
+ # 创建 Gradio 接口
27
+ iface = gr.Interface(
28
+ fn=chatbot_response,
29
+ inputs=gr.inputs.Textbox(lines=2, placeholder='Type a message...'),
30
+ outputs=[gr.outputs.Textbox(label="Chat History"), gr.outputs.Textbox(label="Chat Log")]
31
+ )
32
+
33
+ # 启动 Gradio 应用
34
+ iface.launch()