Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,129 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool
|
| 6 |
|
| 7 |
+
# --- Constants ---
|
| 8 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
+
# --- Advanced Agent Definition ---
|
| 11 |
+
class BasicAgent:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
# Using a powerful model that can handle logic and tool use
|
| 14 |
+
self.model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct")
|
| 15 |
+
|
| 16 |
+
# We give the agent search capabilities and a Python interpreter
|
| 17 |
+
self.agent = CodeAgent(
|
| 18 |
+
tools=[DuckDuckGoSearchTool()],
|
| 19 |
+
model=self.model,
|
| 20 |
+
add_base_tools=True
|
| 21 |
+
)
|
| 22 |
+
print("Advanced smolagent initialized.")
|
| 23 |
+
|
| 24 |
+
def __call__(self, question: str) -> str:
|
| 25 |
+
# Prompting for Exact Match scoring
|
| 26 |
+
clean_prompt = (
|
| 27 |
+
f"Question: {question}\n\n"
|
| 28 |
+
"Instructions: Solve the question above. Provide ONLY the final answer "
|
| 29 |
+
"value without any explanation, units, or extra text. "
|
| 30 |
+
"Do not include 'FINAL ANSWER' in your response."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# The agent will reason and use tools if necessary
|
| 35 |
+
result = self.agent.run(clean_prompt)
|
| 36 |
+
# Ensure we return a clean string
|
| 37 |
+
return str(result).strip()
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Error during agent execution: {e}")
|
| 40 |
+
return "Error solving question"
|
| 41 |
+
|
| 42 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 43 |
+
space_id = os.getenv("SPACE_ID")
|
| 44 |
+
|
| 45 |
+
if not profile:
|
| 46 |
+
return "Please Login to Hugging Face with the button.", None
|
| 47 |
+
|
| 48 |
+
username = f"{profile.username}"
|
| 49 |
+
api_url = DEFAULT_API_URL
|
| 50 |
+
questions_url = f"{api_url}/questions"
|
| 51 |
+
submit_url = f"{api_url}/submit"
|
| 52 |
+
|
| 53 |
+
# 1. Instantiate Agent
|
| 54 |
+
try:
|
| 55 |
+
agent = BasicAgent()
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error initializing agent: {e}", None
|
| 58 |
+
|
| 59 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 60 |
+
|
| 61 |
+
# 2. Fetch Questions
|
| 62 |
+
try:
|
| 63 |
+
response = requests.get(questions_url, timeout=15)
|
| 64 |
+
response.raise_for_status()
|
| 65 |
+
questions_data = response.json()
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error fetching questions: {e}", None
|
| 68 |
+
|
| 69 |
+
# 3. Run Agent on Questions
|
| 70 |
+
results_log = []
|
| 71 |
+
answers_payload = []
|
| 72 |
+
|
| 73 |
+
for item in questions_data:
|
| 74 |
+
task_id = item.get("task_id")
|
| 75 |
+
question_text = item.get("question")
|
| 76 |
+
|
| 77 |
+
if not task_id or question_text is None:
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
# Optional: Handle file downloads for specific tasks
|
| 81 |
+
# You could add logic here to download from f"{api_url}/files/{task_id}"
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
submitted_answer = agent(question_text)
|
| 85 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 86 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 87 |
+
except Exception as e:
|
| 88 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 89 |
+
|
| 90 |
+
# 4. Submit Results
|
| 91 |
+
submission_data = {
|
| 92 |
+
"username": username.strip(),
|
| 93 |
+
"agent_code": agent_code,
|
| 94 |
+
"answers": answers_payload
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 99 |
+
response.raise_for_status()
|
| 100 |
+
result_data = response.json()
|
| 101 |
+
|
| 102 |
+
final_status = (
|
| 103 |
+
f"Submission Successful!\n"
|
| 104 |
+
f"User: {result_data.get('username')}\n"
|
| 105 |
+
f"Score: {result_data.get('score')}% "
|
| 106 |
+
f"({result_data.get('correct_count')}/{result_data.get('total_attempted')} correct)"
|
| 107 |
+
)
|
| 108 |
+
return final_status, pd.DataFrame(results_log)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
| 111 |
+
|
| 112 |
+
# --- Gradio Interface ---
|
| 113 |
+
with gr.Blocks() as demo:
|
| 114 |
+
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 115 |
+
gr.Markdown("This agent uses `smolagents` with a Python interpreter and Web Search to solve tasks.")
|
| 116 |
+
|
| 117 |
+
gr.LoginButton()
|
| 118 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 119 |
+
|
| 120 |
+
status_output = gr.Textbox(label="Status", lines=4)
|
| 121 |
+
results_table = gr.DataFrame(label="Detailed Results", wrap=True)
|
| 122 |
+
|
| 123 |
+
run_button.click(
|
| 124 |
+
fn=run_and_submit_all,
|
| 125 |
+
outputs=[status_output, results_table]
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
demo.launch()
|