choupijiang / app.py
luminoussg's picture
Update app.py
518be16 verified
raw
history blame
2.17 kB
import os
import gradio as gr
import requests
# Get the Hugging Face API key from Spaces secrets.
HF_API_KEY = os.getenv("HF_API_KEY")
# Model endpoints on Hugging Face
MODEL_ENDPOINTS = {
"Qwen2.5-72B-Instruct": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct",
"Llama3.3-70B-Instruct": "https://api-inference.huggingface.co/models/meta-llama/Llama-3.3-70B-Instruct",
"Qwen2.5-Coder-32B-Instruct": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B-Instruct",
}
def query_model(prompt, model_endpoint):
"""
Query a model via Hugging Face Inference API using a requests.post call.
This assumes an OpenAI-compatible endpoint structure.
"""
headers = {
"Authorization": f"Bearer {HF_API_KEY}",
"Content-Type": "application/json"
}
data = {
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 512,
"temperature": 0.7
}
response = requests.post(model_endpoint, headers=headers, json=data)
try:
result = response.json()
except Exception:
return f"Error: Unable to parse JSON. Response: {response.text}"
if "error" in result:
return f"Error: {result['error']}"
try:
return result["choices"][0]["message"]["content"]
except Exception:
return f"Error: Unexpected response format: {result}"
def chat_with_models(user_input, history):
responses = []
for model_name, endpoint in MODEL_ENDPOINTS.items():
model_response = query_model(user_input, endpoint)
responses.append(f"**{model_name}**: {model_response}")
combined_answer = "\n\n".join(responses)
history.append((user_input, combined_answer))
return history, history
with gr.Blocks() as demo:
gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Your Message")
clear = gr.Button("Clear")
def clear_chat():
return [], []
msg.submit(fn=chat_with_models, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
clear.click(fn=clear_chat, outputs=[chatbot, chatbot])
demo.launch()