kakuguo commited on
Commit
d206d07
1 Parent(s): bbc49f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
app.py CHANGED
@@ -1,33 +1,27 @@
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()
8
  import gradio as gr
9
- from Conversation import Conversation
10
- prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
11
- 1. 你的回答必须是中文
12
- 2. 回答限制在100个字以内"""
13
-
14
- conv = Conversation(prompt, 10)
15
-
16
- def answer(question, history=[]):
17
- history.append(question)
18
- response = conv.ask(question)
19
- history.append(response)
20
- responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
21
- return responses, history
22
-
23
- with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as demo:
24
- chatbot = gr.Chatbot(elem_id="chatbot")
25
- state = gr.State([])
26
-
27
- with gr.Row():
28
- #txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
29
- txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
30
-
31
- txt.submit(answer, [txt, state], [chatbot, state])
32
 
33
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
 
 
 
 
 
 
2
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ openai.api_key = "sk-R3HlMsYBk0NpAlLu2aA4B19054Ea4884A2Cf93D25662243d" # Replace with your key
5
+ openai.api_base="https://apai.zyai.online/v1"
6
+
7
+ def predict(message, history):
8
+ history_openai_format = []
9
+ for human, assistant in history:
10
+ history_openai_format.append({"role": "user", "content": human })
11
+ history_openai_format.append({"role": "assistant", "content":assistant})
12
+ history_openai_format.append({"role": "user", "content": message})
13
+
14
+ response = openai.ChatCompletion.create(
15
+ model='gpt-3.5-turbo',
16
+ messages= history_openai_format,
17
+ temperature=1.0,
18
+ stream=True
19
+ )
20
+
21
+ partial_message = ""
22
+ for chunk in response:
23
+ if len(chunk['choices'][0]['delta']) != 0:
24
+ partial_message = partial_message + chunk['choices'][0]['delta']['content']
25
+ yield partial_message
26
+
27
+ gr.ChatInterface(predict).queue().launch()