-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (39 loc) · 1.25 KB
/
main.py
File metadata and controls
58 lines (39 loc) · 1.25 KB
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
"""A main script to run api.
"""
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from src.utility.loggers import logger
from src.inference.predictor import Predictor
class InputData(BaseModel):
"""A class to define data and it's type for input data.
Args:
BaseModel : A base datatype class
"""
data: str
app = FastAPI()
@app.get("/")
async def read_root():
"""basic server health check url
Returns:
dict : A message that server is running
"""
logger.info("API server is running.")
return {"msg": "API server is running"}
@app.post("/model_inference/")
async def get_model_output(input_data: InputData):
"""A method to return model response on API request
Args:
input_data (str): Input data required for model
Returns:
dict : model output
"""
try:
return {"model_output": Predictor.get_model_output(input_data.data)}
except Exception as error:
message = "Error while creating output"
logger.error(message, str(error))
# This is used only for unit testing the application/
# or for running this app as standalone instead of via docker
if __name__ == "__main__":
uvicorn.run("main:app", port=5000, log_level="info")