Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,11 +5,17 @@ from PIL import Image
|
|
5 |
import numpy as np
|
6 |
import io
|
7 |
import uvicorn
|
|
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
#
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
@app.post("/extract-text/")
|
15 |
async def extract_text(file: UploadFile = File(...)):
|
@@ -24,10 +30,9 @@ async def extract_text(file: UploadFile = File(...)):
|
|
24 |
elif img_array.shape[-1] == 4: # RGBA image
|
25 |
img_array = img_array[..., :3]
|
26 |
|
27 |
-
# Perform OCR
|
28 |
-
|
29 |
-
extracted_text = ' '.join([line[1][0] for line in result[0]])
|
30 |
return {"text": extracted_text}
|
31 |
|
32 |
if __name__ == "__main__":
|
33 |
-
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
|
|
|
5 |
import numpy as np
|
6 |
import io
|
7 |
import uvicorn
|
8 |
+
from functools import lru_cache
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
# Define a cache to store the results of the OCR function
|
13 |
+
@lru_cache(maxsize=128)
|
14 |
+
def ocr_cache(img_array):
|
15 |
+
ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
16 |
+
result = ocr.ocr(img_array)
|
17 |
+
extracted_text =''.join([line[1][0] for line in result[0]])
|
18 |
+
return extracted_text
|
19 |
|
20 |
@app.post("/extract-text/")
|
21 |
async def extract_text(file: UploadFile = File(...)):
|
|
|
30 |
elif img_array.shape[-1] == 4: # RGBA image
|
31 |
img_array = img_array[..., :3]
|
32 |
|
33 |
+
# Perform OCR using the cache
|
34 |
+
extracted_text = ocr_cache(tuple(img_array.flatten()))
|
|
|
35 |
return {"text": extracted_text}
|
36 |
|
37 |
if __name__ == "__main__":
|
38 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
|