Spaces:
Build error
Build error
Commit
·
39ee2a2
1
Parent(s):
d7c892b
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
|
|
3 |
|
4 |
# Initial instructions for the assistant
|
5 |
initial_instructions = {
|
@@ -24,22 +25,20 @@ initial_instructions = {
|
|
24 |
}
|
25 |
|
26 |
# Initialize the conversation history with initial instructions
|
27 |
-
conversation_history = [initial_instructions]
|
28 |
|
29 |
def setup_openai(api_key):
|
30 |
openai.api_key = api_key
|
31 |
return "API Key Set Successfully!"
|
32 |
|
33 |
-
def ask_joe(api_key,
|
34 |
-
global conversation_history
|
35 |
-
|
36 |
# set up the api_key
|
37 |
setup_openai(api_key)
|
38 |
-
|
39 |
# Add the user's message to the conversation history
|
40 |
conversation_history.append({
|
41 |
"role": "user",
|
42 |
-
"content":
|
43 |
})
|
44 |
|
45 |
# Use the conversation history as the input to the model
|
@@ -61,25 +60,27 @@ def ask_joe(api_key, text):
|
|
61 |
|
62 |
# Write the conversation history to a file
|
63 |
with open('conversation_history.txt', 'a') as f:
|
64 |
-
f.write(f'User: {
|
65 |
f.write(f'AI: {model_message}\n')
|
66 |
-
|
67 |
-
|
|
|
68 |
|
69 |
def reset_conversation(api_key):
|
70 |
global conversation_history
|
71 |
# Reset the conversation history with initial instructions
|
72 |
-
conversation_history = [initial_instructions]
|
73 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
iface_chat = gr.Interface(fn=ask_joe,
|
77 |
-
inputs=[gr.inputs.Textbox(label="OpenAI API Key"), inputs_chat],
|
78 |
-
outputs=gr.outputs.Textbox(label="Joe's Response"))
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
outputs=gr.outputs.Textbox(label="Reset Status"))
|
83 |
|
84 |
-
|
85 |
-
iface_reset.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
+
import time
|
4 |
|
5 |
# Initial instructions for the assistant
|
6 |
initial_instructions = {
|
|
|
25 |
}
|
26 |
|
27 |
# Initialize the conversation history with initial instructions
|
28 |
+
conversation_history = [["system", initial_instructions["content"]]]
|
29 |
|
30 |
def setup_openai(api_key):
|
31 |
openai.api_key = api_key
|
32 |
return "API Key Set Successfully!"
|
33 |
|
34 |
+
def ask_joe(api_key, chat_history):
|
|
|
|
|
35 |
# set up the api_key
|
36 |
setup_openai(api_key)
|
37 |
+
message = chat_history[-1][0] # get the last user message
|
38 |
# Add the user's message to the conversation history
|
39 |
conversation_history.append({
|
40 |
"role": "user",
|
41 |
+
"content": message
|
42 |
})
|
43 |
|
44 |
# Use the conversation history as the input to the model
|
|
|
60 |
|
61 |
# Write the conversation history to a file
|
62 |
with open('conversation_history.txt', 'a') as f:
|
63 |
+
f.write(f'User: {message}\n')
|
64 |
f.write(f'AI: {model_message}\n')
|
65 |
+
|
66 |
+
time.sleep(1) # sleep for a bit for better UI experience
|
67 |
+
return chat_history + [[message, model_message]]
|
68 |
|
69 |
def reset_conversation(api_key):
|
70 |
global conversation_history
|
71 |
# Reset the conversation history with initial instructions
|
72 |
+
conversation_history = [["system", initial_instructions["content"]]]
|
73 |
+
return []
|
74 |
+
|
75 |
+
with gr.Blocks() as demo:
|
76 |
+
chatbot = gr.Chatbot()
|
77 |
+
api_key = gr.Textbox(default="your-openai-api-key", label="OpenAI API Key")
|
78 |
+
|
79 |
+
api_key.submit(setup_openai, outputs=gr.Text(label="API Key Status"))
|
80 |
|
81 |
+
chatbot.submit(ask_joe, inputs=[api_key, chatbot], outputs=chatbot)
|
|
|
|
|
|
|
82 |
|
83 |
+
reset_button = gr.Button("Reset Conversation")
|
84 |
+
reset_button.click(reset_conversation, inputs=api_key, outputs=chatbot, queue=False)
|
|
|
85 |
|
86 |
+
demo.launch()
|
|