Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,9 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool,
|
| 7 |
|
|
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
@@ -12,52 +13,23 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
print("BasicAgent initialized.")
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# --- REVISED SYSTEM PROMPT FOR EXACT MATCH ---
|
| 28 |
-
# This prompt is designed to make the agent output ONLY the answer,
|
| 29 |
-
# without any preambles, thoughts, or "FINAL ANSWER:" tags.
|
| 30 |
-
# This is critical for exact match scoring.
|
| 31 |
-
SYSTEM_PROMPT = """You are an ultra-concise and accurate AI assistant for the GAIA benchmark.
|
| 32 |
-
Your sole purpose is to provide the exact correct answer to the given question, and NOTHING ELSE.
|
| 33 |
-
Do not include any introductory phrases, thoughts, explanations, or "FINAL ANSWER:" tags.
|
| 34 |
-
Your output must be the answer only.
|
| 35 |
-
|
| 36 |
-
**Here's how you should determine and format your answer:**
|
| 37 |
-
|
| 38 |
-
1. **Understand the Question:** Carefully read the question to identify the precise information needed.
|
| 39 |
-
2. **Gather Information:**
|
| 40 |
-
* Use your internal knowledge if the answer is straightforward or involves simple arithmetic.
|
| 41 |
-
* **Crucially, use the `DuckDuckGoSearchTool`** if the question requires external knowledge, current facts, or complex research. Formulate the most effective search queries to find exact information.
|
| 42 |
-
* Analyze search results meticulously to extract the precise fact(s).
|
| 43 |
-
3. **Formulate the Answer (Strict Formatting Rules):**
|
| 44 |
-
* **Numbers:** If the answer is a number, provide it in digits ONLY. Do NOT use commas for thousands separators, currency symbols (e.g., "$"), percentage signs (e.g., "%"), or any other units (e.g., "kg", "meters").
|
| 45 |
-
* *Example 1 (number):* If the question asks "What is the square root of 81?", your answer should be "9".
|
| 46 |
-
* *Example 2 (large number):* If the question asks "What is the population of City X (approx)?", and you find "1,234,567", your answer must be "1234567".
|
| 47 |
-
* **Strings (words):** If the answer is text, use the fewest words possible. Do NOT include articles (a, an, the). Do NOT use abbreviations. If a number is part of a string (e.g., "twenty books"), write the digit in plain English words (e.g., "twenty").
|
| 48 |
-
* *Example 1 (string):* If the question asks "Who invented the telephone?", your answer should be "Alexander Graham Bell".
|
| 49 |
-
* *Example 2 (string with number in text):* If the question asks "How many primary colors are there?", your answer should be "three".
|
| 50 |
-
* **Comma-separated lists:** If the answer is a list of items, apply the above rules to each item, and separate them with a comma and a single space (ee.g., "red, green, blue").
|
| 51 |
-
* *Example:* If the question asks "List two types of fruit.", your answer should be "apple, banana".
|
| 52 |
-
|
| 53 |
-
Your final output *must* be only the answer, following these exact rules. No surrounding text.
|
| 54 |
"""
|
| 55 |
self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + SYSTEM_PROMPT
|
| 56 |
-
|
| 57 |
def __call__(self, question: str) -> str:
|
| 58 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 59 |
-
# The key change: The agent's run method should directly produce the final answer,
|
| 60 |
-
# without additional wrapping, due to the hyper-specific prompt.
|
| 61 |
final_answer = self.agent.run(question)
|
| 62 |
print(f"Agent returning final answer: {final_answer}")
|
| 63 |
return final_answer
|
|
@@ -69,6 +41,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 69 |
"""
|
| 70 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 71 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
|
|
|
| 72 |
if profile:
|
| 73 |
username= f"{profile.username}"
|
| 74 |
print(f"User logged in: {username}")
|
|
@@ -86,12 +59,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error instantiating agent: {e}")
|
| 88 |
return f"Error initializing agent: {e}", None
|
| 89 |
-
|
| 90 |
# 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)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
agent_code = f"https://huggingface.co/spaces/{os.getenv('SPACE_ID', 'YOUR_HF_USERNAME/YOUR_SPACE_NAME')}/tree/main"
|
| 94 |
-
print(f"Agent code will link to: {agent_code}")
|
| 95 |
|
| 96 |
# 2. Fetch Questions
|
| 97 |
print(f"Fetching questions from: {questions_url}")
|
|
@@ -126,16 +96,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 126 |
continue
|
| 127 |
try:
|
| 128 |
submitted_answer = agent(question_text)
|
| 129 |
-
# IMPORTANT: Post-process the submitted_answer if the model still adds unwanted text.
|
| 130 |
-
# While the prompt aims to prevent it, sometimes models are stubborn.
|
| 131 |
-
# This stripping makes sure only the raw answer is sent.
|
| 132 |
-
submitted_answer = submitted_answer.strip()
|
| 133 |
-
# Remove any trailing "FINAL ANSWER:" or "FINAL ANSWER:" if model still generates it.
|
| 134 |
-
if submitted_answer.startswith("FINAL ANSWER:"):
|
| 135 |
-
submitted_answer = submitted_answer[len("FINAL ANSWER:"):].strip()
|
| 136 |
-
# You might need more sophisticated stripping depending on how the model misbehaves
|
| 137 |
-
# For example, if it adds thoughts, you'd need to extract the last line or a specific pattern.
|
| 138 |
-
|
| 139 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 140 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 141 |
except Exception as e:
|
|
@@ -146,7 +106,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 146 |
print("Agent did not produce any answers to submit.")
|
| 147 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 148 |
|
| 149 |
-
# 4. Prepare Submission
|
| 150 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 151 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 152 |
print(status_update)
|
|
@@ -194,6 +154,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 194 |
results_df = pd.DataFrame(results_log)
|
| 195 |
return status_message, results_df
|
| 196 |
|
|
|
|
| 197 |
# --- Build Gradio Interface using Blocks ---
|
| 198 |
with gr.Blocks() as demo:
|
| 199 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
@@ -202,11 +163,15 @@ with gr.Blocks() as demo:
|
|
| 202 |
"Log in to your Hugging Face account using the button below. This uses your HF username for submission. "
|
| 203 |
"Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score."
|
| 204 |
)
|
|
|
|
| 205 |
gr.LoginButton()
|
|
|
|
| 206 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 207 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 208 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 209 |
-
|
| 210 |
run_button.click(
|
| 211 |
fn=run_and_submit_all,
|
| 212 |
outputs=[status_output, results_table]
|
|
@@ -214,19 +179,24 @@ with gr.Blocks() as demo:
|
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
|
| 217 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 218 |
-
space_id_startup = os.getenv("SPACE_ID")
|
|
|
|
| 219 |
if space_host_startup:
|
| 220 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 221 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 222 |
else:
|
| 223 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 224 |
-
|
|
|
|
| 225 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 226 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 227 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 228 |
else:
|
| 229 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
|
|
|
| 230 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
|
|
|
| 231 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 232 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 7 |
|
| 8 |
+
# (Keep Constants and BasicAgent class as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
+
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
|
| 17 |
+
|
| 18 |
+
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and
|
| 19 |
+
finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 20 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
|
| 21 |
+
list of numbers and/or strings.
|
| 22 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or
|
| 23 |
+
percent sign unless specified otherwise.
|
| 24 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the
|
| 25 |
+
digits in plain text unless specified otherwise.
|
| 26 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element
|
| 27 |
+
to be put in the list is a number or a string.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"""
|
| 29 |
self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + SYSTEM_PROMPT
|
| 30 |
+
|
| 31 |
def __call__(self, question: str) -> str:
|
| 32 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
| 33 |
final_answer = self.agent.run(question)
|
| 34 |
print(f"Agent returning final answer: {final_answer}")
|
| 35 |
return final_answer
|
|
|
|
| 41 |
"""
|
| 42 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 43 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 44 |
+
|
| 45 |
if profile:
|
| 46 |
username= f"{profile.username}"
|
| 47 |
print(f"User logged in: {username}")
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error instantiating agent: {e}")
|
| 61 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 62 |
# 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)
|
| 63 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 64 |
+
print(agent_code)
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# 2. Fetch Questions
|
| 67 |
print(f"Fetching questions from: {questions_url}")
|
|
|
|
| 96 |
continue
|
| 97 |
try:
|
| 98 |
submitted_answer = agent(question_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 100 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 101 |
except Exception as e:
|
|
|
|
| 106 |
print("Agent did not produce any answers to submit.")
|
| 107 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 108 |
|
| 109 |
+
# 4. Prepare Submission
|
| 110 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 111 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 112 |
print(status_update)
|
|
|
|
| 154 |
results_df = pd.DataFrame(results_log)
|
| 155 |
return status_message, results_df
|
| 156 |
|
| 157 |
+
|
| 158 |
# --- Build Gradio Interface using Blocks ---
|
| 159 |
with gr.Blocks() as demo:
|
| 160 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
|
|
| 163 |
"Log in to your Hugging Face account using the button below. This uses your HF username for submission. "
|
| 164 |
"Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score."
|
| 165 |
)
|
| 166 |
+
|
| 167 |
gr.LoginButton()
|
| 168 |
+
|
| 169 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 170 |
+
|
| 171 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 172 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 173 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 174 |
+
|
| 175 |
run_button.click(
|
| 176 |
fn=run_and_submit_all,
|
| 177 |
outputs=[status_output, results_table]
|
|
|
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 182 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 183 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 184 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 185 |
+
|
| 186 |
if space_host_startup:
|
| 187 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 188 |
print(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(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 196 |
else:
|
| 197 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 198 |
+
|
| 199 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 200 |
+
|
| 201 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 202 |
demo.launch(debug=True, share=False)
|