""" Advanced Agentic System Interface ------------------------------- Provides an interface to interact with the autonomous agent system using local LLM for improved performance. """ import gradio as gr import asyncio from typing import Dict, Any, List import json from datetime import datetime import logging import os import socket import requests from requests.adapters import HTTPAdapter, Retry from agentic_system import AgenticSystem from team_management import TeamManager from orchestrator import AgentOrchestrator from reasoning.unified_engine import UnifiedReasoningEngine # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Configure network settings TIMEOUT = int(os.getenv('REQUESTS_TIMEOUT', '30')) RETRIES = 3 # Configure session with retries session = requests.Session() retries = Retry(total=RETRIES, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504]) session.mount('https://', HTTPAdapter(max_retries=retries)) session.mount('http://', HTTPAdapter(max_retries=retries)) def check_network(): """Check network connectivity.""" try: # Try to resolve huggingface.co socket.gethostbyname('huggingface.co') # Try to connect to the API response = session.get('https://huggingface.co/api/health', timeout=TIMEOUT) response.raise_for_status() return True except Exception as e: logger.error(f"Network check failed: {e}") return False class AgentInterface: """Interface for the agentic system.""" def __init__(self): """Initialize the interface components.""" # Check network connectivity if not check_network(): logger.warning("Network connectivity issues detected") self.orchestrator = AgentOrchestrator() self.reasoning_engine = UnifiedReasoningEngine( min_confidence=0.7, parallel_threshold=3, learning_rate=0.1 ) async def process_query(self, message: str) -> str: """Process user query through the reasoning system.""" try: # Prepare context context = { 'timestamp': datetime.now().isoformat(), 'objective': 'Provide helpful and accurate responses', 'mode': 'analytical' } # Get response from reasoning engine result = await self.reasoning_engine.reason( query=message, context=context ) return result.get('answer', 'No response generated') except Exception as e: logger.error(f"Error processing query: {e}") return f"An error occurred: {str(e)}" # Initialize interface interface = AgentInterface() # Create Gradio interface with gr.Blocks(title="Advanced Reasoning System") as demo: gr.Markdown("# Advanced Reasoning System") with gr.Row(): with gr.Column(): input_text = gr.Textbox( label="Your Query", placeholder="Enter your query here...", lines=3 ) submit_btn = gr.Button("Submit") with gr.Column(): output_text = gr.Textbox( label="Response", lines=5 ) submit_btn.click( fn=interface.process_query, inputs=input_text, outputs=output_text ) # Start the server with health check endpoint app = gr.mount_gradio_app(None, demo, path="/") @app.get("/health") async def health_check(): """Health check endpoint.""" if check_network(): return {"status": "healthy"} return {"status": "degraded", "reason": "network_issues"} # Launch the interface if __name__ == "__main__": demo.launch()