Toumaima commited on
Commit
d50ae44
·
verified ·
1 Parent(s): 99dbe60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -60
app.py CHANGED
@@ -66,32 +66,32 @@ class BasicAgent:
66
  return self.solve_riddle(question)
67
  return "FINAL ANSWER: NOT_A_RIDDLE"
68
 
69
- def run_and_submit_all( profile: gr.OAuthProfile | None):
70
- """
71
- Fetches all questions, runs the BasicAgent on them, submits all answers,
72
- and displays the results.
73
- """
74
- # --- Determine HF Space Runtime URL and Repo URL ---
75
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
76
-
77
- if profile:
78
- username= f"{profile.username}"
79
- print(f"User logged in: {username}")
80
- else:
81
- print("User not logged in.")
82
  return "Please Login to Hugging Face with the button.", None
83
 
84
  api_url = DEFAULT_API_URL
85
  questions_url = f"{api_url}/questions"
86
  submit_url = f"{api_url}/submit"
87
 
88
- # 1. Instantiate Agent (modify this part to create your agent)
89
  try:
90
  agent = BasicAgent()
91
  except Exception as e:
92
  print(f"Error instantiating agent: {e}")
93
  return f"Error initializing agent: {e}", None
94
- # In the case of an app running as a hugging Face space, this link points toward your codebase (useful for others so please keep it public)
95
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
96
  print(agent_code)
97
 
@@ -187,48 +187,55 @@ class BasicAgent:
187
  return status_message, results_df
188
 
189
 
190
- # --- Build Gradio Interface using Blocks ---
191
- with gr.Blocks() as demo:
192
- gr.Markdown("# Basic Agent Evaluation Runner")
193
- gr.Markdown(
194
- "Please clone this space, then modify the code to define your agent's logic within the `BasicAgent` class. "
195
- "Log in to your Hugging Face account using the button below. This uses your HF username for submission. "
196
- "Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score."
197
- )
198
-
199
- gr.LoginButton()
200
-
201
- run_button = gr.Button("Run Evaluation & Submit All Answers")
202
-
203
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
204
- # Removed max_rows=10 from DataFrame constructor
205
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
206
-
207
- run_button.click(
208
- fn=run_and_submit_all,
209
- outputs=[status_output, results_table]
210
- )
211
-
212
- if __name__ == "__main__":
213
- print("\n" + "-"*30 + " App Starting " + "-"*30)
214
- # Check for SPACE_HOST and SPACE_ID at startup for information
215
- space_host_startup = os.getenv("SPACE_HOST")
216
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
217
-
218
- if space_host_startup:
219
- print(f"✅ SPACE_HOST found: {space_host_startup}")
220
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
221
- else:
222
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
223
-
224
- if space_id_startup: # Print repo URLs if SPACE_ID is found
225
- print(f"✅ SPACE_ID found: {space_id_startup}")
226
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
227
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
228
- else:
229
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
230
-
231
- print("-"*(60 + len(" App Starting ")) + "\n")
232
-
233
- print("Launching Gradio Interface for Basic Agent Evaluation...")
234
- demo.launch(debug=True, share=False)
 
 
 
 
 
 
 
 
66
  return self.solve_riddle(question)
67
  return "FINAL ANSWER: NOT_A_RIDDLE"
68
 
69
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
70
+ """
71
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
72
+ and displays the results.
73
+ """
74
+ # --- Determine HF Space Runtime URL and Repo URL ---
75
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
76
+
77
+ if profile:
78
+ username= f"{profile.username}"
79
+ print(f"User logged in: {username}")
80
+ else:
81
+ print("User not logged in.")
82
  return "Please Login to Hugging Face with the button.", None
83
 
84
  api_url = DEFAULT_API_URL
85
  questions_url = f"{api_url}/questions"
86
  submit_url = f"{api_url}/submit"
87
 
88
+ # 1. Instantiate Agent ( modify this part to create your agent)
89
  try:
90
  agent = BasicAgent()
91
  except Exception as e:
92
  print(f"Error instantiating agent: {e}")
93
  return f"Error initializing agent: {e}", None
94
+ # 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)
95
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
96
  print(agent_code)
97
 
 
187
  return status_message, results_df
188
 
189
 
190
+ # --- Build Gradio Interface using Blocks ---
191
+ with gr.Blocks() as demo:
192
+ gr.Markdown("# Basic Agent Evaluation Runner")
193
+ gr.Markdown(
194
+ """
195
+ **Instructions:**
196
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
197
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
198
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
199
+ ---
200
+ **Disclaimers:**
201
+ 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).
202
+ 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.
203
+ """
204
+ )
205
+
206
+ gr.LoginButton()
207
+
208
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
209
+
210
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
211
+ # Removed max_rows=10 from DataFrame constructor
212
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
213
+
214
+ run_button.click(
215
+ fn=run_and_submit_all,
216
+ outputs=[status_output, results_table]
217
+ )
218
+
219
+ if __name__ == "__main__":
220
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
221
+ # Check for SPACE_HOST and SPACE_ID at startup for information
222
+ space_host_startup = os.getenv("SPACE_HOST")
223
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
224
+
225
+ if space_host_startup:
226
+ print(f" SPACE_HOST found: {space_host_startup}")
227
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
228
+ else:
229
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
230
+
231
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
232
+ print(f"✅ SPACE_ID found: {space_id_startup}")
233
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
234
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
235
+ else:
236
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
237
+
238
+ print("-"*(60 + len(" App Starting ")) + "\n")
239
+
240
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
241
+ demo.launch(debug=True, share=False)