KroZenDev commited on
Commit
540bee4
·
verified ·
1 Parent(s): 3dd8fe8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from rapidocr_onnxruntime import RapidOCR
3
+ from PIL import Image
4
+ import io
5
+ import numpy as np
6
+
7
+ app = FastAPI()
8
+ ocr = RapidOCR(cls=False)
9
+
10
+ @app.get("/")
11
+ def read_root():
12
+ return {"status": "OCR API is running. Use POST /predict"}
13
+
14
+ @app.post("/predict")
15
+ async def predict(file: UploadFile = File(...)):
16
+ try:
17
+ contents = await file.read()
18
+ image = Image.open(io.BytesIO(contents))
19
+
20
+ # Оптимизируем размер прямо на сервере HF (там 16 ГБ ОЗУ, но скорость важна)
21
+ if image.width > 1024 or image.height > 1024:
22
+ image.thumbnail((1024, 1024))
23
+
24
+ img_array = np.array(image)
25
+ results, _ = ocr(img_array)
26
+
27
+ if not results:
28
+ return {"text": ""}
29
+
30
+ text = " ".join([item[1] for item in results]).strip()
31
+ return {"text": text}
32
+ except Exception as e:
33
+ return {"error": str(e)}