from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from typing import List, Optional # 1. Initialize the App app = FastAPI( title="Sentinel AI Backend", description="API for Blockchain Monitoring and AI Analysis", version="1.0.0" ) # 2. Enable CORS (Essential for React/Frontend to talk to this API) app.add_middleware( CORSMiddleware, allow_origins=["*"], # In production, replace "*" with your frontend URL allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 3. Define Data Models (Schemas) class AnalysisRequest(BaseModel): tx_hash: str network: str = "ethereum" class AnalysisResponse(BaseModel): status: str risk_score: float threats: List[str] # 4. Routes/Endpoints @app.get("/") async def root(): return {"message": "Sentinel AI Engine is Online", "port": 7860} @app.post("/analyze", response_model=AnalysisResponse) async def analyze_transaction(request: AnalysisRequest): try: # Business logic goes here (e.g., calling an AI model or scanning a chain) return { "status": "completed", "risk_score": 0.12, "threats": [] } except Exception as e: raise HTTPException(status_code=500, detail=str(e)) # 5. Run configuration (For local testing) if __name__ == "__main__": import uvicorn # Hugging Face Spaces and most cloud providers expect port 7860 or 8000 uvicorn.run(app, host="0.0.0.0", port=7860)