nagasurendra commited on
Commit
e406848
·
verified ·
1 Parent(s): bfbf75d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -18,10 +18,14 @@ HF_API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-5
18
 
19
  def query_hugging_face(image_data):
20
  if not HF_API_TOKEN:
21
- raise ValueError("Hugging Face API token not set. Please set the HF_API_TOKEN environment variable.")
22
- headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
23
- response = requests.post(HF_API_URL, headers=headers, data=image_data)
24
- return response.json()
 
 
 
 
25
 
26
  @app.route('/')
27
  def index():
 
18
 
19
  def query_hugging_face(image_data):
20
  if not HF_API_TOKEN:
21
+ return {"error": "Hugging Face API token not set. Please set the HF_API_TOKEN environment variable."}
22
+ try:
23
+ headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
24
+ response = requests.post(HF_API_URL, headers=headers, data=image_data, timeout=10)
25
+ response.raise_for_status() # Raise an error for bad status codes
26
+ return response.json()
27
+ except requests.exceptions.RequestException as e:
28
+ return {"error": f"Failed to connect to Hugging Face API: {str(e)}"}
29
 
30
  @app.route('/')
31
  def index():