Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,30 +14,36 @@ MODEL_ENDPOINTS = {
|
|
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
|
19 |
"""
|
20 |
headers = {
|
21 |
"Authorization": f"Bearer {HF_API_KEY}",
|
22 |
"Content-Type": "application/json"
|
23 |
}
|
|
|
24 |
data = {
|
25 |
-
"
|
26 |
-
"
|
27 |
-
|
|
|
|
|
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 |
-
|
|
|
37 |
return f"Error: {result['error']}"
|
38 |
|
39 |
try:
|
40 |
-
|
|
|
41 |
except Exception:
|
42 |
return f"Error: Unexpected response format: {result}"
|
43 |
|
|
|
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 |
+
# Use 'inputs' instead of 'messages' and pass parameters inside the 'parameters' field.
|
25 |
data = {
|
26 |
+
"inputs": prompt,
|
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 there's an error message in the result, return it.
|
41 |
+
if isinstance(result, dict) and "error" in result:
|
42 |
return f"Error: {result['error']}"
|
43 |
|
44 |
try:
|
45 |
+
# Expected result format is a list of outputs with a "generated_text" field.
|
46 |
+
return result[0]["generated_text"]
|
47 |
except Exception:
|
48 |
return f"Error: Unexpected response format: {result}"
|
49 |
|