Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
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")
|
@@ -13,39 +14,39 @@ MODEL_ENDPOINTS = {
|
|
13 |
}
|
14 |
|
15 |
def query_model(prompt, model_endpoint):
|
16 |
-
"""
|
17 |
-
Query a model via the Hugging Face Inference API using a requests.post call.
|
18 |
-
This payload now uses the 'inputs' field as required.
|
19 |
-
"""
|
20 |
headers = {
|
21 |
"Authorization": f"Bearer {HF_API_KEY}",
|
22 |
-
"Content-Type": "application/json"
|
|
|
23 |
}
|
24 |
-
#
|
|
|
25 |
data = {
|
26 |
-
"inputs":
|
27 |
"parameters": {
|
28 |
"max_new_tokens": 512,
|
29 |
"temperature": 0.7,
|
30 |
}
|
31 |
}
|
32 |
-
|
33 |
response = requests.post(model_endpoint, headers=headers, json=data)
|
34 |
|
|
|
|
|
|
|
35 |
try:
|
36 |
result = response.json()
|
37 |
except Exception:
|
38 |
return f"Error: Unable to parse JSON. Response: {response.text}"
|
39 |
|
40 |
-
# If
|
41 |
if isinstance(result, dict) and "error" in result:
|
42 |
return f"Error: {result['error']}"
|
43 |
|
44 |
try:
|
45 |
-
#
|
46 |
-
return result[0]
|
47 |
except Exception:
|
48 |
-
return f"Error: Unexpected response format: {result}"
|
49 |
|
50 |
def chat_with_models(user_input, history):
|
51 |
responses = []
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
+
import json
|
5 |
|
6 |
# Get the Hugging Face API key from Spaces secrets.
|
7 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
|
|
14 |
}
|
15 |
|
16 |
def query_model(prompt, model_endpoint):
|
|
|
|
|
|
|
|
|
17 |
headers = {
|
18 |
"Authorization": f"Bearer {HF_API_KEY}",
|
19 |
+
"Content-Type": "application/json",
|
20 |
+
"Accept": "application/json"
|
21 |
}
|
22 |
+
# Format prompt for instruct-style generation
|
23 |
+
formatted_prompt = f"User: {prompt}\nAssistant:"
|
24 |
data = {
|
25 |
+
"inputs": formatted_prompt,
|
26 |
"parameters": {
|
27 |
"max_new_tokens": 512,
|
28 |
"temperature": 0.7,
|
29 |
}
|
30 |
}
|
|
|
31 |
response = requests.post(model_endpoint, headers=headers, json=data)
|
32 |
|
33 |
+
# For debugging purposes, you can uncomment the following line:
|
34 |
+
# print("Raw response:", response.text)
|
35 |
+
|
36 |
try:
|
37 |
result = response.json()
|
38 |
except Exception:
|
39 |
return f"Error: Unable to parse JSON. Response: {response.text}"
|
40 |
|
41 |
+
# If the API returns an error message, surface it.
|
42 |
if isinstance(result, dict) and "error" in result:
|
43 |
return f"Error: {result['error']}"
|
44 |
|
45 |
try:
|
46 |
+
# Expecting a list of outputs with a "generated_text" field.
|
47 |
+
return result[0].get("generated_text", "No generated_text found in response")
|
48 |
except Exception:
|
49 |
+
return f"Error: Unexpected response format: {json.dumps(result)}"
|
50 |
|
51 |
def chat_with_models(user_input, history):
|
52 |
responses = []
|