Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,71 @@
|
|
1 |
-
import
|
2 |
-
|
|
|
|
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
4 |
from paddleocr import PaddleOCR
|
5 |
from doctr.io import DocumentFile
|
6 |
from doctr.models import ocr_predictor
|
7 |
-
import numpy as np
|
8 |
-
from PIL import Image
|
9 |
import io
|
10 |
|
11 |
-
# Set up logging
|
12 |
-
logging.basicConfig(level=logging.INFO)
|
13 |
-
logger = logging.getLogger(__name__)
|
14 |
-
|
15 |
app = FastAPI()
|
16 |
-
|
17 |
app.add_middleware(
|
18 |
CORSMiddleware,
|
19 |
allow_origins=["*"],
|
20 |
allow_credentials=True,
|
21 |
allow_methods=["*"],
|
22 |
-
allow_headers=["*"]
|
23 |
)
|
|
|
24 |
|
25 |
# Initialize models once at startup
|
26 |
ocr_model = ocr_predictor(pretrained=True)
|
27 |
-
paddle_ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def ocr_with_doctr(file):
|
30 |
text_output = ''
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
for line in block.lines:
|
38 |
-
text_output += " ".join([word.value for word in line.words]) + "\n"
|
39 |
-
except Exception as e:
|
40 |
-
logger.error(f"Error processing PDF: {e}")
|
41 |
-
raise HTTPException(status_code=500, detail=f"Error processing PDF: {e}")
|
42 |
return text_output
|
43 |
|
44 |
def ocr_with_paddle(img):
|
45 |
finaltext = ''
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
text = result[0][i][1][0]
|
51 |
-
finaltext += ' ' + text
|
52 |
-
except Exception as e:
|
53 |
-
logger.error(f"Error processing image: {e}")
|
54 |
-
raise HTTPException(status_code=500, detail=f"Error processing image: {e}")
|
55 |
return finaltext
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
@app.post("/ocr/")
|
58 |
async def perform_ocr(file: UploadFile = File(...)):
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
else:
|
67 |
-
logger.info("Detected image file")
|
68 |
-
img = np.array(Image.open(io.BytesIO(file_bytes)))
|
69 |
-
text_output = ocr_with_paddle(img)
|
70 |
-
|
71 |
-
logger.info("OCR completed successfully")
|
72 |
-
return {"ocr_text": text_output}
|
73 |
-
|
74 |
-
except Exception as e:
|
75 |
-
logger.error(f"Internal server error: {e}")
|
76 |
-
raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
|
77 |
|
78 |
-
@app.get("/")
|
79 |
async def test_call():
|
80 |
-
return {"message": "Hi. I'm running
|
|
|
1 |
+
import os
|
2 |
+
import asyncio
|
3 |
+
from concurrent.futures import ThreadPoolExecutor
|
4 |
+
from fastapi import FastAPI, File, UploadFile
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
from fastapi.middleware.gzip import GZipMiddleware
|
7 |
+
import numpy as np
|
8 |
+
from PIL import Image
|
9 |
from paddleocr import PaddleOCR
|
10 |
from doctr.io import DocumentFile
|
11 |
from doctr.models import ocr_predictor
|
|
|
|
|
12 |
import io
|
13 |
|
|
|
|
|
|
|
|
|
14 |
app = FastAPI()
|
|
|
15 |
app.add_middleware(
|
16 |
CORSMiddleware,
|
17 |
allow_origins=["*"],
|
18 |
allow_credentials=True,
|
19 |
allow_methods=["*"],
|
20 |
+
allow_headers=["*"]
|
21 |
)
|
22 |
+
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
23 |
|
24 |
# Initialize models once at startup
|
25 |
ocr_model = ocr_predictor(pretrained=True)
|
26 |
+
paddle_ocr = PaddleOCR(lang='en', use_angle_cls=True, use_gpu=True)
|
27 |
+
|
28 |
+
# Get the number of available CPUs
|
29 |
+
num_cpus = os.cpu_count()
|
30 |
+
|
31 |
+
# Initialize ThreadPoolExecutor with dynamic number of workers
|
32 |
+
executor = ThreadPoolExecutor(max_workers=num_cpus)
|
33 |
|
34 |
def ocr_with_doctr(file):
|
35 |
text_output = ''
|
36 |
+
doc = DocumentFile.from_pdf(file)
|
37 |
+
result = ocr_model(doc)
|
38 |
+
for page in result.pages:
|
39 |
+
for block in page.blocks:
|
40 |
+
for line in block.lines:
|
41 |
+
text_output += " ".join([word.value for word in line.words]) + "\n"
|
|
|
|
|
|
|
|
|
|
|
42 |
return text_output
|
43 |
|
44 |
def ocr_with_paddle(img):
|
45 |
finaltext = ''
|
46 |
+
result = paddle_ocr.ocr(img)
|
47 |
+
for i in range(len(result[0])):
|
48 |
+
text = result[0][i][1][0]
|
49 |
+
finaltext += ' ' + text
|
|
|
|
|
|
|
|
|
|
|
50 |
return finaltext
|
51 |
|
52 |
+
def generate_text_from_image(img):
|
53 |
+
return ocr_with_paddle(img)
|
54 |
+
|
55 |
+
async def run_blocking_func(func, *args):
|
56 |
+
loop = asyncio.get_event_loop()
|
57 |
+
return await loop.run_in_executor(executor, func, *args)
|
58 |
+
|
59 |
@app.post("/ocr/")
|
60 |
async def perform_ocr(file: UploadFile = File(...)):
|
61 |
+
file_bytes = await file.read()
|
62 |
+
if file.filename.endswith('.pdf'):
|
63 |
+
text_output = await run_blocking_func(ocr_with_doctr, io.BytesIO(file_bytes))
|
64 |
+
else:
|
65 |
+
img = np.array(Image.open(io.BytesIO(file_bytes)))
|
66 |
+
text_output = await run_blocking_func(generate_text_from_image, img)
|
67 |
+
return {"ocr_text": text_output}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
@app.get("/test/")
|
70 |
async def test_call():
|
71 |
+
return {"message": "Hi. I'm running"}
|