mkmanish/truthmark-api
0
1# main.py2 3from fastapi import FastAPI, HTTPException4from fastapi.middleware.cors import CORSMiddleware5from pydantic import BaseModel, Field6from predictor import analyze_text7 8app = FastAPI(9 title="TruthMark API",10 description="AI Content Detection API powered by RoBERTa",11 version="1.0"12)13 14# --------------------15# CORS16# --------------------17app.add_middleware(18 CORSMiddleware,19 allow_origins=["*"],20 allow_credentials=True,21 allow_methods=["*"],22 allow_headers=["*"],23)24 25# --------------------26# Request Schema27# --------------------28class TextRequest(BaseModel):29 text: str = Field(30 ...,31 min_length=10,32 description="Text to analyze (50–350 words recommended)"33 )34 35# --------------------36# Response Schema37# --------------------38class AnalyzeResponse(BaseModel):39 overall_ai_score: float40 overall_human_score: float41 verdict: str42 confidence: str43 badge_color: str44 breakdown: dict45 model_note: str46 limitations: list47 48# --------------------49# Utils50# --------------------51def count_words(text: str):52 return len([w for w in text.strip().split() if w])53 54 55# --------------------56# Endpoint57# --------------------58@app.post("/analyze-text", response_model=AnalyzeResponse)59async def analyze(req: TextRequest):60 text = req.text.strip()61 wc = count_words(text)62 63 if wc < 50:64 raise HTTPException(65 status_code=400,66 detail="Minimum 50 words required for reliable analysis."67 )68 69 if wc > 350:70 raise HTTPException(71 status_code=400,72 detail="Maximum 350 words allowed. Please shorten the text."73 )74 75 return analyze_text(text)76 77@app.get("/")78def root():79 return {"status": "TruthMark API is running"}80 