huohguohbo commited on
Commit
cc82464
·
1 Parent(s): a380611

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import os
2
  import gradio as gr
3
  import openai
4
 
@@ -16,14 +15,27 @@ def chatbot(prompt, api_key):
16
  message = response.choices[0].text.strip()
17
  return message
18
 
19
- input_prompt = gr.inputs.Textbox(lines=7, label="Enter your message")
20
- input_api_key = gr.inputs.Textbox(label="Enter your OpenAI API key")
 
21
 
22
- output_text = gr.outputs.Textbox(label="Bot's reply")
 
 
 
 
 
23
 
24
- title = "OpenAI Chatbot"
25
- description = "An AI chatbot powered by OpenAI's GPT-3 language model."
26
- examples = [["Hi there!", "Hello! How can I assist you today?"]]
27
- iface = gr.Interface(fn=chatbot, inputs=[input_prompt, input_api_key], outputs=output_text, title=title, description=description, examples=examples)
28
 
29
- iface.launch()
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import openai
3
 
 
15
  message = response.choices[0].text.strip()
16
  return message
17
 
18
+ def chat_interface():
19
+ chat_history = []
20
+ chat_history.append("Bot: Hi there! How can I assist you today?")
21
 
22
+ def chat(api_key, reply):
23
+ chat_history.append("You: " + reply)
24
+ prompt = "\n".join(chat_history[-7:])
25
+ bot_response = chatbot(prompt, api_key)
26
+ chat_history.append("Bot: " + bot_response)
27
+ return "\n".join(chat_history[-14:])
28
 
29
+ input_api_key = gr.inputs.Textbox(label="Enter your OpenAI API key")
30
+ input_text = gr.inputs.Textbox(lines=7, label="Enter your message")
31
+ output_text = gr.outputs.Textbox(label="Bot's reply")
32
+ chat_button = gr.outputs.Button(label="Send")
33
 
34
+ iface = gr.Interface(fn=chat, inputs=[input_api_key, input_text], outputs=output_text, title="OpenAI Chatbot", description="An AI chatbot powered by OpenAI's GPT-3 language model.", examples=[["Your OpenAI API key", "Hello"]], allow_flagging=False, layout="vertical", live=False, theme="compact")
35
+
36
+ chat_button.js_on_click(args=dict(output=iface.outputs[0]), code="""output.emit(this.value)""")
37
+
38
+ return iface, chat_button
39
+
40
+ chat_interface(), chat_button = chat_interface()
41
+ chat_button