-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
72 lines (48 loc) · 1.53 KB
/
Copy pathapi.py
File metadata and controls
72 lines (48 loc) · 1.53 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Use A | B syntax for Union types in Python 3.9
from __future__ import annotations
import logging
from fastapi import FastAPI
from pydantic import BaseModel
from python_template.main import __version__
logger = logging.getLogger(__name__)
# Overwrite root log level from `log_conf.yaml`
logger.setLevel(logging.DEBUG)
app = FastAPI()
class Request(BaseModel):
"""Data model for the request body."""
input: int
class Response(BaseModel):
"""Data model for the response body."""
output: int
@app.get("/")
def read_root() -> dict[str, str]:
"""Check API version."""
logger.debug("DEBUG")
logger.warning("WARNING")
logger.info("INFO")
logger.error("ERROR")
logger.critical("CRITICAL")
return {"python_template-api": f"version {__version__}"}
@app.get("/health")
def health() -> dict[str, str]:
"""Health check for container orchestration and monitoring.
Stable endpoint: keep it when replacing the example endpoints below,
since Docker healthchecks and CI smoke tests rely on it.
"""
return {"status": "ok"}
@app.post("/predict")
def predict(request: Request) -> Response:
"""Mock prediction endpoint."""
return Response(output=request.input)
if __name__ == "__main__":
import sys
import uvicorn
port = 7000 if len(sys.argv) < 2 else int(sys.argv[1])
host = "127.0.0.1" if len(sys.argv) < 3 else sys.argv[2]
uvicorn.run(
"python_template.api:app",
host=host,
port=port,
reload=True,
log_config="log_conf.yaml",
)