Spaces:
Sleeping
Sleeping
Back to Gradio
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
# import gradio as gr
|
4 |
import requests
|
5 |
import inspect
|
6 |
import pandas as pd
|
@@ -79,8 +78,7 @@ class BasicAgent:
|
|
79 |
return final_answer
|
80 |
|
81 |
|
82 |
-
|
83 |
-
async def run_and_submit_all():
|
84 |
"""
|
85 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
86 |
and displays the results.
|
@@ -88,12 +86,12 @@ async def run_and_submit_all():
|
|
88 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
89 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
username = "benjosaur"
|
98 |
|
99 |
api_url = DEFAULT_API_URL
|
@@ -226,181 +224,90 @@ async def run_and_submit_all():
|
|
226 |
|
227 |
|
228 |
# # --- Build Gradio Interface using Blocks ---
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
# 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
236 |
-
# 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
237 |
-
# 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
238 |
-
|
239 |
-
# ---
|
240 |
-
# **Disclaimers:**
|
241 |
-
# 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).
|
242 |
-
# 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.
|
243 |
-
# """
|
244 |
-
# )
|
245 |
-
|
246 |
-
# gr.LoginButton()
|
247 |
-
|
248 |
-
# run_button = gr.Button("Run Evaluation & Submit All Answers")
|
249 |
-
|
250 |
-
# status_output = gr.Textbox(
|
251 |
-
# label="Run Status / Submission Result", lines=5, interactive=False
|
252 |
-
# )
|
253 |
-
# # Removed max_rows=10 from DataFrame constructor
|
254 |
-
# results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
255 |
-
|
256 |
-
# run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
257 |
-
|
258 |
-
# async def main():
|
259 |
-
# agent = BasicAgent()
|
260 |
-
# api_url = DEFAULT_API_URL
|
261 |
-
# questions_url = f"{api_url}/questions"
|
262 |
-
# print(f"Fetching questions from: {questions_url}")
|
263 |
-
|
264 |
-
# response = requests.get(questions_url, timeout=15)
|
265 |
-
# response.raise_for_status()
|
266 |
-
# questions_data = response.json()
|
267 |
-
|
268 |
-
# # 3. Run your Agent
|
269 |
-
# results_log = []
|
270 |
-
# answers_payload = []
|
271 |
-
# print(f"Running agent on {len(questions_data)} questions...")
|
272 |
-
# item = questions_data[0]
|
273 |
-
# task_id = item.get("task_id")
|
274 |
-
# question_text = item.get("question")
|
275 |
-
# file_name = item.get("file_name")
|
276 |
-
# submitted_answer = await agent(question_text, task_id, file_name)
|
277 |
-
# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
278 |
-
# results_log.append(
|
279 |
-
# {
|
280 |
-
# "Task ID": task_id,
|
281 |
-
# "Question": question_text,
|
282 |
-
# "Submitted Answer": submitted_answer,
|
283 |
-
# }
|
284 |
-
# )
|
285 |
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
# space_host_startup = os.getenv("SPACE_HOST")
|
290 |
-
# space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
291 |
-
|
292 |
-
# if space_host_startup:
|
293 |
-
# print(f"✅ SPACE_HOST found: {space_host_startup}")
|
294 |
-
# print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
295 |
-
# else:
|
296 |
-
# print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
297 |
-
|
298 |
-
# if space_id_startup: # Print repo URLs if SPACE_ID is found
|
299 |
-
# print(f"✅ SPACE_ID found: {space_id_startup}")
|
300 |
-
# print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
301 |
-
# print(
|
302 |
-
# f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
303 |
-
# )
|
304 |
-
# else:
|
305 |
-
# print(
|
306 |
-
# "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
307 |
-
# )
|
308 |
-
|
309 |
-
# print("-" * (60 + len(" App Starting ")) + "\n")
|
310 |
-
|
311 |
-
# print("Launching Gradio Interface for Basic Agent Evaluation...")
|
312 |
-
# demo.launch(debug=True, share=False)
|
313 |
-
# asyncio.run(run_and_submit_all())
|
314 |
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
},
|
332 |
-
{
|
333 |
-
"task_id": "6f37996b-2ac7-44b0-8e68-6d28256631b4",
|
334 |
-
"submitted_answer": "a, b, c",
|
335 |
-
},
|
336 |
-
{
|
337 |
-
"task_id": "9d191bce-651d-4746-be2d-7ef8ecadb9c2",
|
338 |
-
"submitted_answer": "Extremely",
|
339 |
-
},
|
340 |
-
{
|
341 |
-
"task_id": "cabe07ed-9eca-40ea-8ead-410ef5e83f91",
|
342 |
-
"submitted_answer": "Undetermined",
|
343 |
-
},
|
344 |
-
{
|
345 |
-
"task_id": "3cef3a44-215e-4aed-8e3b-b1e3f08063b7",
|
346 |
-
"submitted_answer": "broccoli, celery, corn, green beans, lettuce, sweet potatoes, zucchini",
|
347 |
-
},
|
348 |
-
{
|
349 |
-
"task_id": "99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3",
|
350 |
-
"submitted_answer": "cornstarch, freshly squeezed lemon juice, granulated sugar, pure vanilla extract, ripe strawberries",
|
351 |
-
},
|
352 |
-
{
|
353 |
-
"task_id": "305ac316-eef6-4446-960a-92d80d542f82",
|
354 |
-
"submitted_answer": "Piotr",
|
355 |
-
},
|
356 |
-
{
|
357 |
-
"task_id": "3f57289b-8c60-48be-bd80-01f8099ca449",
|
358 |
-
"submitted_answer": "525",
|
359 |
-
},
|
360 |
-
{
|
361 |
-
"task_id": "1f975693-876d-457b-a649-393859e79bf3",
|
362 |
-
"submitted_answer": "132, 133, 134, 197, 245",
|
363 |
-
},
|
364 |
-
{
|
365 |
-
"task_id": "840bfca7-4f7b-481a-8794-c560c340185d",
|
366 |
-
"submitted_answer": "unknown",
|
367 |
-
},
|
368 |
-
{
|
369 |
-
"task_id": "bda648d7-d618-4883-88f4-3466eabd860e",
|
370 |
-
"submitted_answer": "Saint Petersburg",
|
371 |
-
},
|
372 |
-
{
|
373 |
-
"task_id": "cf106601-ab4f-4af9-b045-5295fe67b37d",
|
374 |
-
"submitted_answer": "LIE",
|
375 |
-
},
|
376 |
-
{
|
377 |
-
"task_id": "a0c07678-e491-4bbc-8f0b-07405144218f",
|
378 |
-
"submitted_answer": "Itoh, Unknown",
|
379 |
-
},
|
380 |
-
{
|
381 |
-
"task_id": "7bd855d8-463d-4ed5-93ca-5fe35145f733",
|
382 |
-
"submitted_answer": "56973.00",
|
383 |
-
},
|
384 |
-
{
|
385 |
-
"task_id": "5a0c1adf-205e-4841-a666-7c3ef95def9d",
|
386 |
-
"submitted_answer": "Claus",
|
387 |
-
},
|
388 |
-
]
|
389 |
|
|
|
|
|
|
|
|
|
|
|
390 |
api_url = DEFAULT_API_URL
|
391 |
questions_url = f"{api_url}/questions"
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
"agent_code": "https://huggingface.co/spaces/Benjosaur/Final_Assignment_Template/tree/main",
|
396 |
-
"answers": answers_payload,
|
397 |
-
}
|
398 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
399 |
-
response.raise_for_status()
|
400 |
-
if not response.ok:
|
401 |
-
print("Error submitting results!")
|
402 |
-
print("Status code:", response.status_code)
|
403 |
-
print("Response text:", response.text)
|
404 |
response.raise_for_status()
|
405 |
-
|
406 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
78 |
return final_answer
|
79 |
|
80 |
|
81 |
+
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
82 |
"""
|
83 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
84 |
and displays the results.
|
|
|
86 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
87 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
88 |
|
89 |
+
if profile:
|
90 |
+
username = f"{profile.username}"
|
91 |
+
print(f"User logged in: {username}")
|
92 |
+
else:
|
93 |
+
print("User not logged in.")
|
94 |
+
return "Please Login to Hugging Face with the button.", None
|
95 |
username = "benjosaur"
|
96 |
|
97 |
api_url = DEFAULT_API_URL
|
|
|
224 |
|
225 |
|
226 |
# # --- Build Gradio Interface using Blocks ---
|
227 |
+
with gr.Blocks() as demo:
|
228 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
229 |
+
gr.Markdown(
|
230 |
+
"""
|
231 |
+
**Instructions:**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
234 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
235 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
|
237 |
+
---
|
238 |
+
**Disclaimers:**
|
239 |
+
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).
|
240 |
+
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.
|
241 |
+
"""
|
242 |
+
)
|
243 |
+
|
244 |
+
gr.LoginButton()
|
245 |
+
|
246 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
247 |
+
|
248 |
+
status_output = gr.Textbox(
|
249 |
+
label="Run Status / Submission Result", lines=5, interactive=False
|
250 |
+
)
|
251 |
+
# Removed max_rows=10 from DataFrame constructor
|
252 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
|
254 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
255 |
+
|
256 |
+
|
257 |
+
async def main():
|
258 |
+
agent = BasicAgent()
|
259 |
api_url = DEFAULT_API_URL
|
260 |
questions_url = f"{api_url}/questions"
|
261 |
+
print(f"Fetching questions from: {questions_url}")
|
262 |
+
|
263 |
+
response = requests.get(questions_url, timeout=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
response.raise_for_status()
|
265 |
+
questions_data = response.json()
|
266 |
+
|
267 |
+
# 3. Run your Agent
|
268 |
+
results_log = []
|
269 |
+
answers_payload = []
|
270 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
271 |
+
item = questions_data[0]
|
272 |
+
task_id = item.get("task_id")
|
273 |
+
question_text = item.get("question")
|
274 |
+
file_name = item.get("file_name")
|
275 |
+
submitted_answer = await agent(question_text, task_id, file_name)
|
276 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
277 |
+
results_log.append(
|
278 |
+
{
|
279 |
+
"Task ID": task_id,
|
280 |
+
"Question": question_text,
|
281 |
+
"Submitted Answer": submitted_answer,
|
282 |
+
}
|
283 |
+
)
|
284 |
+
|
285 |
+
|
286 |
+
if __name__ == "__main__":
|
287 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
288 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
289 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
290 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
291 |
+
|
292 |
+
if space_host_startup:
|
293 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
294 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
295 |
+
else:
|
296 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
297 |
+
|
298 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
299 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
300 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
301 |
+
print(
|
302 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
303 |
+
)
|
304 |
+
else:
|
305 |
+
print(
|
306 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
307 |
+
)
|
308 |
+
|
309 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
310 |
+
|
311 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
312 |
+
demo.launch(debug=True, share=False)
|
313 |
+
# asyncio.run(run_and_submit_all())
|