Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,126 @@
|
|
| 1 |
# 17/08/2025 -AO
|
|
|
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 13 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
space_id = os.getenv("SPACE_ID")
|
| 18 |
|
| 19 |
if profile:
|
| 20 |
username = f"{profile.username}"
|
|
|
|
| 21 |
else:
|
|
|
|
| 22 |
return "Please Login to Hugging Face with the button.", None
|
| 23 |
|
| 24 |
api_url = DEFAULT_API_URL
|
| 25 |
questions_url = f"{api_url}/questions"
|
| 26 |
submit_url = f"{api_url}/submit"
|
| 27 |
|
| 28 |
-
# 1
|
| 29 |
try:
|
| 30 |
-
agent =
|
| 31 |
except Exception as e:
|
|
|
|
| 32 |
return f"Error initializing agent: {e}", None
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
# 2) Fetch Questions
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
questions_data =
|
| 43 |
if not questions_data:
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 46 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
|
|
|
| 48 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 49 |
|
| 50 |
-
# 3
|
| 51 |
results_log = []
|
| 52 |
answers_payload = []
|
|
|
|
| 53 |
for item in questions_data:
|
| 54 |
task_id = item.get("task_id")
|
| 55 |
question_text = item.get("question")
|
| 56 |
if not task_id or question_text is None:
|
|
|
|
| 57 |
continue
|
| 58 |
try:
|
| 59 |
submitted_answer = agent(question_text)
|
| 60 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 61 |
-
results_log.append({
|
| 62 |
-
"Task ID": task_id,
|
| 63 |
-
"Question": question_text,
|
| 64 |
-
"Submitted Answer": submitted_answer,
|
| 65 |
-
})
|
| 66 |
except Exception as e:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"Question": question_text,
|
| 70 |
-
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 71 |
-
})
|
| 72 |
|
| 73 |
if not answers_payload:
|
|
|
|
| 74 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 75 |
|
| 76 |
-
# 4
|
| 77 |
-
submission_data = {
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"answers": answers_payload,
|
| 81 |
-
}
|
| 82 |
|
|
|
|
|
|
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
result_data =
|
| 87 |
final_status = (
|
| 88 |
f"Submission Successful!\n"
|
| 89 |
f"User: {result_data.get('username')}\n"
|
|
@@ -91,47 +128,80 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 91 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 92 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 93 |
)
|
| 94 |
-
|
|
|
|
|
|
|
| 95 |
except requests.exceptions.HTTPError as e:
|
| 96 |
-
|
| 97 |
try:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
except
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
except requests.exceptions.Timeout:
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
except requests.exceptions.RequestException as e:
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
-
# --- Gradio
|
| 112 |
with gr.Blocks() as demo:
|
| 113 |
-
gr.Markdown("#
|
| 114 |
gr.Markdown(
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
**Instructions:**
|
| 118 |
-
1. Log in with your Hugging Face account.
|
| 119 |
-
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, and submit answers.
|
| 120 |
-
---
|
| 121 |
-
This template is minimal on purpose; extend the agent in agent.py.
|
| 122 |
-
"""
|
| 123 |
-
)
|
| 124 |
-
)
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
gr.LoginButton()
|
|
|
|
| 127 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 128 |
|
| 129 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 130 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 131 |
|
| 132 |
-
run_button.click(
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|
| 136 |
-
print("
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# 17/08/2025 -AO
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
+
from transformers import HfAgent
|
| 8 |
+
from transformers.tools import DuckDuckGoSearchTool
|
| 9 |
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
+
# --- Advanced Agent Definition ---
|
| 14 |
+
class MyAgent:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
# Define the LLM for the agent. Use a supported model from the Hugging Face Hub.
|
| 17 |
+
# Ensure you have the necessary environment variables (e.g., HF_TOKEN).
|
| 18 |
+
# You'll likely need a powerful model to get a high score.
|
| 19 |
+
# Example: Using a powerful hosted model
|
| 20 |
+
self.agent = HfAgent("HuggingFaceH4/zephyr-7b-beta",
|
| 21 |
+
additional_tools=[DuckDuckGoSearchTool()])
|
| 22 |
+
|
| 23 |
+
def __call__(self, question: str) -> str:
|
| 24 |
+
print(f"Agent received question: {question[:50]}...")
|
| 25 |
+
# Use a try-except block to handle potential errors during execution.
|
| 26 |
+
try:
|
| 27 |
+
# Let the agent reason and find the answer.
|
| 28 |
+
# Use verbose=True to see the agent's thought process for debugging.
|
| 29 |
+
result = self.agent.run(question)
|
| 30 |
+
# The GAIA scoring is an "EXACT MATCH".
|
| 31 |
+
# The prompt asks the agent to just return the answer, not a verbose explanation.
|
| 32 |
+
# You might need to add a post-processing step to extract the final answer.
|
| 33 |
+
# For example, by telling the model in the prompt to only output the answer.
|
| 34 |
+
final_answer = str(result)
|
| 35 |
+
print(f"Agent returning answer: {final_answer}")
|
| 36 |
+
return final_answer
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"An error occurred during agent execution: {e}")
|
| 39 |
+
return f"Error: {e}"
|
| 40 |
|
| 41 |
+
# The rest of the `run_and_submit_all` and Gradio code remains the same as in the template.
|
| 42 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 43 |
"""
|
| 44 |
+
Fetches all questions, runs the agent on them, submits all answers,
|
| 45 |
+
and displays the results.
|
| 46 |
"""
|
| 47 |
space_id = os.getenv("SPACE_ID")
|
| 48 |
|
| 49 |
if profile:
|
| 50 |
username = f"{profile.username}"
|
| 51 |
+
print(f"User logged in: {username}")
|
| 52 |
else:
|
| 53 |
+
print("User not logged in.")
|
| 54 |
return "Please Login to Hugging Face with the button.", None
|
| 55 |
|
| 56 |
api_url = DEFAULT_API_URL
|
| 57 |
questions_url = f"{api_url}/questions"
|
| 58 |
submit_url = f"{api_url}/submit"
|
| 59 |
|
| 60 |
+
# 1. Instantiate Agent
|
| 61 |
try:
|
| 62 |
+
agent = MyAgent() # Instantiate your new agent class
|
| 63 |
except Exception as e:
|
| 64 |
+
print(f"Error instantiating agent: {e}")
|
| 65 |
return f"Error initializing agent: {e}", None
|
| 66 |
+
|
| 67 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 68 |
+
print(agent_code)
|
| 69 |
|
| 70 |
+
# 2. Fetch Questions
|
| 71 |
+
print(f"Fetching questions from: {questions_url}")
|
|
|
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
+
response = requests.get(questions_url, timeout=15)
|
| 74 |
+
response.raise_for_status()
|
| 75 |
+
questions_data = response.json()
|
| 76 |
if not questions_data:
|
| 77 |
+
print("Fetched questions list is empty.")
|
| 78 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 79 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 80 |
except requests.exceptions.RequestException as e:
|
| 81 |
+
print(f"Error fetching questions: {e}")
|
| 82 |
return f"Error fetching questions: {e}", None
|
| 83 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 84 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 85 |
+
print(f"Response text: {response.text[:500]}")
|
| 86 |
+
return f"Error decoding server response for questions: {e}", None
|
| 87 |
except Exception as e:
|
| 88 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 89 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 90 |
|
| 91 |
+
# 3. Run your Agent
|
| 92 |
results_log = []
|
| 93 |
answers_payload = []
|
| 94 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 95 |
for item in questions_data:
|
| 96 |
task_id = item.get("task_id")
|
| 97 |
question_text = item.get("question")
|
| 98 |
if not task_id or question_text is None:
|
| 99 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 100 |
continue
|
| 101 |
try:
|
| 102 |
submitted_answer = agent(question_text)
|
| 103 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 104 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 107 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
if not answers_payload:
|
| 110 |
+
print("Agent did not produce any answers to submit.")
|
| 111 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 112 |
|
| 113 |
+
# 4. Prepare Submission
|
| 114 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 115 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 116 |
+
print(status_update)
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
# 5. Submit
|
| 119 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 120 |
try:
|
| 121 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 122 |
+
response.raise_for_status()
|
| 123 |
+
result_data = response.json()
|
| 124 |
final_status = (
|
| 125 |
f"Submission Successful!\n"
|
| 126 |
f"User: {result_data.get('username')}\n"
|
|
|
|
| 128 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 129 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 130 |
)
|
| 131 |
+
print("Submission successful.")
|
| 132 |
+
results_df = pd.DataFrame(results_log)
|
| 133 |
+
return final_status, results_df
|
| 134 |
except requests.exceptions.HTTPError as e:
|
| 135 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 136 |
try:
|
| 137 |
+
error_json = e.response.json()
|
| 138 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 139 |
+
except requests.exceptions.JSONDecodeError:
|
| 140 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 141 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 142 |
+
print(status_message)
|
| 143 |
+
results_df = pd.DataFrame(results_log)
|
| 144 |
+
return status_message, results_df
|
| 145 |
except requests.exceptions.Timeout:
|
| 146 |
+
status_message = "Submission Failed: The request timed out."
|
| 147 |
+
print(status_message)
|
| 148 |
+
results_df = pd.DataFrame(results_log)
|
| 149 |
+
return status_message, results_df
|
| 150 |
except requests.exceptions.RequestException as e:
|
| 151 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 152 |
+
print(status_message)
|
| 153 |
+
results_df = pd.DataFrame(results_log)
|
| 154 |
+
return status_message, results_df
|
| 155 |
except Exception as e:
|
| 156 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 157 |
+
print(status_message)
|
| 158 |
+
results_df = pd.DataFrame(results_log)
|
| 159 |
+
return status_message, results_df
|
| 160 |
|
| 161 |
|
| 162 |
+
# --- Build Gradio Interface using Blocks ---
|
| 163 |
with gr.Blocks() as demo:
|
| 164 |
+
gr.Markdown("# Advanced Agent Evaluation Runner")
|
| 165 |
gr.Markdown(
|
| 166 |
+
"""
|
| 167 |
+
**Instructions:**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 170 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 171 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 172 |
+
"""
|
| 173 |
+
)
|
| 174 |
gr.LoginButton()
|
| 175 |
+
|
| 176 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 177 |
|
| 178 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 179 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 180 |
|
| 181 |
+
run_button.click(
|
| 182 |
+
fn=run_and_submit_all,
|
| 183 |
+
outputs=[status_output, results_table]
|
| 184 |
+
)
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 188 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 189 |
+
space_id_startup = os.getenv("SPACE_ID")
|
| 190 |
+
|
| 191 |
+
if space_host_startup:
|
| 192 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 193 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 194 |
+
else:
|
| 195 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 196 |
+
|
| 197 |
+
if space_id_startup:
|
| 198 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 199 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 200 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 201 |
+
else:
|
| 202 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 203 |
+
|
| 204 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 205 |
+
|
| 206 |
+
print("Launching Gradio Interface for Advanced Agent Evaluation...")
|
| 207 |
+
demo.launch(debug=True, share=False)
|