Spaces:
Runtime error
Runtime error
Upload agents/base_agent.py with huggingface_hub
Browse files- agents/base_agent.py +101 -0
agents/base_agent.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Base Agent Class
|
| 3 |
+
Foundation for all RadioFlow agents
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from abc import ABC, abstractmethod
|
| 7 |
+
from dataclasses import dataclass, field
|
| 8 |
+
from typing import Any, Dict, Optional
|
| 9 |
+
from datetime import datetime
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@dataclass
|
| 14 |
+
class AgentResult:
|
| 15 |
+
"""Standardized result from any agent"""
|
| 16 |
+
agent_name: str
|
| 17 |
+
status: str # "success", "error", "warning"
|
| 18 |
+
data: Dict[str, Any]
|
| 19 |
+
processing_time_ms: float
|
| 20 |
+
timestamp: str = field(default_factory=lambda: datetime.now().isoformat())
|
| 21 |
+
error_message: Optional[str] = None
|
| 22 |
+
|
| 23 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 24 |
+
return {
|
| 25 |
+
"agent_name": self.agent_name,
|
| 26 |
+
"status": self.status,
|
| 27 |
+
"data": self.data,
|
| 28 |
+
"processing_time_ms": self.processing_time_ms,
|
| 29 |
+
"timestamp": self.timestamp,
|
| 30 |
+
"error_message": self.error_message
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class BaseAgent(ABC):
|
| 35 |
+
"""
|
| 36 |
+
Abstract base class for all RadioFlow agents.
|
| 37 |
+
Provides common functionality and interface.
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
def __init__(self, name: str, model_name: str = None):
|
| 41 |
+
self.name = name
|
| 42 |
+
self.model_name = model_name
|
| 43 |
+
self.model = None
|
| 44 |
+
self.processor = None
|
| 45 |
+
self.is_loaded = False
|
| 46 |
+
self._call_count = 0
|
| 47 |
+
self._total_time_ms = 0
|
| 48 |
+
|
| 49 |
+
@abstractmethod
|
| 50 |
+
def load_model(self) -> bool:
|
| 51 |
+
"""Load the model into memory. Returns True if successful."""
|
| 52 |
+
pass
|
| 53 |
+
|
| 54 |
+
@abstractmethod
|
| 55 |
+
def process(self, input_data: Any, context: Optional[Dict] = None) -> AgentResult:
|
| 56 |
+
"""Process input and return result."""
|
| 57 |
+
pass
|
| 58 |
+
|
| 59 |
+
def __call__(self, input_data: Any, context: Optional[Dict] = None) -> AgentResult:
|
| 60 |
+
"""Execute the agent with timing."""
|
| 61 |
+
start_time = time.time()
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
if not self.is_loaded:
|
| 65 |
+
self.load_model()
|
| 66 |
+
|
| 67 |
+
result = self.process(input_data, context)
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
result = AgentResult(
|
| 71 |
+
agent_name=self.name,
|
| 72 |
+
status="error",
|
| 73 |
+
data={},
|
| 74 |
+
processing_time_ms=(time.time() - start_time) * 1000,
|
| 75 |
+
error_message=str(e)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Update metrics
|
| 79 |
+
self._call_count += 1
|
| 80 |
+
self._total_time_ms += result.processing_time_ms
|
| 81 |
+
|
| 82 |
+
return result
|
| 83 |
+
|
| 84 |
+
def get_metrics(self) -> Dict[str, Any]:
|
| 85 |
+
"""Get agent performance metrics."""
|
| 86 |
+
return {
|
| 87 |
+
"agent_name": self.name,
|
| 88 |
+
"model_name": self.model_name,
|
| 89 |
+
"is_loaded": self.is_loaded,
|
| 90 |
+
"call_count": self._call_count,
|
| 91 |
+
"total_time_ms": self._total_time_ms,
|
| 92 |
+
"avg_time_ms": self._total_time_ms / self._call_count if self._call_count > 0 else 0
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
def reset_metrics(self):
|
| 96 |
+
"""Reset performance metrics."""
|
| 97 |
+
self._call_count = 0
|
| 98 |
+
self._total_time_ms = 0
|
| 99 |
+
|
| 100 |
+
def __repr__(self):
|
| 101 |
+
return f"{self.__class__.__name__}(name='{self.name}', model='{self.model_name}', loaded={self.is_loaded})"
|