Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
|
5 |
-
#
|
6 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
7 |
|
8 |
# Model endpoints on Hugging Face
|
@@ -14,20 +14,32 @@ MODEL_ENDPOINTS = {
|
|
14 |
|
15 |
def query_model(prompt, model_endpoint):
|
16 |
"""
|
17 |
-
Query a
|
18 |
-
|
19 |
-
we set openai.api_base to the model's endpoint.
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
messages
|
27 |
-
max_tokens
|
28 |
-
temperature
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
def chat_with_models(user_input, history):
|
33 |
responses = []
|
@@ -39,8 +51,7 @@ def chat_with_models(user_input, history):
|
|
39 |
return history, history
|
40 |
|
41 |
with gr.Blocks() as demo:
|
42 |
-
gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API
|
43 |
-
|
44 |
chatbot = gr.Chatbot()
|
45 |
msg = gr.Textbox(label="Your Message")
|
46 |
clear = gr.Button("Clear")
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
|
5 |
+
# Get the Hugging Face API key from Spaces secrets.
|
6 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
7 |
|
8 |
# Model endpoints on Hugging Face
|
|
|
14 |
|
15 |
def query_model(prompt, model_endpoint):
|
16 |
"""
|
17 |
+
Query a model via Hugging Face Inference API using a requests.post call.
|
18 |
+
This assumes an OpenAI-compatible endpoint structure.
|
|
|
19 |
"""
|
20 |
+
headers = {
|
21 |
+
"Authorization": f"Bearer {HF_API_KEY}",
|
22 |
+
"Content-Type": "application/json"
|
23 |
+
}
|
24 |
+
data = {
|
25 |
+
"messages": [{"role": "user", "content": prompt}],
|
26 |
+
"max_tokens": 512,
|
27 |
+
"temperature": 0.7
|
28 |
+
}
|
29 |
+
|
30 |
+
response = requests.post(model_endpoint, headers=headers, json=data)
|
31 |
+
try:
|
32 |
+
result = response.json()
|
33 |
+
except Exception:
|
34 |
+
return f"Error: Unable to parse JSON. Response: {response.text}"
|
35 |
+
|
36 |
+
if "error" in result:
|
37 |
+
return f"Error: {result['error']}"
|
38 |
+
|
39 |
+
try:
|
40 |
+
return result["choices"][0]["message"]["content"]
|
41 |
+
except Exception:
|
42 |
+
return f"Error: Unexpected response format: {result}"
|
43 |
|
44 |
def chat_with_models(user_input, history):
|
45 |
responses = []
|
|
|
51 |
return history, history
|
52 |
|
53 |
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("# Multi-LLM Chatbot using Hugging Face Inference API")
|
|
|
55 |
chatbot = gr.Chatbot()
|
56 |
msg = gr.Textbox(label="Your Message")
|
57 |
clear = gr.Button("Clear")
|