| import os |
| import requests |
| import pandas as pd |
| import gradio as gr |
| from typing import Optional |
|
|
| |
| from smolagents import CodeAgent, LiteLLMModel, VisitWebpageTool, DuckDuckGoSearchTool |
|
|
| |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| |
| class BasicAgent: |
| def __init__(self): |
| print("π Initializing Mistral-Powered Agent...") |
| |
| |
| mistral_key = os.getenv("MISTRAL_API_KEY") |
| if not mistral_key: |
| |
| print("β οΈ Mistral Key not found. Please set MISTRAL_API_KEY for best results.") |
| |
| raise ValueError("β οΈ MISTRAL_API_KEY missing! Settings -> Secrets me add karo.") |
|
|
| |
| model = LiteLLMModel( |
| model_id="mistral/mistral-large-latest", |
| api_key=mistral_key |
| ) |
|
|
| |
| search_tool = DuckDuckGoSearchTool() |
| visit_tool = VisitWebpageTool() |
|
|
| |
| self.agent = CodeAgent( |
| tools=[search_tool, visit_tool], |
| model=model, |
| additional_authorized_imports=[ |
| "numpy", "pandas", "math", "datetime", "re", "csv", "json", "random", "itertools" |
| ], |
| max_steps=25, |
| verbosity_level=2, |
| |
| name="Mistral_Gaia_Solver" |
| ) |
|
|
| def __call__(self, question: str, file_path: str = None) -> str: |
| |
| prompt = f""" |
| Task: {question} |
| |
| INSTRUCTIONS: |
| 1. Use Python code to solve this step-by-step. |
| 2. If a file is attached, YOU MUST READ IT using Python immediately. |
| 3. Output ONLY the final answer value. |
| """ |
| |
| if file_path: |
| prompt += f"\n\nβ οΈ ATTACHED FILE: '{file_path}'" |
|
|
| try: |
| print(f"π€ Agent working on: {question[:30]}...") |
| response = self.agent.run(prompt) |
| |
| |
| final_answer = str(response).replace("Final Answer:", "").strip() |
| |
| if final_answer.endswith(".") and len(final_answer) < 20: |
| final_answer = final_answer[:-1] |
| |
| return final_answer |
| |
| except Exception as e: |
| print(f"β Error in Agent: {e}") |
| return f"Error: {e}" |
|
|
| |
| def run_and_submit_all(profile: gr.OAuthProfile | None): |
| """ |
| 1. Questions fetch karega. |
| 2. FILE DOWNLOAD karega (Ye missing tha pehle). |
| 3. Agent run karega. |
| 4. Submit karega. |
| """ |
| |
| |
| if profile is None: |
| return "β οΈ Please Login to Hugging Face with the button above.", None |
| |
| username = profile.username |
| space_id = os.getenv("SPACE_ID") |
| |
| |
| api_url = DEFAULT_API_URL |
| questions_url = f"{api_url}/questions" |
| submit_url = f"{api_url}/submit" |
|
|
| |
| try: |
| agent = BasicAgent() |
| except Exception as e: |
| return f"β Agent Init Error: {e}", None |
|
|
| agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main" |
| print(f"π Code Link: {agent_code_link}") |
|
|
| |
| try: |
| print("π₯ Fetching questions...") |
| questions_data = requests.get(questions_url).json() |
| except Exception as e: |
| return f"Error fetching questions: {e}", None |
|
|
| results_log = [] |
| answers_payload = [] |
|
|
| print(f"π Starting processing of {len(questions_data)} questions...") |
|
|
| |
| for item in questions_data: |
| task_id = item["task_id"] |
| question_text = item["question"] |
| file_name = item.get("file_name") |
|
|
| print(f"\n--- Processing Task {task_id} ---") |
| |
| local_file_path = None |
|
|
| |
| if file_name: |
| print(f"π Downloading file: {file_name}") |
| try: |
| file_url = f"{api_url}/files/{task_id}" |
| file_resp = requests.get(file_url, timeout=10) |
| |
| if file_resp.status_code == 200: |
| with open(file_name, "wb") as f: |
| f.write(file_resp.content) |
| local_file_path = file_name |
| print("β
File downloaded successfully.") |
| else: |
| print(f"β File download failed (Status {file_resp.status_code})") |
| except Exception as e: |
| print(f"β File download error: {e}") |
|
|
| |
| try: |
| |
| submitted_answer = agent(question_text, file_path=local_file_path) |
| |
| print(f"π‘ Final Answer: {submitted_answer}") |
| |
| answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) |
| results_log.append({ |
| "Task ID": task_id, |
| "Question": question_text, |
| "File": file_name if file_name else "None", |
| "Answer": submitted_answer |
| }) |
| |
| except Exception as e: |
| results_log.append({"Task ID": task_id, "Error": str(e)}) |
|
|
| |
| if local_file_path and os.path.exists(local_file_path): |
| os.remove(local_file_path) |
|
|
| |
| print("π€ Submitting answers to leaderboard...") |
| submission_data = { |
| "username": username, |
| "agent_code": agent_code_link, |
| "answers": answers_payload |
| } |
|
|
| try: |
| response = requests.post(submit_url, json=submission_data, timeout=60) |
| res_json = response.json() |
| |
| score = res_json.get('score', 0) |
| correct = res_json.get('correct_count', 0) |
| |
| status_msg = ( |
| f"β
Submission Done!\n" |
| f"User: {username}\n" |
| f"π Score: {score}%\n" |
| f"Correct: {correct}" |
| ) |
| return status_msg, pd.DataFrame(results_log) |
|
|
| except Exception as e: |
| return f"β Submission Failed: {e}", pd.DataFrame(results_log) |
|
|
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# π€ GAIA Agent Solver (Mistral + Files Fix)") |
| gr.Markdown(""" |
| **Instruction:** |
| 1. Login via Hugging Face button. |
| 2. Click 'Run Evaluation'. |
| 3. Wait (it takes time to process all questions). |
| """) |
| |
| gr.LoginButton() |
| |
| run_btn = gr.Button("Run Evaluation & Submit", variant="primary") |
| |
| status_out = gr.Textbox(label="Status") |
| results_df = gr.DataFrame(label="Detailed Logs") |
| |
| run_btn.click( |
| fn=run_and_submit_all, |
| outputs=[status_out, results_df] |
| ) |
|
|
| if __name__ == "__main__": |
| |
| demo.queue(default_concurrency_limit=1).launch() |