Spaces:
Build error
Build error
Update app.py
Browse files3 unmodified lines4567898 unmodified lines1819202122232425262728293026 unmodified lines5758596061623 unmodified linesimport requestsfrom PIL import Imagefrom io import BytesIOapp = Flask(__name__)8 unmodified linesdef query_hugging_face(image_data):if not HF_API_TOKEN:return {"error": "Hugging Face API token not set. Please set the HF_API_TOKEN environment variable."}try:headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}response = requests.post(HF_API_URL, headers=headers, data=image_data, timeout=10)response.raise_for_status() # Raise an error for bad status codesreturn response.json()except requests.exceptions.RequestException as e:return {"error": f"Failed to connect to Hugging Face API: {str(e)}"}@app.route('/')26 unmodified lines'hf_result': hf_result})except Exception as e:return jsonify({'status': 'error', 'message': str(e)})if __name__ == '__main__':3 unmodified lines4567891011121314158 unmodified lines24252627282930313233343536373839404142434426 unmodified lines71727374757677787980818283848586873 unmodified linesimport requestsfrom PIL import Imagefrom io import BytesIOimport loggingfrom requests.adapters import HTTPAdapterfrom urllib3.util.retry import Retry# Configure logginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')app = Flask(__name__)8 unmodified linesdef query_hugging_face(image_data):if not HF_API_TOKEN:logging.error("Hugging Face API token not set.")return {"error": "Hugging Face API token not set. Please set the HF_API_TOKEN environment variable."}try:session = requests.Session()# Configure retries: 3 attempts, backoff factor of 1s, retry on 502/503/504retries = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504])session.mount('https://', HTTPAdapter(max_retries=retries))headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}logging.debug(f"Sending request to {HF_API_URL}")response = session.post(HF_API_URL, headers=headers, data=image_data, timeout=10)response.raise_for_status()logging.debug("Hugging Face API request successful.")return response.json()except requests.exceptions.RequestException as e:logging.error(f"Failed to connect to Hugging Face API: {str(e)}")return {"error": f"Failed to connect to Hugging Face API: {str(e)}"}@app.route('/')26 unmodified lines'hf_result': hf_result})except Exception as e:logging.error(f"Error in capture: {str(e)}")return jsonify({'status': 'error', 'message': str(e)})@app.route('/test_connectivity')def test_connectivity():try:response = requests.get('https://www.google.com', timeout=5)logging.debug(f"Connectivity test to Google: {response.status_code}")return jsonify({'status': 'success', 'code': response.status_code})except requests.exceptions.RequestException as e:logging.error(f"Connectivity test failed: {str(e)}")return jsonify({'status': 'error', 'message': str(e)})if __name__ == '__main__':
app.py+27-2