style
Browse files- .style.yapf +4 -0
- app.py +174 -155
- basic_agent.py +57 -53
- basic_agent_test.py +3 -1
.style.yapf
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[style]
|
2 |
+
based_on_style = google
|
3 |
+
indent_width = 2
|
4 |
+
|
app.py
CHANGED
@@ -10,136 +10,152 @@ import basic_agent
|
|
10 |
# --- Constants ---
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
Fetches all questions, runs the basic_agent.BasicAgent on them, submits all answers,
|
16 |
and displays the results.
|
17 |
"""
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
try:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
print(f"Response text: {response.text[:500]}")
|
60 |
-
return f"Error decoding server response for questions: {e}", None
|
61 |
except Exception as e:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
print(
|
|
|
|
|
|
|
|
|
96 |
try:
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
return status_message, results_df
|
121 |
-
except requests.exceptions.Timeout:
|
122 |
-
status_message = "Submission Failed: The request timed out."
|
123 |
-
print(status_message)
|
124 |
-
results_df = pd.DataFrame(results_log)
|
125 |
-
return status_message, results_df
|
126 |
-
except requests.exceptions.RequestException as e:
|
127 |
-
status_message = f"Submission Failed: Network error - {e}"
|
128 |
-
print(status_message)
|
129 |
-
results_df = pd.DataFrame(results_log)
|
130 |
-
return status_message, results_df
|
131 |
-
except Exception as e:
|
132 |
-
status_message = f"An unexpected error occurred during submission: {e}"
|
133 |
-
print(status_message)
|
134 |
-
results_df = pd.DataFrame(results_log)
|
135 |
-
return status_message, results_df
|
136 |
|
137 |
|
138 |
# --- Build Gradio Interface using Blocks ---
|
139 |
with gr.Blocks() as demo:
|
140 |
-
|
141 |
-
|
142 |
-
"""
|
143 |
**Instructions:**
|
144 |
|
145 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
@@ -150,44 +166,47 @@ with gr.Blocks() as demo:
|
|
150 |
**Disclaimers:**
|
151 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
152 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
153 |
-
"""
|
154 |
-
)
|
155 |
|
156 |
-
|
157 |
|
158 |
-
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
outputs=[status_output, results_table]
|
167 |
-
)
|
168 |
|
169 |
if __name__ == "__main__":
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
print(
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
10 |
# --- Constants ---
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
+
|
14 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
15 |
+
"""
|
16 |
Fetches all questions, runs the basic_agent.BasicAgent on them, submits all answers,
|
17 |
and displays the results.
|
18 |
"""
|
19 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
20 |
+
space_id = os.getenv(
|
21 |
+
"SPACE_ID") # Get the SPACE_ID for sending link to the code
|
22 |
+
|
23 |
+
if profile:
|
24 |
+
username = f"{profile.username}"
|
25 |
+
print(f"User logged in: {username}")
|
26 |
+
else:
|
27 |
+
print("User not logged in.")
|
28 |
+
return "Please Login to Hugging Face with the button.", None
|
29 |
+
|
30 |
+
api_url = DEFAULT_API_URL
|
31 |
+
questions_url = f"{api_url}/questions"
|
32 |
+
submit_url = f"{api_url}/submit"
|
33 |
+
|
34 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
35 |
+
try:
|
36 |
+
print("This is new code creating the new basic_agent")
|
37 |
+
agent = basic_agent.BasicAgent()
|
38 |
+
print("This is new code creating the new basic_agent is now done")
|
39 |
+
except Exception as e:
|
40 |
+
print(f"Error instantiating agent: {e}")
|
41 |
+
return f"Error initializing agent: {e}", None
|
42 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
43 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
44 |
+
print(agent_code)
|
45 |
+
|
46 |
+
# 2. Fetch Questions
|
47 |
+
print(f"Fetching questions from: {questions_url}")
|
48 |
+
try:
|
49 |
+
response = requests.get(questions_url, timeout=15)
|
50 |
+
response.raise_for_status()
|
51 |
+
questions_data = response.json()
|
52 |
+
if not questions_data:
|
53 |
+
print("Fetched questions list is empty.")
|
54 |
+
return "Fetched questions list is empty or invalid format.", None
|
55 |
+
print(f"Fetched {len(questions_data)} questions.")
|
56 |
+
except requests.exceptions.RequestException as e:
|
57 |
+
print(f"Error fetching questions: {e}")
|
58 |
+
return f"Error fetching questions: {e}", None
|
59 |
+
except requests.exceptions.JSONDecodeError as e:
|
60 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
61 |
+
print(f"Response text: {response.text[:500]}")
|
62 |
+
return f"Error decoding server response for questions: {e}", None
|
63 |
+
except Exception as e:
|
64 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
65 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
66 |
+
|
67 |
+
# 3. Run your Agent
|
68 |
+
results_log = []
|
69 |
+
answers_payload = []
|
70 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
71 |
+
for item in questions_data:
|
72 |
+
task_id = item.get("task_id")
|
73 |
+
question_text = item.get("question")
|
74 |
+
if not task_id or question_text is None:
|
75 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
76 |
+
continue
|
77 |
try:
|
78 |
+
print(f"calling agent")
|
79 |
+
submitted_answer = agent(question_text)
|
80 |
+
print(f"called agent: {submitted_answer=}")
|
81 |
+
answers_payload.append({
|
82 |
+
"task_id": task_id,
|
83 |
+
"submitted_answer": submitted_answer
|
84 |
+
})
|
85 |
+
results_log.append({
|
86 |
+
"Task ID": task_id,
|
87 |
+
"Question": question_text,
|
88 |
+
"Submitted Answer": submitted_answer
|
89 |
+
})
|
|
|
|
|
90 |
except Exception as e:
|
91 |
+
print(f"Error running agent on task {task_id}: {e}")
|
92 |
+
results_log.append({
|
93 |
+
"Task ID": task_id,
|
94 |
+
"Question": question_text,
|
95 |
+
"Submitted Answer": f"AGENT ERROR: {e}"
|
96 |
+
})
|
97 |
+
|
98 |
+
if not answers_payload:
|
99 |
+
print("Agent did not produce any answers to submit.")
|
100 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(
|
101 |
+
results_log)
|
102 |
+
|
103 |
+
# 4. Prepare Submission
|
104 |
+
submission_data = {
|
105 |
+
"username": username.strip(),
|
106 |
+
"agent_code": agent_code,
|
107 |
+
"answers": answers_payload
|
108 |
+
}
|
109 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
110 |
+
print(status_update)
|
111 |
+
|
112 |
+
# 5. Submit
|
113 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
114 |
+
try:
|
115 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
116 |
+
response.raise_for_status()
|
117 |
+
result_data = response.json()
|
118 |
+
final_status = (
|
119 |
+
f"Submission Successful!\n"
|
120 |
+
f"User: {result_data.get('username')}\n"
|
121 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
122 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
123 |
+
f"Message: {result_data.get('message', 'No message received.')}")
|
124 |
+
print("Submission successful.")
|
125 |
+
results_df = pd.DataFrame(results_log)
|
126 |
+
return final_status, results_df
|
127 |
+
except requests.exceptions.HTTPError as e:
|
128 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
129 |
try:
|
130 |
+
error_json = e.response.json()
|
131 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
132 |
+
except requests.exceptions.JSONDecodeError:
|
133 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
134 |
+
status_message = f"Submission Failed: {error_detail}"
|
135 |
+
print(status_message)
|
136 |
+
results_df = pd.DataFrame(results_log)
|
137 |
+
return status_message, results_df
|
138 |
+
except requests.exceptions.Timeout:
|
139 |
+
status_message = "Submission Failed: The request timed out."
|
140 |
+
print(status_message)
|
141 |
+
results_df = pd.DataFrame(results_log)
|
142 |
+
return status_message, results_df
|
143 |
+
except requests.exceptions.RequestException as e:
|
144 |
+
status_message = f"Submission Failed: Network error - {e}"
|
145 |
+
print(status_message)
|
146 |
+
results_df = pd.DataFrame(results_log)
|
147 |
+
return status_message, results_df
|
148 |
+
except Exception as e:
|
149 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
150 |
+
print(status_message)
|
151 |
+
results_df = pd.DataFrame(results_log)
|
152 |
+
return status_message, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
|
155 |
# --- Build Gradio Interface using Blocks ---
|
156 |
with gr.Blocks() as demo:
|
157 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
158 |
+
gr.Markdown("""
|
|
|
159 |
**Instructions:**
|
160 |
|
161 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
|
|
166 |
**Disclaimers:**
|
167 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
168 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
169 |
+
""")
|
|
|
170 |
|
171 |
+
gr.LoginButton()
|
172 |
|
173 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
174 |
|
175 |
+
status_output = gr.Textbox(label="Run Status / Submission Result",
|
176 |
+
lines=5,
|
177 |
+
interactive=False)
|
178 |
+
# Removed max_rows=10 from DataFrame constructor
|
179 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
180 |
|
181 |
+
run_button.click(fn=run_and_submit_all,
|
182 |
+
outputs=[status_output, results_table])
|
|
|
|
|
183 |
|
184 |
if __name__ == "__main__":
|
185 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
186 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
187 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
188 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
189 |
+
|
190 |
+
if space_host_startup:
|
191 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
192 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
193 |
+
else:
|
194 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
195 |
+
|
196 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
197 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
198 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
199 |
+
print(
|
200 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
201 |
+
)
|
202 |
+
else:
|
203 |
+
print(
|
204 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
205 |
+
)
|
206 |
+
|
207 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
208 |
+
|
209 |
+
print("*" * 10, "This is new code!", "*" * 10)
|
210 |
+
|
211 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
212 |
+
demo.launch(debug=True, share=False)
|
basic_agent.py
CHANGED
@@ -4,7 +4,6 @@ import sys
|
|
4 |
|
5 |
import smolagents
|
6 |
|
7 |
-
|
8 |
LOG = logging.getLogger(__name__)
|
9 |
|
10 |
SYSTEM_PROMPT = """
|
@@ -13,64 +12,69 @@ You are a general AI assistant. I will ask you a question. Report your thoughts,
|
|
13 |
Take the time to plan the steps to reach the solution. Show the steps and then execute the steps.
|
14 |
"""
|
15 |
|
|
|
16 |
# --- Basic Agent Definition ---
|
17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
class BasicAgent:
|
19 |
-
def __init__(self, model_id=None):
|
20 |
-
print("BasicAgent initializing.")
|
21 |
-
# Logs appear to be swallowed.
|
22 |
-
LOG.warning("logging BasicAgent initialized.")
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
)
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
4 |
|
5 |
import smolagents
|
6 |
|
|
|
7 |
LOG = logging.getLogger(__name__)
|
8 |
|
9 |
SYSTEM_PROMPT = """
|
|
|
12 |
Take the time to plan the steps to reach the solution. Show the steps and then execute the steps.
|
13 |
"""
|
14 |
|
15 |
+
|
16 |
# --- Basic Agent Definition ---
|
17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
class BasicAgent:
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def __init__(self, model_id=None):
|
21 |
+
print("BasicAgent initializing.")
|
22 |
+
# Logs appear to be swallowed.
|
23 |
+
LOG.warning("logging BasicAgent initialized.")
|
24 |
+
|
25 |
+
if model_id:
|
26 |
+
self.model_id = model_id
|
27 |
+
else:
|
28 |
+
#self.model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
29 |
+
#self.model_id = "Qwen/Qwen3-4B-FP8"
|
30 |
+
self.model_id = "Qwen/Qwen3-32B"
|
31 |
|
32 |
+
# Run locally.
|
33 |
+
#self.model = smolagents.TransformersModel(
|
34 |
+
# model_id=self.model_id,
|
35 |
+
# max_new_tokens=32000,
|
36 |
+
# )
|
37 |
|
38 |
+
print("BasicAgent making model.")
|
39 |
+
self.model = smolagents.HfApiModel(
|
40 |
+
max_tokens=32000,
|
41 |
+
temperature=0.3,
|
42 |
+
model_id=self.model_id,
|
43 |
+
custom_role_conversions=None,
|
44 |
+
)
|
45 |
+
self.tools = [
|
46 |
+
smolagents.DuckDuckGoSearchTool(),
|
47 |
+
smolagents.VisitWebpageTool(),
|
48 |
+
smolagents.FinalAnswerTool()
|
49 |
+
]
|
50 |
|
51 |
+
print("BasicAgent making search tool.")
|
52 |
+
self.search_agent = smolagents.CodeAgent(
|
53 |
+
name="search_agent",
|
54 |
+
description="Search the web",
|
55 |
+
model=self.model,
|
56 |
+
tools=self.tools,
|
57 |
+
max_steps=6,
|
58 |
+
verbosity_level=2,
|
59 |
+
planning_interval=None,
|
60 |
+
additional_authorized_imports=["duckduckgo_search"],
|
61 |
+
)
|
62 |
|
63 |
+
print("BasicAgent making manager.")
|
64 |
+
self.manager_agent = smolagents.CodeAgent(
|
65 |
+
name="manager_agent",
|
66 |
+
description="Manger of other agents",
|
67 |
+
tools=[smolagents.FinalAnswerTool()],
|
68 |
+
model=self.model,
|
69 |
+
max_steps=6,
|
70 |
+
verbosity_level=2,
|
71 |
+
planning_interval=None,
|
72 |
+
additional_authorized_imports=["duckduckgo_search"],
|
73 |
+
managed_agents=[self.search_agent])
|
|
|
74 |
|
75 |
+
def __call__(self, question: str) -> str:
|
76 |
+
print(f"NEW Agent received question (first 50 chars): {question[:50]}...")
|
77 |
+
prompt = f"{SYSTEM_PROMPT}\n\n{question}"
|
78 |
+
answer = self.manager_agent.run(prompt)
|
79 |
+
print(f"NEW {answer=}")
|
80 |
+
return answer
|
basic_agent_test.py
CHANGED
@@ -8,6 +8,8 @@ LOG = logging.getLogger(__name__)
|
|
8 |
|
9 |
ba = basic_agent.BasicAgent()
|
10 |
|
11 |
-
answer = ba(
|
|
|
|
|
12 |
|
13 |
LOG.warning(f"{answer=}")
|
|
|
8 |
|
9 |
ba = basic_agent.BasicAgent()
|
10 |
|
11 |
+
answer = ba(
|
12 |
+
"Who is the 47th president of the united states? If necessary, use a web search to get the most up to date information."
|
13 |
+
)
|
14 |
|
15 |
LOG.warning(f"{answer=}")
|