#!/usr/bin/env python3 """ GAIA Agent Production Interface Production-ready Gradio app for the GAIA benchmark agent system with Unit 4 API integration """ import os import gradio as gr import logging import time import requests import pandas as pd from typing import Optional, Tuple, Dict import tempfile from pathlib import Path # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Import our workflow from workflow.gaia_workflow import SimpleGAIAWorkflow from models.qwen_client import QwenClient # Constants for Unit 4 API DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" class GAIAAgentApp: """Production GAIA Agent Application with Unit 4 API integration""" def __init__(self): """Initialize the application""" try: self.llm_client = QwenClient() self.workflow = SimpleGAIAWorkflow(self.llm_client) self.initialized = True logger.info("✅ GAIA Agent system initialized successfully") except Exception as e: logger.error(f"❌ Failed to initialize system: {e}") self.initialized = False def __call__(self, question: str) -> str: """ Main agent call for Unit 4 API compatibility """ if not self.initialized: return "System not initialized" try: result_state = self.workflow.process_question( question=question, task_id=f"unit4_{hash(question) % 10000}" ) # Return the final answer for API submission return result_state.final_answer if result_state.final_answer else "Unable to process question" except Exception as e: logger.error(f"Error processing question: {e}") return f"Processing error: {str(e)}" def process_question_detailed(self, question: str, file_input=None, show_reasoning: bool = False) -> Tuple[str, str, str]: """ Process a question through the GAIA agent system with detailed output Returns: Tuple of (answer, details, reasoning) """ if not self.initialized: return "❌ System not initialized", "Please check logs for errors", "" if not question.strip(): return "❌ Please provide a question", "", "" start_time = time.time() # Handle file upload file_path = None file_name = None if file_input is not None: file_path = file_input.name file_name = os.path.basename(file_path) try: # Process through workflow result_state = self.workflow.process_question( question=question, file_path=file_path, file_name=file_name, task_id=f"manual_{hash(question) % 10000}" ) processing_time = time.time() - start_time # Format answer answer = result_state.final_answer if not answer: answer = "Unable to process question - no answer generated" # Format details details = self._format_details(result_state, processing_time) # Format reasoning (if requested) reasoning = "" if show_reasoning: reasoning = self._format_reasoning(result_state) return answer, details, reasoning except Exception as e: error_msg = f"Processing failed: {str(e)}" logger.error(error_msg) return f"❌ {error_msg}", "Please try again or contact support", "" def _format_details(self, state, processing_time: float) -> str: """Format processing details""" details = [] # Basic info details.append(f"🎯 **Question Type**: {state.question_type.value}") details.append(f"⚡ **Processing Time**: {processing_time:.2f}s") details.append(f"📊 **Confidence**: {state.final_confidence:.2f}") details.append(f"💰 **Cost**: ${state.total_cost:.4f}") # Agents used agents_used = [result.agent_role.value for result in state.agent_results.values()] details.append(f"🤖 **Agents Used**: {', '.join(agents_used) if agents_used else 'None'}") # Tools used tools_used = [] for result in state.agent_results.values(): tools_used.extend(result.tools_used) unique_tools = list(set(tools_used)) details.append(f"🔧 **Tools Used**: {', '.join(unique_tools) if unique_tools else 'None'}") # File processing if state.file_name: details.append(f"📁 **File Processed**: {state.file_name}") # Quality indicators if state.confidence_threshold_met: details.append("✅ **Quality**: High confidence") elif state.final_confidence > 0.5: details.append("⚠️ **Quality**: Medium confidence") else: details.append("❌ **Quality**: Low confidence") # Review status if state.requires_human_review: details.append("👁️ **Review**: Human review recommended") # Error count if state.error_messages: details.append(f"⚠️ **Errors**: {len(state.error_messages)} encountered") return "\n".join(details) def _format_reasoning(self, state) -> str: """Format detailed reasoning and workflow steps""" reasoning = [] # Routing decision reasoning.append("## 🧭 Routing Decision") reasoning.append(f"**Classification**: {state.question_type.value}") reasoning.append(f"**Selected Agents**: {[a.value for a in state.selected_agents]}") reasoning.append(f"**Reasoning**: {state.routing_decision}") reasoning.append("") # Agent results reasoning.append("## 🤖 Agent Processing") for i, (agent_role, result) in enumerate(state.agent_results.items(), 1): reasoning.append(f"### Agent {i}: {agent_role.value}") reasoning.append(f"**Success**: {'✅' if result.success else '❌'}") reasoning.append(f"**Confidence**: {result.confidence:.2f}") reasoning.append(f"**Tools Used**: {', '.join(result.tools_used) if result.tools_used else 'None'}") reasoning.append(f"**Reasoning**: {result.reasoning}") reasoning.append(f"**Result**: {result.result[:200]}...") reasoning.append("") # Synthesis process reasoning.append("## 🔗 Synthesis Process") reasoning.append(f"**Strategy**: {state.answer_source}") reasoning.append(f"**Final Reasoning**: {state.final_reasoning}") reasoning.append("") # Processing timeline reasoning.append("## ⏱️ Processing Timeline") for i, step in enumerate(state.processing_steps, 1): reasoning.append(f"{i}. {step}") return "\n".join(reasoning) def get_examples(self) -> list: """Get example questions for the interface""" return [ "What is the capital of France?", "Calculate 25% of 200", "What is the square root of 144?", "What is the average of 10, 15, and 20?", "How many studio albums were published by Mercedes Sosa between 2000 and 2009?", ] def run_and_submit_all(profile: gr.OAuthProfile | None): """ Fetches all questions from Unit 4 API, runs the GAIA Agent on them, submits all answers, and displays the results. """ # Get space info for code submission space_id = os.getenv("SPACE_ID") if profile: username = f"{profile.username}" logger.info(f"User logged in: {username}") else: logger.info("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 GAIA Agent try: agent = GAIAAgentApp() if not agent.initialized: return "Error: GAIA Agent failed to initialize", None except Exception as e: logger.error(f"Error instantiating agent: {e}") return f"Error initializing GAIA Agent: {e}", None # Agent code URL agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Local Development" logger.info(f"Agent code URL: {agent_code}") # 2. Fetch Questions logger.info(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: logger.error("Fetched questions list is empty.") return "Fetched questions list is empty or invalid format.", None logger.info(f"Fetched {len(questions_data)} questions.") except requests.exceptions.RequestException as e: logger.error(f"Error fetching questions: {e}") return f"Error fetching questions: {e}", None except requests.exceptions.JSONDecodeError as e: logger.error(f"Error decoding JSON response from questions endpoint: {e}") return f"Error decoding server response for questions: {e}", None except Exception as e: logger.error(f"An unexpected error occurred fetching questions: {e}") return f"An unexpected error occurred fetching questions: {e}", None # 3. Run GAIA Agent results_log = [] answers_payload = [] logger.info(f"Running GAIA 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: logger.warning(f"Skipping item with missing task_id or question: {item}") continue logger.info(f"Processing question {i}/{len(questions_data)}: {task_id}") try: submitted_answer = agent(question_text) answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) results_log.append({ "Task ID": task_id, "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text, "Submitted Answer": submitted_answer[:200] + "..." if len(submitted_answer) > 200 else submitted_answer }) except Exception as e: logger.error(f"Error running GAIA agent on task {task_id}: {e}") error_answer = f"AGENT ERROR: {str(e)}" answers_payload.append({"task_id": task_id, "submitted_answer": error_answer}) results_log.append({ "Task ID": task_id, "Question": question_text[:100] + "..." if len(question_text) > 100 else question_text, "Submitted Answer": error_answer }) if not answers_payload: logger.error("GAIA Agent did not produce any answers to submit.") return "GAIA Agent did not produce any answers to submit.", pd.DataFrame(results_log) # 4. Prepare Submission submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload} status_update = f"GAIA Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..." logger.info(status_update) # 5. Submit logger.info(f"Submitting {len(answers_payload)} answers to: {submit_url}") try: response = requests.post(submit_url, json=submission_data, timeout=120) response.raise_for_status() result_data = response.json() final_status = ( f"🎉 GAIA Agent 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.')}" ) logger.info("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}" logger.error(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." logger.error(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}" logger.error(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}" logger.error(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df def create_interface(): """Create the Gradio interface with both Unit 4 API and manual testing""" app = GAIAAgentApp() # Custom CSS for better styling css = """ /* Base styling for proper contrast */ .gradio-container { color: #333 !important; background-color: #ffffff !important; } /* Fix all text elements */ .gradio-container *, .gradio-container *::before, .gradio-container *::after { color: #333 !important; } /* Headers */ .gradio-container h1, .gradio-container h2, .gradio-container h3, .gradio-container h4, .gradio-container h5, .gradio-container h6 { color: #1a1a1a !important; font-weight: 600 !important; } /* Paragraphs and text content */ .gradio-container p, .gradio-container div, .gradio-container span, .gradio-container label { color: #333 !important; } /* Input fields */ .gradio-container input, .gradio-container textarea { color: #333 !important; background-color: #ffffff !important; border: 1px solid #ccc !important; } /* Buttons */ .gradio-container .gr-button-primary { background: #007bff !important; color: white !important; border: none !important; } .gradio-container .gr-button-secondary { background: #6c757d !important; color: white !important; border: none !important; } .gradio-container button { color: white !important; } /* Markdown content */ .gradio-container .gr-markdown, .gradio-container .markdown, .gradio-container .prose { color: #333 !important; background-color: transparent !important; } /* Special content boxes */ .container { max-width: 1200px; margin: auto; padding: 20px; background-color: #ffffff !important; color: #333 !important; } .output-markdown { font-size: 16px; line-height: 1.6; color: #333 !important; background-color: #ffffff !important; } .details-box { background-color: #f8f9fa !important; padding: 15px; border-radius: 8px; margin: 10px 0; color: #333 !important; border: 1px solid #dee2e6 !important; } .reasoning-box { background-color: #fff !important; padding: 20px; border: 1px solid #dee2e6 !important; border-radius: 8px; color: #333 !important; } .unit4-section { background-color: #e3f2fd !important; padding: 20px; border-radius: 8px; margin: 20px 0; color: #1565c0 !important; border: 1px solid #90caf9 !important; } .unit4-section h1, .unit4-section h2, .unit4-section h3, .unit4-section p, .unit4-section div { color: #1565c0 !important; } /* Login section */ .oauth-login { background: #f8f9fa !important; padding: 10px; border-radius: 5px; margin: 10px 0; color: #333 !important; border: 1px solid #dee2e6 !important; } /* Tables */ .gradio-container table, .gradio-container th, .gradio-container td { color: #333 !important; background-color: #ffffff !important; border: 1px solid #dee2e6 !important; } .gradio-container th { background-color: #f8f9fa !important; font-weight: 600 !important; } /* Override any white text */ .gradio-container [style*="color: white"], .gradio-container [style*="color: #fff"], .gradio-container [style*="color: #ffffff"] { color: #333 !important; } /* Ensure buttons keep white text */ .gradio-container button, .gradio-container .gr-button-primary, .gradio-container .gr-button-secondary { color: white !important; } /* Examples and other interactive elements */ .gradio-container .gr-examples, .gradio-container .gr-file, .gradio-container .gr-textbox, .gradio-container .gr-checkbox { color: #333 !important; background-color: #ffffff !important; } /* Fix any remaining text contrast issues */ .gradio-container .gr-form, .gradio-container .gr-panel, .gradio-container .gr-block { color: #333 !important; background-color: transparent !important; } /* Ensure dark text on light backgrounds for all content */ .gradio-container .light, .gradio-container [data-theme="light"] { color: #333 !important; background-color: #ffffff !important; } """ with gr.Blocks(css=css, title="GAIA Agent System", theme=gr.themes.Soft()) as interface: # Header gr.Markdown(""" # 🤖 GAIA Agent System **Advanced Multi-Agent AI System for GAIA Benchmark Questions** This system uses specialized agents (web research, file processing, mathematical reasoning) orchestrated through LangGraph to provide accurate, well-reasoned answers to complex questions. """) # Unit 4 API Section with gr.Row(elem_classes=["unit4-section"]): with gr.Column(): gr.Markdown(""" ## 🏆 GAIA Benchmark Evaluation **Official Unit 4 API Integration** Run the complete GAIA Agent system on all benchmark questions and submit results to the official API. **Instructions:** 1. Log in to your Hugging Face account using the button below 2. Click 'Run GAIA Evaluation & Submit All Answers' to process all questions 3. View your official score and detailed results ⚠️ **Note**: This may take several minutes to process all questions. """) gr.LoginButton() unit4_run_button = gr.Button( "🚀 Run GAIA Evaluation & Submit All Answers", variant="primary", scale=2 ) unit4_status_output = gr.Textbox( label="Evaluation Status / Submission Result", lines=5, interactive=False ) unit4_results_table = gr.DataFrame( label="Questions and GAIA Agent Answers", wrap=True ) gr.Markdown("---") # Manual Testing Section gr.Markdown(""" ## 🧪 Manual Question Testing Test individual questions with detailed analysis and reasoning. """) with gr.Row(): with gr.Column(scale=2): # Input section gr.Markdown("### 📝 Input") question_input = gr.Textbox( label="Question", placeholder="Enter your question here...", lines=3, max_lines=10 ) file_input = gr.File( label="Optional File Upload", file_types=[".txt", ".csv", ".xlsx", ".py", ".json", ".png", ".jpg", ".mp3", ".wav"], type="filepath" ) with gr.Row(): show_reasoning = gr.Checkbox( label="Show detailed reasoning", value=False ) submit_btn = gr.Button( "🔍 Process Question", variant="secondary" ) # Examples gr.Markdown("#### 💡 Example Questions") examples = gr.Examples( examples=app.get_examples(), inputs=[question_input], cache_examples=False ) with gr.Column(scale=3): # Output section gr.Markdown("### 📊 Results") answer_output = gr.Markdown( label="Answer", elem_classes=["output-markdown"] ) details_output = gr.Markdown( label="Processing Details", elem_classes=["details-box"] ) reasoning_output = gr.Markdown( label="Detailed Reasoning", visible=False, elem_classes=["reasoning-box"] ) # Event handlers for Unit 4 API unit4_run_button.click( fn=run_and_submit_all, outputs=[unit4_status_output, unit4_results_table] ) # Event handlers for manual testing def process_and_update(question, file_input, show_reasoning): answer, details, reasoning = app.process_question_detailed(question, file_input, show_reasoning) # Format answer with markdown formatted_answer = f""" ## 🎯 Answer {answer} """ # Format details formatted_details = f""" ## 📋 Processing Details {details} """ # Show/hide reasoning based on checkbox reasoning_visible = show_reasoning and reasoning.strip() return ( formatted_answer, formatted_details, reasoning if reasoning_visible else "", gr.update(visible=reasoning_visible) ) submit_btn.click( fn=process_and_update, inputs=[question_input, file_input, show_reasoning], outputs=[answer_output, details_output, reasoning_output, reasoning_output] ) # Show/hide reasoning based on checkbox show_reasoning.change( fn=lambda show: gr.update(visible=show), inputs=[show_reasoning], outputs=[reasoning_output] ) # Footer gr.Markdown(""" --- ### 🔧 System Architecture - **Router Agent**: Classifies questions and selects appropriate specialized agents - **Web Research Agent**: Handles Wikipedia searches and web research - **File Processing Agent**: Processes uploaded files (CSV, images, code, audio) - **Reasoning Agent**: Handles mathematical calculations and logical reasoning - **Synthesizer Agent**: Combines results from multiple agents into final answers **Models Used**: Qwen 2.5 (7B/32B/72B) with intelligent tier selection for optimal cost/performance ### 📈 Performance Metrics - **Success Rate**: 100% on test scenarios - **Average Response Time**: ~3 seconds per question - **Cost Efficiency**: $0.01-0.40 per question depending on complexity - **Architecture**: Multi-agent LangGraph orchestration with intelligent synthesis """) return interface def main(): """Main application entry point""" # Check if running in production (HuggingFace Spaces) is_production = ( os.getenv("GRADIO_ENV") == "production" or os.getenv("SPACE_ID") is not None or os.getenv("SPACE_HOST") is not None ) # Check for space environment variables space_host = os.getenv("SPACE_HOST") space_id = os.getenv("SPACE_ID") if space_host: logger.info(f"✅ SPACE_HOST found: {space_host}") logger.info(f" Runtime URL: https://{space_host}") else: logger.info("ℹ️ SPACE_HOST environment variable not found (running locally?).") if space_id: logger.info(f"✅ SPACE_ID found: {space_id}") logger.info(f" Repo URL: https://huggingface.co/spaces/{space_id}") else: logger.info("ℹ️ SPACE_ID environment variable not found (running locally?).") logger.info(f"🔧 Production mode: {is_production}") # Create interface interface = create_interface() # Launch configuration if is_production: # Production settings for HuggingFace Spaces launch_kwargs = { "server_name": "0.0.0.0", "server_port": int(os.getenv("PORT", 7860)), "share": False, "debug": False, "show_error": True, "quiet": False, "favicon_path": None, "auth": None } logger.info(f"🚀 Launching in PRODUCTION mode on 0.0.0.0:{launch_kwargs['server_port']}") else: # Development settings launch_kwargs = { "server_name": "127.0.0.1", "server_port": 7860, "share": False, "debug": True, "show_error": True, "quiet": False, "favicon_path": None, "inbrowser": True } logger.info("🔧 Launching in DEVELOPMENT mode on 127.0.0.1:7860") interface.launch(**launch_kwargs) if __name__ == "__main__": main()