Spaces:
Sleeping
Sleeping
Download routers/ocr.py from benedictpepper/fitmate-api: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/benedictpepper/fitmate-api/resolve/main/routers/ocr.py
- Command line
-
hf download hf://spaces/benedictpepper/fitmate-api/routers/ocr.py
-
curl -L -o ocr.py https://huggingface.co/spaces/benedictpepper/fitmate-api/resolve/main/routers/ocr.py
1.18 kB
| from fastapi import APIRouter, File, UploadFile, HTTPException, Request | |
| from services.vision import extract_and_translate_text | |
| from main import limiter | |
| import traceback | |
| router = APIRouter(prefix="/api/v1/ocr", tags=["ocr"]) | |
| async def process_image_ocr( | |
| request: Request, | |
| file: UploadFile = File(...), | |
| ): | |
| """ | |
| Raw OCR endpoint β returns extracted text blocks only. | |
| Rate limited: max 10 requests per minute per IP. | |
| """ | |
| if not file.content_type or not file.content_type.startswith("image/"): | |
| raise HTTPException(status_code=400, detail="File must be an image") | |
| try: | |
| content = await file.read() | |
| if len(content) > 10 * 1024 * 1024: | |
| raise HTTPException(status_code=413, detail="Image too large. Maximum size is 10MB.") | |
| results = await extract_and_translate_text(content) | |
| return { | |
| "status": "success", | |
| "results": results | |
| } | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| traceback.print_exc() | |
| raise HTTPException(status_code=500, detail=str(e)) | |