Spaces:
Runtime error
Runtime error
Rivalcoder
commited on
Commit
Β·
2f465bd
1
Parent(s):
210935f
Add files
Browse files- Dockerfile +11 -6
- app.py +40 -37
Dockerfile
CHANGED
|
@@ -12,22 +12,27 @@ RUN apt-get update && apt-get install -y \
|
|
| 12 |
|
| 13 |
WORKDIR /app
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
# Copy requirements
|
| 19 |
COPY requirements.txt .
|
| 20 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
|
| 22 |
-
# β‘ Pre-download EasyOCR
|
| 23 |
RUN python3 - <<'EOF'
|
| 24 |
import easyocr, os
|
| 25 |
MODEL_DIR = "/app/.EasyOCR"
|
| 26 |
USER_NET_DIR = os.path.join(MODEL_DIR, "user_network")
|
| 27 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 28 |
os.makedirs(USER_NET_DIR, exist_ok=True)
|
| 29 |
-
#
|
| 30 |
-
reader = easyocr.Reader(['en'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
EOF
|
| 32 |
|
| 33 |
# Copy app
|
|
|
|
| 12 |
|
| 13 |
WORKDIR /app
|
| 14 |
|
| 15 |
+
# EasyOCR model dir (must be writable)
|
| 16 |
+
ENV EASY_OCR_DIR=/app/.EasyOCR
|
| 17 |
+
RUN mkdir -p $EASY_OCR_DIR/user_network
|
| 18 |
|
| 19 |
+
# Copy requirements
|
| 20 |
COPY requirements.txt .
|
| 21 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 22 |
|
| 23 |
+
# β‘ Pre-download EasyOCR detection + recognition models
|
| 24 |
RUN python3 - <<'EOF'
|
| 25 |
import easyocr, os
|
| 26 |
MODEL_DIR = "/app/.EasyOCR"
|
| 27 |
USER_NET_DIR = os.path.join(MODEL_DIR, "user_network")
|
| 28 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 29 |
os.makedirs(USER_NET_DIR, exist_ok=True)
|
| 30 |
+
# preload both en + hi recognition + detection
|
| 31 |
+
reader = easyocr.Reader(['en', 'hi'],
|
| 32 |
+
model_storage_directory=MODEL_DIR,
|
| 33 |
+
user_network_directory=USER_NET_DIR,
|
| 34 |
+
download_enabled=True
|
| 35 |
+
)
|
| 36 |
EOF
|
| 37 |
|
| 38 |
# Copy app
|
app.py
CHANGED
|
@@ -1,53 +1,56 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import easyocr
|
| 4 |
-
from fastapi import FastAPI,
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
-
from
|
| 7 |
-
from tempfile import NamedTemporaryFile
|
| 8 |
-
import uvicorn
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Force EasyOCR to use writable directories inside /app
|
| 13 |
-
MODEL_DIR = os.path.join(os.getcwd(), ".EasyOCR")
|
| 14 |
-
USER_NET_DIR = os.path.join(os.getcwd(), ".EasyOCR", "user_network")
|
| 15 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 16 |
os.makedirs(USER_NET_DIR, exist_ok=True)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
reader = easyocr.Reader(
|
| 20 |
-
['en', 'hi'],
|
| 21 |
model_storage_directory=MODEL_DIR,
|
| 22 |
-
user_network_directory=USER_NET_DIR
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
try:
|
|
|
|
| 28 |
contents = await file.read()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
if file.filename.lower().endswith(".pdf"):
|
| 32 |
-
# Convert PDF to images
|
| 33 |
-
images = convert_from_bytes(contents)
|
| 34 |
-
for i, image in enumerate(images):
|
| 35 |
-
image_np = np.array(image)
|
| 36 |
-
result = reader.readtext(image_np)
|
| 37 |
-
page_text = " ".join([text for _, text, _ in result])
|
| 38 |
-
extracted_text += f"--- Page {i+1} ---\n{page_text}\n\n"
|
| 39 |
-
else:
|
| 40 |
-
# Treat as image
|
| 41 |
-
with NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
| 42 |
-
temp_file.write(contents)
|
| 43 |
-
temp_file.flush()
|
| 44 |
-
results = reader.readtext(temp_file.name)
|
| 45 |
-
extracted_text = " ".join([text for _, text, _ in results])
|
| 46 |
-
|
| 47 |
-
return JSONResponse({"extracted_text": extracted_text})
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 10 |
+
# =========================
|
| 11 |
+
MODEL_DIR = "/app/.EasyOCR"
|
| 12 |
+
USER_NET_DIR = os.path.join(MODEL_DIR, "user_network")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 15 |
os.makedirs(USER_NET_DIR, exist_ok=True)
|
| 16 |
|
| 17 |
+
# β
preload reader with cached models
|
| 18 |
reader = easyocr.Reader(
|
| 19 |
+
['en', 'hi'], # langs (can reduce to ['en'] if you want smaller image)
|
| 20 |
model_storage_directory=MODEL_DIR,
|
| 21 |
+
user_network_directory=USER_NET_DIR,
|
| 22 |
+
download_enabled=False # π« no runtime downloads
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# =========================
|
| 26 |
+
# FastAPI app
|
| 27 |
+
# =========================
|
| 28 |
+
app = FastAPI()
|
| 29 |
+
|
| 30 |
+
@app.get("/")
|
| 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 |
+
# run OCR
|
| 42 |
+
results = reader.readtext(image)
|
| 43 |
|
| 44 |
+
# format results
|
| 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 |
+
return JSONResponse(content={"results": text_results})
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|