Update hf_demo.py
Browse files- hf_demo.py +85 -4
hf_demo.py
CHANGED
|
@@ -1,10 +1,19 @@
|
|
| 1 |
-
# hf_demo.py – ARF v4 API
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from agentic_reliability_framework.core.governance.risk_engine import RiskEngine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
app = FastAPI(title="ARF v4 API")
|
| 8 |
|
| 9 |
# Enable CORS for your frontend
|
| 10 |
app.add_middleware(
|
|
@@ -13,9 +22,28 @@ app.add_middleware(
|
|
| 13 |
allow_methods=["*"],
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
| 17 |
risk_engine = RiskEngine()
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@app.get("/health")
|
| 20 |
async def health():
|
| 21 |
return {"status": "ok", "version": "4.0.0"}
|
|
@@ -29,6 +57,59 @@ async def get_risk():
|
|
| 29 |
"status": "critical" if risk_score.mean > 0.8 else "normal"
|
| 30 |
}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Optional Gradio interface
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=lambda: f"ARF v4 - Current risk: {risk_engine.get_current_risk().mean:.2f}",
|
|
|
|
| 1 |
+
# hf_demo.py – ARF v4 API with Memory
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# ARF v4 imports
|
| 9 |
from agentic_reliability_framework.core.governance.risk_engine import RiskEngine
|
| 10 |
+
from agentic_reliability_framework.runtime.memory import (
|
| 11 |
+
create_faiss_index,
|
| 12 |
+
RAGGraphMemory,
|
| 13 |
+
MemoryConstants
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
app = FastAPI(title="ARF v4 API with Memory")
|
| 17 |
|
| 18 |
# Enable CORS for your frontend
|
| 19 |
app.add_middleware(
|
|
|
|
| 22 |
allow_methods=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# ---------------------------------------------------------------------------
|
| 26 |
+
# Initialize ARF components
|
| 27 |
+
# ---------------------------------------------------------------------------
|
| 28 |
risk_engine = RiskEngine()
|
| 29 |
|
| 30 |
+
# Create FAISS index and memory (using default dimension from constants)
|
| 31 |
+
faiss_index = create_faiss_index(dim=MemoryConstants.VECTOR_DIM)
|
| 32 |
+
memory = RAGGraphMemory(faiss_index)
|
| 33 |
+
|
| 34 |
+
# ---------------------------------------------------------------------------
|
| 35 |
+
# API Endpoints
|
| 36 |
+
# ---------------------------------------------------------------------------
|
| 37 |
+
|
| 38 |
+
@app.get("/")
|
| 39 |
+
async def root():
|
| 40 |
+
return {
|
| 41 |
+
"service": "ARF OSS API",
|
| 42 |
+
"version": "4.0.0",
|
| 43 |
+
"status": "operational",
|
| 44 |
+
"memory_stats": memory.get_graph_stats() if memory.has_historical_data() else "empty"
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
@app.get("/health")
|
| 48 |
async def health():
|
| 49 |
return {"status": "ok", "version": "4.0.0"}
|
|
|
|
| 57 |
"status": "critical" if risk_score.mean > 0.8 else "normal"
|
| 58 |
}
|
| 59 |
|
| 60 |
+
@app.post("/api/v1/incident")
|
| 61 |
+
async def store_incident(event_data: dict, analysis: dict):
|
| 62 |
+
"""
|
| 63 |
+
Store an incident in memory.
|
| 64 |
+
event_data should contain at least 'component', 'latency_p99', 'error_rate', etc.
|
| 65 |
+
"""
|
| 66 |
+
# In a real implementation you would parse a ReliabilityEvent.
|
| 67 |
+
# For simplicity, we pass a dict – you may need to adapt.
|
| 68 |
+
try:
|
| 69 |
+
incident_id = memory.store_incident(event_data, analysis)
|
| 70 |
+
return {"incident_id": incident_id}
|
| 71 |
+
except Exception as e:
|
| 72 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 73 |
+
|
| 74 |
+
@app.get("/api/v1/memory/similar")
|
| 75 |
+
async def find_similar_incidents(action: str, k: int = 5):
|
| 76 |
+
"""
|
| 77 |
+
Find incidents similar to the given action text.
|
| 78 |
+
This uses a simple embedding fallback (random) for OSS.
|
| 79 |
+
For production, integrate sentence-transformers.
|
| 80 |
+
"""
|
| 81 |
+
# Create a dummy event from the action text
|
| 82 |
+
class DummyEvent:
|
| 83 |
+
def __init__(self, action):
|
| 84 |
+
self.component = "user_action"
|
| 85 |
+
self.latency_p99 = 0.0
|
| 86 |
+
self.error_rate = 0.0
|
| 87 |
+
self.throughput = 0
|
| 88 |
+
self.cpu_util = 0.0
|
| 89 |
+
self.memory_util = 0.0
|
| 90 |
+
self.timestamp = datetime.now()
|
| 91 |
+
self.severity = "low"
|
| 92 |
+
event = DummyEvent(action)
|
| 93 |
+
analysis = {"action": action}
|
| 94 |
+
similar = memory.find_similar(event, analysis, k=k)
|
| 95 |
+
# Convert nodes to dict
|
| 96 |
+
results = []
|
| 97 |
+
for node in similar:
|
| 98 |
+
results.append({
|
| 99 |
+
"incident_id": node.incident_id,
|
| 100 |
+
"component": node.component,
|
| 101 |
+
"severity": node.severity,
|
| 102 |
+
"timestamp": node.timestamp,
|
| 103 |
+
"metrics": node.metrics,
|
| 104 |
+
"agent_analysis": node.agent_analysis,
|
| 105 |
+
"similarity_score": node.metadata.get("similarity_score", 0.0)
|
| 106 |
+
})
|
| 107 |
+
return {"similar": results, "count": len(results)}
|
| 108 |
+
|
| 109 |
+
@app.get("/api/v1/memory/stats")
|
| 110 |
+
async def memory_stats():
|
| 111 |
+
return memory.get_graph_stats()
|
| 112 |
+
|
| 113 |
# Optional Gradio interface
|
| 114 |
iface = gr.Interface(
|
| 115 |
fn=lambda: f"ARF v4 - Current risk: {risk_engine.get_current_risk().mean:.2f}",
|