Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, UploadFile, File | |
| from fastapi.responses import JSONResponse | |
| import uvicorn | |
| import easyocr | |
| import os | |
| import shutil | |
| import uuid | |
| app = FastAPI() | |
| reader = easyocr.Reader(['en', 'hi', 'mr'], gpu=False) | |
| def extract_keywords(image_path): | |
| results = reader.readtext(image_path, detail=1) | |
| keywords = [] | |
| for _, text, _ in results: | |
| if text: | |
| parts = text.split() | |
| for p in parts: | |
| cleaned = p.strip().upper() | |
| if cleaned: | |
| keywords.append(cleaned) | |
| return keywords | |
| async def ocr_endpoint(file: UploadFile = File(...)): | |
| tmp_name = f"/tmp/{uuid.uuid4().hex}_{file.filename}" | |
| with open(tmp_name, "wb") as buffer: | |
| shutil.copyfileobj(file.file, buffer) | |
| keywords = extract_keywords(tmp_name) | |
| return JSONResponse({"ocrKeywords": keywords}) | |