gabriel-melki commited on
Commit
77c169c
·
1 Parent(s): 0af1991

Ensure users are connected to use capabilities

Browse files
Files changed (2) hide show
  1. src/eval/submission.py +5 -75
  2. src/ui/builder.py +19 -4
src/eval/submission.py CHANGED
@@ -13,9 +13,6 @@ def run_and_submit_all(agent, profile: gr.OAuthProfile | None):
13
  Fetches all questions, runs the BasicAgent on them, submits all answers,
14
  and displays the results.
15
  """
16
- # --- Determine HF Space Runtime URL and Repo URL ---
17
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
18
-
19
  if profile:
20
  username= f"{profile.username}"
21
  print(f"User logged in: {username}")
@@ -26,18 +23,8 @@ def run_and_submit_all(agent, profile: gr.OAuthProfile | None):
26
  api_url = DEFAULT_API_URL
27
  questions_url = f"{api_url}/questions"
28
  submit_url = f"{api_url}/submit"
29
-
30
- # 1. Instantiate Agent ( modify this part to create your agent)
31
- try:
32
- agent = agent
33
- except Exception as e:
34
- print(f"Error instantiating agent: {e}")
35
- return f"Error initializing agent: {e}", None
36
- # 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)
37
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
38
- print(agent_code)
39
-
40
- # 2. Fetch Questions
41
  print(f"Fetching questions from: {questions_url}")
42
  try:
43
  response = requests.get(questions_url, timeout=15)
@@ -58,7 +45,7 @@ def run_and_submit_all(agent, profile: gr.OAuthProfile | None):
58
  print(f"An unexpected error occurred fetching questions: {e}")
59
  return f"An unexpected error occurred fetching questions: {e}", None
60
 
61
- # 3. Run your Agent
62
  results_log = []
63
  answers_payload = []
64
  is_correct_answers = []
@@ -91,12 +78,12 @@ def run_and_submit_all(agent, profile: gr.OAuthProfile | None):
91
  print("Agent did not produce any answers to submit.")
92
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
93
 
94
- # 4. Prepare Submission
95
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
96
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
97
  print(status_update)
98
 
99
- # 5. Submit
100
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
101
  try:
102
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -140,60 +127,3 @@ def run_and_submit_all(agent, profile: gr.OAuthProfile | None):
140
  print(status_message)
141
  results_df = pd.DataFrame(results_log)
142
  return status_message, results_df
143
-
144
-
145
-
146
- # --- Build Gradio Interface using Blocks ---
147
- with gr.Blocks() as demo:
148
- gr.Markdown("# Basic Agent Evaluation Runner")
149
- gr.Markdown(
150
- """
151
- **Instructions:**
152
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
153
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
154
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
155
- ---
156
- **Disclaimers:**
157
- 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).
158
- 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.
159
- """
160
- )
161
-
162
- gr.LoginButton()
163
-
164
- run_button = gr.Button("Run Evaluation & Submit All Answers")
165
-
166
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
167
- # Removed max_rows=10 from DataFrame constructor
168
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
169
-
170
- def run_with_login_state(profile: gr.OAuthProfile):
171
- return run_and_submit_all(agent, profile)
172
-
173
- run_button.click(
174
- fn=run_with_login_state,
175
- outputs=[status_output, results_table]
176
- )
177
-
178
- print("\n" + "-"*30 + " App Starting " + "-"*30)
179
- # Check for SPACE_HOST and SPACE_ID at startup for information
180
- space_host_startup = os.getenv("SPACE_HOST")
181
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
182
-
183
- if space_host_startup:
184
- print(f"✅ SPACE_HOST found: {space_host_startup}")
185
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
186
- else:
187
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
188
-
189
- if space_id_startup: # Print repo URLs if SPACE_ID is found
190
- print(f"✅ SPACE_ID found: {space_id_startup}")
191
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
192
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
193
- else:
194
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
195
-
196
- print("-"*(60 + len(" App Starting ")) + "\n")
197
-
198
- print("Launching Gradio Interface for Basic Agent Evaluation...")
199
- demo.launch(debug=True, share=False)
 
13
  Fetches all questions, runs the BasicAgent on them, submits all answers,
14
  and displays the results.
15
  """
 
 
 
16
  if profile:
17
  username= f"{profile.username}"
18
  print(f"User logged in: {username}")
 
23
  api_url = DEFAULT_API_URL
24
  questions_url = f"{api_url}/questions"
25
  submit_url = f"{api_url}/submit"
26
+
27
+ # 1. Fetch Questions
 
 
 
 
 
 
 
 
 
 
28
  print(f"Fetching questions from: {questions_url}")
29
  try:
30
  response = requests.get(questions_url, timeout=15)
 
45
  print(f"An unexpected error occurred fetching questions: {e}")
46
  return f"An unexpected error occurred fetching questions: {e}", None
47
 
48
+ # 2. Run your Agent
49
  results_log = []
50
  answers_payload = []
51
  is_correct_answers = []
 
78
  print("Agent did not produce any answers to submit.")
79
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
80
 
81
+ # 3. Prepare Submission
82
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
83
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
84
  print(status_update)
85
 
86
+ # 4. Submit
87
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
88
  try:
89
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
127
  print(status_message)
128
  results_df = pd.DataFrame(results_log)
129
  return status_message, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/ui/builder.py CHANGED
@@ -4,6 +4,7 @@ import re
4
  import shutil
5
  from typing import Optional
6
  import gradio as gr
 
7
 
8
  from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
9
  from smolagents.agents import ActionStep, MultiStepAgent
@@ -162,6 +163,7 @@ def stream_to_gradio(
162
  yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
163
 
164
 
 
165
  class GradioUI:
166
  """A one-line interface to launch your agent in Gradio"""
167
  def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
@@ -233,7 +235,14 @@ class GradioUI:
233
 
234
  return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
235
 
236
- def log_user_message(self, text_input, file_uploads_log):
 
 
 
 
 
 
 
237
  return (
238
  text_input
239
  + (
@@ -254,6 +263,7 @@ class GradioUI:
254
  """
255
  )
256
  gr.LoginButton()
 
257
  gr.Markdown(
258
  """
259
  ---
@@ -280,12 +290,17 @@ class GradioUI:
280
  self.upload_file,
281
  [upload_file, file_uploads_log],
282
  [upload_status, file_uploads_log],
 
283
  )
284
  text_input = gr.Textbox(lines=1, label="Chat Message")
 
 
 
 
285
  text_input.submit(
286
- self.log_user_message,
287
- [text_input, file_uploads_log],
288
- [stored_messages, text_input],
289
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
290
 
291
  gr.Markdown(
 
4
  import shutil
5
  from typing import Optional
6
  import gradio as gr
7
+ from functools import partial
8
 
9
  from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
10
  from smolagents.agents import ActionStep, MultiStepAgent
 
163
  yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
164
 
165
 
166
+
167
  class GradioUI:
168
  """A one-line interface to launch your agent in Gradio"""
169
  def __init__(self, agent: MultiStepAgent, file_upload_folder: str | None = None):
 
235
 
236
  return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
237
 
238
+ def log_user_message(self, text_input, file_uploads_log, profile: gr.OAuthProfile | None):
239
+ if profile:
240
+ username= f"{profile.username}"
241
+ print(f"User logged in: {username}")
242
+ else:
243
+ print("User not logged in.")
244
+ return "Please Login to Hugging Face with the button.", None
245
+
246
  return (
247
  text_input
248
  + (
 
263
  """
264
  )
265
  gr.LoginButton()
266
+
267
  gr.Markdown(
268
  """
269
  ---
 
290
  self.upload_file,
291
  [upload_file, file_uploads_log],
292
  [upload_status, file_uploads_log],
293
+
294
  )
295
  text_input = gr.Textbox(lines=1, label="Chat Message")
296
+
297
+ def log_user_message_with_login_state(text_input, file_uploads_log, profile: gr.OAuthProfile):
298
+ return self.log_user_message(text_input=text_input, file_uploads_log=file_uploads_log, profile=profile)
299
+
300
  text_input.submit(
301
+ fn=log_user_message_with_login_state,
302
+ inputs=[text_input, file_uploads_log],
303
+ outputs=[stored_messages, text_input],
304
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
305
 
306
  gr.Markdown(