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)