Spaces:
Sleeping
Sleeping
Update tools.py
Browse files5 unmodified lines678910114 unmodified lines161718192021222324252627282930313233345 unmodified lines4041424344454647484950515253545556575859605 unmodified linesTool = objectDEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# Tool to download and read file attachments (text) from the scoring API# Plain class (no Tool inheritance) β called directly, not via CodeAgent4 unmodified linesself.api_url = api_urlprint("FileReaderTool initialized.")def __call__(self, task_id: str) -> str:file_url = f"{self.api_url}/files/{task_id}"print(f"FileReaderTool downloading from: {file_url}")try:resp = requests.get(file_url, timeout=30)resp.raise_for_status()content = resp.textprint(f"FileReaderTool downloaded {len(content)} chars.")return contentexcept requests.exceptions.RequestException as e:error_msg = f"Failed to download file for task {task_id}: {e}"print(error_msg)return error_msg# Tool to download image attachments and return them as base645 unmodified linesself.api_url = api_urlprint("ImageReaderTool initialized.")def __call__(self, task_id: str) -> str:file_url = f"{self.api_url}/files/{task_id}"print(f"ImageReaderTool downloading from: {file_url}")try:resp = requests.get(file_url, timeout=30)resp.raise_for_status()# Detect content type for the base64 data URIcontent_type = resp.headers.get("Content-Type", "image/png")image_b64 = base64.b64encode(resp.content).decode("utf-8")print(f"ImageReaderTool downloaded image ({len(resp.content)} bytes, {content_type}).")return f"data:{content_type};base64,{image_b64}"except requests.exceptions.RequestException as e:error_msg = f"Failed to download image for task {task_id}: {e}"print(error_msg)return error_msg#search web tool β uses ddgs directly to avoid smolagents DuckDuckGoSearchTool package check5 unmodified lines67891011124 unmodified lines1718192021222324252627282930313233343536373839405 unmodified lines464748495051525354555657585960616263646566676869705 unmodified linesTool = objectDEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"HF_DATASET_BASE = "https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/2023/test"# Tool to download and read file attachments (text) from the scoring API# Plain class (no Tool inheritance) β called directly, not via CodeAgent4 unmodified linesself.api_url = api_urlprint("FileReaderTool initialized.")def __call__(self, task_id: str, file_name: str = "") -> str:# Try HF dataset URL first (actual file location), then scoring API fallbackurls_to_try = []if file_name:urls_to_try.append(f"{HF_DATASET_BASE}/{file_name}")urls_to_try.append(f"{self.api_url}/files/{task_id}")for url in urls_to_try:print(f"FileReaderTool trying: {url}")try:resp = requests.get(url, timeout=30)if resp.status_code == 200:content = resp.textprint(f"FileReaderTool downloaded {len(content)} chars.")return contentexcept requests.exceptions.RequestException:continuereturn f"Failed to download file for task {task_id}"# Tool to download image attachments and return them as base645 unmodified linesself.api_url = api_urlprint("ImageReaderTool initialized.")def __call__(self, task_id: str, file_name: str = "") -> str:# Try HF dataset URL first (actual file location), then scoring API fallbackurls_to_try = []if file_name:urls_to_try.append(f"{HF_DATASET_BASE}/{file_name}")urls_to_try.append(f"{self.api_url}/files/{task_id}")for url in urls_to_try:print(f"ImageReaderTool trying: {url}")try:resp = requests.get(url, timeout=30)if resp.status_code == 200:content_type = resp.headers.get("Content-Type", "image/png")image_b64 = base64.b64encode(resp.content).decode("utf-8")print(f"ImageReaderTool downloaded image ({len(resp.content)} bytes, {content_type}).")return f"data:{content_type};base64,{image_b64}"except requests.exceptions.RequestException:continuereturn f"Failed to download image for task {task_id}"#search web tool β uses ddgs directly to avoid smolagents DuckDuckGoSearchTool package check
tools.py+38-28