Download app/classify.py from CJR61/drpig-web: direct link, hf CLI and curl.
- Browser
- Download file 907 Bytes
-
https://huggingface.co/spaces/CJR61/drpig-web/resolve/main/app/classify.py
- Command line
-
hf download hf://spaces/CJR61/drpig-web/app/classify.py
-
curl -L -o classify.py https://huggingface.co/spaces/CJR61/drpig-web/resolve/main/app/classify.py
907 Bytes
| import os | |
| import asyncio | |
| import httpx | |
| PAPI_URL = os.getenv("PAPI_URL", "https://cjr61-papi-cc.hf.space/predict/") | |
| PAPI_KEY = os.getenv("PAPI_CC_KEY", "") | |
| MAX_RETRY = 3 # total attempts | |
| BACKOFF = [1, 3] # wait seconds before 2nd and 3rd attempt | |
| async def classify_image(file_bytes: bytes, filename: str) -> dict: | |
| headers = {"X-API-Key": PAPI_KEY} | |
| last_exc = None | |
| for attempt in range(MAX_RETRY): | |
| if attempt > 0: | |
| await asyncio.sleep(BACKOFF[attempt - 1]) | |
| try: | |
| async with httpx.AsyncClient(timeout=40) as client: | |
| resp = await client.post( | |
| PAPI_URL, headers=headers, | |
| files={"file": (filename, file_bytes, "image/jpeg")} | |
| ) | |
| resp.raise_for_status() | |
| return resp.json() | |
| except Exception as e: | |
| last_exc = e | |
| raise last_exc | |