Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
6 |
|
7 |
# --- Constants ---
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -19,8 +20,8 @@ class BasicAgent:
|
|
19 |
|
20 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
21 |
"""
|
22 |
-
Fetches all questions, runs the
|
23 |
-
|
24 |
"""
|
25 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
26 |
space_id = os.getenv("SPACE_ID")
|
@@ -34,14 +35,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
34 |
|
35 |
api_url = DEFAULT_API_URL
|
36 |
questions_url = f"{api_url}/questions"
|
|
|
37 |
|
38 |
# 1. Instantiate Agent
|
39 |
try:
|
40 |
-
agent =
|
41 |
except Exception as e:
|
42 |
print(f"Error instantiating agent: {e}")
|
43 |
return f"Error initializing agent: {e}", None
|
44 |
|
|
|
|
|
|
|
45 |
# 2. Fetch Questions
|
46 |
print(f"Fetching questions from: {questions_url}")
|
47 |
try:
|
@@ -58,6 +63,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
58 |
|
59 |
# 3. Run Agent
|
60 |
results_log = []
|
|
|
61 |
print(f"Running agent on {len(questions_data)} questions...")
|
62 |
for item in questions_data:
|
63 |
task_id = item.get("task_id")
|
@@ -67,26 +73,30 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
67 |
continue
|
68 |
try:
|
69 |
submitted_answer = agent(question_text)
|
|
|
70 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
71 |
except Exception as e:
|
72 |
print(f"Error running agent on task {task_id}: {e}")
|
73 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
74 |
|
75 |
-
if not
|
76 |
-
print("Agent did not produce any answers.")
|
77 |
-
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
#
|
80 |
-
print(f"
|
81 |
for result in results_log:
|
82 |
print(f"Task ID: {result['Task ID']}")
|
83 |
print(f"Question: {result['Question']}")
|
84 |
print(f"Submitted Answer: {result['Submitted Answer']}")
|
85 |
-
|
86 |
-
|
87 |
-
# Returning the results as a DataFrame
|
88 |
results_df = pd.DataFrame(results_log)
|
89 |
-
return "
|
90 |
|
91 |
|
92 |
# --- Build Gradio Interface using Blocks ---
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from agent import DuckDuckGoAgent
|
7 |
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
20 |
|
21 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
22 |
"""
|
23 |
+
Fetches all questions, runs the DuckDuckGoAgent on them, submits all answers,
|
24 |
+
and displays the results.
|
25 |
"""
|
26 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
27 |
space_id = os.getenv("SPACE_ID")
|
|
|
35 |
|
36 |
api_url = DEFAULT_API_URL
|
37 |
questions_url = f"{api_url}/questions"
|
38 |
+
submit_url = f"{api_url}/submit"
|
39 |
|
40 |
# 1. Instantiate Agent
|
41 |
try:
|
42 |
+
agent = DuckDuckGoAgent()
|
43 |
except Exception as e:
|
44 |
print(f"Error instantiating agent: {e}")
|
45 |
return f"Error initializing agent: {e}", None
|
46 |
|
47 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
48 |
+
print(agent_code)
|
49 |
+
|
50 |
# 2. Fetch Questions
|
51 |
print(f"Fetching questions from: {questions_url}")
|
52 |
try:
|
|
|
63 |
|
64 |
# 3. Run Agent
|
65 |
results_log = []
|
66 |
+
answers_payload = []
|
67 |
print(f"Running agent on {len(questions_data)} questions...")
|
68 |
for item in questions_data:
|
69 |
task_id = item.get("task_id")
|
|
|
73 |
continue
|
74 |
try:
|
75 |
submitted_answer = agent(question_text)
|
76 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
77 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
78 |
except Exception as e:
|
79 |
print(f"Error running agent on task {task_id}: {e}")
|
80 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
81 |
|
82 |
+
if not answers_payload:
|
83 |
+
print("Agent did not produce any answers to submit.")
|
84 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
85 |
+
|
86 |
+
# 4. Preparar la sumisión
|
87 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
88 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
89 |
+
print(status_update)
|
90 |
|
91 |
+
# 5. Imprimir los resultados en lugar de enviar
|
92 |
+
print(f"Agent finished. Results (not submitting):")
|
93 |
for result in results_log:
|
94 |
print(f"Task ID: {result['Task ID']}")
|
95 |
print(f"Question: {result['Question']}")
|
96 |
print(f"Submitted Answer: {result['Submitted Answer']}")
|
97 |
+
|
|
|
|
|
98 |
results_df = pd.DataFrame(results_log)
|
99 |
+
return "Results printed instead of submitted.", results_df
|
100 |
|
101 |
|
102 |
# --- Build Gradio Interface using Blocks ---
|