GabrielSchultz commited on
Commit
3f8ecf2
·
verified ·
1 Parent(s): 190b623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import gradio as gr
2
  import requests
3
  import os
 
4
 
5
  api_key = os.getenv("apichatbotacess")
6
 
7
  # Define the API calling function
8
- def api_call(prompt, thread_id, model_name, chat_history):
9
  url = "https://multimodalcompany-dev--talent-chatbot-beta-dev-dev.modal.run"
10
  headers = {
11
  "key": api_key,
@@ -13,7 +14,7 @@ def api_call(prompt, thread_id, model_name, chat_history):
13
  }
14
  data = {
15
  "prompt": prompt,
16
- "thread_id": thread_id,
17
  "nameM": model_name,
18
  "max_new_tokens": 4096,
19
  "top_p": 0.95,
@@ -31,17 +32,29 @@ def api_call(prompt, thread_id, model_name, chat_history):
31
  response = requests.post(url, headers=headers, json=data)
32
  return response.json()["message"]
33
 
 
 
 
 
34
  with gr.Blocks() as demo:
35
  model_name = gr.Dropdown(choices=["OpenAI", "Claude"], label="Select Model")
36
- thread_id_input = gr.Textbox(label="Enter Thread ID", placeholder="Thread ID", lines=1)
37
  chatbot = gr.Chatbot()
38
  msg = gr.Textbox(label="Your Message")
39
 
40
- def respond(message, thread_id, chat_history, model_name):
41
- bot_message = api_call(message, thread_id, model_name, chat_history)
42
  chat_history.append((message, bot_message))
43
  return "", chat_history
44
 
45
- msg.submit(respond, [msg, thread_id_input, gr.State([]), model_name], [msg, chatbot])
 
 
 
 
 
 
 
 
 
46
 
47
- demo.launch()
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ import random
5
 
6
  api_key = os.getenv("apichatbotacess")
7
 
8
  # Define the API calling function
9
+ def api_call(prompt, model_name, chat_history):
10
  url = "https://multimodalcompany-dev--talent-chatbot-beta-dev-dev.modal.run"
11
  headers = {
12
  "key": api_key,
 
14
  }
15
  data = {
16
  "prompt": prompt,
17
+ "thread_id": random.randint(1, 10000), # Use a random number for thread_id
18
  "nameM": model_name,
19
  "max_new_tokens": 4096,
20
  "top_p": 0.95,
 
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
+ bot_message = api_call(message, model_name, chat_history)
46
  chat_history.append((message, bot_message))
47
  return "", chat_history
48
 
49
+ def reset_chat_history(model_name):
50
+ # Reset the chat history when the model_name changes
51
+ global chat_history
52
+ chat_history = []
53
+ return chat_history, []
54
+
55
+ # Link the reset function to the model_name dropdown
56
+ model_name.change(reset_chat_history, inputs=model_name, outputs=[chatbot, gr.State(chat_history)])
57
+
58
+ msg.submit(respond, [msg, model_name, gr.State(chat_history)], [msg, chatbot])
59
 
60
+ demo.launch()