Ayan8901 commited on
Commit
3029df9
·
verified ·
1 Parent(s): cb170c0

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +15 -0
  2. main.py +38 -0
  3. requirements.txt +0 -0
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ RUN apt-get update && apt-get install -y \
6
+ libglib2.0-0 \
7
+ libgl1 \
8
+ && rm -rf /var/lib/apt/lists/*
9
+
10
+ COPY requirements.txt .
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ COPY . .
14
+
15
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from doctr.io import DocumentFile
3
+ from doctr.models import ocr_predictor
4
+ from PIL import Image
5
+ import tempfile
6
+ import io
7
+ import os
8
+
9
+ app = FastAPI()
10
+
11
+ print("Loading OCR model...")
12
+ model = ocr_predictor(pretrained=True)
13
+ print("OCR ready.")
14
+
15
+ @app.get("/")
16
+ def root():
17
+ return {"status": "OCR running"}
18
+
19
+ @app.post("/ocr")
20
+ async def ocr_images(file: UploadFile = File(...)):
21
+ try:
22
+ contents = await file.read()
23
+ pil_image = Image.open(io.BytesIO(contents)).convert("RGB")
24
+
25
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
26
+ pil_image.save(tmp.name)
27
+ temp_path = tmp.name
28
+
29
+ doc = DocumentFile.from_images([temp_path])
30
+ result = model(doc)
31
+ text = result.render()
32
+
33
+ os.remove(temp_path)
34
+
35
+ return {"success": True, "text": text}
36
+
37
+ except Exception as e:
38
+ return {"success": False, "error": str(e)}
requirements.txt ADDED
Binary file (128 Bytes). View file