Rename app.py to main.py
Browse files13 unmodified lines1415161718192021222324252627282930313233343513 unmodified linesallow_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 frontendimage_bytes = await file.read()image = Image.open(io.BytesIO(image_bytes))# Execute the raw text extraction# detailed=0 simplifies output to just stringstext_results = reader.readtext(image, detail=0)# Return cleanly structured JSON datareturn {"success": True, "text": " ".join(text_results)}except Exception as e:return {"success": False, "error": str(e)}13 unmodified lines14151617181920212223242526272829303132333435363738394041424344454647484950515213 unmodified linesallow_headers=["*"],)# 2. Pre-initialize the OCR engines once during bootup# EasyOCR requires English ('en') to be loaded alongside Urdu ('ur') for best accuracyprint("Loading English OCR engine...")en_reader = easyocr.Reader(['en'])print("Loading Urdu OCR engine...")ur_reader = easyocr.Reader(['ur', 'en'])# --- ENDPOINTS ---@app.post("/api/en")async def ocr_english(file: UploadFile = File(...)):try:image_bytes = await file.read()image = Image.open(io.BytesIO(image_bytes))# Extract English texttext_results = en_reader.readtext(image, detail=0)return {"success": True, "language": "english", "text": " ".join(text_results)}except Exception as e:return {"success": False, "error": str(e)}@app.post("/api/ur")async def ocr_urdu(file: UploadFile = File(...)):try:image_bytes = await file.read()image = Image.open(io.BytesIO(image_bytes))# Extract Urdu texttext_results = ur_reader.readtext(image, detail=0)return {"success": True, "language": "urdu", "text": " ".join(text_results)}except Exception as e:return {"success": False, "error": str(e)}
app.py → main.pyRenamed+27-10