Spaces:
Sleeping
Sleeping
Refactored and cleaned up.
Browse files- .gitignore +1 -0
- app.py +33 -61
- configuration.py +12 -0
- functions/agent.py +23 -0
- functions/tools.py +1 -0
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
|
|
| 1 |
.venv
|
| 2 |
.vscode
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
.venv
|
| 3 |
.vscode
|
app.py
CHANGED
|
@@ -1,32 +1,18 @@
|
|
| 1 |
-
|
| 2 |
-
'''HuggingFace Agents course final project GAIA agent benchmark'''
|
| 3 |
|
| 4 |
# Standard library
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
# Third-party
|
| 8 |
import gradio as gr
|
| 9 |
-
import requests
|
| 10 |
import pandas as pd
|
| 11 |
|
| 12 |
# Local/Project
|
| 13 |
-
from
|
| 14 |
-
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 15 |
|
| 16 |
-
# (Keep Constants as is)
|
| 17 |
# --- Constants ---
|
| 18 |
-
DEFAULT_API_URL
|
| 19 |
-
"https://agents-course-unit4-scoring.hf.space"
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
INSTRUCTIONS = (
|
| 23 |
-
"""
|
| 24 |
-
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.\n"
|
| 25 |
-
"If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.\n"
|
| 26 |
-
"If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.\n"
|
| 27 |
-
"If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
|
| 28 |
-
"""
|
| 29 |
-
)
|
| 30 |
|
| 31 |
|
| 32 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
@@ -35,47 +21,33 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 35 |
and displays the results.
|
| 36 |
"""
|
| 37 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 38 |
-
space_id = os.getenv(
|
| 39 |
|
| 40 |
if profile:
|
| 41 |
-
username= f
|
| 42 |
-
print(f
|
| 43 |
-
|
| 44 |
else:
|
| 45 |
-
print(
|
| 46 |
-
return
|
| 47 |
|
| 48 |
api_url = DEFAULT_API_URL
|
| 49 |
-
questions_url = f
|
| 50 |
-
submit_url = f
|
| 51 |
|
| 52 |
-
# 1. Instantiate Agent (
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
wikipedia_tool = Tool.from_langchain(
|
| 56 |
-
load_tools(["wikipedia"])[0]
|
| 57 |
-
)
|
| 58 |
-
model = InferenceClientModel(
|
| 59 |
-
"Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 60 |
-
)
|
| 61 |
-
agent = CodeAgent(
|
| 62 |
-
tools=[wikipedia_tool, DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 63 |
-
model=model
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
except Exception as e: # pylint: disable=W0703
|
| 67 |
print(f"Error instantiating agent: {e}")
|
| 68 |
return f"Error initializing agent: {e}", None
|
| 69 |
|
| 70 |
# In the case of an app running as a hugging Face space, this link points toward your
|
| 71 |
# codebase (useful for others so please keep it public)
|
| 72 |
-
agent_code =
|
| 73 |
-
f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 74 |
-
)
|
| 75 |
print(agent_code)
|
| 76 |
|
| 77 |
# 2. Fetch Questions
|
| 78 |
-
print(f
|
| 79 |
|
| 80 |
try:
|
| 81 |
response = requests.get(questions_url, timeout=15)
|
|
@@ -83,36 +55,36 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 83 |
questions_data = response.json()
|
| 84 |
|
| 85 |
if not questions_data:
|
| 86 |
-
print(
|
| 87 |
-
return
|
| 88 |
|
| 89 |
-
print(f
|
| 90 |
|
| 91 |
except requests.exceptions.JSONDecodeError as e:
|
| 92 |
-
print(f
|
| 93 |
-
print(f
|
| 94 |
-
return f
|
| 95 |
|
| 96 |
except requests.exceptions.RequestException as e:
|
| 97 |
-
print(f
|
| 98 |
-
return f
|
| 99 |
|
| 100 |
except Exception as e: # pylint: disable=W0703
|
| 101 |
-
print(f
|
| 102 |
-
return f
|
| 103 |
|
| 104 |
# 3. Run your Agent
|
| 105 |
results_log = []
|
| 106 |
answers_payload = []
|
| 107 |
|
| 108 |
-
print(f
|
| 109 |
|
| 110 |
for item in questions_data:
|
| 111 |
task_id = item.get("task_id")
|
| 112 |
question_text = item.get("question")
|
| 113 |
|
| 114 |
if not task_id or question_text is None:
|
| 115 |
-
print(f
|
| 116 |
continue
|
| 117 |
|
| 118 |
try:
|
|
@@ -129,7 +101,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 129 |
})
|
| 130 |
|
| 131 |
except Exception as e: # pylint: disable=W0703
|
| 132 |
-
print(f
|
| 133 |
results_log.append({
|
| 134 |
"Task ID": task_id,
|
| 135 |
"Question": question_text,
|
|
@@ -137,8 +109,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 137 |
})
|
| 138 |
|
| 139 |
if not answers_payload:
|
| 140 |
-
print(
|
| 141 |
-
return
|
| 142 |
|
| 143 |
# 4. Prepare Submission
|
| 144 |
submission_data = {
|
|
@@ -147,12 +119,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 147 |
"answers": answers_payload
|
| 148 |
}
|
| 149 |
status_update = (
|
| 150 |
-
f
|
| 151 |
)
|
| 152 |
print(status_update)
|
| 153 |
|
| 154 |
# 5. Submit
|
| 155 |
-
print(f
|
| 156 |
try:
|
| 157 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 158 |
response.raise_for_status()
|
|
@@ -165,7 +137,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 165 |
f"{result_data.get('total_attempted', '?')} correct)\n"
|
| 166 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 167 |
)
|
| 168 |
-
print(
|
| 169 |
results_df = pd.DataFrame(results_log)
|
| 170 |
return final_status, results_df
|
| 171 |
|
|
|
|
| 1 |
+
'''HuggingFace Agents course final project GAIA agent benchmark.'''
|
|
|
|
| 2 |
|
| 3 |
# Standard library
|
| 4 |
import os
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
# Third-party
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
import pandas as pd
|
| 10 |
|
| 11 |
# Local/Project
|
| 12 |
+
from functions.agent import create_agent
|
|
|
|
| 13 |
|
|
|
|
| 14 |
# --- Constants ---
|
| 15 |
+
from configuration import DEFAULT_API_URL, INSTRUCTIONS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 21 |
and displays the results.
|
| 22 |
"""
|
| 23 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 24 |
+
space_id = os.getenv('SPACE_ID')
|
| 25 |
|
| 26 |
if profile:
|
| 27 |
+
username = f'{profile.username}'
|
| 28 |
+
print(f'User logged in: {username}')
|
|
|
|
| 29 |
else:
|
| 30 |
+
print('User not logged in.')
|
| 31 |
+
return 'Please Login to Hugging Face with the button.', None
|
| 32 |
|
| 33 |
api_url = DEFAULT_API_URL
|
| 34 |
+
questions_url = f'{api_url}/questions'
|
| 35 |
+
submit_url = f'{api_url}/submit'
|
| 36 |
|
| 37 |
+
# 1. Instantiate Agent (imported from agent.py)
|
| 38 |
try:
|
| 39 |
+
agent = create_agent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e: # pylint: disable=W0703
|
| 41 |
print(f"Error instantiating agent: {e}")
|
| 42 |
return f"Error initializing agent: {e}", None
|
| 43 |
|
| 44 |
# In the case of an app running as a hugging Face space, this link points toward your
|
| 45 |
# codebase (useful for others so please keep it public)
|
| 46 |
+
agent_code = f'https://huggingface.co/spaces/{space_id}/tree/main'
|
|
|
|
|
|
|
| 47 |
print(agent_code)
|
| 48 |
|
| 49 |
# 2. Fetch Questions
|
| 50 |
+
print(f'Fetching questions from: {questions_url}')
|
| 51 |
|
| 52 |
try:
|
| 53 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 55 |
questions_data = response.json()
|
| 56 |
|
| 57 |
if not questions_data:
|
| 58 |
+
print('Fetched questions list is empty.')
|
| 59 |
+
return 'Fetched questions list is empty or invalid format.', None
|
| 60 |
|
| 61 |
+
print(f'Fetched {len(questions_data)} questions.')
|
| 62 |
|
| 63 |
except requests.exceptions.JSONDecodeError as e:
|
| 64 |
+
print(f'Error decoding JSON response from questions endpoint: {e}')
|
| 65 |
+
print(f'Response text: {response.text[:500]}')
|
| 66 |
+
return f'Error decoding server response for questions: {e}', None
|
| 67 |
|
| 68 |
except requests.exceptions.RequestException as e:
|
| 69 |
+
print(f'Error fetching questions: {e}')
|
| 70 |
+
return f'Error fetching questions: {e}', None
|
| 71 |
|
| 72 |
except Exception as e: # pylint: disable=W0703
|
| 73 |
+
print(f'An unexpected error occurred fetching questions: {e}')
|
| 74 |
+
return f'An unexpected error occurred fetching questions: {e}', None
|
| 75 |
|
| 76 |
# 3. Run your Agent
|
| 77 |
results_log = []
|
| 78 |
answers_payload = []
|
| 79 |
|
| 80 |
+
print(f'Running agent on {len(questions_data)} questions...')
|
| 81 |
|
| 82 |
for item in questions_data:
|
| 83 |
task_id = item.get("task_id")
|
| 84 |
question_text = item.get("question")
|
| 85 |
|
| 86 |
if not task_id or question_text is None:
|
| 87 |
+
print(f'Skipping item with missing task_id or question: {item}')
|
| 88 |
continue
|
| 89 |
|
| 90 |
try:
|
|
|
|
| 101 |
})
|
| 102 |
|
| 103 |
except Exception as e: # pylint: disable=W0703
|
| 104 |
+
print(f'Error running agent on task {task_id}: {e}')
|
| 105 |
results_log.append({
|
| 106 |
"Task ID": task_id,
|
| 107 |
"Question": question_text,
|
|
|
|
| 109 |
})
|
| 110 |
|
| 111 |
if not answers_payload:
|
| 112 |
+
print('Agent did not produce any answers to submit.')
|
| 113 |
+
return 'Agent did not produce any answers to submit.', pd.DataFrame(results_log)
|
| 114 |
|
| 115 |
# 4. Prepare Submission
|
| 116 |
submission_data = {
|
|
|
|
| 119 |
"answers": answers_payload
|
| 120 |
}
|
| 121 |
status_update = (
|
| 122 |
+
f'Agent finished. Submitting {len(answers_payload)} answers for user "{username}"...'
|
| 123 |
)
|
| 124 |
print(status_update)
|
| 125 |
|
| 126 |
# 5. Submit
|
| 127 |
+
print(f'Submitting {len(answers_payload)} answers to: {submit_url}')
|
| 128 |
try:
|
| 129 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 130 |
response.raise_for_status()
|
|
|
|
| 137 |
f"{result_data.get('total_attempted', '?')} correct)\n"
|
| 138 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 139 |
)
|
| 140 |
+
print('Submission successful.')
|
| 141 |
results_df = pd.DataFrame(results_log)
|
| 142 |
return final_status, results_df
|
| 143 |
|
configuration.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
"""
|
| 3 |
+
Configuration constants for the GAIA agent project.
|
| 4 |
+
Contains API URLs and agent instructions used throughout the application.
|
| 5 |
+
"""
|
| 6 |
+
# pylint: disable=line-too-long
|
| 7 |
+
|
| 8 |
+
DEFAULT_API_URL = 'https://agents-course-unit4-scoring.hf.space'
|
| 9 |
+
|
| 10 |
+
INSTRUCTIONS = """
|
| 11 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 12 |
+
"""
|
functions/agent.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''Agent definition for GAIA question answering system.'''
|
| 2 |
+
|
| 3 |
+
# Imports for agent creation
|
| 4 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool, Tool
|
| 5 |
+
from langchain_community.agent_toolkits.load_tools import load_tools
|
| 6 |
+
|
| 7 |
+
def create_agent():
|
| 8 |
+
'''Creates agent for GAIA question answering system.'''
|
| 9 |
+
|
| 10 |
+
wikipedia_tool = Tool.from_langchain(
|
| 11 |
+
load_tools(["wikipedia"])[0]
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
model = InferenceClientModel(
|
| 15 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
agent = CodeAgent(
|
| 19 |
+
tools=[wikipedia_tool, DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 20 |
+
model=model
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
return agent
|
functions/tools.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
'''Tools for GAIA question answering agent.'''
|