GabrielSchultz commited on
Commit
3c507cd
·
verified ·
1 Parent(s): 26883af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -9
app.py CHANGED
@@ -32,17 +32,14 @@ def api_call(prompt, thread_id, model_name, chat_history):
32
  response = requests.post(url, headers=headers, json=data)
33
  return response.json()["message"]
34
 
35
- # Initialize the chat history
36
- chat_history = []
37
-
38
  # Gradio interface
39
  with gr.Blocks() as demo:
40
  model_name = gr.Dropdown(choices=["OpenAI", "Claude"], label="Select Model")
41
  chatbot = gr.Chatbot()
42
  msg = gr.Textbox(label="Your Message")
 
43
 
44
  def respond(message, model_name, chat_history):
45
- global chat_history
46
  # Append the user's message to the chat history immediately
47
  chat_history.append((message, None))
48
  # Get the bot's response
@@ -52,13 +49,12 @@ with gr.Blocks() as demo:
52
  return "", chat_history
53
 
54
  def reset_chat_history(model_name):
55
- global chat_history
56
- chat_history = []
57
- return [], chat_history
58
 
59
  # Link the reset function to the model_name dropdown
60
- model_name.change(fn=reset_chat_history, inputs=model_name, outputs=[chatbot, gr.State(chat_history)])
61
 
62
- msg.submit(fn=respond, inputs=[msg, model_name, gr.State(chat_history)], outputs=[msg, chatbot])
63
 
64
  demo.launch(share=True)
 
32
  response = requests.post(url, headers=headers, json=data)
33
  return response.json()["message"]
34
 
 
 
 
35
  # Gradio interface
36
  with gr.Blocks() as demo:
37
  model_name = gr.Dropdown(choices=["OpenAI", "Claude"], label="Select Model")
38
  chatbot = gr.Chatbot()
39
  msg = gr.Textbox(label="Your Message")
40
+ state = gr.State([]) # Initialize state for chat history
41
 
42
  def respond(message, model_name, chat_history):
 
43
  # Append the user's message to the chat history immediately
44
  chat_history.append((message, None))
45
  # Get the bot's response
 
49
  return "", chat_history
50
 
51
  def reset_chat_history(model_name):
52
+ # Reset the chat history when the model_name changes
53
+ return [], []
 
54
 
55
  # Link the reset function to the model_name dropdown
56
+ model_name.change(fn=reset_chat_history, inputs=model_name, outputs=[chatbot, state])
57
 
58
+ msg.submit(fn=respond, inputs=[msg, model_name, state], outputs=[msg, chatbot])
59
 
60
  demo.launch(share=True)