File size: 1,146 Bytes
4fee431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
from fastapi import HTTPException, status, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
import os

from features.nepali_text_classifier.inferencer import classify_text

security = HTTPBearer()

async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
    token = credentials.credentials
    expected_token = os.getenv("MY_SECRET_TOKEN")
    if token != expected_token:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Invalid or expired token"
        )
    return token

async def nepali_text_analysis(text: str):
    # Fix: split once and reuse
    words = text.split()
    if len(words) < 10:
        raise HTTPException(status_code=400, detail="Text must contain at least 10 words")
    if len(text) > 10000:
        raise HTTPException(status_code=413, detail="Text must be less than 10,000 characters")

    label, confidence = await asyncio.to_thread(classify_text, text)
    return {
        "result": label,
        "ai_likelihood": confidence
    }

def classify(text: str):
    return classify_text(text)