abhinavvvvv commited on
Commit
a72da64
·
1 Parent(s): 1f05ed6

redeployment

Browse files
.dockerignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ .env
6
+ temp/
Dockerfile CHANGED
@@ -1,25 +1,18 @@
1
- FROM python:3.10
2
 
3
  WORKDIR /app
4
 
5
  RUN apt-get update && apt-get install -y \
6
- libglib2.0-0 \
7
- libsm6 \
8
- libxext6 \
9
- libxrender1 \
10
- libgomp1 \
11
  libgl1 \
 
12
  && rm -rf /var/lib/apt/lists/*
13
 
14
- ENV PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK=True
15
- ENV OMP_NUM_THREADS=1
16
-
17
  COPY requirements.txt .
18
- RUN pip install --upgrade pip
19
  RUN pip install --no-cache-dir -r requirements.txt
20
 
21
- COPY app ./app
22
 
23
  EXPOSE 7860
24
 
25
- CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-7860}"]
 
1
+ FROM python:3.10-slim
2
 
3
  WORKDIR /app
4
 
5
  RUN apt-get update && apt-get install -y \
 
 
 
 
 
6
  libgl1 \
7
+ libglib2.0-0 \
8
  && rm -rf /var/lib/apt/lists/*
9
 
 
 
 
10
  COPY requirements.txt .
11
+
12
  RUN pip install --no-cache-dir -r requirements.txt
13
 
14
+ COPY . .
15
 
16
  EXPOSE 7860
17
 
18
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
app/main.py CHANGED
@@ -1,28 +1,37 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
- from app.ocr_engine import OCREngine
3
- from app.schemas import OCRResponse, OCRLine
 
 
4
 
5
- app = FastAPI(
6
- title="Invoice OCR API",
7
- version="1.0.0"
8
- )
9
 
10
- ocr_engine = OCREngine()
11
 
 
 
12
 
13
- @app.get("/")
14
- def health():
15
- return {"status": "OCR API running"}
16
 
17
-
18
- @app.post("/ocr", response_model=OCRResponse)
19
  async def ocr_endpoint(file: UploadFile = File(...)):
20
- if not file.content_type.startswith("image/"):
21
- raise HTTPException(status_code=400, detail="File must be an image")
 
 
 
 
 
 
 
 
22
 
23
- image_bytes = await file.read()
24
- lines = ocr_engine.extract_text(image_bytes)
 
 
25
 
26
- return OCRResponse(
27
- lines=[OCRLine(**line) for line in lines]
28
- )
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
+ import shutil
4
+ import os
5
+ import uuid
6
 
7
+ from app.ocr import run_ocr
 
 
 
8
 
9
+ app = FastAPI(title="Invoice OCR API")
10
 
11
+ UPLOAD_DIR = "temp"
12
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
13
 
 
 
 
14
 
15
+ @app.post("/ocr")
 
16
  async def ocr_endpoint(file: UploadFile = File(...)):
17
+ try:
18
+ file_id = str(uuid.uuid4())
19
+ file_path = os.path.join(UPLOAD_DIR, f"{file_id}.jpg")
20
+
21
+ with open(file_path, "wb") as buffer:
22
+ shutil.copyfileobj(file.file, buffer)
23
+
24
+ result = run_ocr(file_path)
25
+
26
+ os.remove(file_path)
27
 
28
+ return JSONResponse({
29
+ "status": "success",
30
+ "text_blocks": result
31
+ })
32
 
33
+ except Exception as e:
34
+ return JSONResponse({
35
+ "status": "error",
36
+ "message": str(e)
37
+ }, status_code=500)
app/ocr.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from paddleocr import PaddleOCR
2
+ import os
3
+
4
+ # Load once at startup
5
+ ocr = PaddleOCR(
6
+ use_angle_cls=True,
7
+ lang='en',
8
+ use_gpu=False,
9
+ show_log=False
10
+ )
11
+
12
+ def run_ocr(image_path: str):
13
+ result = ocr.ocr(image_path, cls=True)
14
+
15
+ extracted_text = []
16
+ for line in result:
17
+ for word_info in line:
18
+ text = word_info[1][0]
19
+ confidence = float(word_info[1][1])
20
+ extracted_text.append({
21
+ "text": text,
22
+ "confidence": confidence
23
+ })
24
+
25
+ return extracted_text
app/ocr_engine.py DELETED
@@ -1,39 +0,0 @@
1
- from paddleocr import PaddleOCR
2
- import numpy as np
3
- import cv2
4
-
5
-
6
- class OCREngine:
7
- def __init__(self):
8
- self.ocr = PaddleOCR(
9
- ocr_version="PP-OCRv4",
10
- use_doc_orientation_classify=False,
11
- use_doc_unwarping=False,
12
- use_textline_orientation=False,
13
- lang="en"
14
- )
15
-
16
- def extract_text(self, image_bytes):
17
- nparr = np.frombuffer(image_bytes, np.uint8)
18
- img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
19
-
20
- if img is None:
21
- return []
22
-
23
- # Do NOT resize aggressively
24
- result = self.ocr.ocr(img)
25
-
26
- lines = []
27
-
28
- if result and isinstance(result, list) and len(result) > 0:
29
- for line in result[0]:
30
- text = line[1][0]
31
- score = float(line[1][1])
32
-
33
- if text.strip():
34
- lines.append({
35
- "text": text,
36
- "confidence": score
37
- })
38
-
39
- return lines
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/{schemas.py → utils.py} RENAMED
File without changes
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
- fastapi
2
- uvicorn[standard]
3
- paddleocr==3.4.0
4
- paddlepaddle==3.2.2
5
  python-multipart
6
- opencv-python-headless
7
  pillow
 
 
1
+ fastapi==0.110.0
2
+ uvicorn==0.29.0
3
+ paddleocr==2.7.0.3
4
+ paddlepaddle==2.6.1
5
  python-multipart
 
6
  pillow
7
+ opencv-python-headless