SrcLurker commited on
Commit
cc9ee3d
·
1 Parent(s): 240c726
Files changed (4) hide show
  1. .style.yapf +4 -0
  2. app.py +174 -155
  3. basic_agent.py +57 -53
  4. 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
- def run_and_submit_all( profile: gr.OAuthProfile | None):
14
- """
 
15
  Fetches all questions, runs the basic_agent.BasicAgent on them, submits all answers,
16
  and displays the results.
17
  """
18
- # --- Determine HF Space Runtime URL and Repo URL ---
19
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
20
-
21
- if profile:
22
- username= f"{profile.username}"
23
- print(f"User logged in: {username}")
24
- else:
25
- print("User not logged in.")
26
- return "Please Login to Hugging Face with the button.", None
27
-
28
- api_url = DEFAULT_API_URL
29
- questions_url = f"{api_url}/questions"
30
- submit_url = f"{api_url}/submit"
31
-
32
- # 1. Instantiate Agent ( modify this part to create your agent)
33
- try:
34
- print("This is new code creating the new basic_agent")
35
- agent = basic_agent.BasicAgent()
36
- print("This is new code creating the new basic_agent is now done")
37
- except Exception as e:
38
- print(f"Error instantiating agent: {e}")
39
- return f"Error initializing agent: {e}", None
40
- # 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)
41
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
42
- print(agent_code)
43
-
44
- # 2. Fetch Questions
45
- print(f"Fetching questions from: {questions_url}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  try:
47
- response = requests.get(questions_url, timeout=15)
48
- response.raise_for_status()
49
- questions_data = response.json()
50
- if not questions_data:
51
- print("Fetched questions list is empty.")
52
- return "Fetched questions list is empty or invalid format.", None
53
- print(f"Fetched {len(questions_data)} questions.")
54
- except requests.exceptions.RequestException as e:
55
- print(f"Error fetching questions: {e}")
56
- return f"Error fetching questions: {e}", None
57
- except requests.exceptions.JSONDecodeError as e:
58
- print(f"Error decoding JSON response from questions endpoint: {e}")
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
- print(f"An unexpected error occurred fetching questions: {e}")
63
- return f"An unexpected error occurred fetching questions: {e}", None
64
-
65
- # 3. Run your Agent
66
- results_log = []
67
- answers_payload = []
68
- print(f"Running agent on {len(questions_data)} questions...")
69
- for item in questions_data:
70
- task_id = item.get("task_id")
71
- question_text = item.get("question")
72
- if not task_id or question_text is None:
73
- print(f"Skipping item with missing task_id or question: {item}")
74
- continue
75
- try:
76
- print(f"calling agent")
77
- submitted_answer = agent(question_text)
78
- print(f"called agent: {submitted_answer=}")
79
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
80
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
81
- except Exception as e:
82
- print(f"Error running agent on task {task_id}: {e}")
83
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
84
-
85
- if not answers_payload:
86
- print("Agent did not produce any answers to submit.")
87
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
88
-
89
- # 4. Prepare Submission
90
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
91
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
92
- print(status_update)
93
-
94
- # 5. Submit
95
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
 
 
 
96
  try:
97
- response = requests.post(submit_url, json=submission_data, timeout=60)
98
- response.raise_for_status()
99
- result_data = response.json()
100
- final_status = (
101
- f"Submission Successful!\n"
102
- f"User: {result_data.get('username')}\n"
103
- f"Overall Score: {result_data.get('score', 'N/A')}% "
104
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
105
- f"Message: {result_data.get('message', 'No message received.')}"
106
- )
107
- print("Submission successful.")
108
- results_df = pd.DataFrame(results_log)
109
- return final_status, results_df
110
- except requests.exceptions.HTTPError as e:
111
- error_detail = f"Server responded with status {e.response.status_code}."
112
- try:
113
- error_json = e.response.json()
114
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
115
- except requests.exceptions.JSONDecodeError:
116
- error_detail += f" Response: {e.response.text[:500]}"
117
- status_message = f"Submission Failed: {error_detail}"
118
- print(status_message)
119
- results_df = pd.DataFrame(results_log)
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
- gr.Markdown("# Basic Agent Evaluation Runner")
141
- gr.Markdown(
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
- gr.LoginButton()
157
 
158
- run_button = gr.Button("Run Evaluation & Submit All Answers")
159
 
160
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
161
- # Removed max_rows=10 from DataFrame constructor
162
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
 
163
 
164
- run_button.click(
165
- fn=run_and_submit_all,
166
- outputs=[status_output, results_table]
167
- )
168
 
169
  if __name__ == "__main__":
170
- print("\n" + "-"*30 + " App Starting " + "-"*30)
171
- # Check for SPACE_HOST and SPACE_ID at startup for information
172
- space_host_startup = os.getenv("SPACE_HOST")
173
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
174
-
175
- if space_host_startup:
176
- print(f"✅ SPACE_HOST found: {space_host_startup}")
177
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
178
- else:
179
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
180
-
181
- if space_id_startup: # Print repo URLs if SPACE_ID is found
182
- print(f"✅ SPACE_ID found: {space_id_startup}")
183
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
184
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
185
- else:
186
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
187
-
188
- print("-"*(60 + len(" App Starting ")) + "\n")
189
-
190
- print("*"*10, "This is new code!", "*"*10)
191
-
192
- print("Launching Gradio Interface for Basic Agent Evaluation...")
193
- demo.launch(debug=True, share=False)
 
 
 
 
 
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
- if model_id:
25
- self.model_id = model_id
26
- else:
27
- #self.model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
28
- #self.model_id = "Qwen/Qwen3-4B-FP8"
29
- self.model_id = "Qwen/Qwen3-32B"
 
 
 
 
 
30
 
31
- # Run locally.
32
- #self.model = smolagents.TransformersModel(
33
- # model_id=self.model_id,
34
- # max_new_tokens=32000,
35
- # )
36
 
37
- print("BasicAgent making model.")
38
- self.model = smolagents.HfApiModel(
39
- max_tokens=32000,
40
- temperature=0.3,
41
- model_id=self.model_id,
42
- custom_role_conversions=None,
43
- )
44
- self.tools = [smolagents.DuckDuckGoSearchTool(), smolagents.VisitWebpageTool(), smolagents.FinalAnswerTool()]
 
 
 
 
45
 
46
- print("BasicAgent making search tool.")
47
- self.search_agent = smolagents.CodeAgent(
48
- name="search_agent",
49
- description="Search the web",
50
- model=self.model,
51
- tools=self.tools,
52
- max_steps=6,
53
- verbosity_level=2,
54
- planning_interval=None,
55
- additional_authorized_imports=["duckduckgo_search"],
56
- )
57
 
58
- print("BasicAgent making manager.")
59
- self.manager_agent = smolagents.CodeAgent(
60
- name="manager_agent",
61
- description="Manger of other agents",
62
- tools=[smolagents.FinalAnswerTool()],
63
- model=self.model,
64
- max_steps=6,
65
- verbosity_level=2,
66
- planning_interval=None,
67
- additional_authorized_imports=["duckduckgo_search"],
68
- managed_agents=[self.search_agent]
69
- )
70
 
71
- def __call__(self, question: str) -> str:
72
- print(f"NEW Agent received question (first 50 chars): {question[:50]}...")
73
- prompt = f"{SYSTEM_PROMPT}\n\n{question}"
74
- answer = self.manager_agent.run(prompt)
75
- print(f"NEW {answer=}")
76
- return answer
 
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("Who is the 47th president of the united states? If necessary, use a web search to get the most up to date information.")
 
 
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=}")