File size: 2,191 Bytes
82e5a99 b254b47 3f8ecf2 b254b47 3433ea2 769a56c b254b47 cad9a33 190b623 b254b47 f8e94b1 65eef27 b254b47 65eef27 b254b47 3f8ecf2 b254b47 14a13ab b254b47 65eef27 3433ea2 65eef27 b254b47 3f8ecf2 3c507cd 65eef27 3f8ecf2 65eef27 3f8ecf2 65eef27 b254b47 65eef27 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | import gradio as gr
import requests
import os
import random
api_key = os.getenv("apichatbotacess")
# Define the API calling function
def api_call(prompt, thread_id, model_name, chat_history):
url = "https://multimodalcompany-dev--talent-chatbot-beta-dev.modal.run"
headers = {
"key": api_key,
"Content-Type": "application/json"
}
data = {
"prompt": prompt,
"thread_id": thread_id,
"nameM": model_name,
"max_new_tokens": 4096,
"top_p": 0.95,
"temperature": 0.7,
"repetition_penalty": 1.15,
"do_sample": False,
"stream": False,
"user_id": "talent-demo-user",
"topic_id": "talent-chatbot",
"persist_data": True,
"max_chat_history_tokens": 2000,
"max_chat_history_days": 10,
"chat_history": chat_history # Include chat history
}
response = requests.post(url, headers=headers, json=data)
try:
return response.json()["message"]
except requests.exceptions.JSONDecodeError:
print("Error: Invalid JSON response")
return "Error: Invalid JSON response"
# Initialize the chat history
chat_history = []
# Gradio interface
with gr.Blocks() as demo:
model_name = gr.Dropdown(choices=["OpenAI", "Claude"], label="Select Model")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Your Message")
def respond(message, model_name):
global chat_history
# Make the API call with the current message
bot_message = api_call(message, random.randint(1, 10000), model_name, chat_history)
# Append the current message and response to chat history
chat_history.append((message, bot_message))
return "", chat_history
def reset_chat_history(model_name):
# Reset the chat history when the model_name changes
global chat_history
chat_history = []
return [], chat_history
# Link the reset function to the model_name dropdown
model_name.change(fn=reset_chat_history, inputs=model_name, outputs=[chatbot, gr.State(chat_history)])
msg.submit(fn=respond, inputs=[msg, model_name], outputs=[msg, chatbot])
demo.launch()
|