Spaces:
Runtime error
Runtime error
Thomas Taylor
commited on
Commit
·
3baf2c4
1
Parent(s):
7fb6d39
feat: improving agent
Browse files- .gitignore +6 -0
- __pycache__/tools.cpython-310.pyc +0 -0
- app.py +148 -60
- requirements.txt +3 -1
- tools.py +33 -0
.gitignore
CHANGED
|
@@ -1,2 +1,8 @@
|
|
| 1 |
.env
|
| 2 |
.venv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
.env
|
| 2 |
.venv
|
| 3 |
+
|
| 4 |
+
model_answer.json
|
| 5 |
+
|
| 6 |
+
__pycache__
|
| 7 |
+
|
| 8 |
+
|
__pycache__/tools.cpython-310.pyc
ADDED
|
Binary file (1.09 kB). View file
|
|
|
app.py
CHANGED
|
@@ -3,20 +3,47 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
|
| 13 |
agent = CodeAgent(
|
| 14 |
-
tools=[
|
| 15 |
-
model=
|
| 16 |
-
planning_interval=3
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
-
|
| 20 |
# --- Basic Agent Definition ---
|
| 21 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 22 |
class BasicAgent:
|
|
@@ -24,7 +51,22 @@ class BasicAgent:
|
|
| 24 |
print("BasicAgent initialized.")
|
| 25 |
def __call__(self, question: str) -> str:
|
| 26 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
print(f"Agent returning fixed answer: {final_answer}")
|
| 29 |
return final_answer
|
| 30 |
|
|
@@ -78,75 +120,121 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 78 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 79 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 80 |
|
| 81 |
-
# 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
results_log = []
|
| 83 |
answers_payload = []
|
| 84 |
-
print(f"
|
| 85 |
for item in questions_data:
|
| 86 |
task_id = item.get("task_id")
|
| 87 |
question_text = item.get("question")
|
| 88 |
if not task_id or question_text is None:
|
| 89 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 90 |
continue
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
if not answers_payload:
|
| 100 |
-
print("
|
| 101 |
-
return "
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
try:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
f"
|
| 116 |
-
f"User: {
|
| 117 |
-
f"Overall Score: {
|
| 118 |
-
f"({
|
| 119 |
-
f"Message: {
|
| 120 |
)
|
| 121 |
-
print(
|
| 122 |
-
results_df = pd.DataFrame(results_log)
|
| 123 |
-
return final_status, results_df
|
| 124 |
-
except requests.exceptions.HTTPError as e:
|
| 125 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 126 |
-
try:
|
| 127 |
-
error_json = e.response.json()
|
| 128 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 129 |
-
except requests.exceptions.JSONDecodeError:
|
| 130 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 131 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 132 |
-
print(status_message)
|
| 133 |
-
results_df = pd.DataFrame(results_log)
|
| 134 |
-
return status_message, results_df
|
| 135 |
-
except requests.exceptions.Timeout:
|
| 136 |
-
status_message = "Submission Failed: The request timed out."
|
| 137 |
-
print(status_message)
|
| 138 |
-
results_df = pd.DataFrame(results_log)
|
| 139 |
-
return status_message, results_df
|
| 140 |
-
except requests.exceptions.RequestException as e:
|
| 141 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 142 |
-
print(status_message)
|
| 143 |
-
results_df = pd.DataFrame(results_log)
|
| 144 |
-
return status_message, results_df
|
| 145 |
except Exception as e:
|
| 146 |
-
|
| 147 |
-
print(
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
# --- Build Gradio Interface using Blocks ---
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel,WebSearchTool, VisitWebpageTool, ToolCallingAgent,LiteLLMModel,OpenAIServerModel
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
load_dotenv()
|
| 11 |
|
| 12 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 13 |
+
|
| 14 |
+
model = OpenAIServerModel(
|
| 15 |
+
model_id="gemini-2.5-flash-lite-preview-06-17",
|
| 16 |
+
# Google Gemini OpenAI-compatible API base URL
|
| 17 |
+
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 18 |
+
api_key=GEMINI_API_KEY,
|
| 19 |
+
)
|
| 20 |
+
# web_agent = ToolCallingAgent(
|
| 21 |
+
# tools=[WebSearchTool(), visit_webpage],
|
| 22 |
+
# model=model,
|
| 23 |
+
# max_steps=10,
|
| 24 |
+
# name="web_search_agent",
|
| 25 |
+
# description="Runs web searches for you.",
|
| 26 |
+
# )
|
| 27 |
+
|
| 28 |
+
# manager_agent = CodeAgent(
|
| 29 |
+
# tools=[],
|
| 30 |
+
# model=model,
|
| 31 |
+
# managed_agents=[web_agent],
|
| 32 |
+
# additional_authorized_imports=["time", "numpy", "pandas"],
|
| 33 |
+
# )
|
| 34 |
# (Keep Constants as is)
|
| 35 |
# --- Constants ---
|
| 36 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 37 |
|
| 38 |
|
| 39 |
agent = CodeAgent(
|
| 40 |
+
tools=[WebSearchTool(), VisitWebpageTool()],
|
| 41 |
+
model=model,
|
| 42 |
+
planning_interval=3,
|
| 43 |
+
additional_authorized_imports=["time", "numpy", "pandas", "requests", "bs4", "re", "markdownify"],
|
| 44 |
+
max_steps=5
|
| 45 |
)
|
| 46 |
|
|
|
|
| 47 |
# --- Basic Agent Definition ---
|
| 48 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 49 |
class BasicAgent:
|
|
|
|
| 51 |
print("BasicAgent initialized.")
|
| 52 |
def __call__(self, question: str) -> str:
|
| 53 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 54 |
+
PROMPT = """
|
| 55 |
+
You are a helpful assistant that can answer questions and help with tasks.
|
| 56 |
+
You will receive a question that can be either a question, a task, some common knowledge, some information related to documents, combination of all.
|
| 57 |
+
|
| 58 |
+
You can use the following tools to help you:
|
| 59 |
+
- DuckDuckGoSearchTool: Search the web for information.
|
| 60 |
+
- WebSearchTool: Search the web for information.
|
| 61 |
+
- VisitWebpageTool: Visit a webpage and return the content.
|
| 62 |
+
|
| 63 |
+
You will the answer only, no other text.
|
| 64 |
+
Provide the answer as a string. Do not include any other text. Provide the answer in <answer> tags.
|
| 65 |
+
Question: {question}
|
| 66 |
+
Answer:
|
| 67 |
+
"""
|
| 68 |
+
agent_answer = agent.run(PROMPT.format(question=question))
|
| 69 |
+
final_answer = agent_answer.split("<answer>")[1].split("</answer>")[0]
|
| 70 |
print(f"Agent returning fixed answer: {final_answer}")
|
| 71 |
return final_answer
|
| 72 |
|
|
|
|
| 120 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 121 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 122 |
|
| 123 |
+
# 3. Load cached answers from model_answer.json (if present)
|
| 124 |
+
answers_file = Path(__file__).with_name("model_answer.json")
|
| 125 |
+
cached_answers = []
|
| 126 |
+
if answers_file.exists():
|
| 127 |
+
try:
|
| 128 |
+
cached_answers = json.loads(answers_file.read_text(encoding="utf-8"))
|
| 129 |
+
print(f"Loaded {len(cached_answers)} cached answers from {answers_file.name}.")
|
| 130 |
+
except json.JSONDecodeError as e:
|
| 131 |
+
print(f"Warning: Could not parse {answers_file.name}: {e}. Continuing without cached answers.")
|
| 132 |
+
cached_answers = []
|
| 133 |
+
else:
|
| 134 |
+
print(f"No cached answers file found at {answers_file}. Will rely entirely on the agent.")
|
| 135 |
+
|
| 136 |
+
# Make a lookup dict by task_id for quick access
|
| 137 |
+
cached_by_task_id = {item.get("task_id"): item.get("answer") for item in cached_answers if item.get("task_id")}
|
| 138 |
+
|
| 139 |
+
# 4. Run your Agent OR use cached answers
|
| 140 |
results_log = []
|
| 141 |
answers_payload = []
|
| 142 |
+
print(f"Answering {len(questions_data)} questions (cached answers will be used when available)...")
|
| 143 |
for item in questions_data:
|
| 144 |
task_id = item.get("task_id")
|
| 145 |
question_text = item.get("question")
|
| 146 |
if not task_id or question_text is None:
|
| 147 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 148 |
continue
|
| 149 |
+
|
| 150 |
+
# Prefer cached answer if we have one
|
| 151 |
+
submitted_answer = cached_by_task_id.get(task_id)
|
| 152 |
+
if submitted_answer is None:
|
| 153 |
+
try:
|
| 154 |
+
submitted_answer = agent(question_text)
|
| 155 |
+
print(f"Generated answer for task {task_id}: {submitted_answer}")
|
| 156 |
+
except Exception as e:
|
| 157 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 158 |
+
submitted_answer = f"AGENT ERROR: {e}"
|
| 159 |
+
else:
|
| 160 |
+
print(f"Using cached answer for task {task_id}: {submitted_answer}")
|
| 161 |
+
|
| 162 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 163 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 164 |
|
| 165 |
if not answers_payload:
|
| 166 |
+
print("No answers produced to submit.")
|
| 167 |
+
return "No answers produced to submit.", pd.DataFrame(results_log)
|
| 168 |
|
| 169 |
+
# 5. Submit each answer individually
|
| 170 |
+
print(f"Submitting {len(answers_payload)} answers one-by-one to: {submit_url}")
|
| 171 |
+
successes = 0
|
| 172 |
+
submission_results = []
|
| 173 |
+
|
| 174 |
+
for answer_item in answers_payload:
|
| 175 |
+
submission_data = {
|
| 176 |
+
"username": username.strip(),
|
| 177 |
+
"agent_code": agent_code,
|
| 178 |
+
"answers": [answer_item], # single answer per request
|
| 179 |
+
}
|
| 180 |
+
try:
|
| 181 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 182 |
+
response.raise_for_status()
|
| 183 |
+
result_json = response.json()
|
| 184 |
+
successes += 1
|
| 185 |
+
score = result_json.get('score', 0)
|
| 186 |
+
message = result_json.get('message', 'No message')
|
| 187 |
+
print(f"Submitted task {answer_item['task_id']} ✓ Score: {score} Message: {message}")
|
| 188 |
+
submission_results.append({
|
| 189 |
+
"task_id": answer_item['task_id'],
|
| 190 |
+
"score": score,
|
| 191 |
+
"success": True,
|
| 192 |
+
"message": message
|
| 193 |
+
})
|
| 194 |
+
except Exception as e:
|
| 195 |
+
print(f"Failed to submit task {answer_item['task_id']}: {e}")
|
| 196 |
+
submission_results.append({
|
| 197 |
+
"task_id": answer_item['task_id'],
|
| 198 |
+
"score": 0,
|
| 199 |
+
"success": False,
|
| 200 |
+
"message": str(e)
|
| 201 |
+
})
|
| 202 |
|
| 203 |
+
# Calculate overall statistics
|
| 204 |
+
total_score = sum(result['score'] for result in submission_results if result['success'])
|
| 205 |
+
successful_submissions = len([r for r in submission_results if r['success']])
|
| 206 |
+
correct_answers = len([r for r in submission_results if r['score'] > 0])
|
| 207 |
+
|
| 208 |
+
# ALSO do a batch submission for leaderboard purposes
|
| 209 |
+
print(f"\n--- BATCH SUBMISSION FOR LEADERBOARD ---")
|
| 210 |
+
batch_submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 211 |
try:
|
| 212 |
+
batch_response = requests.post(submit_url, json=batch_submission_data, timeout=60)
|
| 213 |
+
batch_response.raise_for_status()
|
| 214 |
+
batch_result = batch_response.json()
|
| 215 |
+
batch_status = (
|
| 216 |
+
f"BATCH SUBMISSION:\n"
|
| 217 |
+
f"User: {batch_result.get('username')}\n"
|
| 218 |
+
f"Overall Score: {batch_result.get('score', 'N/A')}% "
|
| 219 |
+
f"({batch_result.get('correct_count', '?')}/{batch_result.get('total_attempted', '?')} correct)\n"
|
| 220 |
+
f"Message: {batch_result.get('message', 'No message received.')}"
|
| 221 |
)
|
| 222 |
+
print(batch_status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
except Exception as e:
|
| 224 |
+
batch_status = f"Batch submission failed: {e}"
|
| 225 |
+
print(batch_status)
|
| 226 |
+
|
| 227 |
+
final_status = (
|
| 228 |
+
f"Individual Submission Results:\n"
|
| 229 |
+
f"Successfully submitted: {successful_submissions}/{len(answers_payload)} answers\n"
|
| 230 |
+
f"Total accumulated score: {total_score}\n"
|
| 231 |
+
f"Average score per question: {total_score/len(answers_payload):.1f}\n"
|
| 232 |
+
f"Questions answered correctly: {correct_answers}/{len(answers_payload)}\n\n"
|
| 233 |
+
f"{batch_status}"
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
results_df = pd.DataFrame(results_log)
|
| 237 |
+
return final_status, results_df
|
| 238 |
|
| 239 |
|
| 240 |
# --- Build Gradio Interface using Blocks ---
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
smolagents[toolkit]
|
| 4 |
-
smolagents
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
smolagents[toolkit]
|
| 4 |
+
smolagents
|
| 5 |
+
smolagents[litellm]
|
| 6 |
+
smolagents[openai]
|
tools.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import requests
|
| 3 |
+
from markdownify import markdownify
|
| 4 |
+
from requests.exceptions import RequestException
|
| 5 |
+
from smolagents import tool
|
| 6 |
+
|
| 7 |
+
@tool
|
| 8 |
+
def visit_webpage(url: str) -> str:
|
| 9 |
+
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
url: The URL of the webpage to visit.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
# Send a GET request to the URL
|
| 19 |
+
response = requests.get(url)
|
| 20 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 21 |
+
|
| 22 |
+
# Convert the HTML content to Markdown
|
| 23 |
+
markdown_content = markdownify(response.text).strip()
|
| 24 |
+
|
| 25 |
+
# Remove multiple line breaks
|
| 26 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 27 |
+
|
| 28 |
+
return markdown_content
|
| 29 |
+
|
| 30 |
+
except RequestException as e:
|
| 31 |
+
return f"Error fetching the webpage: {str(e)}"
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"An unexpected error occurred: {str(e)}"
|