""" Enhanced Hybrid Agent Evaluation Runner""" import os import inspect import gradio as gr import requests import pandas as pd from langchain_core.messages import HumanMessage from agent import HybridLangGraphAgnoSystem # --- Constants --- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # --- Enhanced Basic Agent Definition --- class BasicAgent: """A hybrid LangGraph + Agno agent with performance optimization.""" def __init__(self): print("BasicAgent initialized with Hybrid LangGraph + Agno System.") self.hybrid_system = HybridLangGraphAgnoSystem() def __call__(self, question: str) -> str: print(f"Agent received question: {question}") try: # Process query using hybrid system result = self.hybrid_system.process_query(question) # Extract final answer answer = result.get("answer", "No response generated") # Clean up the answer - extract only final answer if present if "FINAL ANSWER:" in answer: final_answer = answer.split("FINAL ANSWER:")[-1].strip() else: final_answer = answer.strip() # Log performance metrics for debugging metrics = result.get("performance_metrics", {}) provider = result.get("provider_used", "Unknown") processing_time = metrics.get("total_time", 0) print(f"Provider used: {provider}, Processing time: {processing_time:.2f}s") return final_answer except Exception as e: print(f"Error in agent processing: {e}") return f"Error: {str(e)}" def run_and_submit_all(profile: gr.OAuthProfile | None): """ Fetches all questions, runs the Enhanced Hybrid Agent on them, submits all answers, and displays the results with performance metrics. """ # --- Determine HF Space Runtime URL and Repo URL --- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code if profile: username= f"{profile.username}" print(f"User logged in: {username}") else: print("User not logged in.") return "Please Login to Hugging Face with the button.", None api_url = DEFAULT_API_URL questions_url = f"{api_url}/questions" submit_url = f"{api_url}/submit" # 1. Instantiate Enhanced Hybrid Agent try: agent = BasicAgent() print("āœ… Hybrid LangGraph + Agno Agent initialized successfully") except Exception as e: print(f"āŒ Error instantiating hybrid agent: {e}") return f"Error initializing hybrid agent: {e}", None agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" print(f"šŸ”— Agent code repository: {agent_code}") # 2. Fetch Questions print(f"šŸ“„ Fetching questions from: {questions_url}") try: response = requests.get(questions_url, timeout=15) response.raise_for_status() questions_data = response.json() if not questions_data: print("āŒ Fetched questions list is empty.") return "Fetched questions list is empty or invalid format.", None print(f"āœ… Fetched {len(questions_data)} questions successfully.") except requests.exceptions.RequestException as e: print(f"āŒ Error fetching questions: {e}") return f"Error fetching questions: {e}", None except Exception as e: print(f"āŒ An unexpected error occurred fetching questions: {e}") return f"An unexpected error occurred fetching questions: {e}", None # 3. Run Enhanced Hybrid Agent with Performance Tracking results_log = [] answers_payload = [] performance_stats = { "langgraph_math": 0, "agno_research": 0, "langgraph_retrieval": 0, "agno_general": 0, "errors": 0, "total_processing_time": 0 } print(f"šŸš€ Running Enhanced Hybrid Agent on {len(questions_data)} questions...") for i, item in enumerate(questions_data, 1): task_id = item.get("task_id") question_text = item.get("question") if not task_id or question_text is None: print(f"āš ļø Skipping item {i} with missing task_id or question: {item}") continue print(f"šŸ”„ Processing question {i}/{len(questions_data)}: {task_id}") try: # Get detailed result from hybrid system detailed_result = agent.hybrid_system.process_query(question_text) submitted_answer = detailed_result.get("answer", "No response") # Extract final answer if "FINAL ANSWER:" in submitted_answer: clean_answer = submitted_answer.split("FINAL ANSWER:")[-1].strip() else: clean_answer = submitted_answer.strip() # Track performance metrics provider = detailed_result.get("provider_used", "Unknown") processing_time = detailed_result.get("performance_metrics", {}).get("total_time", 0) # Update performance stats if "LangGraph" in provider: if "Math" in provider: performance_stats["langgraph_math"] += 1 else: performance_stats["langgraph_retrieval"] += 1 elif "Agno" in provider: if "Research" in provider: performance_stats["agno_research"] += 1 else: performance_stats["agno_general"] += 1 performance_stats["total_processing_time"] += processing_time answers_payload.append({"task_id": task_id, "submitted_answer": clean_answer}) results_log.append({ "Task ID": task_id, "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text, "Submitted Answer": clean_answer, "Provider": provider, "Processing Time (s)": f"{processing_time:.2f}" }) print(f"āœ… Question {i} processed successfully using {provider}") except Exception as e: print(f"āŒ Error running agent on task {task_id}: {e}") performance_stats["errors"] += 1 results_log.append({ "Task ID": task_id, "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text, "Submitted Answer": f"AGENT ERROR: {e}", "Provider": "Error", "Processing Time (s)": "0.00" }) if not answers_payload: print("āŒ Agent did not produce any answers to submit.") return "Agent did not produce any answers to submit.", pd.DataFrame(results_log) # 4. Performance Summary avg_processing_time = performance_stats["total_processing_time"] / len(answers_payload) if answers_payload else 0 performance_summary = f""" šŸ“Š Performance Summary: • LangGraph Math: {performance_stats['langgraph_math']} queries • Agno Research: {performance_stats['agno_research']} queries • LangGraph Retrieval: {performance_stats['langgraph_retrieval']} queries • Agno General: {performance_stats['agno_general']} queries • Errors: {performance_stats['errors']} queries • Average Processing Time: {avg_processing_time:.2f}s • Total Processing Time: {performance_stats['total_processing_time']:.2f}s """ print(performance_summary) # 5. Prepare Submission submission_data = { "username": username.strip(), "agent_code": agent_code, "answers": answers_payload } status_update = f"šŸŽÆ Hybrid Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..." print(status_update) # 6. Submit Results print(f"šŸ“¤ Submitting {len(answers_payload)} answers to: {submit_url}") try: response = requests.post(submit_url, json=submission_data, timeout=120) # Increased timeout response.raise_for_status() result_data = response.json() final_status = ( f"šŸŽ‰ Submission Successful!\n" f"šŸ‘¤ User: {result_data.get('username')}\n" f"šŸ† Overall Score: {result_data.get('score', 'N/A')}% " f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n" f"šŸ’¬ Message: {result_data.get('message', 'No message received.')}\n" f"{performance_summary}" ) print("āœ… Submission successful.") results_df = pd.DataFrame(results_log) return final_status, results_df except requests.exceptions.HTTPError as e: error_detail = f"Server responded with status {e.response.status_code}." try: error_json = e.response.json() error_detail += f" Detail: {error_json.get('detail', e.response.text)}" except requests.exceptions.JSONDecodeError: error_detail += f" Response: {e.response.text[:500]}" status_message = f"āŒ Submission Failed: {error_detail}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except requests.exceptions.Timeout: status_message = "āŒ Submission Failed: The request timed out." print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except requests.exceptions.RequestException as e: status_message = f"āŒ Submission Failed: Network error - {e}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except Exception as e: status_message = f"āŒ An unexpected error occurred during submission: {e}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df # --- Enhanced Gradio Interface --- with gr.Blocks(title="Enhanced Hybrid Agent Evaluation") as demo: gr.Markdown("# šŸš€ Enhanced Hybrid LangGraph + Agno Agent Evaluation Runner") gr.Markdown( """ ## šŸŽÆ **Advanced AI Agent System** This evaluation runner uses a **Hybrid LangGraph + Agno Agent System** that combines the best of both frameworks: ### 🧠 **Intelligent Routing System** - **šŸ”¢ Mathematical Queries** → LangGraph (Groq Llama 3.3 70B) - *Optimized for speed* - **šŸ” Complex Research** → Agno (Gemini 2.0 Flash-Lite) - *Optimized for reasoning* - **šŸ“š Factual Retrieval** → LangGraph + FAISS Vector Store - *Optimized for accuracy* - **šŸŽ­ General Queries** → Agno Multi-Agent System - *Optimized for comprehensiveness* ### ⚔ **Performance Features** - **Rate Limiting**: Intelligent rate management for free tier models - **Caching**: Performance optimization with query caching - **Fallback Systems**: Automatic provider switching on failures - **Performance Tracking**: Real-time metrics and provider usage stats ### šŸ›  **Tools & Capabilities** - Mathematical calculations (add, subtract, multiply, divide, modulus) - Web search (Tavily, Wikipedia, ArXiv) - FAISS vector database for similar question retrieval - Memory persistence across sessions --- **Instructions:** 1. šŸ” Log in to your Hugging Face account using the button below 2. šŸš€ Click 'Run Evaluation & Submit All Answers' to start the evaluation 3. šŸ“Š Monitor real-time performance metrics and provider usage 4. šŸ† View your final score and detailed results **Note:** The hybrid system automatically selects the optimal AI provider for each question type to maximize both speed and accuracy. """ ) gr.LoginButton() with gr.Row(): run_button = gr.Button( "šŸš€ Run Evaluation & Submit All Answers", variant="primary", size="lg" ) status_output = gr.Textbox( label="šŸ“Š Run Status / Submission Result", lines=10, interactive=False, placeholder="Status updates will appear here..." ) results_table = gr.DataFrame( label="šŸ“‹ Questions, Answers & Performance Metrics", wrap=True, height=400 ) run_button.click( fn=run_and_submit_all, outputs=[status_output, results_table] ) # Add footer with system info gr.Markdown( """ --- ### šŸ”§ **System Information** - **Primary Models**: Groq Llama 3.3 70B, Gemini 2.0 Flash-Lite, NVIDIA Llama 3.1 70B - **Frameworks**: LangGraph + Agno Hybrid Architecture - **Vector Store**: FAISS with NVIDIA Embeddings - **Rate Limiting**: Advanced rate management with exponential backoff - **Memory**: Persistent agent memory with session summaries """ ) if __name__ == "__main__": print("\n" + "="*80) print("šŸš€ ENHANCED HYBRID AGENT EVALUATION RUNNER") print("="*80) # Check for environment variables space_host_startup = os.getenv("SPACE_HOST") space_id_startup = os.getenv("SPACE_ID") if space_host_startup: print(f"āœ… SPACE_HOST found: {space_host_startup}") print(f" 🌐 Runtime URL: https://{space_host_startup}.hf.space") else: print("ā„¹ļø SPACE_HOST environment variable not found (running locally?).") if space_id_startup: print(f"āœ… SPACE_ID found: {space_id_startup}") print(f" šŸ“ Repo URL: https://huggingface.co/spaces/{space_id_startup}") print(f" 🌳 Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main") else: print("ā„¹ļø SPACE_ID environment variable not found (running locally?).") print("\nšŸŽÆ System Features:") print(" • Hybrid LangGraph + Agno Architecture") print(" • Intelligent Query Routing") print(" • Performance Optimization") print(" • Advanced Rate Limiting") print(" • FAISS Vector Database") print(" • Multi-Provider Fallbacks") print("\n" + "="*80) print("šŸŽ‰ Launching Enhanced Gradio Interface...") print("="*80 + "\n") demo.launch(debug=True, share=False)