ken4 commited on
Commit
6f5a464
·
verified ·
1 Parent(s): f82e840

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +10 -0
  2. app.py +53 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
+ from paddleocr import PaddleOCR
4
+ import numpy as np
5
+ from PIL import Image
6
+ from io import BytesIO
7
+ import requests
8
+
9
+ app = FastAPI(title="PaddleOCR API", description="OCR via URL or File Upload")
10
+
11
+ ocr = PaddleOCR(use_angle_cls=True, lang='en')
12
+
13
+ def run_ocr(image_bytes):
14
+ img = Image.open(BytesIO(image_bytes)).convert("RGB")
15
+ result = ocr.ocr(np.array(img), cls=True)
16
+
17
+ parsed_texts = []
18
+ outputs = []
19
+
20
+ for line in result:
21
+ for box, (text, conf) in line:
22
+ parsed_texts.append(text)
23
+ outputs.append({
24
+ "text": text,
25
+ "confidence": float(conf),
26
+ "bounding_box": [[float(x), float(y)] for x, y in box]
27
+ })
28
+
29
+ final_text = " ".join(parsed_texts)
30
+
31
+ return {
32
+ "results": outputs,
33
+ "parsedText": parsed_texts,
34
+ "finalText": final_text
35
+ }
36
+
37
+ @app.get("/ocr")
38
+ async def ocr_url(url: str):
39
+ try:
40
+ img_data = requests.get(url, timeout=10).content
41
+ data = run_ocr(img_data)
42
+ return JSONResponse({"success": True, **data})
43
+ except Exception as e:
44
+ return JSONResponse({"success": False, "error": str(e)}, status_code=400)
45
+
46
+ @app.post("/ocr")
47
+ async def ocr_buffer(file: UploadFile = File(...)):
48
+ try:
49
+ image_bytes = await file.read()
50
+ data = run_ocr(image_bytes)
51
+ return JSONResponse({"success": True, **data})
52
+ except Exception as e:
53
+ return JSONResponse({"success": False, "error": str(e)}, status_code=400)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ paddleocr
4
+ paddlepaddle-lite
5
+ numpy
6
+ Pillow
7
+ requests
8
+ python-multipart