Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,41 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
4 |
import pandas as pd
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
# --- Basic Agent Definition ---
|
9 |
-
|
10 |
-
|
11 |
-
print("BasicAgent initialized.")
|
12 |
-
def __call__(self, question: str) -> str:
|
13 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
14 |
-
fixed_answer = "This is a default answer."
|
15 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
16 |
-
return fixed_answer
|
17 |
-
|
18 |
-
def run_smol_agent():
|
19 |
-
question = "Search for the best music recommendations for a party at the Wayne's mansion."
|
20 |
-
response = agent.run(question) # Usa el agente mejorado aquí
|
21 |
-
print(f"Smol Agent Response: {response}")
|
22 |
-
return response
|
23 |
|
24 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
25 |
"""
|
26 |
-
Fetches all questions, runs the
|
27 |
and displays the results.
|
28 |
"""
|
29 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
30 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
31 |
|
32 |
if profile:
|
33 |
-
username
|
34 |
print(f"User logged in: {username}")
|
35 |
else:
|
36 |
print("User not logged in.")
|
37 |
return "Please Login to Hugging Face with the button.", None
|
38 |
|
39 |
-
api_url =
|
40 |
questions_url = f"{api_url}/questions"
|
41 |
submit_url = f"{api_url}/submit"
|
42 |
|
43 |
-
# 1. Instantiate Agent
|
44 |
try:
|
45 |
-
agent =
|
46 |
except Exception as e:
|
47 |
print(f"Error instantiating agent: {e}")
|
48 |
return f"Error initializing agent: {e}", None
|
49 |
-
|
50 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
51 |
print(agent_code)
|
52 |
|
@@ -57,19 +46,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
57 |
response.raise_for_status()
|
58 |
questions_data = response.json()
|
59 |
if not questions_data:
|
60 |
-
|
61 |
-
|
62 |
print(f"Fetched {len(questions_data)} questions.")
|
63 |
except requests.exceptions.RequestException as e:
|
64 |
print(f"Error fetching questions: {e}")
|
65 |
return f"Error fetching questions: {e}", None
|
66 |
-
except requests.exceptions.JSONDecodeError as e:
|
67 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
68 |
-
print(f"Response text: {response.text[:500]}")
|
69 |
-
return f"Error decoding server response for questions: {e}", None
|
70 |
-
except Exception as e:
|
71 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
72 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
73 |
|
74 |
# 3. Run your Agent
|
75 |
results_log = []
|
@@ -82,12 +64,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
82 |
print(f"Skipping item with missing task_id or question: {item}")
|
83 |
continue
|
84 |
try:
|
85 |
-
submitted_answer = agent
|
86 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
87 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
88 |
except Exception as e:
|
89 |
-
|
90 |
-
|
91 |
|
92 |
if not answers_payload:
|
93 |
print("Agent did not produce any answers to submit.")
|
@@ -161,9 +143,9 @@ with gr.Blocks() as demo:
|
|
161 |
gr.LoginButton()
|
162 |
|
163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
164 |
-
run_smol_agent_button = gr.Button("Run SmolAgent Search")
|
165 |
|
166 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
168 |
|
169 |
run_button.click(
|
@@ -172,4 +154,25 @@ with gr.Blocks() as demo:
|
|
172 |
)
|
173 |
|
174 |
if __name__ == "__main__":
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
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"
|
9 |
|
10 |
# --- Basic Agent Definition ---
|
11 |
+
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
12 |
+
from agent import BasicAgent # Importa el agente
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
15 |
"""
|
16 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
17 |
and displays the results.
|
18 |
"""
|
19 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
20 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
21 |
|
22 |
if profile:
|
23 |
+
username= f"{profile.username}"
|
24 |
print(f"User logged in: {username}")
|
25 |
else:
|
26 |
print("User not logged in.")
|
27 |
return "Please Login to Hugging Face with the button.", None
|
28 |
|
29 |
+
api_url = DEFAULT_API_URL
|
30 |
questions_url = f"{api_url}/questions"
|
31 |
submit_url = f"{api_url}/submit"
|
32 |
|
33 |
+
# 1. Instantiate Agent
|
34 |
try:
|
35 |
+
agent = BasicAgent() # Inicializa el agente con búsqueda en DuckDuckGo
|
36 |
except Exception as e:
|
37 |
print(f"Error instantiating agent: {e}")
|
38 |
return f"Error initializing agent: {e}", None
|
|
|
39 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
40 |
print(agent_code)
|
41 |
|
|
|
46 |
response.raise_for_status()
|
47 |
questions_data = response.json()
|
48 |
if not questions_data:
|
49 |
+
print("Fetched questions list is empty.")
|
50 |
+
return "Fetched questions list is empty or invalid format.", None
|
51 |
print(f"Fetched {len(questions_data)} questions.")
|
52 |
except requests.exceptions.RequestException as e:
|
53 |
print(f"Error fetching questions: {e}")
|
54 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# 3. Run your Agent
|
57 |
results_log = []
|
|
|
64 |
print(f"Skipping item with missing task_id or question: {item}")
|
65 |
continue
|
66 |
try:
|
67 |
+
submitted_answer = agent(question_text)
|
68 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
69 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
70 |
except Exception as e:
|
71 |
+
print(f"Error running agent on task {task_id}: {e}")
|
72 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
73 |
|
74 |
if not answers_payload:
|
75 |
print("Agent did not produce any answers to submit.")
|
|
|
143 |
gr.LoginButton()
|
144 |
|
145 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
146 |
|
147 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
148 |
+
# Removed max_rows=10 from DataFrame constructor
|
149 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
150 |
|
151 |
run_button.click(
|
|
|
154 |
)
|
155 |
|
156 |
if __name__ == "__main__":
|
157 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
158 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
159 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
160 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
161 |
+
|
162 |
+
if space_host_startup:
|
163 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
164 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
165 |
+
else:
|
166 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
167 |
+
|
168 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
169 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
170 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
171 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
172 |
+
else:
|
173 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
174 |
+
|
175 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
176 |
+
|
177 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
178 |
+
demo.launch(debug=True, share=False)
|