Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,19 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
from gradio.themes.base import Base
|
| 5 |
|
| 6 |
-
# Import the agent logic
|
| 7 |
-
# This assumes agent.py is in the same directory.
|
| 8 |
-
from agent import
|
| 9 |
|
| 10 |
# -----------------------------------------------------------------------------
|
| 11 |
# Configuration
|
| 12 |
# -----------------------------------------------------------------------------
|
| 13 |
|
| 14 |
-
# The base URL for the
|
| 15 |
-
|
| 16 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 17 |
|
| 18 |
-
# IMPORTANT: Replace this with the URL of your Hugging Face Space.
|
| 19 |
# This link is submitted to the benchmark for verification and reproducibility.
|
| 20 |
-
# Example: "https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME"
|
| 21 |
AGENT_CODE_LINK = "https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME"
|
| 22 |
|
| 23 |
# -----------------------------------------------------------------------------
|
|
@@ -26,7 +24,7 @@ AGENT_CODE_LINK = "https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME"
|
|
| 26 |
|
| 27 |
# Read the agent.py file content to display it in the UI for transparency
|
| 28 |
try:
|
| 29 |
-
with open("agent.py", "r", encoding="utf-8") as f:
|
| 30 |
AGENT_CODE = f.read()
|
| 31 |
except FileNotFoundError:
|
| 32 |
AGENT_CODE = "Could not load agent.py. Please ensure the file is in the same directory."
|
|
@@ -39,14 +37,14 @@ seafoam = Seafoam()
|
|
| 39 |
|
| 40 |
# Markdown content for the UI description
|
| 41 |
UI_DESCRIPTION = """
|
| 42 |
-
#
|
| 43 |
-
This Gradio application runs
|
| 44 |
|
| 45 |
**How it works:**
|
| 46 |
-
1. **Login:** You must log in with your Hugging Face account using the button below. This is required for submission to the
|
| 47 |
2. **Run Evaluation:** Click the "Run Evaluation & Submit All Answers" button.
|
| 48 |
-
3. **Process:** The application will fetch all test questions from the
|
| 49 |
-
4. **Results:** The final score
|
| 50 |
|
| 51 |
Below is the core agent logic (`agent.py`) used for this evaluation.
|
| 52 |
"""
|
|
@@ -58,13 +56,14 @@ Below is the core agent logic (`agent.py`) used for this evaluation.
|
|
| 58 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 59 |
"""
|
| 60 |
Orchestrates the entire evaluation process:
|
| 61 |
-
1. Fetches questions from the
|
| 62 |
2. Runs the agent on each question.
|
| 63 |
3. Submits the answers and returns the final score.
|
| 64 |
|
| 65 |
This function is a generator, yielding status updates to the UI for a responsive experience.
|
| 66 |
"""
|
| 67 |
# Step 1: Verify user is logged in using Hugging Face OAuth.
|
|
|
|
| 68 |
if not profile:
|
| 69 |
yield "Error: You must be logged in with your Hugging Face account to submit. Please use the login button at the top."
|
| 70 |
return
|
|
@@ -72,12 +71,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 72 |
username = profile.username
|
| 73 |
yield f"β
Logged in as **{username}**. Starting evaluation..."
|
| 74 |
|
| 75 |
-
# Step 2: Fetch all questions from the
|
| 76 |
try:
|
| 77 |
-
yield "Fetching questions from the API..."
|
| 78 |
-
response = requests.get(f"{
|
| 79 |
response.raise_for_status()
|
| 80 |
-
questions = response.json()
|
| 81 |
yield f"β
Successfully fetched {len(questions)} questions."
|
| 82 |
except requests.exceptions.RequestException as e:
|
| 83 |
yield f"β Error fetching questions: {e}. Please check your connection or the API status."
|
|
@@ -91,55 +90,64 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 91 |
total_questions = len(questions)
|
| 92 |
for i, task in enumerate(questions):
|
| 93 |
task_id = task["task_id"]
|
|
|
|
| 94 |
question_text = task["question"]
|
| 95 |
|
| 96 |
-
# Clean the workspace directory for each new question to prevent contamination from previous tasks
|
| 97 |
-
if os.path.exists(WORKSPACE_DIR):
|
| 98 |
-
for item in os.listdir(WORKSPACE_DIR):
|
| 99 |
-
item_path = os.path.join(WORKSPACE_DIR, item)
|
| 100 |
-
if os.path.isfile(item_path):
|
| 101 |
-
os.remove(item_path)
|
| 102 |
-
|
| 103 |
yield f"--- \nβοΈ **Processing Question {i + 1}/{total_questions} (Task ID: {task_id})** \n> {question_text}"
|
| 104 |
|
| 105 |
try:
|
| 106 |
# Invoke the agent logic imported from agent.py
|
| 107 |
-
submitted_answer =
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
yield f"β‘οΈ Agent answered: **{submitted_answer}**"
|
| 110 |
except Exception as e:
|
| 111 |
yield f"β An error occurred while running the agent on task {task_id}: {e}"
|
| 112 |
-
# Submit a placeholder answer to not break the submission format
|
| 113 |
-
answers.append({"task_id": task_id, "
|
| 114 |
continue
|
| 115 |
|
| 116 |
# Step 4: Submit all collected answers to the API
|
| 117 |
submission_payload = {
|
| 118 |
"username": username,
|
| 119 |
-
"agent_code": AGENT_CODE, # Submit the full agent code for verification
|
| 120 |
"answers": answers,
|
|
|
|
| 121 |
}
|
| 122 |
|
| 123 |
yield "\n---\nπ All questions processed. Submitting answers to the leaderboard..."
|
| 124 |
try:
|
| 125 |
-
response = requests.post(f"{
|
| 126 |
response.raise_for_status()
|
| 127 |
result = response.json()
|
| 128 |
|
| 129 |
# Display the final score and results using Markdown for rich formatting
|
| 130 |
-
final_score = result.get("
|
| 131 |
-
|
|
|
|
| 132 |
result_text = "## π Submission Successful! π\n\n"
|
| 133 |
if final_score is not None:
|
| 134 |
-
result_text += f"**
|
| 135 |
else:
|
| 136 |
-
result_text += "**Final Score:** Not available
|
| 137 |
-
|
| 138 |
-
result_text += "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
yield result_text
|
| 140 |
|
| 141 |
except requests.exceptions.RequestException as e:
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
yield f"β An unexpected error occurred during submission: {e}"
|
| 145 |
|
|
@@ -147,7 +155,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 147 |
# -----------------------------------------------------------------------------
|
| 148 |
# Gradio Interface Definition
|
| 149 |
# -----------------------------------------------------------------------------
|
| 150 |
-
with gr.Blocks(theme=seafoam, title="
|
| 151 |
gr.Markdown(UI_DESCRIPTION)
|
| 152 |
|
| 153 |
with gr.Row():
|
|
@@ -169,11 +177,13 @@ with gr.Blocks(theme=seafoam, title="HF Agents Course Final Project") as demo:
|
|
| 169 |
output_textbox = gr.Markdown(
|
| 170 |
label="Evaluation Progress & Final Score",
|
| 171 |
value="Click the run button to start the evaluation process.",
|
|
|
|
| 172 |
)
|
| 173 |
|
| 174 |
# Connect the button click to the main function.
|
| 175 |
-
#
|
| 176 |
-
#
|
|
|
|
| 177 |
run_button.click(
|
| 178 |
fn=run_and_submit_all,
|
| 179 |
outputs=[output_textbox],
|
|
|
|
| 3 |
import requests
|
| 4 |
from gradio.themes.base import Base
|
| 5 |
|
| 6 |
+
# Import the agent logic from agent.py
|
| 7 |
+
# This assumes agent.py, which contains the `solve` function, is in the same directory.
|
| 8 |
+
from agent import solve
|
| 9 |
|
| 10 |
# -----------------------------------------------------------------------------
|
| 11 |
# Configuration
|
| 12 |
# -----------------------------------------------------------------------------
|
| 13 |
|
| 14 |
+
# The base URL for the GAIA benchmark API
|
| 15 |
+
GAIA_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 16 |
|
| 17 |
+
# IMPORTANT: Replace this with the URL of your Hugging Face Space or public GitHub repository.
|
| 18 |
# This link is submitted to the benchmark for verification and reproducibility.
|
|
|
|
| 19 |
AGENT_CODE_LINK = "https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME"
|
| 20 |
|
| 21 |
# -----------------------------------------------------------------------------
|
|
|
|
| 24 |
|
| 25 |
# Read the agent.py file content to display it in the UI for transparency
|
| 26 |
try:
|
| 27 |
+
with open("agent.py", "r", encoding="utf-utf-8") as f:
|
| 28 |
AGENT_CODE = f.read()
|
| 29 |
except FileNotFoundError:
|
| 30 |
AGENT_CODE = "Could not load agent.py. Please ensure the file is in the same directory."
|
|
|
|
| 37 |
|
| 38 |
# Markdown content for the UI description
|
| 39 |
UI_DESCRIPTION = """
|
| 40 |
+
# GAIA Benchmark Evaluation Agent
|
| 41 |
+
This Gradio application runs a multi-tool AI agent designed to solve the GAIA (General AI Assistants) benchmark.
|
| 42 |
|
| 43 |
**How it works:**
|
| 44 |
+
1. **Login:** You must log in with your Hugging Face account using the button below. This is required for submission to the official leaderboard.
|
| 45 |
2. **Run Evaluation:** Click the "Run Evaluation & Submit All Answers" button.
|
| 46 |
+
3. **Process:** The application will fetch all test-level questions from the GAIA API, run the agent for each question to find an answer, and then submit all answers to the leaderboard.
|
| 47 |
+
4. **Results:** The final score will be displayed in the output box. The entire process may take several minutes to complete.
|
| 48 |
|
| 49 |
Below is the core agent logic (`agent.py`) used for this evaluation.
|
| 50 |
"""
|
|
|
|
| 56 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 57 |
"""
|
| 58 |
Orchestrates the entire evaluation process:
|
| 59 |
+
1. Fetches questions from the GAIA API.
|
| 60 |
2. Runs the agent on each question.
|
| 61 |
3. Submits the answers and returns the final score.
|
| 62 |
|
| 63 |
This function is a generator, yielding status updates to the UI for a responsive experience.
|
| 64 |
"""
|
| 65 |
# Step 1: Verify user is logged in using Hugging Face OAuth.
|
| 66 |
+
# The `profile` object is automatically passed by Gradio due to the function's type hint.
|
| 67 |
if not profile:
|
| 68 |
yield "Error: You must be logged in with your Hugging Face account to submit. Please use the login button at the top."
|
| 69 |
return
|
|
|
|
| 71 |
username = profile.username
|
| 72 |
yield f"β
Logged in as **{username}**. Starting evaluation..."
|
| 73 |
|
| 74 |
+
# Step 2: Fetch all questions from the GAIA API
|
| 75 |
try:
|
| 76 |
+
yield "Fetching questions from the GAIA API..."
|
| 77 |
+
response = requests.get(f"{GAIA_API_URL}/questions")
|
| 78 |
response.raise_for_status()
|
| 79 |
+
questions = response.json()["questions"]
|
| 80 |
yield f"β
Successfully fetched {len(questions)} questions."
|
| 81 |
except requests.exceptions.RequestException as e:
|
| 82 |
yield f"β Error fetching questions: {e}. Please check your connection or the API status."
|
|
|
|
| 90 |
total_questions = len(questions)
|
| 91 |
for i, task in enumerate(questions):
|
| 92 |
task_id = task["task_id"]
|
| 93 |
+
# Correctly access the question text using the 'question' key.
|
| 94 |
question_text = task["question"]
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
yield f"--- \nβοΈ **Processing Question {i + 1}/{total_questions} (Task ID: {task_id})** \n> {question_text}"
|
| 97 |
|
| 98 |
try:
|
| 99 |
# Invoke the agent logic imported from agent.py
|
| 100 |
+
submitted_answer = solve(question_text)
|
| 101 |
+
|
| 102 |
+
# **CORRECTED PAYLOAD STRUCTURE**
|
| 103 |
+
# Each answer must be a dictionary with 'task_id' and 'submitted_answer' keys.
|
| 104 |
+
answers.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 105 |
+
|
| 106 |
yield f"β‘οΈ Agent answered: **{submitted_answer}**"
|
| 107 |
except Exception as e:
|
| 108 |
yield f"β An error occurred while running the agent on task {task_id}: {e}"
|
| 109 |
+
# Submit a placeholder answer to not break the submission format, using the correct keys.
|
| 110 |
+
answers.append({"task_id": task_id, "submitted_answer": "AGENT_ERROR"})
|
| 111 |
continue
|
| 112 |
|
| 113 |
# Step 4: Submit all collected answers to the API
|
| 114 |
submission_payload = {
|
| 115 |
"username": username,
|
|
|
|
| 116 |
"answers": answers,
|
| 117 |
+
"code_link": AGENT_CODE_LINK,
|
| 118 |
}
|
| 119 |
|
| 120 |
yield "\n---\nπ All questions processed. Submitting answers to the leaderboard..."
|
| 121 |
try:
|
| 122 |
+
response = requests.post(f"{GAIA_API_URL}/submit", json=submission_payload)
|
| 123 |
response.raise_for_status()
|
| 124 |
result = response.json()
|
| 125 |
|
| 126 |
# Display the final score and results using Markdown for rich formatting
|
| 127 |
+
final_score = result.get("final_score")
|
| 128 |
+
score_breakdown = result.get("score_breakdown", {})
|
| 129 |
+
|
| 130 |
result_text = "## π Submission Successful! π\n\n"
|
| 131 |
if final_score is not None:
|
| 132 |
+
result_text += f"**Final Score:** {final_score:.2f}%\n\n"
|
| 133 |
else:
|
| 134 |
+
result_text += "**Final Score:** Not available\n\n"
|
| 135 |
+
|
| 136 |
+
result_text += "**Score Breakdown by Level:**\n"
|
| 137 |
+
if score_breakdown:
|
| 138 |
+
for level, score in score_breakdown.items():
|
| 139 |
+
result_text += f"- **Level {level}:** {score:.2f}%\n"
|
| 140 |
+
else:
|
| 141 |
+
result_text += "- No breakdown available.\n"
|
| 142 |
+
|
| 143 |
yield result_text
|
| 144 |
|
| 145 |
except requests.exceptions.RequestException as e:
|
| 146 |
+
error_text = f"β Error submitting answers: {e}."
|
| 147 |
+
# Provide detailed error from API response if available
|
| 148 |
+
if 'response' in locals() and hasattr(response, 'text'):
|
| 149 |
+
error_text += f" API Response: `{response.text}`"
|
| 150 |
+
yield error_text
|
| 151 |
except Exception as e:
|
| 152 |
yield f"β An unexpected error occurred during submission: {e}"
|
| 153 |
|
|
|
|
| 155 |
# -----------------------------------------------------------------------------
|
| 156 |
# Gradio Interface Definition
|
| 157 |
# -----------------------------------------------------------------------------
|
| 158 |
+
with gr.Blocks(theme=seafoam, title="GAIA Benchmark Agent Evaluation") as demo:
|
| 159 |
gr.Markdown(UI_DESCRIPTION)
|
| 160 |
|
| 161 |
with gr.Row():
|
|
|
|
| 177 |
output_textbox = gr.Markdown(
|
| 178 |
label="Evaluation Progress & Final Score",
|
| 179 |
value="Click the run button to start the evaluation process.",
|
| 180 |
+
rtl=False
|
| 181 |
)
|
| 182 |
|
| 183 |
# Connect the button click to the main function.
|
| 184 |
+
# As per Gradio best practices, no `inputs` are specified. Gradio automatically
|
| 185 |
+
# passes the OAuth profile because of the `profile: gr.OAuthProfile | None`
|
| 186 |
+
# type hint in the `run_and_submit_all` function signature.
|
| 187 |
run_button.click(
|
| 188 |
fn=run_and_submit_all,
|
| 189 |
outputs=[output_textbox],
|