-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (39 loc) · 1.34 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import Dict, List
from damage_detection_model.model import CarDamageDetectionModel
from pydantic import BaseModel, Field
from fastapi import FastAPI, Request
import uvicorn
class AccidaModelInput(BaseModel):
image_url: str
class AccidaModelOutputMeta(BaseModel):
is_damage: bool
polygon: List = Field(default_factory=list)
class AccidaModelOutput(BaseModel):
image_url: str
section: str
damage_level: int
damage_prob: float
scratch: AccidaModelOutputMeta
dent: AccidaModelOutputMeta
spacing: AccidaModelOutputMeta
class Model:
def inference(self, _input: ...) -> ...:
pass
class AccidaModel(Model):
def __init__(self, model):
self.model = model
def setup(self):
self.model.setup()
def inference(self, _input: AccidaModelInput) -> AccidaModelOutput:
inference_result, _, _ = self.model.do_inference(_input.image_url, "url")
return AccidaModelOutput(**inference_result)
app = FastAPI()
app.model = AccidaModel(model=CarDamageDetectionModel(model_version="4.0.0"))
@app.on_event("startup")
async def startup():
app.model.setup()
@app.post("/inference", response_model=AccidaModelOutput)
def inference(body: AccidaModelInput, request: Request):
return request.app.model.inference(body)
if __name__ == '__main__':
uvicorn.run(app)