File size: 2,015 Bytes
a783939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware

from .config import settings
from .core.errors import install_exception_handlers
from .core.logging import setup_logging
from .db.engine import init_db
from .routers import analyze, feedback, health, history, stats


@asynccontextmanager
async def lifespan(app: FastAPI):
    setup_logging()
    init_db()
    app.state.whisper = None
    app.state.clf = None

    try:
        from .ml.whisper import load_whisper
        app.state.whisper = load_whisper(settings)
    except Exception:
        app.state.whisper = None

    try:
        from .ml.classifier import load_classifier
        app.state.clf = load_classifier(settings.clf_path)
    except Exception:
        app.state.clf = None

    yield

    app.state.whisper = None
    app.state.clf = None


app = FastAPI(
    title="AP27510301 Fraud Detector API",
    version=settings.app_version,
    description=(
        "Anti-fraud detection API for Kazakhstan. "
        "Analyzes Russian and Kazakh text and audio for signs of social engineering."
    ),
    lifespan=lifespan,
    docs_url="/docs",
    redoc_url="/redoc",
)

# Compress JSON / text responses >1 KB. History pages and audio analyses
# with feature evidence routinely fit this, this is essentially free
# bandwidth savings for the mobile client over cellular networks.
app.add_middleware(GZipMiddleware, minimum_size=1024, compresslevel=5)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["X-Device-Id", "Content-Type", "Accept-Language"],
)

install_exception_handlers(app)

app.include_router(health.router, prefix="/v1")
app.include_router(analyze.router, prefix="/v1")
app.include_router(history.router, prefix="/v1")
app.include_router(feedback.router, prefix="/v1")
app.include_router(stats.router, prefix="/v1")
app.include_router(health.router)