Spaces:
Runtime error
Runtime error
Commit
·
cc82464
1
Parent(s):
a380611
Update app.py
Browse files
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 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|