Spaces:
No application file
No application file
File size: 1,548 Bytes
9d721fa | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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) |