EATosin commited on
Commit
77079a2
·
verified ·
1 Parent(s): 04ae0b4

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +22 -12
main.py CHANGED
@@ -1,7 +1,9 @@
1
  from fastapi import FastAPI, HTTPException
 
2
  from pydantic import BaseModel
3
  import uvicorn
4
  import logging
 
5
 
6
  # Import our custom modules
7
  from anomaly_detector import AnomalyDetector
@@ -10,16 +12,20 @@ from rag_agent import SentinelAgent
10
  # 1. Initialize the App
11
  app = FastAPI(
12
  title="Sentinel MLOps Agent",
13
- description="Autonomous Anomaly Detection & RAG Investigation API",
14
  version="1.0"
15
  )
16
 
17
  # 2. Load the Engines
18
  print("🔋 Starting Sentinel Engines...")
19
- detector = AnomalyDetector()
20
- agent = SentinelAgent()
 
 
 
 
21
 
22
- # 3. Define the Data Format (Validation)
23
  class MetricData(BaseModel):
24
  timestamp: str
25
  service_name: str
@@ -30,8 +36,8 @@ class MetricData(BaseModel):
30
  async def monitor_system(data: MetricData):
31
  """
32
  Receives live system data.
33
- - If Normal: Returns OK.
34
- - If Anomaly: Triggers Gemini 2.5 to investigate.
35
  """
36
  # Step A: Check for Physics/Math Anomaly
37
  is_anomaly, msg, z_score = detector.update(data.cpu_usage)
@@ -44,7 +50,7 @@ async def monitor_system(data: MetricData):
44
  }
45
 
46
  # Step B: If Anomaly, Wake up the Agent
47
- print(f"🚨 ALERT: Anomaly on {data.service_name} detected!")
48
 
49
  report = agent.investigate(
50
  anomaly_value=data.cpu_usage,
@@ -59,10 +65,14 @@ async def monitor_system(data: MetricData):
59
  "investigation_report": report
60
  }
61
 
62
- @app.get("/")
63
- def home():
64
- return {"message": "Sentinel AI Agent is Active. Send POST to /monitor"}
 
 
65
 
66
- # 5. Run the Server (Mobile Friendly)
67
  if __name__ == "__main__":
68
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
 
 
1
  from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import RedirectResponse
3
  from pydantic import BaseModel
4
  import uvicorn
5
  import logging
6
+ import os
7
 
8
  # Import our custom modules
9
  from anomaly_detector import AnomalyDetector
 
12
  # 1. Initialize the App
13
  app = FastAPI(
14
  title="Sentinel MLOps Agent",
15
+ description="Autonomous Anomaly Detection & RAG Investigation API. Powered by Gemini 2.5.",
16
  version="1.0"
17
  )
18
 
19
  # 2. Load the Engines
20
  print("🔋 Starting Sentinel Engines...")
21
+ try:
22
+ detector = AnomalyDetector()
23
+ agent = SentinelAgent()
24
+ print("✅ Engines Active.")
25
+ except Exception as e:
26
+ print(f"❌ Error loading engines: {e}")
27
 
28
+ # 3. Define the Data Format
29
  class MetricData(BaseModel):
30
  timestamp: str
31
  service_name: str
 
36
  async def monitor_system(data: MetricData):
37
  """
38
  Receives live system data.
39
+ - If Normal (< 2.5 sigma): Returns OK.
40
+ - If Anomaly (> 2.5 sigma): Triggers Gemini 2.5 to investigate logs.
41
  """
42
  # Step A: Check for Physics/Math Anomaly
43
  is_anomaly, msg, z_score = detector.update(data.cpu_usage)
 
50
  }
51
 
52
  # Step B: If Anomaly, Wake up the Agent
53
+ print(f"🚨 ALERT: Anomaly on {data.service_name} detected! (Z={z_score:.2f})")
54
 
55
  report = agent.investigate(
56
  anomaly_value=data.cpu_usage,
 
65
  "investigation_report": report
66
  }
67
 
68
+ # 5. Root Redirect (The Magic Trick)
69
+ @app.get("/", include_in_schema=False)
70
+ def root():
71
+ # Automatically redirects users to the Swagger UI
72
+ return RedirectResponse(url="/docs")
73
 
74
+ # 6. Run the Server
75
  if __name__ == "__main__":
76
+ # Use the port required by Hugging Face (7860) or default to 8000
77
+ port = int(os.getenv("PORT", 7860))
78
+ uvicorn.run(app, host="0.0.0.0", port=port)