ahmedzein commited on
Commit
cf0f2e0
Β·
verified Β·
1 Parent(s): 7b1e284

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -24
main.py CHANGED
@@ -1,4 +1,4 @@
1
- # import io
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
- # From PIL import Image
10
  from pdftoword import convertPDFtoWORD
11
 
12
- # from model import inference
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
- # 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
 
 
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