Added error handling
Browse files
app.py
CHANGED
|
@@ -17,15 +17,23 @@ def call_private_api(user_input):
|
|
| 17 |
"Content-Type": "text/plain"
|
| 18 |
#"Content-Type": "application/json"
|
| 19 |
}
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# We take that string and put it into a GET request
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Simple UI to test it
|
| 31 |
gr.Interface(fn=call_private_api, inputs="text", outputs="json").launch()
|
|
|
|
| 17 |
"Content-Type": "text/plain"
|
| 18 |
#"Content-Type": "application/json"
|
| 19 |
}
|
| 20 |
+
|
| 21 |
+
print(f"User input is: {user_input}")
|
| 22 |
|
| 23 |
# We take that string and put it into a GET request
|
| 24 |
+
try:
|
| 25 |
+
response = requests.get(
|
| 26 |
+
PRIVATE_API_URL,
|
| 27 |
+
headers=headers,
|
| 28 |
+
params={"text": user_input},
|
| 29 |
+
timeout=300,
|
| 30 |
+
)
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
return response.json()
|
| 33 |
+
else:
|
| 34 |
+
return {"error": f"Status {response.status_code}", "detail": response.text}
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return {"error": f"Connection failed: {str(e)}"}
|
| 37 |
|
| 38 |
# Simple UI to test it
|
| 39 |
gr.Interface(fn=call_private_api, inputs="text", outputs="json").launch()
|