Update app.py
Browse files
app.py
CHANGED
@@ -7,19 +7,20 @@ from importlib import resources
|
|
7 |
import requests
|
8 |
import yaml
|
9 |
import numpy as np
|
10 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
# (Keep Constants as is)
|
13 |
# --- Constants ---
|
14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
|
16 |
-
# ---
|
17 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
class GetTaskFileTool(Tool):
|
24 |
name = "get_task_file_tool"
|
25 |
description = """This tool downloads the file content associated with the given task_id if exists. Returns absolute file path"""
|
@@ -59,19 +60,21 @@ class LoadTextFileTool(Tool):
|
|
59 |
with open(file_path, 'r', encoding='utf-8') as file:
|
60 |
return file.read()
|
61 |
|
|
|
62 |
prompts = yaml.safe_load(
|
63 |
resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
|
64 |
)
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
+ prompts["system_prompt"])
|
68 |
-
|
69 |
def init_agent():
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
temperature=0.7
|
75 |
)
|
76 |
agent = CodeAgent(
|
77 |
tools=[
|
@@ -83,29 +86,18 @@ def init_agent():
|
|
83 |
LoadXlsxFileTool(),
|
84 |
LoadTextFileTool()
|
85 |
],
|
86 |
-
model=
|
87 |
prompt_templates=prompts,
|
88 |
max_steps=15,
|
89 |
additional_authorized_imports = ["pandas"]
|
90 |
)
|
91 |
return agent
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
100 |
-
"""
|
101 |
-
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
102 |
-
and displays the results.
|
103 |
-
"""
|
104 |
-
# --- Determine HF Space Runtime URL and Repo URL ---
|
105 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
106 |
|
|
|
|
|
|
|
107 |
if profile:
|
108 |
-
username= f"{profile.username}"
|
109 |
print(f"User logged in: {username}")
|
110 |
else:
|
111 |
print("User not logged in.")
|
@@ -115,13 +107,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
115 |
questions_url = f"{api_url}/questions"
|
116 |
submit_url = f"{api_url}/submit"
|
117 |
|
118 |
-
# 1. Instantiate Agent
|
119 |
try:
|
120 |
agent = init_agent()
|
121 |
except Exception as e:
|
122 |
print(f"Error instantiating agent: {e}")
|
123 |
return f"Error initializing agent: {e}", None
|
124 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
125 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
126 |
print(agent_code)
|
127 |
|
@@ -132,16 +123,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
132 |
response.raise_for_status()
|
133 |
questions_data = response.json()
|
134 |
if not questions_data:
|
135 |
-
|
136 |
-
|
137 |
print(f"Fetched {len(questions_data)} questions.")
|
138 |
except requests.exceptions.RequestException as e:
|
139 |
print(f"Error fetching questions: {e}")
|
140 |
return f"Error fetching questions: {e}", None
|
141 |
except requests.exceptions.JSONDecodeError as e:
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
except Exception as e:
|
146 |
print(f"An unexpected error occurred fetching questions: {e}")
|
147 |
return f"An unexpected error occurred fetching questions: {e}", None
|
@@ -159,17 +150,19 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
159 |
print(f"Skipping item with missing task_id or question: {item}")
|
160 |
continue
|
161 |
try:
|
162 |
-
submitted_answer = agent.run(
|
|
|
|
|
163 |
if isinstance(submitted_answer, (np.integer, np.floating)):
|
164 |
-
submitted_answer = submitted_answer.item()
|
165 |
elif isinstance(submitted_answer, list):
|
166 |
submitted_answer = [x.item() if isinstance(x, (np.integer, np.floating)) else x for x in submitted_answer]
|
167 |
submitted_answer = str(submitted_answer)
|
168 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
169 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
170 |
except Exception as e:
|
171 |
-
|
172 |
-
|
173 |
|
174 |
if not answers_payload:
|
175 |
print("Agent did not produce any answers to submit.")
|
@@ -223,7 +216,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
223 |
results_df = pd.DataFrame(results_log)
|
224 |
return status_message, results_df
|
225 |
|
226 |
-
|
227 |
# --- Build Gradio Interface using Blocks ---
|
228 |
with gr.Blocks() as demo:
|
229 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
@@ -245,7 +237,6 @@ with gr.Blocks() as demo:
|
|
245 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
246 |
|
247 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
248 |
-
# Removed max_rows=10 from DataFrame constructor
|
249 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
250 |
|
251 |
run_button.click(
|
@@ -255,9 +246,8 @@ with gr.Blocks() as demo:
|
|
255 |
|
256 |
if __name__ == "__main__":
|
257 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
258 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
259 |
space_host_startup = os.getenv("SPACE_HOST")
|
260 |
-
space_id_startup = os.getenv("SPACE_ID")
|
261 |
|
262 |
if space_host_startup:
|
263 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
@@ -265,7 +255,7 @@ if __name__ == "__main__":
|
|
265 |
else:
|
266 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
267 |
|
268 |
-
if space_id_startup:
|
269 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
270 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
271 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
@@ -273,6 +263,5 @@ if __name__ == "__main__":
|
|
273 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
274 |
|
275 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
276 |
-
|
277 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
278 |
demo.launch(debug=True, share=False)
|
|
|
7 |
import requests
|
8 |
import yaml
|
9 |
import numpy as np
|
10 |
+
from smolagents import (
|
11 |
+
CodeAgent,
|
12 |
+
DuckDuckGoSearchTool,
|
13 |
+
VisitWebpageTool,
|
14 |
+
WikipediaSearchTool,
|
15 |
+
Tool,
|
16 |
+
OpenAIServerModel,
|
17 |
+
SpeechToTextTool
|
18 |
+
)
|
19 |
|
|
|
20 |
# --- Constants ---
|
21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
22 |
|
23 |
+
# --- Custom Tools ---
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
class GetTaskFileTool(Tool):
|
25 |
name = "get_task_file_tool"
|
26 |
description = """This tool downloads the file content associated with the given task_id if exists. Returns absolute file path"""
|
|
|
60 |
with open(file_path, 'r', encoding='utf-8') as file:
|
61 |
return file.read()
|
62 |
|
63 |
+
# --- Prompts ---
|
64 |
prompts = yaml.safe_load(
|
65 |
resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
|
66 |
)
|
67 |
+
prompts["system_prompt"] = (
|
68 |
+
"You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. "
|
69 |
+
+ prompts["system_prompt"]
|
70 |
+
)
|
71 |
|
72 |
+
# --- Agent Initialization ---
|
|
|
|
|
73 |
def init_agent():
|
74 |
+
openai_model = OpenAIServerModel(
|
75 |
+
model_id="gpt-4o", # or "gpt-4" etc.
|
76 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
77 |
+
temperature=0
|
|
|
78 |
)
|
79 |
agent = CodeAgent(
|
80 |
tools=[
|
|
|
86 |
LoadXlsxFileTool(),
|
87 |
LoadTextFileTool()
|
88 |
],
|
89 |
+
model=openai_model,
|
90 |
prompt_templates=prompts,
|
91 |
max_steps=15,
|
92 |
additional_authorized_imports = ["pandas"]
|
93 |
)
|
94 |
return agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
# --- Main Runner ---
|
97 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
98 |
+
space_id = os.getenv("SPACE_ID")
|
99 |
if profile:
|
100 |
+
username = f"{profile.username}"
|
101 |
print(f"User logged in: {username}")
|
102 |
else:
|
103 |
print("User not logged in.")
|
|
|
107 |
questions_url = f"{api_url}/questions"
|
108 |
submit_url = f"{api_url}/submit"
|
109 |
|
110 |
+
# 1. Instantiate Agent
|
111 |
try:
|
112 |
agent = init_agent()
|
113 |
except Exception as e:
|
114 |
print(f"Error instantiating agent: {e}")
|
115 |
return f"Error initializing agent: {e}", None
|
|
|
116 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
117 |
print(agent_code)
|
118 |
|
|
|
123 |
response.raise_for_status()
|
124 |
questions_data = response.json()
|
125 |
if not questions_data:
|
126 |
+
print("Fetched questions list is empty.")
|
127 |
+
return "Fetched questions list is empty or invalid format.", None
|
128 |
print(f"Fetched {len(questions_data)} questions.")
|
129 |
except requests.exceptions.RequestException as e:
|
130 |
print(f"Error fetching questions: {e}")
|
131 |
return f"Error fetching questions: {e}", None
|
132 |
except requests.exceptions.JSONDecodeError as e:
|
133 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
134 |
+
print(f"Response text: {response.text[:500]}")
|
135 |
+
return f"Error decoding server response for questions: {e}", None
|
136 |
except Exception as e:
|
137 |
print(f"An unexpected error occurred fetching questions: {e}")
|
138 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
150 |
print(f"Skipping item with missing task_id or question: {item}")
|
151 |
continue
|
152 |
try:
|
153 |
+
submitted_answer = agent.run(
|
154 |
+
f"Task id: {task_id}. Task file: {file_name if file_name != '' else 'is absent'}. Task: " + question_text
|
155 |
+
)
|
156 |
if isinstance(submitted_answer, (np.integer, np.floating)):
|
157 |
+
submitted_answer = submitted_answer.item()
|
158 |
elif isinstance(submitted_answer, list):
|
159 |
submitted_answer = [x.item() if isinstance(x, (np.integer, np.floating)) else x for x in submitted_answer]
|
160 |
submitted_answer = str(submitted_answer)
|
161 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
162 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
163 |
except Exception as e:
|
164 |
+
print(f"Error running agent on task {task_id}: {e}")
|
165 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
166 |
|
167 |
if not answers_payload:
|
168 |
print("Agent did not produce any answers to submit.")
|
|
|
216 |
results_df = pd.DataFrame(results_log)
|
217 |
return status_message, results_df
|
218 |
|
|
|
219 |
# --- Build Gradio Interface using Blocks ---
|
220 |
with gr.Blocks() as demo:
|
221 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
|
237 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
238 |
|
239 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
240 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
241 |
|
242 |
run_button.click(
|
|
|
246 |
|
247 |
if __name__ == "__main__":
|
248 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
249 |
space_host_startup = os.getenv("SPACE_HOST")
|
250 |
+
space_id_startup = os.getenv("SPACE_ID")
|
251 |
|
252 |
if space_host_startup:
|
253 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
255 |
else:
|
256 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
257 |
|
258 |
+
if space_id_startup:
|
259 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
260 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
261 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
|
|
263 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
264 |
|
265 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
|
|
266 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
267 |
demo.launch(debug=True, share=False)
|