rr19tech commited on
Commit
2ae338e
·
verified ·
1 Parent(s): 50ee3a0

Added error handling

Browse files
Files changed (1) hide show
  1. app.py +15 -7
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
- response = requests.get(
23
- PRIVATE_API_URL,
24
- headers=headers,
25
- params={"text": user_input} # 'q' is the name FastAPI expects in the URL
26
- )
27
- print(response.status_code)
28
- return response.json()
 
 
 
 
 
 
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()