Download app.py from Talent-Multimodal/Talent_Chatbot_Beta: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/Talent-Multimodal/Talent_Chatbot_Beta/resolve/main/app.py
- Command line
-
hf download hf://spaces/Talent-Multimodal/Talent_Chatbot_Beta/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Talent-Multimodal/Talent_Chatbot_Beta/resolve/main/app.py
2.19 kB
| 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() | |