nagasurendra commited on
Commit
efbda21
·
verified ·
1 Parent(s): d195b94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -2
app.py CHANGED
@@ -4,6 +4,12 @@ import base64
4
  import requests
5
  from PIL import Image
6
  from io import BytesIO
 
 
 
 
 
 
7
 
8
  app = Flask(__name__)
9
 
@@ -18,13 +24,21 @@ 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
  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('/')
@@ -57,6 +71,17 @@ def capture():
57
  'hf_result': hf_result
58
  })
59
  except Exception as e:
 
 
 
 
 
 
 
 
 
 
 
60
  return jsonify({'status': 'error', 'message': str(e)})
61
 
62
  if __name__ == '__main__':
 
4
  import requests
5
  from PIL import Image
6
  from io import BytesIO
7
+ import logging
8
+ from requests.adapters import HTTPAdapter
9
+ from urllib3.util.retry import Retry
10
+
11
+ # Configure logging
12
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
13
 
14
  app = Flask(__name__)
15
 
 
24
 
25
  def query_hugging_face(image_data):
26
  if not HF_API_TOKEN:
27
+ logging.error("Hugging Face API token not set.")
28
  return {"error": "Hugging Face API token not set. Please set the HF_API_TOKEN environment variable."}
29
  try:
30
+ session = requests.Session()
31
+ # Configure retries: 3 attempts, backoff factor of 1s, retry on 502/503/504
32
+ retries = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504])
33
+ session.mount('https://', HTTPAdapter(max_retries=retries))
34
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
35
+ logging.debug(f"Sending request to {HF_API_URL}")
36
+ response = session.post(HF_API_URL, headers=headers, data=image_data, timeout=10)
37
+ response.raise_for_status()
38
+ logging.debug("Hugging Face API request successful.")
39
  return response.json()
40
  except requests.exceptions.RequestException as e:
41
+ logging.error(f"Failed to connect to Hugging Face API: {str(e)}")
42
  return {"error": f"Failed to connect to Hugging Face API: {str(e)}"}
43
 
44
  @app.route('/')
 
71
  'hf_result': hf_result
72
  })
73
  except Exception as e:
74
+ logging.error(f"Error in capture: {str(e)}")
75
+ return jsonify({'status': 'error', 'message': str(e)})
76
+
77
+ @app.route('/test_connectivity')
78
+ def test_connectivity():
79
+ try:
80
+ response = requests.get('https://www.google.com', timeout=5)
81
+ logging.debug(f"Connectivity test to Google: {response.status_code}")
82
+ return jsonify({'status': 'success', 'code': response.status_code})
83
+ except requests.exceptions.RequestException as e:
84
+ logging.error(f"Connectivity test failed: {str(e)}")
85
  return jsonify({'status': 'error', 'message': str(e)})
86
 
87
  if __name__ == '__main__':