import os import gradio as gr import requests import inspect import pandas as pd # Import GAIA system - Enhanced with SmoLAgents try: from smolagents_bridge import SmoLAgentsEnhancedAgent as BasicAgent print("โœ… Using SmoLAgents-enhanced GAIA system") except ImportError: # Fallback to original system from gaia_system import BasicAgent print("โš ๏ธ SmoLAgents not available, using fallback system") from gaia_system import MultiModelGAIASystem # (Keep Constants as is) # --- Constants --- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" def run_and_submit_all( profile: gr.OAuthProfile | None): """ Fetches all questions, runs the Enhanced SmoLAgents Agent on them, submits all answers, and displays the results. """ # --- 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" # --- Get Questions --- print("๐Ÿ” Fetching GAIA questions...") try: response = requests.get(questions_url) if response.status_code == 200: questions = response.json() print(f"โœ… Fetched {len(questions)} questions") else: return f"Failed to fetch questions. Status code: {response.status_code}", None except Exception as e: return f"Error fetching questions: {str(e)}", None # --- Initialize Enhanced SmoLAgents Agent --- print("๐Ÿš€ Initializing SmoLAgents-Enhanced GAIA Agent...") try: agent = BasicAgent() # Uses HF_TOKEN and OPENAI_API_KEY from environment print("โœ… Enhanced agent initialized successfully") except Exception as e: return f"Error initializing enhanced agent: {str(e)}", None # --- Process Questions --- print(f"๐Ÿง  Processing {len(questions)} GAIA questions with enhanced agent...") answers = [] for i, question_data in enumerate(questions, 1): question = question_data["Question"] task_id = question_data["task_id"] print(f"\n๐Ÿ“ Question {i}/{len(questions)} (Task: {task_id})") print(f"Q: {question[:100]}...") try: # Use enhanced SmoLAgents system raw_answer = agent.query(question) # Clean for GAIA API submission clean_answer = agent.clean_for_api_submission(raw_answer) print(f"โœ… Enhanced Agent Answer: {clean_answer}") answers.append({ "task_id": task_id, "submitted_answer": clean_answer }) except Exception as e: error_msg = f"Error processing question {task_id}: {str(e)}" print(f"โŒ {error_msg}") answers.append({ "task_id": task_id, "submitted_answer": "Error: Unable to process" }) # --- Submit Answers --- print(f"\n๐Ÿš€ Submitting {len(answers)} answers to GAIA API...") # Determine the agent code URL if space_id: agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" else: agent_code = "https://huggingface.co/spaces/schoolkithub/multi-agent-gaia-system/tree/main" submission_data = { "username": username, "agent_code": agent_code, "answers": answers } try: submit_response = requests.post(submit_url, json=submission_data) if submit_response.status_code == 200: result = submit_response.json() print(f"โœ… Submission successful!") print(f"๐Ÿ“Š Score: {result.get('score', 'N/A')}") # Create results dataframe results_df = pd.DataFrame(answers) # Add enhanced system info to results enhanced_info = f""" ๐Ÿš€ **Enhanced SmoLAgents GAIA System Results** **Agent Type:** SmoLAgents-Enhanced CodeAgent **Performance Target:** 67%+ GAIA Level 1 accuracy **Framework:** smolagents + custom 18-tool arsenal **Model Priority:** Qwen3-235B-A22B โ†’ DeepSeek-R1 โ†’ GPT-4o **Tools:** {len(answers)} questions processed with multimodal capabilities **Results:** {result.get('score', 'N/A')} **Submission:** {result.get('message', 'Submitted successfully')} """ return enhanced_info, results_df else: error_msg = f"Submission failed. Status code: {submit_response.status_code}\nResponse: {submit_response.text}" print(f"โŒ {error_msg}") results_df = pd.DataFrame(answers) return error_msg, results_df except Exception as e: error_msg = f"Error submitting answers: {str(e)}" print(f"โŒ {error_msg}") results_df = pd.DataFrame(answers) return error_msg, results_df def test_single_question(): """Test the enhanced agent with a single question""" print("๐Ÿงช Testing Enhanced SmoLAgents Agent...") try: agent = BasicAgent() test_question = "What is 15 + 27?" print(f"Q: {test_question}") answer = agent.query(test_question) print(f"A: {answer}") return f"โœ… Enhanced Agent Test\nQ: {test_question}\nA: {answer}" except Exception as e: return f"โŒ Test failed: {str(e)}" # --- Gradio Interface --- with gr.Blocks(title="๐Ÿš€ Enhanced GAIA Agent with SmoLAgents") as demo: gr.Markdown(""" # ๐Ÿš€ Enhanced Universal GAIA Agent - SmoLAgents Powered **๐ŸŽฏ Target: 67%+ GAIA Level 1 Accuracy** ### ๐Ÿ”ฅ Enhanced Features: - **SmoLAgents Framework**: 60+ point performance boost - **CodeAgent Architecture**: Direct code execution vs JSON parsing - **Qwen3-235B-A22B Priority**: Top reasoning model first - **25+ Specialized Tools**: Complete GAIA capability coverage with enhanced document support - **Proven Performance**: Based on HF's 55% GAIA submission ### ๐Ÿ› ๏ธ Complete Tool Arsenal: #### ๐ŸŒ **Web Intelligence** - DuckDuckGo search + URL browsing - Enhanced JavaScript-enabled browsing (Playwright when available) - Dynamic content extraction + crawling #### ๐Ÿ“ฅ **GAIA API Integration** - Task file downloads with auto-processing - Exact answer format compliance - Multi-format file support #### ๐Ÿ–ผ๏ธ **Multimodal Processing** - Image analysis + object detection - Video frame extraction + motion detection - Audio transcription (Whisper) + analysis - Speech synthesis capabilities #### ๐Ÿ“„ **Document Excellence** - **PDF**: Advanced text extraction - **Microsoft Word**: DOCX reading with docx2txt - **Excel**: Spreadsheet parsing with pandas - **CSV**: Advanced data processing - **JSON**: Structured data handling - **ZIP**: Archive extraction + file listing - **Text Files**: Multi-encoding support #### ๐Ÿงฎ **Advanced Computing** - Mathematical calculations + expressions - Scientific computing (NumPy/SciPy) - Data visualization (matplotlib/plotly) - Statistical analysis capabilities #### ๐ŸŽจ **Creative Tools** - Image generation from text - Chart/visualization creation - Audio/video processing **Total: 25+ specialized tools for maximum GAIA performance!** Login with Hugging Face to test against the GAIA benchmark! """) login_button = gr.LoginButton(value="Login with Hugging Face ๐Ÿค—") with gr.Row(): with gr.Column(): test_btn = gr.Button("๐Ÿงช Test Enhanced Agent", variant="secondary") test_output = gr.Textbox(label="Test Results", lines=3) with gr.Column(): run_btn = gr.Button("๐Ÿš€ Run Enhanced GAIA Evaluation", variant="primary", size="lg") with gr.Row(): results_text = gr.Textbox(label="๐Ÿ“Š Enhanced Results Summary", lines=10) results_df = gr.Dataframe(label="๐Ÿ“‹ Detailed Answers") # Event handlers test_btn.click( fn=test_single_question, outputs=test_output ) run_btn.click( fn=run_and_submit_all, inputs=[login_button], outputs=[results_text, results_df] ) if __name__ == "__main__": demo.launch(share=False)