hectorjelly commited on
Commit
18e2b8f
1 Parent(s): 39ee2a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -26
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, 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
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', '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()
 
 
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