updated app.py
Browse files
app.py
CHANGED
@@ -3,51 +3,57 @@ import gradio as gr
|
|
3 |
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 |
# --- Basic Agent Definition ---
|
12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
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 |
-
|
19 |
-
|
20 |
-
return
|
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 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
29 |
-
|
30 |
if profile:
|
31 |
username= f"{profile.username}"
|
32 |
print(f"User logged in: {username}")
|
33 |
else:
|
34 |
print("User not logged in.")
|
35 |
return "Please Login to Hugging Face with the button.", None
|
36 |
-
|
37 |
api_url = DEFAULT_API_URL
|
38 |
questions_url = f"{api_url}/questions"
|
39 |
submit_url = f"{api_url}/submit"
|
40 |
-
|
41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
42 |
try:
|
43 |
-
agent = BasicAgent()
|
44 |
except Exception as e:
|
45 |
print(f"Error instantiating agent: {e}")
|
46 |
return f"Error initializing agent: {e}", None
|
47 |
# 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)
|
48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
49 |
print(agent_code)
|
50 |
-
|
51 |
# 2. Fetch Questions
|
52 |
print(f"Fetching questions from: {questions_url}")
|
53 |
try:
|
@@ -68,7 +74,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
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
|
71 |
-
|
72 |
# 3. Run your Agent
|
73 |
results_log = []
|
74 |
answers_payload = []
|
@@ -86,16 +92,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
86 |
except Exception as e:
|
87 |
print(f"Error running agent on task {task_id}: {e}")
|
88 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
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(), "agent_code": agent_code, "answers": answers_payload}
|
96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
97 |
print(status_update)
|
98 |
-
|
99 |
# 5. Submit
|
100 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
101 |
try:
|
@@ -138,59 +144,66 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
138 |
print(status_message)
|
139 |
results_df = pd.DataFrame(results_log)
|
140 |
return status_message, results_df
|
141 |
-
|
142 |
-
|
143 |
# --- Build Gradio Interface using Blocks ---
|
144 |
with gr.Blocks() as demo:
|
145 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
146 |
gr.Markdown(
|
147 |
"""
|
148 |
**Instructions:**
|
149 |
-
|
150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
153 |
-
|
154 |
---
|
155 |
**Disclaimers:**
|
156 |
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).
|
157 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
158 |
"""
|
159 |
)
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
gr.LoginButton()
|
162 |
-
|
163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
164 |
-
|
165 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
166 |
# Removed max_rows=10 from DataFrame constructor
|
167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
168 |
-
|
169 |
run_button.click(
|
170 |
fn=run_and_submit_all,
|
|
|
171 |
outputs=[status_output, results_table]
|
172 |
)
|
173 |
-
|
174 |
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") # Get SPACE_ID at startup
|
179 |
-
|
180 |
if space_host_startup:
|
181 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
182 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
183 |
else:
|
184 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
185 |
-
|
186 |
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
187 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
188 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
189 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
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)
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, PythonInterpreterTool, WikipediaSearchTool, FinalAnswerTool
|
7 |
+
|
8 |
# (Keep Constants as is)
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
+
|
12 |
# --- Basic Agent Definition ---
|
13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
14 |
class BasicAgent:
|
15 |
+
def __init__(self, api_key: str = None):
|
16 |
+
if not api_key:
|
17 |
+
print("Did not receive API key.")
|
18 |
+
else:
|
19 |
+
print(f"First 5 characters of the provided API key: {api_key[:5]}")
|
20 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
21 |
print("BasicAgent initialized.")
|
22 |
def __call__(self, question: str) -> str:
|
23 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
24 |
+
agent = CodeAgent(tools=[DuckDuckGoSearchTool(),PythonInterpreterTool(),WikipediaSearchTool(),FinalAnswerTool()],model=OpenAIServerModel(model_id="gpt-4o"))
|
25 |
+
answer=agent.run(question)
|
26 |
+
return answer
|
27 |
+
|
28 |
+
def run_and_submit_all(api_key: str, profile: gr.OAuthProfile | None = None):
|
29 |
"""
|
30 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
31 |
and displays the results.
|
32 |
"""
|
33 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
34 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
35 |
+
|
36 |
if profile:
|
37 |
username= f"{profile.username}"
|
38 |
print(f"User logged in: {username}")
|
39 |
else:
|
40 |
print("User not logged in.")
|
41 |
return "Please Login to Hugging Face with the button.", None
|
42 |
+
|
43 |
api_url = DEFAULT_API_URL
|
44 |
questions_url = f"{api_url}/questions"
|
45 |
submit_url = f"{api_url}/submit"
|
46 |
+
|
47 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
48 |
try:
|
49 |
+
agent = BasicAgent(api_key=api_key)
|
50 |
except Exception as e:
|
51 |
print(f"Error instantiating agent: {e}")
|
52 |
return f"Error initializing agent: {e}", None
|
53 |
# 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)
|
54 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
55 |
print(agent_code)
|
56 |
+
|
57 |
# 2. Fetch Questions
|
58 |
print(f"Fetching questions from: {questions_url}")
|
59 |
try:
|
|
|
74 |
except Exception as e:
|
75 |
print(f"An unexpected error occurred fetching questions: {e}")
|
76 |
return f"An unexpected error occurred fetching questions: {e}", None
|
77 |
+
|
78 |
# 3. Run your Agent
|
79 |
results_log = []
|
80 |
answers_payload = []
|
|
|
92 |
except Exception as e:
|
93 |
print(f"Error running agent on task {task_id}: {e}")
|
94 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
95 |
+
|
96 |
if not answers_payload:
|
97 |
print("Agent did not produce any answers to submit.")
|
98 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
99 |
+
|
100 |
# 4. Prepare Submission
|
101 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
102 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
103 |
print(status_update)
|
104 |
+
|
105 |
# 5. Submit
|
106 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
107 |
try:
|
|
|
144 |
print(status_message)
|
145 |
results_df = pd.DataFrame(results_log)
|
146 |
return status_message, results_df
|
147 |
+
|
148 |
+
|
149 |
# --- Build Gradio Interface using Blocks ---
|
150 |
with gr.Blocks() as demo:
|
151 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
152 |
gr.Markdown(
|
153 |
"""
|
154 |
**Instructions:**
|
155 |
+
|
156 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
157 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
158 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
159 |
+
|
160 |
---
|
161 |
**Disclaimers:**
|
162 |
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).
|
163 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
164 |
"""
|
165 |
)
|
166 |
+
|
167 |
+
# Add API Key input field
|
168 |
+
api_key_input = gr.Textbox(
|
169 |
+
label="API Key",
|
170 |
+
placeholder="Enter your OpenAI API key here (sk-...)",
|
171 |
+
type="password"
|
172 |
+
)
|
173 |
gr.LoginButton()
|
174 |
+
|
175 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
176 |
+
|
177 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
178 |
# Removed max_rows=10 from DataFrame constructor
|
179 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
180 |
+
|
181 |
run_button.click(
|
182 |
fn=run_and_submit_all,
|
183 |
+
inputs=[api_key_input],
|
184 |
outputs=[status_output, results_table]
|
185 |
)
|
186 |
+
|
187 |
if __name__ == "__main__":
|
188 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
189 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
190 |
space_host_startup = os.getenv("SPACE_HOST")
|
191 |
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
192 |
+
|
193 |
if space_host_startup:
|
194 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
195 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
196 |
else:
|
197 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
198 |
+
|
199 |
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
200 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
201 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
202 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
203 |
else:
|
204 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
205 |
+
|
206 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
207 |
+
|
208 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
209 |
demo.launch(debug=True, share=False)
|