Update app.py
Browse files
app.py
CHANGED
|
@@ -9,91 +9,104 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 9 |
|
| 10 |
class BasicAgent:
|
| 11 |
def __init__(self):
|
| 12 |
-
#
|
| 13 |
-
#
|
|
|
|
| 14 |
self.model = InferenceClientModel(model_id="Qwen/Qwen2.5-72B-Instruct")
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
| 17 |
self.agent = CodeAgent(
|
| 18 |
tools=[],
|
| 19 |
model=self.model,
|
| 20 |
-
add_base_tools=
|
| 21 |
)
|
| 22 |
-
print("Agent initialized with
|
| 23 |
|
| 24 |
def __call__(self, question: str) -> str:
|
| 25 |
-
#
|
|
|
|
| 26 |
clean_prompt = (
|
| 27 |
-
f"
|
| 28 |
-
"Final Answer
|
| 29 |
-
"
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
# The agent will
|
| 34 |
result = self.agent.run(clean_prompt)
|
| 35 |
return str(result).strip()
|
| 36 |
except Exception as e:
|
| 37 |
-
print(f"
|
| 38 |
-
return "Error"
|
| 39 |
|
| 40 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 41 |
-
# Determine the Space ID for the code link
|
| 42 |
space_id = os.getenv("SPACE_ID")
|
| 43 |
|
| 44 |
if not profile:
|
| 45 |
-
return "Please Login to Hugging Face
|
| 46 |
|
| 47 |
-
username = profile.username
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# 1. Fetch
|
| 51 |
try:
|
| 52 |
-
response = requests.get(
|
| 53 |
response.raise_for_status()
|
| 54 |
questions_data = response.json()
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error fetching questions: {e}", None
|
| 57 |
|
| 58 |
-
# 2.
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
results_log = []
|
|
|
|
| 62 |
|
| 63 |
-
print(f"
|
| 64 |
|
| 65 |
for item in questions_data:
|
| 66 |
task_id = item.get("task_id")
|
| 67 |
question_text = item.get("question")
|
| 68 |
|
| 69 |
-
if not task_id:
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
"Task ID": task_id,
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
})
|
| 80 |
-
|
| 81 |
-
# 3. Submit to the leaderboard
|
| 82 |
submission_data = {
|
| 83 |
-
"username": username,
|
| 84 |
"agent_code": agent_code,
|
| 85 |
"answers": answers_payload
|
| 86 |
}
|
| 87 |
|
| 88 |
try:
|
| 89 |
-
response = requests.post(
|
| 90 |
response.raise_for_status()
|
| 91 |
result_data = response.json()
|
| 92 |
|
| 93 |
final_status = (
|
| 94 |
f"Submission Successful!\n"
|
| 95 |
f"User: {result_data.get('username')}\n"
|
| 96 |
-
f"Score: {result_data.get('score', 0)}% "
|
| 97 |
f"({result_data.get('correct_count', 0)}/{result_data.get('total_attempted', 0)} correct)"
|
| 98 |
)
|
| 99 |
return final_status, pd.DataFrame(results_log)
|
|
@@ -102,14 +115,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 102 |
|
| 103 |
# --- Gradio UI ---
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
-
gr.Markdown("# GAIA
|
| 106 |
-
gr.Markdown("Click Login
|
| 107 |
|
| 108 |
gr.LoginButton()
|
| 109 |
-
run_button = gr.Button("Run Evaluation & Submit", variant="primary")
|
| 110 |
|
| 111 |
-
status_output = gr.Textbox(label="Status
|
| 112 |
-
results_table = gr.DataFrame(label="
|
| 113 |
|
| 114 |
run_button.click(
|
| 115 |
fn=run_and_submit_all,
|
|
|
|
| 9 |
|
| 10 |
class BasicAgent:
|
| 11 |
def __init__(self):
|
| 12 |
+
# 1. Initialize the Model (the 'brain')
|
| 13 |
+
# This wrapper is the most stable version for HF Inference API in 2026.
|
| 14 |
+
# It will automatically use your HF_TOKEN secret if added to the Space.
|
| 15 |
self.model = InferenceClientModel(model_id="Qwen/Qwen2.5-72B-Instruct")
|
| 16 |
|
| 17 |
+
# 2. Initialize the CodeAgent (the 'body')
|
| 18 |
+
# - tools=[]: We start with no external tools.
|
| 19 |
+
# - add_base_tools=False: This prevents the 'ddgs' / DuckDuckGo error.
|
| 20 |
+
# Note: CodeAgent still has a built-in Python interpreter to solve math/logic!
|
| 21 |
self.agent = CodeAgent(
|
| 22 |
tools=[],
|
| 23 |
model=self.model,
|
| 24 |
+
add_base_tools=False
|
| 25 |
)
|
| 26 |
+
print("Agent successfully initialized with Python Interpreter (No ddgs needed).")
|
| 27 |
|
| 28 |
def __call__(self, question: str) -> str:
|
| 29 |
+
# 3. Prompting for Exact Match scoring
|
| 30 |
+
# We tell the agent to be as direct as possible.
|
| 31 |
clean_prompt = (
|
| 32 |
+
f"Question: {question}\n\n"
|
| 33 |
+
"Final Answer Requirement: Provide ONLY the numeric or text value of the answer. "
|
| 34 |
+
"Do not include any explanation, units, or 'The answer is' text. "
|
| 35 |
+
"Do not include 'FINAL ANSWER' in your output."
|
| 36 |
)
|
| 37 |
|
| 38 |
try:
|
| 39 |
+
# The agent will write and run Python code if the question requires it.
|
| 40 |
result = self.agent.run(clean_prompt)
|
| 41 |
return str(result).strip()
|
| 42 |
except Exception as e:
|
| 43 |
+
print(f"Error during agent execution: {e}")
|
| 44 |
+
return "Error solving question"
|
| 45 |
|
| 46 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 47 |
space_id = os.getenv("SPACE_ID")
|
| 48 |
|
| 49 |
if not profile:
|
| 50 |
+
return "Please Login to Hugging Face with the button.", None
|
| 51 |
|
| 52 |
+
username = f"{profile.username}"
|
| 53 |
+
api_url = DEFAULT_API_URL
|
| 54 |
+
questions_url = f"{api_url}/questions"
|
| 55 |
+
submit_url = f"{api_url}/submit"
|
| 56 |
+
|
| 57 |
+
# URL to your code for verification
|
| 58 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "https://huggingface.co/spaces"
|
| 59 |
|
| 60 |
+
# 1. Fetch Questions
|
| 61 |
try:
|
| 62 |
+
response = requests.get(questions_url, timeout=15)
|
| 63 |
response.raise_for_status()
|
| 64 |
questions_data = response.json()
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error fetching questions: {e}", None
|
| 67 |
|
| 68 |
+
# 2. Run Agent
|
| 69 |
+
# Instantiate inside the function to ensure a fresh session
|
| 70 |
+
try:
|
| 71 |
+
agent = BasicAgent()
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return f"Error initializing agent: {e}", None
|
| 74 |
+
|
| 75 |
results_log = []
|
| 76 |
+
answers_payload = []
|
| 77 |
|
| 78 |
+
print(f"Starting evaluation for {len(questions_data)} questions...")
|
| 79 |
|
| 80 |
for item in questions_data:
|
| 81 |
task_id = item.get("task_id")
|
| 82 |
question_text = item.get("question")
|
| 83 |
|
| 84 |
+
if not task_id or question_text is None:
|
| 85 |
+
continue
|
| 86 |
|
| 87 |
+
try:
|
| 88 |
+
submitted_answer = agent(question_text)
|
| 89 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 90 |
+
results_log.append({"Task ID": task_id, "Question": question_text[:80], "Submitted Answer": submitted_answer})
|
| 91 |
+
except Exception as e:
|
| 92 |
+
results_log.append({"Task ID": task_id, "Question": question_text[:80], "Submitted Answer": f"ERROR: {e}"})
|
| 93 |
+
|
| 94 |
+
# 3. Submit Results
|
|
|
|
|
|
|
|
|
|
| 95 |
submission_data = {
|
| 96 |
+
"username": username.strip(),
|
| 97 |
"agent_code": agent_code,
|
| 98 |
"answers": answers_payload
|
| 99 |
}
|
| 100 |
|
| 101 |
try:
|
| 102 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 103 |
response.raise_for_status()
|
| 104 |
result_data = response.json()
|
| 105 |
|
| 106 |
final_status = (
|
| 107 |
f"Submission Successful!\n"
|
| 108 |
f"User: {result_data.get('username')}\n"
|
| 109 |
+
f"Overall Score: {result_data.get('score', 0)}% "
|
| 110 |
f"({result_data.get('correct_count', 0)}/{result_data.get('total_attempted', 0)} correct)"
|
| 111 |
)
|
| 112 |
return final_status, pd.DataFrame(results_log)
|
|
|
|
| 115 |
|
| 116 |
# --- Gradio UI ---
|
| 117 |
with gr.Blocks() as demo:
|
| 118 |
+
gr.Markdown("# GAIA Final Evaluation Solver")
|
| 119 |
+
gr.Markdown("Click 'Login' then 'Run' to solve all questions and submit your score.")
|
| 120 |
|
| 121 |
gr.LoginButton()
|
| 122 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 123 |
|
| 124 |
+
status_output = gr.Textbox(label="Submission Status", lines=5)
|
| 125 |
+
results_table = gr.DataFrame(label="Agent Answers Log", wrap=True)
|
| 126 |
|
| 127 |
run_button.click(
|
| 128 |
fn=run_and_submit_all,
|