Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,25 +4,55 @@ import os
|
|
4 |
|
5 |
# Load API URL and token from environment variables
|
6 |
API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/your-model")
|
7 |
-
API_TOKEN = os.getenv("HF_API_TOKEN", "your-default-token") # Replace with your
|
8 |
|
9 |
# Function to call the Hugging Face Inference API
|
10 |
def call_huggingface_api(input_text):
|
11 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
12 |
payload = {"inputs": input_text}
|
13 |
try:
|
14 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
15 |
print(f"Request sent to: {API_URL}")
|
16 |
print(f"Payload: {payload}")
|
|
|
|
|
|
|
17 |
print(f"Response Status Code: {response.status_code}")
|
|
|
18 |
if response.status_code == 200:
|
19 |
data = response.json()
|
|
|
|
|
20 |
return f"Question: {input_text}\nAnswer: {data.get('answer', 'No answer found.')}\nConfidence: {data.get('confidence', 'N/A')}"
|
21 |
else:
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
except Exception as e:
|
25 |
-
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Gradio Interface
|
28 |
gr.Interface(
|
|
|
4 |
|
5 |
# Load API URL and token from environment variables
|
6 |
API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/your-model")
|
7 |
+
API_TOKEN = os.getenv("HF_API_TOKEN", "your-default-token") # Replace with your actual token for fallback
|
8 |
|
9 |
# Function to call the Hugging Face Inference API
|
10 |
def call_huggingface_api(input_text):
|
11 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
12 |
payload = {"inputs": input_text}
|
13 |
try:
|
|
|
14 |
print(f"Request sent to: {API_URL}")
|
15 |
print(f"Payload: {payload}")
|
16 |
+
|
17 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
+
|
19 |
print(f"Response Status Code: {response.status_code}")
|
20 |
+
|
21 |
if response.status_code == 200:
|
22 |
data = response.json()
|
23 |
+
print(f"Response Data: {data}")
|
24 |
+
# Assuming 'answer' and 'confidence' are the correct keys in the response JSON
|
25 |
return f"Question: {input_text}\nAnswer: {data.get('answer', 'No answer found.')}\nConfidence: {data.get('confidence', 'N/A')}"
|
26 |
else:
|
27 |
+
# Handle non-200 status codes and provide a detailed error message
|
28 |
+
error_message = f"Error: {response.status_code} - {response.text}"
|
29 |
+
print(f"Error Response: {error_message}")
|
30 |
+
return error_message
|
31 |
+
|
32 |
+
except requests.exceptions.RequestException as e:
|
33 |
+
# Catch network-related errors
|
34 |
+
error_message = f"Network error during API call: {e}"
|
35 |
+
print(error_message)
|
36 |
+
return error_message
|
37 |
+
|
38 |
+
except ValueError as e:
|
39 |
+
# Handle invalid JSON response
|
40 |
+
error_message = f"Error parsing response JSON: {e}"
|
41 |
+
print(error_message)
|
42 |
+
return error_message
|
43 |
+
|
44 |
+
except KeyError as e:
|
45 |
+
# Handle unexpected response structure or missing keys
|
46 |
+
error_message = f"KeyError: Missing expected key in response JSON: {e}"
|
47 |
+
print(error_message)
|
48 |
+
return error_message
|
49 |
+
|
50 |
except Exception as e:
|
51 |
+
# Handle any other unexpected errors
|
52 |
+
error_message = f"Unexpected error during API call: {e}"
|
53 |
+
print(error_message)
|
54 |
+
return error_message
|
55 |
+
|
56 |
|
57 |
# Gradio Interface
|
58 |
gr.Interface(
|