Update main.py
Browse files
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
import os
|
3 |
|
4 |
from fastapi import FastAPI, File, HTTPException, UploadFile
|
@@ -6,10 +6,10 @@ from fastapi.responses import JSONResponse
|
|
6 |
from starlette.responses import FileResponse
|
7 |
from starlette.middleware.cors import CORSMiddleware
|
8 |
|
9 |
-
|
10 |
from pdftoword import convertPDFtoWORD
|
11 |
|
12 |
-
|
13 |
|
14 |
|
15 |
app = FastAPI()
|
@@ -27,27 +27,27 @@ allow_headers=["*"], # Allows all headers
|
|
27 |
|
28 |
@app.post("/upload")
|
29 |
async def extract_table_data(image: UploadFile = File(...)):
|
30 |
-
return f"table ocr is disabled π"
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
|
52 |
|
53 |
|
|
|
1 |
+
import io
|
2 |
import os
|
3 |
|
4 |
from fastapi import FastAPI, File, HTTPException, UploadFile
|
|
|
6 |
from starlette.responses import FileResponse
|
7 |
from starlette.middleware.cors import CORSMiddleware
|
8 |
|
9 |
+
from PIL import Image
|
10 |
from pdftoword import convertPDFtoWORD
|
11 |
|
12 |
+
from model import inference
|
13 |
|
14 |
|
15 |
app = FastAPI()
|
|
|
27 |
|
28 |
@app.post("/upload")
|
29 |
async def extract_table_data(image: UploadFile = File(...)):
|
30 |
+
# return f"table ocr is disabled π"
|
31 |
+
try:
|
32 |
+
# Read image data
|
33 |
+
image_data = await image.read()
|
34 |
+
|
35 |
+
# Open image in memory
|
36 |
+
image = Image.open(io.BytesIO(image_data))
|
37 |
+
rgb_img = image.convert("RGB")
|
38 |
+
rgb_img.save('output.jpg')
|
39 |
+
image = Image.open('output.jpg')
|
40 |
+
|
41 |
+
table_fram= inference(image)
|
42 |
+
if table_fram.empty:
|
43 |
+
return "<h2 style=\"color: darkslategrey;\">π‘ the image has no tables π‘</h2>"
|
44 |
+
|
45 |
+
return table_fram.to_html(escape=True,border=1,index=False).replace('\n', '')
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
# Handle and log exceptions appropriately
|
49 |
+
print(f"Error processing image: {e}")
|
50 |
+
raise HTTPException(status_code=500, detail="Internal server error")
|
51 |
|
52 |
|
53 |
|