Genzo1010 commited on
Commit
3275e3e
1 Parent(s): 125b268

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import asyncio
3
- from asyncio import BoundedSemaphore
4
  from fastapi import FastAPI, File, UploadFile
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from fastapi.middleware.gzip import GZipMiddleware
@@ -23,45 +23,49 @@ 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=False)
27
 
28
- # Create a bounded semaphore to limit concurrent requests
29
- semaphore = BoundedSemaphore(10)
30
 
31
- async def ocr_with_doctr(file):
32
- async with semaphore:
33
- doc = DocumentFile.from_pdf(file)
34
- result = ocr_model(doc)
35
- text_output = ''
36
- for page in result.pages:
37
- for block in page.blocks:
38
- for line in block.lines:
39
- text_output += " ".join([word.value for word in line.words]) + "\n"
40
- return text_output
41
 
42
- async def ocr_with_paddle(img):
43
- async with semaphore:
44
- result = paddle_ocr.ocr(img)
45
- finaltext = ''
46
- for i in range(len(result[0])):
47
- text = result[0][i][1][0]
48
- finaltext +='' + text
49
- return finaltext
 
50
 
51
- async def generate_text_from_image(img):
52
- return await ocr_with_paddle(img)
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  @app.post("/ocr/")
55
  async def perform_ocr(file: UploadFile = File(...)):
56
  file_bytes = await file.read()
57
  if file.filename.endswith('.pdf'):
58
- text_output = await ocr_with_doctr(io.BytesIO(file_bytes))
59
  else:
60
- img = Image.open(io.BytesIO(file_bytes))
61
- img.thumbnail((1024, 1024)) # Reduce image size
62
- text_output = await generate_text_from_image(img)
63
  return {"ocr_text": text_output}
64
 
65
  @app.get("/test/")
66
  async def test_call():
67
- 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
 
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"}