Spaces:
Running
Running
initial
Browse files- agents/BasicAgent.py +58 -0
- app.py +39 -32
- requirements.txt +6 -1
agents/BasicAgent.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, HfApiModel
|
4 |
+
|
5 |
+
|
6 |
+
class BasicAgent(CodeAgent):
|
7 |
+
def __init__(self):
|
8 |
+
print("Initializing BasicAgent...")
|
9 |
+
|
10 |
+
model_name = os.getenv("MODEL_NAME")
|
11 |
+
|
12 |
+
if not model_name:
|
13 |
+
raise ValueError(
|
14 |
+
"Environment variable 'MODEL_NAME' is required but not set.")
|
15 |
+
|
16 |
+
print(f"LLM Config: Model='{model_name}'")
|
17 |
+
|
18 |
+
try:
|
19 |
+
llm = HfApiModel(
|
20 |
+
model_id=model_name,
|
21 |
+
temperature=0.1,
|
22 |
+
)
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error initializing LLM: {e}")
|
25 |
+
raise
|
26 |
+
|
27 |
+
print("Initializing tools...")
|
28 |
+
try:
|
29 |
+
search_tool = DuckDuckGoSearchTool()
|
30 |
+
visit_tool = VisitWebpageTool()
|
31 |
+
tools = [search_tool, visit_tool]
|
32 |
+
print(f"Tools initialized: {[tool.name for tool in tools]}")
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Error initializing tools: {e}")
|
35 |
+
raise
|
36 |
+
|
37 |
+
try:
|
38 |
+
super().__init__(
|
39 |
+
model=llm,
|
40 |
+
tools=tools,
|
41 |
+
)
|
42 |
+
print("BasicAgent (CodeAgent) initialized successfully.")
|
43 |
+
except Exception as e:
|
44 |
+
print(f"Error initializing CodeAgent: {e}")
|
45 |
+
raise
|
46 |
+
|
47 |
+
def __call__(self, question: str) -> str:
|
48 |
+
print(
|
49 |
+
f"BasicAgent received question (first 50 chars): {question[:50]}...")
|
50 |
+
try:
|
51 |
+
final_answer = self.run(question)
|
52 |
+
print(
|
53 |
+
f"BasicAgent returning answer (first 50 chars): {str(final_answer)[:50]}...")
|
54 |
+
return str(final_answer)
|
55 |
+
except Exception as e:
|
56 |
+
print(
|
57 |
+
f"Error during agent execution for question '{question[:50]}...': {e}")
|
58 |
+
return f"AGENT ERROR: Failed to process the question due to: {e}"
|
app.py
CHANGED
@@ -4,31 +4,30 @@ import requests
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
class BasicAgent:
|
14 |
-
def __init__(self):
|
15 |
-
print("BasicAgent initialized.")
|
16 |
-
def __call__(self, question: str) -> str:
|
17 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
18 |
-
fixed_answer = "This is a default answer."
|
19 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
20 |
-
return fixed_answer
|
21 |
-
|
22 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
23 |
"""
|
24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
25 |
and displays the results.
|
26 |
"""
|
27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
28 |
-
|
|
|
29 |
|
30 |
if profile:
|
31 |
-
username= f"{profile.username}"
|
32 |
print(f"User logged in: {username}")
|
33 |
else:
|
34 |
print("User not logged in.")
|
@@ -55,16 +54,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
55 |
response.raise_for_status()
|
56 |
questions_data = response.json()
|
57 |
if not questions_data:
|
58 |
-
|
59 |
-
|
60 |
print(f"Fetched {len(questions_data)} questions.")
|
61 |
except requests.exceptions.RequestException as e:
|
62 |
print(f"Error fetching questions: {e}")
|
63 |
return f"Error fetching questions: {e}", None
|
64 |
except requests.exceptions.JSONDecodeError as e:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
except Exception as e:
|
69 |
print(f"An unexpected error occurred fetching questions: {e}")
|
70 |
return f"An unexpected error occurred fetching questions: {e}", None
|
@@ -81,18 +80,22 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
81 |
continue
|
82 |
try:
|
83 |
submitted_answer = agent(question_text)
|
84 |
-
answers_payload.append(
|
85 |
-
|
|
|
|
|
86 |
except Exception as e:
|
87 |
-
|
88 |
-
|
|
|
89 |
|
90 |
if not answers_payload:
|
91 |
print("Agent did not produce any answers to submit.")
|
92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
93 |
|
94 |
-
# 4. Prepare Submission
|
95 |
-
submission_data = {"username": username.strip(
|
|
|
96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
97 |
print(status_update)
|
98 |
|
@@ -162,9 +165,11 @@ with gr.Blocks() as demo:
|
|
162 |
|
163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
164 |
|
165 |
-
status_output = gr.Textbox(
|
|
|
166 |
# Removed max_rows=10 from DataFrame constructor
|
167 |
-
results_table = gr.DataFrame(
|
|
|
168 |
|
169 |
run_button.click(
|
170 |
fn=run_and_submit_all,
|
@@ -175,22 +180,24 @@ if __name__ == "__main__":
|
|
175 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
176 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
177 |
space_host_startup = os.getenv("SPACE_HOST")
|
178 |
-
space_id_startup = os.getenv("SPACE_ID")
|
179 |
|
180 |
if space_host_startup:
|
181 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
182 |
-
print(
|
|
|
183 |
else:
|
184 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
185 |
|
186 |
-
if space_id_startup:
|
187 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
188 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
189 |
-
print(
|
|
|
190 |
else:
|
191 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
192 |
|
193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
194 |
|
195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
196 |
-
demo.launch(debug=True, share=False)
|
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
|
7 |
+
from huggingface_hub import login
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
from agents.BasicAgent import BasicAgent
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
login(os.environ['HF_TOKEN'])
|
14 |
+
|
15 |
# (Keep Constants as is)
|
16 |
# --- Constants ---
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
18 |
|
19 |
+
|
20 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
"""
|
22 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
23 |
and displays the results.
|
24 |
"""
|
25 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
26 |
+
# Get the SPACE_ID for sending link to the code
|
27 |
+
space_id = os.getenv("SPACE_ID")
|
28 |
|
29 |
if profile:
|
30 |
+
username = f"{profile.username}"
|
31 |
print(f"User logged in: {username}")
|
32 |
else:
|
33 |
print("User not logged in.")
|
|
|
54 |
response.raise_for_status()
|
55 |
questions_data = response.json()
|
56 |
if not questions_data:
|
57 |
+
print("Fetched questions list is empty.")
|
58 |
+
return "Fetched questions list is empty or invalid format.", None
|
59 |
print(f"Fetched {len(questions_data)} questions.")
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
print(f"Error fetching questions: {e}")
|
62 |
return f"Error fetching questions: {e}", None
|
63 |
except requests.exceptions.JSONDecodeError as e:
|
64 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
65 |
+
print(f"Response text: {response.text[:500]}")
|
66 |
+
return f"Error decoding server response for questions: {e}", None
|
67 |
except Exception as e:
|
68 |
print(f"An unexpected error occurred fetching questions: {e}")
|
69 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
80 |
continue
|
81 |
try:
|
82 |
submitted_answer = agent(question_text)
|
83 |
+
answers_payload.append(
|
84 |
+
{"task_id": task_id, "submitted_answer": submitted_answer})
|
85 |
+
results_log.append(
|
86 |
+
{"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
87 |
except Exception as e:
|
88 |
+
print(f"Error running agent on task {task_id}: {e}")
|
89 |
+
results_log.append(
|
90 |
+
{"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
91 |
|
92 |
if not answers_payload:
|
93 |
print("Agent did not produce any answers to submit.")
|
94 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
95 |
|
96 |
+
# 4. Prepare Submission
|
97 |
+
submission_data = {"username": username.strip(
|
98 |
+
), "agent_code": agent_code, "answers": answers_payload}
|
99 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
100 |
print(status_update)
|
101 |
|
|
|
165 |
|
166 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
167 |
|
168 |
+
status_output = gr.Textbox(
|
169 |
+
label="Run Status / Submission Result", lines=5, interactive=False)
|
170 |
# Removed max_rows=10 from DataFrame constructor
|
171 |
+
results_table = gr.DataFrame(
|
172 |
+
label="Questions and Agent Answers", wrap=True)
|
173 |
|
174 |
run_button.click(
|
175 |
fn=run_and_submit_all,
|
|
|
180 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
181 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
182 |
space_host_startup = os.getenv("SPACE_HOST")
|
183 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
184 |
|
185 |
if space_host_startup:
|
186 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
187 |
+
print(
|
188 |
+
f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
189 |
else:
|
190 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
191 |
|
192 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
193 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
194 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
195 |
+
print(
|
196 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
197 |
else:
|
198 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
199 |
|
200 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
201 |
|
202 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
203 |
+
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
@@ -1,2 +1,7 @@
|
|
1 |
gradio
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
gradio
|
2 |
+
gradio[oauth]
|
3 |
+
huggingface_hub
|
4 |
+
python-dotenv
|
5 |
+
requests
|
6 |
+
smolagents
|
7 |
+
smolagents[litellm]
|