昭鱼 commited on
Commit
5448374
·
1 Parent(s): fdda6f6
Files changed (2) hide show
  1. app.py +66 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
+
5
+ client = OpenAI(
6
+ api_key=os.getenv("OPEN_AI_KEY"),
7
+ base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
8
+ )
9
+
10
+
11
+ class Conversation:
12
+ def __init__(self, prompt, num_of_round):
13
+ self.prompt = prompt
14
+ self.num_of_round = num_of_round
15
+ self.messages = []
16
+ self.messages.append({"role": "system", "content": self.prompt})
17
+
18
+ def ask(self, question):
19
+ try:
20
+ self.messages.append({"role": "user", "content": question})
21
+ response = client.chat.completions.create(
22
+ model="qwen-plus",
23
+ stop=None,
24
+ temperature=0.5,
25
+ n=1,
26
+ messages=self.messages,
27
+ top_p=1,
28
+ )
29
+ except Exception as e:
30
+ print(e)
31
+ return e
32
+
33
+ message = response.choices[0].message.content.strip()
34
+ self.messages.append({"role": "assistant", "content": message})
35
+
36
+ if len(self.messages) > self.num_of_round * 2 + 1:
37
+ del self.messages[1:3]
38
+
39
+ return message
40
+
41
+
42
+ prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
43
+ 1. 你的回答必须是中文
44
+ 2. 回答限制在100个字以内"""
45
+
46
+ conv = Conversation(prompt, 10)
47
+
48
+
49
+ def answer(question, history=[]):
50
+ history.append(question)
51
+ response = conv.ask(question)
52
+ history.append(response)
53
+ responses = [(u, b) for u, b in zip(history[::2], history[1::2])]
54
+ return responses, history
55
+
56
+
57
+ with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo:
58
+ chatbot = gr.Chatbot(elem_id="chatbot")
59
+ state = gr.State([])
60
+
61
+ with gr.Row():
62
+ txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
63
+
64
+ txt.submit(answer, [txt, state], [chatbot, state])
65
+
66
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ conda-forge
2
+ gradio