Update app.py
Browse files
app.py
CHANGED
|
@@ -12,10 +12,6 @@ def api_call(prompt, thread_id, model_name, chat_history):
|
|
| 12 |
"key": api_key,
|
| 13 |
"Content-Type": "application/json"
|
| 14 |
}
|
| 15 |
-
|
| 16 |
-
# Ensure chat_history is formatted as a list of strings
|
| 17 |
-
formatted_chat_history = [[str(item[0]), str(item[1])] if item[1] is not None else [str(item[0]), ""] for item in chat_history]
|
| 18 |
-
|
| 19 |
data = {
|
| 20 |
"prompt": prompt,
|
| 21 |
"thread_id": thread_id,
|
|
@@ -31,48 +27,41 @@ def api_call(prompt, thread_id, model_name, chat_history):
|
|
| 31 |
"persist_data": True,
|
| 32 |
"max_chat_history_tokens": 2000,
|
| 33 |
"max_chat_history_days": 10,
|
| 34 |
-
"chat_history":
|
| 35 |
}
|
| 36 |
-
|
| 37 |
response = requests.post(url, headers=headers, json=data)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return response.json().get("message", "Error: 'message' key not found in the response")
|
| 47 |
-
except ValueError:
|
| 48 |
-
print("Error parsing JSON response")
|
| 49 |
-
return "Error: Unable to parse the response"
|
| 50 |
-
else:
|
| 51 |
-
return f"Error: Received status code {response.status_code}"
|
| 52 |
|
| 53 |
# Gradio interface
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
model_name = gr.Dropdown(choices=["OpenAI", "Claude"], label="Select Model")
|
| 56 |
chatbot = gr.Chatbot()
|
| 57 |
msg = gr.Textbox(label="Your Message")
|
| 58 |
-
state = gr.State([]) # Initialize state for chat history
|
| 59 |
|
| 60 |
-
def respond(message, model_name
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Get the bot's response
|
| 64 |
bot_message = api_call(message, random.randint(1, 10000), model_name, chat_history)
|
| 65 |
-
#
|
| 66 |
-
chat_history
|
| 67 |
return "", chat_history
|
| 68 |
|
| 69 |
def reset_chat_history(model_name):
|
| 70 |
# Reset the chat history when the model_name changes
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Link the reset function to the model_name dropdown
|
| 74 |
-
model_name.change(fn=reset_chat_history, inputs=model_name, outputs=[chatbot,
|
| 75 |
|
| 76 |
-
msg.submit(fn=respond, inputs=[msg, model_name
|
| 77 |
|
| 78 |
-
demo.launch(
|
|
|
|
| 12 |
"key": api_key,
|
| 13 |
"Content-Type": "application/json"
|
| 14 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
data = {
|
| 16 |
"prompt": prompt,
|
| 17 |
"thread_id": thread_id,
|
|
|
|
| 27 |
"persist_data": True,
|
| 28 |
"max_chat_history_tokens": 2000,
|
| 29 |
"max_chat_history_days": 10,
|
| 30 |
+
"chat_history": chat_history # Include chat history
|
| 31 |
}
|
|
|
|
| 32 |
response = requests.post(url, headers=headers, json=data)
|
| 33 |
+
try:
|
| 34 |
+
return response.json()["message"]
|
| 35 |
+
except requests.exceptions.JSONDecodeError:
|
| 36 |
+
print("Error: Invalid JSON response")
|
| 37 |
+
return "Error: Invalid JSON response"
|
| 38 |
+
|
| 39 |
+
# Initialize the chat history
|
| 40 |
+
chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Gradio interface
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
model_name = gr.Dropdown(choices=["OpenAI", "Claude"], label="Select Model")
|
| 45 |
chatbot = gr.Chatbot()
|
| 46 |
msg = gr.Textbox(label="Your Message")
|
|
|
|
| 47 |
|
| 48 |
+
def respond(message, model_name):
|
| 49 |
+
global chat_history
|
| 50 |
+
# Make the API call with the current message
|
|
|
|
| 51 |
bot_message = api_call(message, random.randint(1, 10000), model_name, chat_history)
|
| 52 |
+
# Append the current message and response to chat history
|
| 53 |
+
chat_history.append((message, bot_message))
|
| 54 |
return "", chat_history
|
| 55 |
|
| 56 |
def reset_chat_history(model_name):
|
| 57 |
# Reset the chat history when the model_name changes
|
| 58 |
+
global chat_history
|
| 59 |
+
chat_history = []
|
| 60 |
+
return [], chat_history
|
| 61 |
|
| 62 |
# Link the reset function to the model_name dropdown
|
| 63 |
+
model_name.change(fn=reset_chat_history, inputs=model_name, outputs=[chatbot, gr.State(chat_history)])
|
| 64 |
|
| 65 |
+
msg.submit(fn=respond, inputs=[msg, model_name], outputs=[msg, chatbot])
|
| 66 |
|
| 67 |
+
demo.launch()
|