Spaces:
Build error
Build error
Delete agentsTools
Browse filesFiles changed (3)
- agentsTools/toolVisitWebpage.pyDeleted-44
- agentsTools/tool_fetch_task_file.pyDeleted-42
- agentsTools/tool_read_excel_as_json.pyDeleted-54
1234567891011121314151617181920212223242526272829303132333435363738394041424344from smolagents import tool
import requests
from bs4 import BeautifulSoup
MAX_WEBPAGE_SIZE = 3000 # max characters to return from scraped content
@tool
def visit_webpage(url: str) -> dict:
"""
Visits a webpage and extracts clean text from it.
Args:
url: The URL of the page to visit (e.g., https://en.wikipedia.org/wiki/OpenAI)
Returns:
dict: A dictionary containing:
- "text": Truncated page content
- "url": The original URL
- "status": HTTP status or error info
"""
print(f" Tool:visit_webpage visiting {url}...")
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
text = soup.get_text(separator="\n", strip=True)
short_text = text[:MAX_WEBPAGE_SIZE]
print(f"✅ Extracted {len(short_text)} characters from {url}")
return {
"text": short_text,
"url": url,
"status": f"Success ({response.status_code})"
}
except Exception as e:
print(f"🚨 Error in visit_webpage: {e}")
return {
"text": "",
"url": url,
"status": f"Error: {e}"
}123456789101112131415161718192021222324252627282930313233343536373839404142from smolagents import tool
import os
import requests
DEFAULT_API_URL = os.getenv("AGENT_API_URL", "https://agents-course-unit4-scoring.hf.space")
@tool
def fetch_task_file(task_id: str) -> dict:
"""
Fetches the file associated with a task ID using the API URL from env.
Args:
task_id: The task ID to fetch.
Returns:
dict: task_id, content, status
"""
full_url = f"{DEFAULT_API_URL}/files/{task_id}"
print(f"📥 Tool:fetch_task_file requesting {full_url}")
try:
response = requests.get(full_url, timeout=10)
if response.status_code == 200:
return {
"task_id": task_id,
"content": response.text[:5000],
"status": "Success"
}
return {
"task_id": task_id,
"content": "",
"status": f"{response.status_code} - {response.reason}"
}
except Exception as e:
return {
"task_id": task_id,
"content": "",
"status": f"Error: {str(e)}"
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354from smolagents import tool
import os
import requests
import pandas as pd
from io import BytesIO
DEFAULT_API_URL = os.getenv("AGENT_API_URL", "https://agents-course-unit4-scoring.hf.space")
@tool
def read_excel_as_json(task_id: str) -> dict:
"""
Fetches and parses an Excel file as structured JSON for a given task_id.
Args:
task_id: The task ID to fetch.
Returns:
{
"task_id": str,
"sheets": {
"SheetName1": [ {col1: val1, col2: val2, ...}, ... ],
...
},
"status": "Success" | "Error"
}
"""
url = f"{DEFAULT_API_URL}/files/{task_id}"
try:
response = requests.get(url, timeout=10)
if response.status_code != 200:
return {"task_id": task_id, "sheets": {}, "status": f"{response.status_code} - Failed"}
xls = pd.ExcelFile(BytesIO(response.content))
sheets_json = {}
for sheet in xls.sheet_names:
df = xls.parse(sheet)
df = df.dropna(how="all") # Remove completely empty rows
rows = df.head(20).to_dict(orient="records") # limit to first 20 rows
sheets_json[sheet] = rows
return {
"task_id": task_id,
"sheets": sheets_json,
"status": "Success"
}
except Exception as e:
return {
"task_id": task_id,
"sheets": {},
"status": f"Error: {str(e)}"
}agentsTools/toolVisitWebpage.pyDeleted-44
agentsTools/tool_fetch_task_file.pyDeleted-42
agentsTools/tool_read_excel_as_json.pyDeleted-54