Spaces:
Runtime error
Runtime error
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Load Model into 16GB RAM
|
| 10 |
+
# use_angle_cls=False is faster and uses less memory
|
| 11 |
+
ocr = PaddleOCR(use_angle_cls=False, lang='en', use_gpu=False)
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def home():
|
| 15 |
+
return {"status": "OCR Ready"}
|
| 16 |
+
|
| 17 |
+
@app.post("/ocr")
|
| 18 |
+
async def get_ocr(file: UploadFile = File(...)):
|
| 19 |
+
try:
|
| 20 |
+
# Read image
|
| 21 |
+
content = await file.read()
|
| 22 |
+
image = Image.open(io.BytesIO(content)).convert("RGB")
|
| 23 |
+
img_array = np.array(image)
|
| 24 |
+
|
| 25 |
+
# Run PaddleOCR
|
| 26 |
+
result = ocr.ocr(img_array, cls=False)
|
| 27 |
+
|
| 28 |
+
# Extract text
|
| 29 |
+
full_text = ""
|
| 30 |
+
if result and result[0]:
|
| 31 |
+
text_lines = [line[1][0] for line in result[0]]
|
| 32 |
+
full_text = "\n".join(text_lines)
|
| 33 |
+
|
| 34 |
+
return {"text": full_text}
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return {"text": "", "error": str(e)}
|