Fix build error
Browse files21 unmodified lines22232425262728293031323334352 unmodified lines383940414243445 unmodified lines505152535455565739 unmodified lines9798991001011021038 unmodified lines11211311411511611711816 unmodified lines1351361371381391401417 unmodified lines1491501511521531541556 unmodified lines1621631641651661671686 unmodified lines17517617717817918018121 unmodified linesDEV_URL = "https://PPAL-SongLab-UGA-fruit-analyzer-dev.hf.space"PROD_URL = "https://PPAL-SongLab-UGA-fruit-analyzer.hf.space"HF_RATE_LIMIT_STATUS = 429def request_with_429_backoff(method, url, *, max_retries=3, **kwargs):last_response = Nonefor attempt in range(max_retries + 1):response = method(url, **kwargs)last_response = responseif response.status_code != HF_RATE_LIMIT_STATUS:return responseretry_after = response.headers.get("retry-after")2 unmodified linesexcept ValueError:wait_s = Noneif wait_s is None:wait_s = [2.0, 5.0, 10.0, 15.0][min(attempt, 3)]if attempt < max_retries:time.sleep(min(max(wait_s, 0.5), 20.0))return last_response5 unmodified linesretry_after = response.headers.get("retry-after")suffix = f" Retry after about {retry_after} seconds." if retry_after else " Please wait a minute and try again."return f"Hugging Face is rate limiting this Space after several retry attempts.{suffix}"return str(exc)@app.get("/")def read_root():return {"status": "Render Proxy is awake!"}39 unmodified lines# FIX: Define the blocking requestdef make_post():return request_with_429_backoff(requests.post,target_url,headers=headers,8 unmodified linesresponse.raise_for_status()return response.json()except requests.exceptions.RequestException as e:return {"success": False, "message": f"Proxy Error (Hugging Face): {hf_error_message(e)}"}except Exception as e:return {"success": False, "message": f"Proxy Error (Internal): {str(e)}"}16 unmodified linesdata.setdefault("username", username)def make_post():return request_with_429_backoff(requests.post,target_url,headers=headers,7 unmodified linesresponse.raise_for_status()return response.json()except requests.exceptions.RequestException as e:return {"success": False, "message": f"Proxy Error (Hugging Face): {hf_error_message(e)}", "results": []}except Exception as e:return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "results": []}6 unmodified linesheaders = {"Authorization": f"Bearer {HF_TOKEN}"}def make_post():return request_with_429_backoff(requests.post,target_url,headers=headers,6 unmodified linesresponse.raise_for_status()return response.json()except requests.exceptions.RequestException as e:return {"success": False, "message": f"Proxy Error (Hugging Face): {hf_error_message(e)}", "rows": []}except Exception as e:return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "rows": []}21 unmodified lines2223242526272829303132333435362 unmodified lines394041424344455 unmodified lines5152535455565758596061626364656667686970717273747576777839 unmodified lines1181191201211221231248 unmodified lines13313413513613713813916 unmodified lines1561571581591601611627 unmodified lines1701711721731741751766 unmodified lines1831841851861871881896 unmodified lines19619719819920020120221 unmodified linesDEV_URL = "https://PPAL-SongLab-UGA-fruit-analyzer-dev.hf.space"PROD_URL = "https://PPAL-SongLab-UGA-fruit-analyzer.hf.space"HF_RATE_LIMIT_STATUS = 429HF_TRANSIENT_STATUS = {429, 502, 503, 504}def request_with_hf_backoff(method, url, *, max_retries=3, **kwargs):last_response = Nonefor attempt in range(max_retries + 1):response = method(url, **kwargs)last_response = responseif response.status_code not in HF_TRANSIENT_STATUS:return responseretry_after = response.headers.get("retry-after")2 unmodified linesexcept ValueError:wait_s = Noneif wait_s is None:wait_s = [2.0, 5.0, 10.0, 15.0][min(attempt, 3)] if response.status_code == HF_RATE_LIMIT_STATUS else [3.0, 8.0, 15.0, 20.0][min(attempt, 3)]if attempt < max_retries:time.sleep(min(max(wait_s, 0.5), 20.0))return last_response5 unmodified linesretry_after = response.headers.get("retry-after")suffix = f" Retry after about {retry_after} seconds." if retry_after else " Please wait a minute and try again."return f"Hugging Face is rate limiting this Space after several retry attempts.{suffix}"if response is not None and response.status_code in {502, 503, 504}:return "Hugging Face Space is temporarily unavailable or waking up after several retry attempts. Please wait a moment and try again."if isinstance(exc, requests.exceptions.Timeout):return "Hugging Face Space did not respond before the proxy timeout. Please try again."if isinstance(exc, requests.exceptions.ConnectionError):return "Could not connect to the Hugging Face Space. Please try again."return str(exc)def is_retryable_hf_exception(exc):response = getattr(exc, "response", None)return bool(response is not None and response.status_code in HF_TRANSIENT_STATUS)def proxy_error_payload(prefix, exc, **extra):return {"success": False,"message": f"{prefix}: {hf_error_message(exc)}","retryable": is_retryable_hf_exception(exc),**extra,}@app.get("/")def read_root():return {"status": "Render Proxy is awake!"}39 unmodified lines# FIX: Define the blocking requestdef make_post():return request_with_hf_backoff(requests.post,target_url,headers=headers,8 unmodified linesresponse.raise_for_status()return response.json()except requests.exceptions.RequestException as e:return proxy_error_payload("Proxy Error (Hugging Face)", e)except Exception as e:return {"success": False, "message": f"Proxy Error (Internal): {str(e)}"}16 unmodified linesdata.setdefault("username", username)def make_post():return request_with_hf_backoff(requests.post,target_url,headers=headers,7 unmodified linesresponse.raise_for_status()return response.json()except requests.exceptions.RequestException as e:return proxy_error_payload("Proxy Error (Hugging Face)", e, results=[])except Exception as e:return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "results": []}6 unmodified linesheaders = {"Authorization": f"Bearer {HF_TOKEN}"}def make_post():return request_with_hf_backoff(requests.post,target_url,headers=headers,6 unmodified linesresponse.raise_for_status()return response.json()except requests.exceptions.RequestException as e:return proxy_error_payload("Proxy Error (Hugging Face)", e, rows=[])except Exception as e:return {"success": False, "message": f"Proxy Error (Internal): {str(e)}", "rows": []}
main.py+30-9