Spaces:
Sleeping
Sleeping
Commit ·
a930a5f
1
Parent(s): 5c344c8
Update tools and CodeAgent
Browse files
app.py
CHANGED
|
@@ -1,24 +1,113 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
-
import pandas as pd
|
| 5 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class BasicAgent:
|
| 11 |
|
| 12 |
def __init__(self, metadata_path="metadata.jsonl"):
|
| 13 |
self.metadata = self._load_metadata(metadata_path)
|
| 14 |
print("BasicAgent initialized with metadata")
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def _load_metadata(self, file_path):
|
| 18 |
"""Load metadata from a JSONL file, parsing each line as a JSON object."""
|
| 19 |
data = []
|
| 20 |
try:
|
| 21 |
-
with open(file_path,
|
| 22 |
for line_number, line in enumerate(f, 1):
|
| 23 |
line = line.strip()
|
| 24 |
if not line:
|
|
@@ -34,7 +123,9 @@ class BasicAgent:
|
|
| 34 |
print(f"Loaded metadata from '{file_path}' with {len(data)} entries")
|
| 35 |
return data
|
| 36 |
except FileNotFoundError:
|
| 37 |
-
print(
|
|
|
|
|
|
|
| 38 |
return []
|
| 39 |
except Exception as e:
|
| 40 |
print(f"Unexpected error loading metadata from '{file_path}': {e}")
|
|
@@ -81,55 +172,37 @@ class BasicAgent:
|
|
| 81 |
return "No action executed"
|
| 82 |
|
| 83 |
def __call__(self, question: str) -> str:
|
| 84 |
-
"""
|
| 85 |
-
User Question
|
| 86 |
-
↓
|
| 87 |
-
Planner
|
| 88 |
-
↓
|
| 89 |
-
Reasoning Loop
|
| 90 |
-
↓
|
| 91 |
-
Tool Selection
|
| 92 |
-
↓
|
| 93 |
-
Tool Execution
|
| 94 |
-
↓
|
| 95 |
-
Observation
|
| 96 |
-
↓
|
| 97 |
-
Repeat until solved
|
| 98 |
-
↓
|
| 99 |
-
Final Answer
|
| 100 |
-
"""
|
| 101 |
-
|
| 102 |
|
| 103 |
print(f"Agent received question: {question}")
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
print("Action:", action)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
-
return "unknown"
|
| 125 |
-
|
| 126 |
|
| 127 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 128 |
"""
|
| 129 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 130 |
and displays the results.
|
| 131 |
"""
|
| 132 |
-
space_id = os.getenv("SPACE_ID")
|
| 133 |
|
| 134 |
if profile:
|
| 135 |
username = f"{profile.username}"
|
|
@@ -178,17 +251,35 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 178 |
continue
|
| 179 |
try:
|
| 180 |
submitted_answer = agent(question_text)
|
| 181 |
-
answers_payload.append(
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
except Exception as e:
|
| 184 |
print(f"Error running agent on task {task_id}: {e}")
|
| 185 |
-
results_log.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
if not answers_payload:
|
| 188 |
print("Agent did not produce any answers to submit.")
|
| 189 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 190 |
|
| 191 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 193 |
print(status_update)
|
| 194 |
|
|
@@ -233,6 +324,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 233 |
results_df = pd.DataFrame(results_log)
|
| 234 |
return status_message, results_df
|
| 235 |
|
|
|
|
| 236 |
# --- Build Gradio Interface using Blocks ---
|
| 237 |
with gr.Blocks() as demo:
|
| 238 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
@@ -253,16 +345,15 @@ with gr.Blocks() as demo:
|
|
| 253 |
|
| 254 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 255 |
|
| 256 |
-
status_output = gr.Textbox(
|
|
|
|
|
|
|
| 257 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 258 |
|
| 259 |
-
run_button.click(
|
| 260 |
-
fn=run_and_submit_all,
|
| 261 |
-
outputs=[status_output, results_table]
|
| 262 |
-
)
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|
| 265 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 266 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 267 |
space_id_startup = os.getenv("SPACE_ID")
|
| 268 |
|
|
@@ -275,11 +366,15 @@ if __name__ == "__main__":
|
|
| 275 |
if space_id_startup:
|
| 276 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 277 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 278 |
-
print(
|
|
|
|
|
|
|
| 279 |
else:
|
| 280 |
-
print(
|
|
|
|
|
|
|
| 281 |
|
| 282 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 283 |
|
| 284 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 285 |
demo.launch(debug=True, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
import requests
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
|
| 7 |
+
from smolagents import CodeAgent, tool, InferenceClientModel, DuckDuckGoSearchTool
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
+
GLOBAL_AGENT = None
|
| 12 |
+
search_tool = DuckDuckGoSearchTool()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# -----------------------
|
| 16 |
+
# TOOL IMPLEMENTATIONS
|
| 17 |
+
# -----------------------
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@tool
|
| 21 |
+
def rag_search(query: str) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Retrieve relevant information from the local FAISS knowledge base.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
agent = GLOBAL_AGENT
|
| 27 |
+
|
| 28 |
+
if agent.index is None:
|
| 29 |
+
return "Knowledge base empty."
|
| 30 |
+
|
| 31 |
+
query_embedding = agent.embed_model.encode([query])
|
| 32 |
+
|
| 33 |
+
distances, indices = agent.index.search(np.array(query_embedding), 3)
|
| 34 |
+
|
| 35 |
+
results = []
|
| 36 |
+
|
| 37 |
+
for idx in indices[0]:
|
| 38 |
+
item = agent.metadata[idx]
|
| 39 |
+
|
| 40 |
+
question = item.get("Question", "")
|
| 41 |
+
answer = item.get("Final answer", "")
|
| 42 |
+
|
| 43 |
+
results.append(f"Question: {question}\nAnswer: {answer}")
|
| 44 |
+
|
| 45 |
+
return "\n\n".join(results)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@tool
|
| 49 |
+
def calculator(expression: str) -> str:
|
| 50 |
+
"""
|
| 51 |
+
Evaluate mathematical expressions.
|
| 52 |
+
Example: 5*23+12
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
return str(eval(expression))
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"CALCULATION_ERROR:{e}"
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@tool
|
| 62 |
+
def web_search(query: str) -> str:
|
| 63 |
+
"""
|
| 64 |
+
Search the web for up-to-date information.
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
results = search_tool.run(query)
|
| 69 |
+
return str(results)[:1000]
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"WEB_SEARCH_FAILED:{e}"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@tool
|
| 76 |
+
def image_reader(agent, image_path=None):
|
| 77 |
+
"""Placeholder multimodal tool."""
|
| 78 |
+
return "IMAGE_ANALYSIS_NOT_IMPLEMENTED"
|
| 79 |
+
|
| 80 |
|
| 81 |
class BasicAgent:
|
| 82 |
|
| 83 |
def __init__(self, metadata_path="metadata.jsonl"):
|
| 84 |
self.metadata = self._load_metadata(metadata_path)
|
| 85 |
print("BasicAgent initialized with metadata")
|
| 86 |
+
global GLOBAL_AGENT
|
| 87 |
+
GLOBAL_AGENT = self
|
| 88 |
+
self.embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 89 |
+
|
| 90 |
+
documents = [item.get("Question", "") for item in self.metadata]
|
| 91 |
+
|
| 92 |
+
if documents:
|
| 93 |
+
embeddings = self.embed_model.encode(documents)
|
| 94 |
+
dimension = len(embeddings[0])
|
| 95 |
+
self.index = faiss.IndexFlatL2(dimension)
|
| 96 |
+
self.index.add(np.array(embeddings))
|
| 97 |
+
else:
|
| 98 |
+
self.index = None
|
| 99 |
+
|
| 100 |
+
self.agent = CodeAgent(
|
| 101 |
+
tools=[rag_search, calculator, web_search, image_reader],
|
| 102 |
+
model=InferenceClientModel(),
|
| 103 |
+
max_steps=6,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
def _load_metadata(self, file_path):
|
| 107 |
"""Load metadata from a JSONL file, parsing each line as a JSON object."""
|
| 108 |
data = []
|
| 109 |
try:
|
| 110 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 111 |
for line_number, line in enumerate(f, 1):
|
| 112 |
line = line.strip()
|
| 113 |
if not line:
|
|
|
|
| 123 |
print(f"Loaded metadata from '{file_path}' with {len(data)} entries")
|
| 124 |
return data
|
| 125 |
except FileNotFoundError:
|
| 126 |
+
print(
|
| 127 |
+
f"Metadata file '{file_path}' not found. Proceeding without metadata."
|
| 128 |
+
)
|
| 129 |
return []
|
| 130 |
except Exception as e:
|
| 131 |
print(f"Unexpected error loading metadata from '{file_path}': {e}")
|
|
|
|
| 172 |
return "No action executed"
|
| 173 |
|
| 174 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
print(f"Agent received question: {question}")
|
| 177 |
|
| 178 |
+
try:
|
| 179 |
+
response = self.agent.run(
|
| 180 |
+
f"""
|
| 181 |
+
You are a reasoning agent solving benchmark questions.
|
| 182 |
|
| 183 |
+
Use tools when needed:
|
| 184 |
+
- rag_search for local knowledge
|
| 185 |
+
- web_search for internet lookup
|
| 186 |
+
- calculator for math
|
| 187 |
|
| 188 |
+
Question: {question}
|
|
|
|
| 189 |
|
| 190 |
+
Return only the final answer.
|
| 191 |
+
"""
|
| 192 |
+
)
|
| 193 |
|
| 194 |
+
return str(response)
|
| 195 |
|
| 196 |
+
except Exception as e:
|
| 197 |
+
return f"AGENT_ERROR:{e}"
|
| 198 |
|
|
|
|
|
|
|
| 199 |
|
| 200 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 201 |
"""
|
| 202 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 203 |
and displays the results.
|
| 204 |
"""
|
| 205 |
+
space_id = os.getenv("SPACE_ID")
|
| 206 |
|
| 207 |
if profile:
|
| 208 |
username = f"{profile.username}"
|
|
|
|
| 251 |
continue
|
| 252 |
try:
|
| 253 |
submitted_answer = agent(question_text)
|
| 254 |
+
answers_payload.append(
|
| 255 |
+
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 256 |
+
)
|
| 257 |
+
results_log.append(
|
| 258 |
+
{
|
| 259 |
+
"Task ID": task_id,
|
| 260 |
+
"Question": question_text,
|
| 261 |
+
"Submitted Answer": submitted_answer,
|
| 262 |
+
}
|
| 263 |
+
)
|
| 264 |
except Exception as e:
|
| 265 |
print(f"Error running agent on task {task_id}: {e}")
|
| 266 |
+
results_log.append(
|
| 267 |
+
{
|
| 268 |
+
"Task ID": task_id,
|
| 269 |
+
"Question": question_text,
|
| 270 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 271 |
+
}
|
| 272 |
+
)
|
| 273 |
|
| 274 |
if not answers_payload:
|
| 275 |
print("Agent did not produce any answers to submit.")
|
| 276 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 277 |
|
| 278 |
+
submission_data = {
|
| 279 |
+
"username": username.strip(),
|
| 280 |
+
"agent_code": agent_code,
|
| 281 |
+
"answers": answers_payload,
|
| 282 |
+
}
|
| 283 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 284 |
print(status_update)
|
| 285 |
|
|
|
|
| 324 |
results_df = pd.DataFrame(results_log)
|
| 325 |
return status_message, results_df
|
| 326 |
|
| 327 |
+
|
| 328 |
# --- Build Gradio Interface using Blocks ---
|
| 329 |
with gr.Blocks() as demo:
|
| 330 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
|
|
| 345 |
|
| 346 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 347 |
|
| 348 |
+
status_output = gr.Textbox(
|
| 349 |
+
label="Run Status / Submission Result", lines=5, interactive=False
|
| 350 |
+
)
|
| 351 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 352 |
|
| 353 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
if __name__ == "__main__":
|
| 356 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
| 357 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 358 |
space_id_startup = os.getenv("SPACE_ID")
|
| 359 |
|
|
|
|
| 366 |
if space_id_startup:
|
| 367 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 368 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 369 |
+
print(
|
| 370 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
| 371 |
+
)
|
| 372 |
else:
|
| 373 |
+
print(
|
| 374 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
| 375 |
+
)
|
| 376 |
|
| 377 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 378 |
|
| 379 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 380 |
demo.launch(debug=True, share=False)
|