Create app.py
Browse files1234567891011121314151617181920212223242526272829303132333435from fastapi import FastAPI, UploadFile, Filefrom fastapi.middleware.cors import CORSMiddlewareimport easyocrimport iofrom PIL import Image
app = FastAPI()
# 1. Enable global frontend access (CORS) so your site can use it directlyapp.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"],)
# 2. Pre-initialize the OCR reader once during bootup (handles English, Spanish, etc.)reader = easyocr.Reader(['en'])
@app.post("/ocr")async def process_ocr(file: UploadFile = File(...)): try: # Read the raw image file sent from your frontend image_bytes = await file.read() image = Image.open(io.BytesIO(image_bytes)) # Execute the raw text extraction # detailed=0 simplifies output to just strings text_results = reader.readtext(image, detail=0) # Return cleanly structured JSON data return {"success": True, "text": " ".join(text_results)} except Exception as e: return {"success": False, "error": str(e)}app.pyAdded+35