Flintoff commited on
Commit
4340cbc
1 Parent(s): 4d4a50c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -4
app.py CHANGED
@@ -1,7 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello" + name +"!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Search models, datasets, users...
4
+ Models
5
+ Datasets
6
+ Spaces
7
+ Docs
8
+ Solutions
9
+ Pricing
10
+
11
+
12
+
13
+ Spaces:
14
+
15
+ xuwenhao83
16
+ /
17
+ simple_chatbot Copied
18
+ like
19
+ 1
20
+ App
21
+ Files and versions
22
+ Community
23
+ simple_chatbot
24
+ /
25
+ app.py
26
+ Stanley Xu
27
+ simple chatbot app
28
+ 3dcf060
29
+ 29 days ago
30
+ raw
31
+ history
32
+ blame
33
+ contribute
34
+ delete
35
+ No virus
36
+ 1.81 kB
37
+ import openai
38
+ import os
39
  import gradio as gr
40
 
41
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
42
+
43
+ class Conversation:
44
+ def __init__(self, prompt, num_of_round):
45
+ self.prompt = prompt
46
+ self.num_of_round = num_of_round
47
+ self.messages = []
48
+ self.messages.append({"role": "system", "content": self.prompt})
49
+
50
+ def ask(self, question):
51
+ try:
52
+ self.messages.append( {"role": "user", "content": question})
53
+ response = openai.ChatCompletion.create(
54
+ model="gpt-3.5-turbo",
55
+ messages=self.messages,
56
+ temperature=0.5,
57
+ max_tokens=2048,
58
+ top_p=1,
59
+ )
60
+ except Exception as e:
61
+ print(e)
62
+ return e
63
+
64
+ message = response["choices"][0]["message"]["content"]
65
+ self.messages.append({"role": "assistant", "content": message})
66
+
67
+ if len(self.messages) > self.num_of_round*2 + 1:
68
+ del self.messages[1:3]
69
+ return message
70
+
71
+
72
+ prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
73
+ 1. 你的回答必须是中文
74
+ 2. 回答限制在100个字以内"""
75
+
76
+ conv = Conversation(prompt, 5)
77
+
78
+ def predict(input, history=[]):
79
+ history.append(input)
80
+ response = conv.ask(input)
81
+ history.append(response)
82
+ responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
83
+ return responses, history
84
+
85
+ with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:500px}") as demo:
86
+ chatbot = gr.Chatbot(elem_id="chatbot")
87
+ state = gr.State([])
88
+
89
+ with gr.Row():
90
+ txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
91
+
92
+ txt.submit(predict, [txt, state], [chatbot, state])
93
 
94
+ demo.launch()