Spaces:
Runtime error
Runtime error
Rivalcoder
commited on
Commit
Β·
8029506
1
Parent(s):
3a14d83
Add files
Browse files- app.py +40 -15
- requirements.txt +0 -2
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import easyocr
|
|
|
|
| 4 |
from fastapi import FastAPI, UploadFile, File
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# =========================
|
| 9 |
# EasyOCR config
|
|
@@ -16,10 +19,10 @@ os.makedirs(USER_NET_DIR, exist_ok=True)
|
|
| 16 |
|
| 17 |
# β
preload reader with cached models
|
| 18 |
reader = easyocr.Reader(
|
| 19 |
-
['en', 'hi'], # langs (
|
| 20 |
model_storage_directory=MODEL_DIR,
|
| 21 |
user_network_directory=USER_NET_DIR,
|
| 22 |
-
download_enabled=False # π«
|
| 23 |
)
|
| 24 |
|
| 25 |
# =========================
|
|
@@ -31,26 +34,48 @@ app = FastAPI()
|
|
| 31 |
async def root():
|
| 32 |
return {"message": "OCR API is running on Hugging Face π"}
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
@app.post("/ocr")
|
| 35 |
async def ocr(file: UploadFile = File(...)):
|
| 36 |
try:
|
| 37 |
-
# read image into memory
|
| 38 |
contents = await file.read()
|
| 39 |
-
image = Image.open(io.BytesIO(contents))
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
text_results = []
|
| 46 |
-
for bbox, text, prob in results:
|
| 47 |
-
text_results.append({
|
| 48 |
-
"bbox": bbox,
|
| 49 |
-
"text": text,
|
| 50 |
-
"confidence": float(prob)
|
| 51 |
-
})
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
import easyocr
|
| 4 |
+
import numpy as np
|
| 5 |
from fastapi import FastAPI, UploadFile, File
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
from PIL import Image
|
| 8 |
+
from pdf2image import convert_from_bytes
|
| 9 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 10 |
|
| 11 |
# =========================
|
| 12 |
# EasyOCR config
|
|
|
|
| 19 |
|
| 20 |
# β
preload reader with cached models
|
| 21 |
reader = easyocr.Reader(
|
| 22 |
+
['en', 'hi'], # langs (reduce if only English needed)
|
| 23 |
model_storage_directory=MODEL_DIR,
|
| 24 |
user_network_directory=USER_NET_DIR,
|
| 25 |
+
download_enabled=False # π« block downloads at runtime
|
| 26 |
)
|
| 27 |
|
| 28 |
# =========================
|
|
|
|
| 34 |
async def root():
|
| 35 |
return {"message": "OCR API is running on Hugging Face π"}
|
| 36 |
|
| 37 |
+
|
| 38 |
+
def run_ocr_on_image(image: Image.Image):
|
| 39 |
+
"""Convert PIL β numpy and run OCR"""
|
| 40 |
+
image_np = np.array(image)
|
| 41 |
+
results = reader.readtext(image_np)
|
| 42 |
+
text_results = []
|
| 43 |
+
for bbox, text, prob in results:
|
| 44 |
+
text_results.append({
|
| 45 |
+
"bbox": bbox,
|
| 46 |
+
"text": text,
|
| 47 |
+
"confidence": float(prob)
|
| 48 |
+
})
|
| 49 |
+
return text_results
|
| 50 |
+
|
| 51 |
+
|
| 52 |
@app.post("/ocr")
|
| 53 |
async def ocr(file: UploadFile = File(...)):
|
| 54 |
try:
|
|
|
|
| 55 |
contents = await file.read()
|
|
|
|
| 56 |
|
| 57 |
+
# Detect file type
|
| 58 |
+
if file.filename.lower().endswith(".pdf"):
|
| 59 |
+
# β
Convert PDF to images
|
| 60 |
+
pages = convert_from_bytes(contents)
|
| 61 |
+
|
| 62 |
+
# β
Run OCR in parallel
|
| 63 |
+
text_results = []
|
| 64 |
+
with ThreadPoolExecutor() as executor:
|
| 65 |
+
results_list = list(executor.map(run_ocr_on_image, pages))
|
| 66 |
+
for i, page_results in enumerate(results_list, start=1):
|
| 67 |
+
text_results.append({
|
| 68 |
+
"page": i,
|
| 69 |
+
"results": page_results
|
| 70 |
+
})
|
| 71 |
|
| 72 |
+
return JSONResponse(content={"pdf_results": text_results})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
else:
|
| 75 |
+
# β
Normal image case
|
| 76 |
+
image = Image.open(io.BytesIO(contents))
|
| 77 |
+
text_results = run_ocr_on_image(image)
|
| 78 |
+
return JSONResponse(content={"results": text_results})
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
requirements.txt
CHANGED
|
@@ -5,5 +5,3 @@ pdf2image
|
|
| 5 |
numpy
|
| 6 |
Pillow
|
| 7 |
python-multipart
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 5 |
numpy
|
| 6 |
Pillow
|
| 7 |
python-multipart
|
|
|
|
|
|