Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,67 @@
|
|
1 |
import os
|
|
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
-
import
|
5 |
-
|
6 |
from agent import BasicAgent
|
7 |
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
|
10 |
-
class GAIAAgent:
|
11 |
-
def __init__(self):
|
12 |
-
self.agent = BasicAgent()
|
13 |
-
def __call__(self, question: str, task_id: str) -> str:
|
14 |
-
return self.agent(question, task_id)
|
15 |
-
|
16 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
17 |
space_id = os.getenv("SPACE_ID")
|
18 |
-
if
|
|
|
|
|
|
|
|
|
19 |
return "Please Login to Hugging Face with the button.", None
|
20 |
|
21 |
-
username = profile.username.strip()
|
22 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
23 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
24 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
|
|
25 |
|
|
|
26 |
try:
|
27 |
response = requests.get(questions_url, timeout=15)
|
28 |
response.raise_for_status()
|
29 |
questions_data = response.json()
|
|
|
|
|
|
|
|
|
30 |
except Exception as e:
|
|
|
31 |
return f"Error fetching questions: {e}", None
|
32 |
|
33 |
-
agent =
|
34 |
results_log = []
|
35 |
answers_payload = []
|
|
|
36 |
|
37 |
for item in questions_data:
|
38 |
task_id = item.get("task_id")
|
39 |
question_text = item.get("question")
|
40 |
if not task_id or question_text is None:
|
|
|
41 |
continue
|
42 |
try:
|
43 |
-
submitted_answer = agent(question_text
|
|
|
|
|
|
|
44 |
except Exception as e:
|
45 |
-
|
46 |
-
|
47 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
48 |
|
49 |
-
|
|
|
|
|
50 |
|
|
|
|
|
|
|
|
|
|
|
51 |
try:
|
52 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
53 |
response.raise_for_status()
|
@@ -59,30 +73,33 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
59 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
60 |
f"Message: {result_data.get('message', 'No message received.')}"
|
61 |
)
|
62 |
-
|
|
|
|
|
63 |
except Exception as e:
|
64 |
-
|
|
|
|
|
|
|
65 |
|
66 |
with gr.Blocks() as demo:
|
67 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
68 |
gr.Markdown("""
|
69 |
**Instructions:**
|
70 |
-
1. Please clone this space
|
71 |
-
2. Log in to Hugging Face
|
72 |
-
3. Click 'Run Evaluation & Submit All Answers' to run
|
|
|
|
|
|
|
|
|
73 |
""")
|
74 |
-
|
75 |
gr.LoginButton()
|
76 |
-
|
77 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
78 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
79 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers")
|
80 |
-
|
81 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
-
print("
|
85 |
-
|
86 |
-
if space_id:
|
87 |
-
print(f"🔗 Space: https://huggingface.co/spaces/{space_id}")
|
88 |
-
demo.launch(debug=True)
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
+
import time
|
|
|
6 |
from agent import BasicAgent
|
7 |
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
11 |
space_id = os.getenv("SPACE_ID")
|
12 |
+
if profile:
|
13 |
+
username = f"{profile.username}"
|
14 |
+
print(f"User logged in: {username}")
|
15 |
+
else:
|
16 |
+
print("User not logged in.")
|
17 |
return "Please Login to Hugging Face with the button.", None
|
18 |
|
|
|
|
|
19 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
20 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
21 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
22 |
|
23 |
+
print(f"Fetching questions from: {questions_url}")
|
24 |
try:
|
25 |
response = requests.get(questions_url, timeout=15)
|
26 |
response.raise_for_status()
|
27 |
questions_data = response.json()
|
28 |
+
if not questions_data:
|
29 |
+
print("Fetched questions list is empty.")
|
30 |
+
return "Fetched questions list is empty or invalid format.", None
|
31 |
+
print(f"Fetched {len(questions_data)} questions.")
|
32 |
except Exception as e:
|
33 |
+
print(f"Error fetching questions: {e}")
|
34 |
return f"Error fetching questions: {e}", None
|
35 |
|
36 |
+
agent = BasicAgent()
|
37 |
results_log = []
|
38 |
answers_payload = []
|
39 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
40 |
|
41 |
for item in questions_data:
|
42 |
task_id = item.get("task_id")
|
43 |
question_text = item.get("question")
|
44 |
if not task_id or question_text is None:
|
45 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
46 |
continue
|
47 |
try:
|
48 |
+
submitted_answer = agent(question_text)
|
49 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
50 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
51 |
+
time.sleep(2)
|
52 |
except Exception as e:
|
53 |
+
print(f"Error running agent on task {task_id}: {e}")
|
54 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
55 |
|
56 |
+
if not answers_payload:
|
57 |
+
print("Agent did not produce any answers to submit.")
|
58 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
59 |
|
60 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
61 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
62 |
+
print(status_update)
|
63 |
+
|
64 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
65 |
try:
|
66 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
67 |
response.raise_for_status()
|
|
|
73 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
74 |
f"Message: {result_data.get('message', 'No message received.')}"
|
75 |
)
|
76 |
+
print("Submission successful.")
|
77 |
+
results_df = pd.DataFrame(results_log)
|
78 |
+
return final_status, results_df
|
79 |
except Exception as e:
|
80 |
+
status_message = f"Submission Failed: {e}"
|
81 |
+
print(status_message)
|
82 |
+
results_df = pd.DataFrame(results_log)
|
83 |
+
return status_message, results_df
|
84 |
|
85 |
with gr.Blocks() as demo:
|
86 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
87 |
gr.Markdown("""
|
88 |
**Instructions:**
|
89 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
90 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
91 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
92 |
+
---
|
93 |
+
**Disclaimers:**
|
94 |
+
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).
|
95 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution.
|
96 |
""")
|
|
|
97 |
gr.LoginButton()
|
|
|
98 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
99 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
100 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
101 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
105 |
+
demo.launch(debug=True, share=False)
|
|
|
|
|
|