""" Patch main.py to add /analyze HTTP endpoint + serve the demo page. This is the actual entry point for Hugging Face Spaces. Run: python hf_main.py """ from __future__ import annotations import asyncio import time from fastapi.responses import HTMLResponse from pydantic import BaseModel # Import and extend the base app from app_demo import fact_app as app from main import AnalysisBatch, process_claim class AnalyzeRequest(BaseModel): client_id: str = "http-client" claims: list[str] platform: str = "web" timestamp: float = 0.0 @app.post("/analyze") async def analyze_http(req: AnalyzeRequest): """ HTTP fallback endpoint for the demo when WebSocket is not available. Mirrors the WS analysis pipeline over a standard POST request. """ sem = asyncio.Semaphore(3) tasks = [process_claim(sem, claim, req.platform) for claim in req.claims[:5]] results = await asyncio.gather(*tasks, return_exceptions=True) items = [] for r in results: if not isinstance(r, Exception): d = r.model_dump() d["platform"] = req.platform items.append(d) return { "type": "analysis_batch", "results": items, "request_timestamp": req.timestamp or time.time(), } if __name__ == "__main__": import uvicorn, os uvicorn.run( "hf_main:app", host="0.0.0.0", port=int(os.getenv("PORT", "7860")), reload=False, log_level="info", )