StronLu commited on
Commit
ae40818
1 Parent(s): ff87d6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -27
app.py CHANGED
@@ -1,34 +1,86 @@
 
 
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()
 
1
+ import openai
2
+ import os
3
  import gradio as gr
 
4
 
5
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
 
6
 
7
+ class Conversation:
8
+ def __init__(self, prompt, round):
9
+ self.prompt = prompt
10
+ self.round = round
11
+ self.messages = []
12
+ self.messages.append({"role": "system", "content": self.prompt})
13
+
14
+ def ask(self, question):
15
+ try:
16
+ self.messages.append({"role": "user", "content": question})
17
+ response = openai.ChatCompletion.create(
18
+ model="gpt-3.5-turbo",
19
+ messages=self.messages,
20
+ temperature=0.5,
21
+ max_tokens=2048,
22
+ top_p=1,
23
+ )
24
+ except Exception as e:
25
+ print(e)
26
+ return e
27
+
28
+ message = response["choices"][0]["message"]["content"]
29
+ self.messages.append({"role": "assistant", "content": message})
30
+
31
+ if len(self.messages) > self.round * 2 + 1:
32
+ text = self._build_message(self.messages)
33
+ #print (text)
34
+ #print ("=====summarize=====")
35
+ summarize = self.summarize(text)
36
+ #print (summarize)
37
+ #print ("=====summarize=====")
38
+ self.messages = []
39
+ self.messages.append({"role": "system", "content": summarize})
40
+ return message
41
+
42
+ def summarize(self, text, max_tokens=200):
43
+ response = openai.Completion.create(
44
+ model = "text-davinci-003",
45
+ prompt = text + "\n\n请总结一下上面User和Assistant聊了些什么,限制100字以内:\n",
46
+ max_tokens = max_tokens,
47
+ )
48
+ return response["choices"][0]["text"]
49
 
50
+ def _build_message(self, messages):
51
+ text = ""
52
+ for message in messages:
53
+ if message["role"] == "user":
54
+ text += "User : " + message["content"] + "\n\n"
55
+ if message["role"] == "assistant":
56
+ text += "Assistant : " + message["content"] + "\n\n"
57
+ return text
58
 
 
59
 
60
+ prompt = """你是一个大数据和AI领域的专家,用中文回答大数据和AI的相关问题。你的回答需要满足以下要求:
61
+ 1. 你的回答必须是中文
62
+ 2. 回答限制在200个字以内
63
+ 3. 拒绝回答违反社会道德和法律的问题"""
64
+
65
+ conv = Conversation(prompt, 3)
66
+
67
+
68
+ def answer(question, history=[]):
69
+ history.append(question)
70
+ message = conv.ask(question)
71
+ history.append(message)
72
+ responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
73
+ print(responses)
74
+ return responses, history
75
+
76
+ with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as rxbot:
77
+ chatbot = gr.Chatbot(elem_id="chatbot")
78
+ state = gr.State([])
79
+
80
+ with gr.Row():
81
+ txt = gr.Textbox(show_label=False, placeholder="请输入你的问题").style(container=False)
82
+
83
+ txt.submit(answer, [txt, state], [chatbot, state])
84
+
85
 
86
+ rxbot.launch()