Spaces:
Sleeping
Sleeping
Download api/remediation.py from CloudPulse/ML_classifier: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/spaces/CloudPulse/ML_classifier/resolve/main/api/remediation.py
- Command line
-
hf download hf://spaces/CloudPulse/ML_classifier/api/remediation.py
-
curl -L -o remediation.py https://huggingface.co/spaces/CloudPulse/ML_classifier/resolve/main/api/remediation.py
2.36 kB
| """ | |
| Claude Haiku remediation generator. | |
| Same prompt template as notebook 10. Returns a typed RemediationBlock or None | |
| on failure. The endpoint never blows up on a remediation error — it returns the | |
| classification result and lets the dashboard show a placeholder. | |
| """ | |
| import json | |
| import os | |
| from typing import Optional | |
| from anthropic import Anthropic | |
| from .schemas import RemediationBlock | |
| REMEDIATION_SYSTEM = """You are a senior site reliability engineer producing concise, | |
| actionable remediation guidance for cloud infrastructure incidents. | |
| Respond ONLY with valid JSON matching this exact schema: | |
| { | |
| "summary": "one or two sentence description of what broke", | |
| "root_cause_hypothesis": "the most likely root cause given the incident description", | |
| "immediate_mitigation": ["concrete action 1", "concrete action 2", "concrete action 3"], | |
| "investigation": ["specific log or metric to check 1", "..."], | |
| "prevention": ["long-term action 1", "..."] | |
| } | |
| Be concrete. No hedging language. No general SRE advice. Specific to this incident.""" | |
| _client: Optional[Anthropic] = None | |
| def _get_client() -> Anthropic: | |
| global _client | |
| if _client is None: | |
| _client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"]) | |
| return _client | |
| def generate_remediation( | |
| description: str, | |
| severity: str, | |
| model: str = "claude-haiku-4-5-20251001", | |
| ) -> Optional[RemediationBlock]: | |
| """Calls Claude Haiku and parses the JSON remediation. Returns None on failure.""" | |
| try: | |
| client = _get_client() | |
| response = client.messages.create( | |
| model=model, | |
| max_tokens=800, | |
| system=REMEDIATION_SYSTEM, | |
| messages=[{ | |
| "role": "user", | |
| "content": f"Severity: {severity}\nDescription: {description}", | |
| }], | |
| ) | |
| text = response.content[0].text.strip() | |
| # Strip markdown fences | |
| if text.startswith("```"): | |
| text = text.split("```")[1] | |
| if text.startswith("json"): | |
| text = text[4:] | |
| text = text.strip() | |
| data = json.loads(text) | |
| return RemediationBlock(**data) | |
| except Exception: | |
| # Caller decides what to do. Failing the whole request because remediation | |
| # generation hiccuped would make the dashboard worse, not better. | |
| return None | |