|
from fastapi import FastAPI, File, UploadFile
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import StreamingResponse,FileResponse , JSONResponse,HTMLResponse
|
|
from pydantic import BaseModel
|
|
|
|
|
|
import uvicorn
|
|
import cv2
|
|
import tempfile
|
|
import shutil
|
|
import os
|
|
import warnings
|
|
import base64
|
|
import numpy as np
|
|
from pathlib import Path
|
|
|
|
from app.src.model_loader import vit_loader,vgg_loader
|
|
from app.src.logger import setup_logger
|
|
|
|
|
|
warnings.filterwarnings("ignore")
|
|
|
|
|
|
app=FastAPI(title="Document_Classifire",
|
|
description="FastAPI",
|
|
version="0.115.4")
|
|
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"Fast API":"API is woorking"}
|
|
|
|
|
|
|
|
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
|
|
warnings.filterwarnings("ignore")
|
|
|
|
logger = setup_logger()
|
|
|
|
@app.post("/vit_model")
|
|
async def vit_clf(cut_off:float=0.5,image_file: UploadFile = File(...)):
|
|
|
|
try:
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
temp_file_path = os.path.join(temp_dir,image_file.filename)
|
|
|
|
with open(temp_file_path, "wb") as temp_file:
|
|
shutil.copyfileobj(image_file.file, temp_file)
|
|
result=vit_loader().predict(image_path=Path(temp_file_path), cut_off=cut_off)
|
|
logger.info(result)
|
|
|
|
if result is not None:
|
|
return JSONResponse(content={"status":1,"document_classe":result})
|
|
else:
|
|
return JSONResponse(content={"status":0,"document_classe":None})
|
|
|
|
except Exception as e:
|
|
logger.error(str(e))
|
|
return JSONResponse(content={"status":0,"error_message":str(e)})
|
|
|
|
|
|
|
|
|
|
@app.post("/vgg_model")
|
|
async def vgg_clf(image_file: UploadFile = File(...)):
|
|
|
|
try:
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
temp_file_path = os.path.join(temp_dir,image_file.filename)
|
|
|
|
with open(temp_file_path, "wb") as temp_file:
|
|
shutil.copyfileobj(image_file.file, temp_file)
|
|
result=vgg_loader().predict(image_path=Path(temp_file_path))
|
|
logger.info(result)
|
|
|
|
if result is not None:
|
|
return JSONResponse(content={"status":1,"document_classe":result})
|
|
else:
|
|
return JSONResponse(content={"status":0,"document_classe":None})
|
|
|
|
except Exception as e:
|
|
logger.error(str(e))
|
|
return JSONResponse(content={"status":0,"document_classe":str(e)})
|
|
|
|
|
|
|
|
|