File size: 821 Bytes
590e30d
e62960c
8096acb
590e30d
 
 
 
 
 
 
 
 
 
 
8096acb
 
 
 
 
 
 
 
590e30d
 
e62960c
590e30d
 
 
 
 
 
 
 
 
e62960c
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 contextlib import asynccontextmanager
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from . import Item, ContentModerator, get_classifier


@asynccontextmanager
async def lifespan(_: FastAPI):
    get_classifier()
    yield


app = FastAPI(title="Content Moderator API", lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, replace "*" with the app's specific URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


def get_moderator():
    return ContentModerator(get_classifier())


@app.get("/health")
def health():
    return {"status": "ok"}


@app.post("/predict")
def predict(item: Item, moderator: ContentModerator = Depends(get_moderator)):
    return moderator.predict_text(item.text)