-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
36 lines (25 loc) · 1.03 KB
/
main.py
File metadata and controls
36 lines (25 loc) · 1.03 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
from typing import Dict
from fastapi import Depends, FastAPI
from pydantic import BaseModel
from .classifier.model import Model, get_model
app = FastAPI()
class SentimentRequest(BaseModel):
text: str
class SentimentResponse(BaseModel):
probabilities: Dict[str, float]
sentiment: str
confidence: float
# Start up event
@app.on_event("startup")
async def startup():
import gc
# PyTorch internally uses a lot of private objects, which activates the garbage collector.
# To avoid the garbage collector to be activated, we freeze the garbage collector.
# This will put the references of the PyTorch objects to the permanent generation, which is not cleaned by the garbage collector.
gc.freeze()
@app.post("/predict", response_model=SentimentResponse)
def predict(request: SentimentRequest, model: Model = Depends(get_model)):
sentiment, confidence, probabilities = model.predict(request.text)
return SentimentResponse(
sentiment=sentiment, confidence=confidence, probabilities=probabilities
)