Spaces:
Build error
Build error
hectorjelly
commited on
Commit
•
18e2b8f
1
Parent(s):
39ee2a2
Update app.py
Browse files
app.py
CHANGED
@@ -31,15 +31,12 @@ def setup_openai(api_key):
|
|
31 |
openai.api_key = api_key
|
32 |
return "API Key Set Successfully!"
|
33 |
|
34 |
-
def ask_joe(api_key,
|
35 |
# set up the api_key
|
36 |
setup_openai(api_key)
|
37 |
-
|
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
|
45 |
response = openai.ChatCompletion.create(
|
@@ -53,34 +50,25 @@ def ask_joe(api_key, chat_history):
|
|
53 |
model_message = response.choices[0].message['content'].strip()
|
54 |
|
55 |
# Add the model's message to the conversation history
|
56 |
-
conversation_history.append(
|
57 |
-
"role": "assistant",
|
58 |
-
"content": model_message
|
59 |
-
})
|
60 |
|
61 |
# Write the conversation history to a file
|
62 |
-
with open('conversation_history.txt', '
|
63 |
-
|
64 |
-
|
65 |
|
66 |
time.sleep(1) # sleep for a bit for better UI experience
|
67 |
-
return
|
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 |
-
|
84 |
-
|
|
|
85 |
|
86 |
-
|
|
|
|
31 |
openai.api_key = api_key
|
32 |
return "API Key Set Successfully!"
|
33 |
|
34 |
+
def ask_joe(api_key, message):
|
35 |
# set up the api_key
|
36 |
setup_openai(api_key)
|
37 |
+
|
38 |
# Add the user's message to the conversation history
|
39 |
+
conversation_history.append(["user", message])
|
|
|
|
|
|
|
40 |
|
41 |
# Use the conversation history as the input to the model
|
42 |
response = openai.ChatCompletion.create(
|
|
|
50 |
model_message = response.choices[0].message['content'].strip()
|
51 |
|
52 |
# Add the model's message to the conversation history
|
53 |
+
conversation_history.append(["assistant", model_message])
|
|
|
|
|
|
|
54 |
|
55 |
# Write the conversation history to a file
|
56 |
+
with open('conversation_history.txt', 'w') as f:
|
57 |
+
for role, content in conversation_history:
|
58 |
+
f.write(f'{role.capitalize()}: {content}\n')
|
59 |
|
60 |
time.sleep(1) # sleep for a bit for better UI experience
|
61 |
+
return model_message
|
62 |
|
63 |
def reset_conversation(api_key):
|
64 |
global conversation_history
|
65 |
# Reset the conversation history with initial instructions
|
66 |
conversation_history = [["system", initial_instructions["content"]]]
|
67 |
+
return "Conversation reset"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
api_key = gr.inputs.Textbox(label="OpenAI API Key")
|
70 |
+
message = gr.inputs.Textbox(label="Your question")
|
71 |
+
output_message = gr.outputs.Textbox(label="Joe's answer")
|
72 |
|
73 |
+
reset_interface = gr.Interface(fn=reset_conversation, inputs=api_key, outputs=output_message)
|
74 |
+
ask_joe_interface = gr.Interface(fn=ask_joe, inputs=[api
|