-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·38 lines (32 loc) · 1.07 KB
/
app.py
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
from fastapi import FastAPI, File, UploadFile
from starlette.responses import Response
import uvicorn
import time
import torch
from inference_torch import prediction
from preprocess.converter import TokenLabelConverter
from models.model import Model, ViTSTR
model = torch.load('./models/last_model.pth')
converter = TokenLabelConverter()
# Create Fast API
app = FastAPI()
@app.get("/")
async def index():
return {"messages": "Open the documentations /docs or /redoc"}
@app.post("/text_recogz")
async def predict(file: UploadFile = File(...)):
try:
image = await file.read()
start_time = time.time()
pred_str = prediction(model, image, converter, 'api')
end_time = time.time()
return {
"filename": str(file.filename),
"contentype": str(file.content_type),
"output text": str(pred_str),
"inference time": str(end_time - start_time)
}
except:
return Response("Internal server error", status_code=500)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)