Update hf_demo.py
Browse files- hf_demo.py +65 -1
hf_demo.py
CHANGED
|
@@ -61,4 +61,68 @@ async def store_incident(event_data: dict, analysis: dict):
|
|
| 61 |
raise HTTPException(status_code=500, detail=str(e))
|
| 62 |
|
| 63 |
@app.get("/api/v1/memory/similar")
|
| 64 |
-
async def find_similar_incidents(action: str, k
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
raise HTTPException(status_code=500, detail=str(e))
|
| 62 |
|
| 63 |
@app.get("/api/v1/memory/similar")
|
| 64 |
+
async def find_similar_incidents(action: str, k: int = 5):
|
| 65 |
+
class DummyEvent:
|
| 66 |
+
def __init__(self, action):
|
| 67 |
+
self.component = "user_action"
|
| 68 |
+
self.latency_p99 = 0.0
|
| 69 |
+
self.error_rate = 0.0
|
| 70 |
+
self.throughput = 0
|
| 71 |
+
self.cpu_util = 0.0
|
| 72 |
+
self.memory_util = 0.0
|
| 73 |
+
self.timestamp = datetime.now()
|
| 74 |
+
self.severity = "low"
|
| 75 |
+
|
| 76 |
+
event = DummyEvent(action)
|
| 77 |
+
analysis = {"action": action}
|
| 78 |
+
similar = memory.find_similar(event, analysis, k=k)
|
| 79 |
+
results = []
|
| 80 |
+
for node in similar:
|
| 81 |
+
results.append({
|
| 82 |
+
"incident_id": node.incident_id,
|
| 83 |
+
"component": node.component,
|
| 84 |
+
"severity": node.severity,
|
| 85 |
+
"timestamp": node.timestamp,
|
| 86 |
+
"metrics": node.metrics,
|
| 87 |
+
"agent_analysis": node.agent_analysis,
|
| 88 |
+
"similarity_score": node.metadata.get("similarity_score", 0.0)
|
| 89 |
+
})
|
| 90 |
+
return {"similar": results, "count": len(results)}
|
| 91 |
+
|
| 92 |
+
@app.get("/api/v1/memory/stats")
|
| 93 |
+
async def memory_stats():
|
| 94 |
+
return memory.get_graph_stats()
|
| 95 |
+
|
| 96 |
+
# ---------------------------------------------------------------------------
|
| 97 |
+
# Optional Gradio interface
|
| 98 |
+
# ---------------------------------------------------------------------------
|
| 99 |
+
def current_risk_text():
|
| 100 |
+
return f"ARF v4 - Current risk: {risk_engine.get_current_risk().mean:.2f}"
|
| 101 |
+
|
| 102 |
+
def health_check():
|
| 103 |
+
return {
|
| 104 |
+
"status": "running",
|
| 105 |
+
"version": "ARF v4 Demo",
|
| 106 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
with gr.Blocks() as demo:
|
| 110 |
+
gr.Markdown("## ARF v4 Demo & Health Check")
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
# Risk output
|
| 114 |
+
gr.Markdown("### Current Risk")
|
| 115 |
+
gr.Textbox(value=current_risk_text(), label="Risk Output", interactive=False)
|
| 116 |
+
|
| 117 |
+
with gr.Row():
|
| 118 |
+
# Health check
|
| 119 |
+
gr.Markdown("### Health Check")
|
| 120 |
+
health_output = gr.JSON()
|
| 121 |
+
gr.Button("Check Health").click(fn=health_check, outputs=health_output)
|
| 122 |
+
|
| 123 |
+
# Mount Gradio on FastAPI
|
| 124 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 125 |
+
|
| 126 |
+
# ============== MAIN ENTRY POINT ==============
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
demo.launch(server_name="0.0.0.0")
|