Spaces:
Sleeping
Sleeping
from typing import Any, Optional | |
from smolagents.tools import Tool | |
import requests | |
import markdownify | |
import smolagents | |
import playwright.sync_api as playwright | |
import re | |
import yaml | |
from smolagents import CodeAgent, HfApiModel | |
from tools.final_answer import FinalAnswerTool | |
import json | |
class getDeviceInfo(Tool): | |
name = "visit_webpage" | |
description = "Visits a webpage at the given URL, reads its content as a markdown string, and extracts device information." | |
inputs = {'url': {'type': 'string', 'description': 'The URL of the webpage to visit.'}} | |
output_type = "string" # ✅ Fix: Use "string" instead of "json" | |
def forward(self, url: str) -> str: # ✅ Return a JSON string | |
try: | |
from smolagents.utils import truncate_content | |
except ImportError as e: | |
raise ImportError( | |
"You must install the `smolagents` package to run this tool." | |
) from e | |
try: | |
# Fetch the webpage | |
response = requests.get(url, timeout=20) | |
response.raise_for_status() | |
markdown_content = markdownify.markdownify(response.text).strip() | |
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content) | |
webpage_content = truncate_content(markdown_content, 10000) | |
except requests.exceptions.Timeout: | |
webpage_content = "The request timed out." | |
except requests.exceptions.RequestException as e: | |
webpage_content = f"Error fetching the webpage: {str(e)}" | |
except Exception as e: | |
webpage_content = f"Unexpected error: {str(e)}" | |
# Extract device information using Playwright | |
with playwright.sync_api.sync_playwright() as p: | |
browser = p.chromium.launch(headless=True) | |
page = browser.new_page() | |
script = """() => ({ | |
userAgent: navigator.userAgent, | |
platform: navigator.platform, | |
language: navigator.language, | |
screenWidth: window.screen.width, | |
screenHeight: window.screen.height | |
})""" | |
page.goto("about:blank") | |
device_info = page.evaluate(script) | |
browser.close() | |
# ✅ Convert output to JSON string for compatibility | |
result = { | |
"webpage_content": webpage_content, | |
"device_info": device_info | |
} | |
return json.dumps(result) # ✅ Ensure return type matches "string" |