Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +22 -10
- app.py +364 -126
- requirements.txt +3 -1
Dockerfile
CHANGED
|
@@ -3,37 +3,49 @@ FROM python:3.10-slim
|
|
| 3 |
# Set environment variables
|
| 4 |
ENV PYTHONUNBUFFERED=1 \
|
| 5 |
DEBIAN_FRONTEND=noninteractive \
|
| 6 |
-
REQUESTS_TIMEOUT=30
|
|
|
|
| 7 |
|
| 8 |
# Install system dependencies
|
| 9 |
RUN apt-get update && apt-get install -y \
|
| 10 |
build-essential \
|
| 11 |
curl \
|
| 12 |
git \
|
|
|
|
|
|
|
| 13 |
&& rm -rf /var/lib/apt/lists/*
|
| 14 |
|
| 15 |
# Set working directory
|
| 16 |
WORKDIR /app
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Copy requirements first to leverage Docker cache
|
| 19 |
COPY requirements.txt .
|
| 20 |
|
| 21 |
-
# Install Python dependencies
|
| 22 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Copy application code
|
| 25 |
COPY . .
|
| 26 |
|
| 27 |
-
# Set up DNS configuration
|
| 28 |
-
RUN echo "nameserver 8.8.8.8" > /etc/resolv.conf && \
|
| 29 |
-
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
|
| 30 |
-
|
| 31 |
# Expose port
|
| 32 |
EXPOSE 7860
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Command to run the application
|
| 39 |
CMD ["python", "app.py"]
|
|
|
|
| 3 |
# Set environment variables
|
| 4 |
ENV PYTHONUNBUFFERED=1 \
|
| 5 |
DEBIAN_FRONTEND=noninteractive \
|
| 6 |
+
REQUESTS_TIMEOUT=30 \
|
| 7 |
+
PYTHONPATH=/app
|
| 8 |
|
| 9 |
# Install system dependencies
|
| 10 |
RUN apt-get update && apt-get install -y \
|
| 11 |
build-essential \
|
| 12 |
curl \
|
| 13 |
git \
|
| 14 |
+
dnsutils \
|
| 15 |
+
iputils-ping \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
# Set working directory
|
| 19 |
WORKDIR /app
|
| 20 |
|
| 21 |
+
# Set up DNS configuration with multiple DNS servers for redundancy
|
| 22 |
+
RUN echo "nameserver 8.8.8.8" > /etc/resolv.conf && \
|
| 23 |
+
echo "nameserver 8.8.4.4" >> /etc/resolv.conf && \
|
| 24 |
+
echo "nameserver 1.1.1.1" >> /etc/resolv.conf && \
|
| 25 |
+
echo "options timeout:1 attempts:5" >> /etc/resolv.conf
|
| 26 |
+
|
| 27 |
# Copy requirements first to leverage Docker cache
|
| 28 |
COPY requirements.txt .
|
| 29 |
|
| 30 |
+
# Install Python dependencies with retry mechanism
|
| 31 |
+
RUN pip install --no-cache-dir -r requirements.txt || \
|
| 32 |
+
(sleep 5 && pip install --no-cache-dir -r requirements.txt) || \
|
| 33 |
+
(sleep 10 && pip install --no-cache-dir -r requirements.txt)
|
| 34 |
|
| 35 |
# Copy application code
|
| 36 |
COPY . .
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Expose port
|
| 39 |
EXPOSE 7860
|
| 40 |
|
| 41 |
+
# Add network verification script
|
| 42 |
+
RUN echo '#!/bin/sh\n\
|
| 43 |
+
ping -c 1 huggingface.co || ping -c 1 8.8.8.8\n\
|
| 44 |
+
' > /healthcheck.sh && chmod +x /healthcheck.sh
|
| 45 |
+
|
| 46 |
+
# Healthcheck with more lenient settings
|
| 47 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=5 \
|
| 48 |
+
CMD /healthcheck.sh || exit 1
|
| 49 |
|
| 50 |
# Command to run the application
|
| 51 |
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
"""
|
| 2 |
Advanced Agentic System Interface
|
| 3 |
-------------------------------
|
| 4 |
-
Provides
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
|
@@ -15,11 +18,13 @@ import os
|
|
| 15 |
import socket
|
| 16 |
import requests
|
| 17 |
from requests.adapters import HTTPAdapter, Retry
|
|
|
|
|
|
|
| 18 |
|
| 19 |
from agentic_system import AgenticSystem
|
| 20 |
-
from team_management import TeamManager
|
| 21 |
from orchestrator import AgentOrchestrator
|
| 22 |
-
from reasoning import UnifiedReasoningEngine
|
| 23 |
|
| 24 |
# Configure logging
|
| 25 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -27,144 +32,377 @@ logger = logging.getLogger(__name__)
|
|
| 27 |
|
| 28 |
# Configure network settings
|
| 29 |
TIMEOUT = int(os.getenv('REQUESTS_TIMEOUT', '30'))
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def __init__(self):
|
| 45 |
-
"""Initialize the interface components."""
|
| 46 |
# Check network connectivity
|
| 47 |
if not check_network():
|
| 48 |
-
logger.warning("Network connectivity issues detected")
|
| 49 |
|
|
|
|
| 50 |
self.orchestrator = AgentOrchestrator()
|
| 51 |
-
self.
|
| 52 |
-
min_confidence=0.7,
|
| 53 |
-
parallel_threshold=3,
|
| 54 |
-
learning_rate=0.1
|
| 55 |
-
)
|
| 56 |
self.team_manager = TeamManager(self.orchestrator)
|
| 57 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
async def
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
response += "\nInsights:\n" + "\n".join(f"- {insight}" for insight in result.meta_insights)
|
| 76 |
else:
|
| 77 |
-
response =
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return response
|
| 80 |
-
|
| 81 |
except Exception as e:
|
| 82 |
-
logger.error(f"Error processing
|
| 83 |
-
return f"
|
| 84 |
|
| 85 |
-
def
|
| 86 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
return {
|
| 88 |
-
"
|
| 89 |
-
"
|
| 90 |
-
"
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
}
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
label="Response",
|
| 131 |
-
lines=10
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
# Add health check endpoint
|
| 161 |
-
demo.load(
|
| 162 |
-
fn=interface.health_check,
|
| 163 |
-
inputs=None,
|
| 164 |
-
outputs=None,
|
| 165 |
-
api_name="health"
|
| 166 |
-
)
|
| 167 |
|
| 168 |
-
# Launch the interface
|
| 169 |
if __name__ == "__main__":
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Advanced Agentic System Interface
|
| 3 |
-------------------------------
|
| 4 |
+
Provides a chat interface to interact with the autonomous agent teams:
|
| 5 |
+
- Team A: Coders (App/Software Developers)
|
| 6 |
+
- Team B: Business (Entrepreneurs)
|
| 7 |
+
- Team C: Research (Deep Online Research)
|
| 8 |
+
- Team D: Crypto & Sports Trading
|
| 9 |
"""
|
| 10 |
|
| 11 |
import gradio as gr
|
|
|
|
| 18 |
import socket
|
| 19 |
import requests
|
| 20 |
from requests.adapters import HTTPAdapter, Retry
|
| 21 |
+
from urllib3.util.retry import Retry
|
| 22 |
+
import time
|
| 23 |
|
| 24 |
from agentic_system import AgenticSystem
|
| 25 |
+
from team_management import TeamManager, TeamType, TeamObjective
|
| 26 |
from orchestrator import AgentOrchestrator
|
| 27 |
+
from reasoning import UnifiedReasoningEngine as ReasoningEngine
|
| 28 |
|
| 29 |
# Configure logging
|
| 30 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 32 |
|
| 33 |
# Configure network settings
|
| 34 |
TIMEOUT = int(os.getenv('REQUESTS_TIMEOUT', '30'))
|
| 35 |
+
MAX_RETRIES = 5
|
| 36 |
+
RETRY_BACKOFF = 1
|
| 37 |
+
|
| 38 |
+
def setup_requests_session():
|
| 39 |
+
"""Configure requests session with retries."""
|
| 40 |
+
session = requests.Session()
|
| 41 |
+
retry_strategy = Retry(
|
| 42 |
+
total=MAX_RETRIES,
|
| 43 |
+
backoff_factor=RETRY_BACKOFF,
|
| 44 |
+
status_forcelist=[408, 429, 500, 502, 503, 504],
|
| 45 |
+
allowed_methods=["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"]
|
| 46 |
+
)
|
| 47 |
+
adapter = HTTPAdapter(max_retries=retry_strategy)
|
| 48 |
+
session.mount("https://", adapter)
|
| 49 |
+
session.mount("http://", adapter)
|
| 50 |
+
return session
|
| 51 |
+
|
| 52 |
+
def check_network(max_attempts=3):
|
| 53 |
+
"""Check network connectivity with retries."""
|
| 54 |
+
session = setup_requests_session()
|
| 55 |
+
|
| 56 |
+
for attempt in range(max_attempts):
|
| 57 |
+
try:
|
| 58 |
+
# Try multiple DNS servers
|
| 59 |
+
for dns in ['8.8.8.8', '8.8.4.4', '1.1.1.1']:
|
| 60 |
+
try:
|
| 61 |
+
socket.gethostbyname('huggingface.co')
|
| 62 |
+
break
|
| 63 |
+
except socket.gaierror:
|
| 64 |
+
continue
|
| 65 |
+
|
| 66 |
+
# Test connection to Hugging Face
|
| 67 |
+
response = session.get('https://huggingface.co/api/health',
|
| 68 |
+
timeout=TIMEOUT)
|
| 69 |
+
if response.status_code == 200:
|
| 70 |
+
return True
|
| 71 |
+
|
| 72 |
+
except (requests.RequestException, socket.gaierror) as e:
|
| 73 |
+
logger.warning(f"Network check attempt {attempt + 1} failed: {e}")
|
| 74 |
+
if attempt < max_attempts - 1:
|
| 75 |
+
time.sleep(RETRY_BACKOFF * (attempt + 1))
|
| 76 |
+
continue
|
| 77 |
+
|
| 78 |
+
logger.error("Network connectivity check failed after all attempts")
|
| 79 |
+
return False
|
| 80 |
+
|
| 81 |
+
class ChatInterface:
|
| 82 |
def __init__(self):
|
|
|
|
| 83 |
# Check network connectivity
|
| 84 |
if not check_network():
|
| 85 |
+
logger.warning("Network connectivity issues detected - continuing with degraded functionality")
|
| 86 |
|
| 87 |
+
# Initialize core components
|
| 88 |
self.orchestrator = AgentOrchestrator()
|
| 89 |
+
self.agentic_system = AgenticSystem()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
self.team_manager = TeamManager(self.orchestrator)
|
| 91 |
+
self.chat_history = []
|
| 92 |
+
self.active_objectives = {}
|
| 93 |
+
|
| 94 |
+
# Set up network session
|
| 95 |
+
self.session = setup_requests_session()
|
| 96 |
+
|
| 97 |
+
# Initialize teams
|
| 98 |
+
asyncio.run(self.team_manager.initialize_team_agents())
|
| 99 |
|
| 100 |
+
async def process_message(
|
| 101 |
+
self,
|
| 102 |
+
message: str,
|
| 103 |
+
history: List[List[str]]
|
| 104 |
+
) -> str:
|
| 105 |
+
"""Process incoming chat message."""
|
| 106 |
try:
|
| 107 |
+
# Check network before processing
|
| 108 |
+
if not check_network():
|
| 109 |
+
return "Network connectivity issues detected. Some features may be limited."
|
| 110 |
+
|
| 111 |
+
# Analyze message intent
|
| 112 |
+
intent = await self._analyze_intent(message)
|
| 113 |
+
|
| 114 |
+
if intent["type"] == "query":
|
| 115 |
+
response = await self._handle_query(message)
|
| 116 |
+
elif intent["type"] == "objective":
|
| 117 |
+
response = await self._handle_objective(message)
|
| 118 |
+
elif intent["type"] == "status":
|
| 119 |
+
response = await self._handle_status_request(message)
|
|
|
|
| 120 |
else:
|
| 121 |
+
response = await self._handle_general_chat(message)
|
| 122 |
+
|
| 123 |
+
# Update chat history
|
| 124 |
+
self.chat_history.append({
|
| 125 |
+
"role": "user",
|
| 126 |
+
"content": message,
|
| 127 |
+
"timestamp": datetime.now()
|
| 128 |
+
})
|
| 129 |
+
self.chat_history.append({
|
| 130 |
+
"role": "assistant",
|
| 131 |
+
"content": response,
|
| 132 |
+
"timestamp": datetime.now()
|
| 133 |
+
})
|
| 134 |
+
|
| 135 |
return response
|
| 136 |
+
|
| 137 |
except Exception as e:
|
| 138 |
+
logger.error(f"Error processing message: {str(e)}")
|
| 139 |
+
return f"Error processing message: {str(e)}"
|
| 140 |
|
| 141 |
+
async def _analyze_intent(self, message: str) -> Dict[str, Any]:
|
| 142 |
+
"""Analyze user message intent."""
|
| 143 |
+
# Use reasoning engine to analyze intent
|
| 144 |
+
analysis = await self.orchestrator.reasoning_engine.reason(
|
| 145 |
+
query=message,
|
| 146 |
+
context={
|
| 147 |
+
"chat_history": self.chat_history,
|
| 148 |
+
"active_objectives": self.active_objectives
|
| 149 |
+
}
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
return {
|
| 153 |
+
"type": analysis.get("intent_type", "general"),
|
| 154 |
+
"confidence": analysis.get("confidence", 0.5),
|
| 155 |
+
"entities": analysis.get("entities", []),
|
| 156 |
+
"action_required": analysis.get("action_required", False)
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
async def _handle_query(self, message: str) -> str:
|
| 160 |
+
"""Handle information queries."""
|
| 161 |
+
# Get relevant teams for the query
|
| 162 |
+
recommended_teams = await self.team_manager.get_team_recommendations(message)
|
| 163 |
+
|
| 164 |
+
# Get responses from relevant teams
|
| 165 |
+
responses = []
|
| 166 |
+
for team_type in recommended_teams:
|
| 167 |
+
team_response = await self._get_team_response(team_type, message)
|
| 168 |
+
responses.append(team_response)
|
| 169 |
+
|
| 170 |
+
# Combine and format responses
|
| 171 |
+
combined_response = self._format_team_responses(responses)
|
| 172 |
+
|
| 173 |
+
return combined_response
|
| 174 |
+
|
| 175 |
+
async def _handle_objective(self, message: str) -> str:
|
| 176 |
+
"""Handle new objective creation."""
|
| 177 |
+
# Analyze objective requirements
|
| 178 |
+
analysis = await self.orchestrator.reasoning_engine.reason(
|
| 179 |
+
query=f"Analyze objective requirements: {message}",
|
| 180 |
+
context={"teams": self.team_manager.teams}
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
# Determine required teams
|
| 184 |
+
required_teams = [
|
| 185 |
+
TeamType[team.upper()]
|
| 186 |
+
for team in analysis.get("required_teams", [])
|
| 187 |
+
]
|
| 188 |
+
|
| 189 |
+
# Create cross-team objective
|
| 190 |
+
objective_id = await self.team_manager.create_cross_team_objective(
|
| 191 |
+
objective=message,
|
| 192 |
+
required_teams=required_teams
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
self.active_objectives[objective_id] = {
|
| 196 |
+
"description": message,
|
| 197 |
+
"teams": required_teams,
|
| 198 |
+
"status": "initiated",
|
| 199 |
+
"created_at": datetime.now()
|
| 200 |
}
|
| 201 |
+
|
| 202 |
+
return self._format_objective_creation(objective_id)
|
| 203 |
|
| 204 |
+
async def _handle_status_request(self, message: str) -> str:
|
| 205 |
+
"""Handle status check requests."""
|
| 206 |
+
# Get system status
|
| 207 |
+
system_status = await self.agentic_system.get_system_status()
|
| 208 |
+
|
| 209 |
+
# Get team status
|
| 210 |
+
team_status = {}
|
| 211 |
+
for team_id, team in self.team_manager.teams.items():
|
| 212 |
+
team_status[team.name] = await self.team_manager.monitor_objective_progress(team_id)
|
| 213 |
+
|
| 214 |
+
# Get objective status
|
| 215 |
+
objective_status = {}
|
| 216 |
+
for obj_id, obj in self.active_objectives.items():
|
| 217 |
+
objective_status[obj_id] = await self.team_manager.monitor_objective_progress(obj_id)
|
| 218 |
+
|
| 219 |
+
return self._format_status_response(system_status, team_status, objective_status)
|
| 220 |
|
| 221 |
+
async def _handle_general_chat(self, message: str) -> str:
|
| 222 |
+
"""Handle general chat interactions."""
|
| 223 |
+
# Use reasoning engine for response generation
|
| 224 |
+
response = await self.orchestrator.reasoning_engine.reason(
|
| 225 |
+
query=message,
|
| 226 |
+
context={
|
| 227 |
+
"chat_history": self.chat_history,
|
| 228 |
+
"system_state": await self.agentic_system.get_system_status()
|
| 229 |
+
}
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
return response.get("response", "I'm not sure how to respond to that.")
|
| 233 |
+
|
| 234 |
+
async def _get_team_response(self, team_type: TeamType, query: str) -> Dict[str, Any]:
|
| 235 |
+
"""Get response from a specific team."""
|
| 236 |
+
team_id = next(
|
| 237 |
+
(tid for tid, team in self.team_manager.teams.items()
|
| 238 |
+
if team.type == team_type),
|
| 239 |
+
None
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
if not team_id:
|
| 243 |
+
return {
|
| 244 |
+
"team": team_type.value,
|
| 245 |
+
"response": "Team not available",
|
| 246 |
+
"confidence": 0.0
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
# Get team agents
|
| 250 |
+
team_agents = self.team_manager.agents[team_id]
|
| 251 |
+
|
| 252 |
+
# Aggregate responses from team agents
|
| 253 |
+
responses = []
|
| 254 |
+
for agent in team_agents.values():
|
| 255 |
+
agent_response = await agent.process_query(query)
|
| 256 |
+
responses.append(agent_response)
|
| 257 |
+
|
| 258 |
+
# Combine responses
|
| 259 |
+
combined_response = self._combine_agent_responses(responses)
|
| 260 |
+
|
| 261 |
+
return {
|
| 262 |
+
"team": team_type.value,
|
| 263 |
+
"response": combined_response,
|
| 264 |
+
"confidence": sum(r.get("confidence", 0) for r in responses) / len(responses)
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
def _combine_agent_responses(self, responses: List[Dict[str, Any]]) -> str:
|
| 268 |
+
"""Combine multiple agent responses into a coherent response."""
|
| 269 |
+
# Sort by confidence
|
| 270 |
+
valid_responses = [
|
| 271 |
+
r for r in responses
|
| 272 |
+
if r.get("success", False) and r.get("response")
|
| 273 |
+
]
|
| 274 |
+
|
| 275 |
+
if not valid_responses:
|
| 276 |
+
return "No valid response available"
|
| 277 |
+
|
| 278 |
+
sorted_responses = sorted(
|
| 279 |
+
valid_responses,
|
| 280 |
+
key=lambda x: x.get("confidence", 0),
|
| 281 |
+
reverse=True
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
# Take the highest confidence response
|
| 285 |
+
best_response = sorted_responses[0]
|
| 286 |
+
|
| 287 |
+
return best_response.get("response", "No response available")
|
| 288 |
+
|
| 289 |
+
def _format_team_responses(self, responses: List[Dict[str, Any]]) -> str:
|
| 290 |
+
"""Format team responses into a readable message."""
|
| 291 |
+
formatted = []
|
| 292 |
+
|
| 293 |
+
for response in responses:
|
| 294 |
+
if response.get("confidence", 0) > 0.3: # Confidence threshold
|
| 295 |
+
formatted.append(
|
| 296 |
+
f"Team {response['team'].title()}:\n"
|
| 297 |
+
f"{response['response']}\n"
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
if not formatted:
|
| 301 |
+
return "No team was able to provide a confident response."
|
| 302 |
+
|
| 303 |
+
return "\n".join(formatted)
|
| 304 |
+
|
| 305 |
+
def _format_objective_creation(self, objective_id: str) -> str:
|
| 306 |
+
"""Format objective creation response."""
|
| 307 |
+
objective = self.active_objectives[objective_id]
|
| 308 |
+
|
| 309 |
+
return (
|
| 310 |
+
f"Objective created successfully!\n\n"
|
| 311 |
+
f"Objective ID: {objective_id}\n"
|
| 312 |
+
f"Description: {objective['description']}\n"
|
| 313 |
+
f"Assigned Teams: {', '.join(t.value for t in objective['teams'])}\n"
|
| 314 |
+
f"Status: {objective['status']}\n"
|
| 315 |
+
f"Created: {objective['created_at'].strftime('%Y-%m-%d %H:%M:%S')}"
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
def _format_status_response(
|
| 319 |
+
self,
|
| 320 |
+
system_status: Dict[str, Any],
|
| 321 |
+
team_status: Dict[str, Any],
|
| 322 |
+
objective_status: Dict[str, Any]
|
| 323 |
+
) -> str:
|
| 324 |
+
"""Format status response."""
|
| 325 |
+
# Format system status
|
| 326 |
+
status = [
|
| 327 |
+
"System Status:",
|
| 328 |
+
f"- State: {system_status['state']}",
|
| 329 |
+
f"- Active Agents: {system_status['agent_count']}",
|
| 330 |
+
f"- Active Tasks: {system_status['active_tasks']}",
|
| 331 |
+
"\nTeam Status:"
|
| 332 |
+
]
|
| 333 |
+
|
| 334 |
+
# Add team status
|
| 335 |
+
for team_name, team_info in team_status.items():
|
| 336 |
+
status.extend([
|
| 337 |
+
f"\n{team_name}:",
|
| 338 |
+
f"- Active Agents: {team_info['active_agents']}",
|
| 339 |
+
f"- Completion Rate: {team_info['completion_rate']:.2%}",
|
| 340 |
+
f"- Collaboration Score: {team_info['collaboration_score']:.2f}"
|
| 341 |
+
])
|
| 342 |
+
|
| 343 |
+
# Add objective status
|
| 344 |
+
if objective_status:
|
| 345 |
+
status.append("\nActive Objectives:")
|
| 346 |
+
for obj_id, obj_info in objective_status.items():
|
| 347 |
+
obj = self.active_objectives[obj_id]
|
| 348 |
+
status.extend([
|
| 349 |
+
f"\n{obj['description']}:",
|
| 350 |
+
f"- Status: {obj['status']}",
|
| 351 |
+
f"- Teams: {', '.join(t.value for t in obj['teams'])}",
|
| 352 |
+
f"- Progress: {sum(t['completion_rate'] for t in obj_info.values())/len(obj_info):.2%}"
|
| 353 |
+
])
|
| 354 |
+
|
| 355 |
+
return "\n".join(status)
|
| 356 |
+
|
| 357 |
+
class VentureUI:
|
| 358 |
+
def __init__(self, app):
|
| 359 |
+
self.app = app
|
| 360 |
+
|
| 361 |
+
def create_interface(self):
|
| 362 |
+
return gr.Interface(
|
| 363 |
+
fn=self.app,
|
| 364 |
+
inputs=[
|
| 365 |
+
gr.Textbox(
|
| 366 |
+
label="Message",
|
| 367 |
+
placeholder="Chat with the Agentic System...",
|
| 368 |
+
lines=2
|
| 369 |
+
),
|
| 370 |
+
gr.State([]) # For chat history
|
| 371 |
+
],
|
| 372 |
+
outputs=gr.Textbox(
|
| 373 |
label="Response",
|
| 374 |
+
lines=10
|
| 375 |
+
),
|
| 376 |
+
title="Advanced Agentic System Chat Interface",
|
| 377 |
+
description="""
|
| 378 |
+
Chat with our autonomous agent teams:
|
| 379 |
+
- Team A: Coders (App/Software Developers)
|
| 380 |
+
- Team B: Business (Entrepreneurs)
|
| 381 |
+
- Team C: Research (Deep Online Research)
|
| 382 |
+
- Team D: Crypto & Sports Trading
|
| 383 |
+
|
| 384 |
+
You can:
|
| 385 |
+
1. Ask questions
|
| 386 |
+
2. Create new objectives
|
| 387 |
+
3. Check status of teams and objectives
|
| 388 |
+
4. Get insights and recommendations
|
| 389 |
+
""",
|
| 390 |
+
theme=gr.themes.Soft(),
|
| 391 |
+
allow_flagging="never"
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
def create_chat_interface() -> gr.Interface:
|
| 395 |
+
"""Create Gradio chat interface."""
|
| 396 |
+
chat = ChatInterface()
|
| 397 |
+
ui = VentureUI(chat.process_message)
|
| 398 |
+
return ui.create_interface()
|
| 399 |
+
|
| 400 |
+
# Create and launch the interface
|
| 401 |
+
interface = create_chat_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
|
|
|
| 403 |
if __name__ == "__main__":
|
| 404 |
+
interface.launch(
|
| 405 |
+
server_name="0.0.0.0",
|
| 406 |
+
server_port=7860,
|
| 407 |
+
share=False
|
| 408 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
fastapi>=0.68.0
|
| 2 |
uvicorn>=0.15.0
|
| 3 |
pydantic>=2.0.0
|
| 4 |
-
gradio
|
| 5 |
llama-cpp-python>=0.2.23
|
| 6 |
huggingface-hub>=0.19.4
|
| 7 |
numpy>=1.24.0
|
|
@@ -17,3 +17,5 @@ joblib==1.3.2
|
|
| 17 |
tqdm>=4.66.0
|
| 18 |
python-dotenv>=0.19.0
|
| 19 |
httpx>=0.24.0
|
|
|
|
|
|
|
|
|
| 1 |
fastapi>=0.68.0
|
| 2 |
uvicorn>=0.15.0
|
| 3 |
pydantic>=2.0.0
|
| 4 |
+
gradio>=5.11.0
|
| 5 |
llama-cpp-python>=0.2.23
|
| 6 |
huggingface-hub>=0.19.4
|
| 7 |
numpy>=1.24.0
|
|
|
|
| 17 |
tqdm>=4.66.0
|
| 18 |
python-dotenv>=0.19.0
|
| 19 |
httpx>=0.24.0
|
| 20 |
+
requests>=2.31.0
|
| 21 |
+
urllib3>=2.0.7
|