Spaces:
Runtime error
Runtime error
File size: 1,947 Bytes
d28ba37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import os
import shutil
from pathlib import Path, PurePath
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, Form, HTTPException
from dotenv import load_dotenv
from folder_creation import make_directory_if_not_exists
from pdf_to_img import pdf_to_image
from table_extraction import Table_extraction
from minio_services import MINIO
load_dotenv()
IMAGES_DIR = os.getenv('IMAGES_DIR')
PDF_DIR = os.getenv('PDF_DIR')
OUTPUTS_DIR = os.getenv('OUTPUTS_DIR')
HOST = os.getenv('HOST')
ACCESS_KEY = os.getenv('ACCESS_KEY')
MINIO_KEY = os.getenv('MINIO_KEY')
BUCKET_NAME = os.getenv('BUCKET_NAME')
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/")
async def main(file_name: str = Form(None), uid: str = Form(None)):
try:
make_directory_if_not_exists(IMAGES_DIR)
IMAGES_DIR1 = IMAGES_DIR + uid + '/'
make_directory_if_not_exists(IMAGES_DIR1)
make_directory_if_not_exists(OUTPUTS_DIR)
OUTPUTS_DIR1 = OUTPUTS_DIR + uid + '/'
make_directory_if_not_exists(OUTPUTS_DIR1)
print(file_name)
if PurePath(file_name).suffix == '.pdf':
imagename = pdf_to_image(file_name, IMAGES_DIR1)
else:
imagename = IMAGES_DIR1+Path(file_name).name
shutil.copy(file_name, imagename)
model = Table_extraction(imagename, OUTPUTS_DIR1)
op_img = model.get_results()
minio = MINIO(HOST, ACCESS_KEY, MINIO_KEY, BUCKET_NAME, uid, op_img)
minio.upload_to_minio()
obj = minio.download_from_minio()
shutil.rmtree(IMAGES_DIR)
shutil.rmtree(OUTPUTS_DIR)
return obj
except Exception as e:
shutil.rmtree(IMAGES_DIR)
shutil.rmtree(OUTPUTS_DIR)
raise HTTPException(status_code=404, detail=str(e))
|