SilviuMatei commited on
Commit
c00f902
·
verified ·
1 Parent(s): acb3f50

Update tools/getDeviceInfo.py

Browse files
Files changed (1) hide show
  1. tools/getDeviceInfo.py +13 -26
tools/getDeviceInfo.py CHANGED
@@ -8,68 +8,55 @@ import re
8
  import yaml
9
  from smolagents import CodeAgent, HfApiModel
10
  from tools.final_answer import FinalAnswerTool
 
11
 
12
  class getDeviceInfo(Tool):
13
  name = "visit_webpage"
14
  description = "Visits a webpage at the given URL, reads its content as a markdown string, and extracts device information."
15
  inputs = {'url': {'type': 'string', 'description': 'The URL of the webpage to visit.'}}
16
- output_type = "json" # ✅ Fix: Use "json" instead of "dict"
17
 
18
  def forward(self, url: str) -> str: # ✅ Return a JSON string
19
- import json
20
-
21
  try:
22
- import requests
23
- from markdownify import markdownify
24
- from requests.exceptions import RequestException
25
  from smolagents.utils import truncate_content
26
  except ImportError as e:
27
  raise ImportError(
28
- "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
29
  ) from e
30
 
31
  try:
32
- # Send a GET request to the URL with a 20-second timeout
33
  response = requests.get(url, timeout=20)
34
- response.raise_for_status() # Raise an exception for bad status codes
35
-
36
- # Convert the HTML content to Markdown
37
- markdown_content = markdownify(response.text).strip()
38
-
39
- # Remove multiple line breaks
40
  markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
41
-
42
  webpage_content = truncate_content(markdown_content, 10000)
43
 
44
  except requests.exceptions.Timeout:
45
- webpage_content = "The request timed out. Please try again later or check the URL."
46
- except RequestException as e:
47
  webpage_content = f"Error fetching the webpage: {str(e)}"
48
  except Exception as e:
49
- webpage_content = f"An unexpected error occurred: {str(e)}"
50
 
51
  # Extract device information using Playwright
52
  with playwright.sync_api.sync_playwright() as p:
53
  browser = p.chromium.launch(headless=True)
54
  page = browser.new_page()
55
-
56
- script = """
57
- () => ({
58
  userAgent: navigator.userAgent,
59
  platform: navigator.platform,
60
  language: navigator.language,
61
  screenWidth: window.screen.width,
62
  screenHeight: window.screen.height
63
- })
64
- """
65
-
66
  page.goto("about:blank")
67
  device_info = page.evaluate(script)
68
  browser.close()
69
 
70
- # ✅ Convert output to JSON string
71
  result = {
72
  "webpage_content": webpage_content,
73
  "device_info": device_info
74
  }
75
- return json.dumps(result) # ✅ Ensure return type matches "json"
 
8
  import yaml
9
  from smolagents import CodeAgent, HfApiModel
10
  from tools.final_answer import FinalAnswerTool
11
+ import json
12
 
13
  class getDeviceInfo(Tool):
14
  name = "visit_webpage"
15
  description = "Visits a webpage at the given URL, reads its content as a markdown string, and extracts device information."
16
  inputs = {'url': {'type': 'string', 'description': 'The URL of the webpage to visit.'}}
17
+ output_type = "string" # ✅ Fix: Use "string" instead of "json"
18
 
19
  def forward(self, url: str) -> str: # ✅ Return a JSON string
 
 
20
  try:
 
 
 
21
  from smolagents.utils import truncate_content
22
  except ImportError as e:
23
  raise ImportError(
24
+ "You must install the `smolagents` package to run this tool."
25
  ) from e
26
 
27
  try:
28
+ # Fetch the webpage
29
  response = requests.get(url, timeout=20)
30
+ response.raise_for_status()
31
+ markdown_content = markdownify.markdownify(response.text).strip()
 
 
 
 
32
  markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
 
33
  webpage_content = truncate_content(markdown_content, 10000)
34
 
35
  except requests.exceptions.Timeout:
36
+ webpage_content = "The request timed out."
37
+ except requests.exceptions.RequestException as e:
38
  webpage_content = f"Error fetching the webpage: {str(e)}"
39
  except Exception as e:
40
+ webpage_content = f"Unexpected error: {str(e)}"
41
 
42
  # Extract device information using Playwright
43
  with playwright.sync_api.sync_playwright() as p:
44
  browser = p.chromium.launch(headless=True)
45
  page = browser.new_page()
46
+ script = """() => ({
 
 
47
  userAgent: navigator.userAgent,
48
  platform: navigator.platform,
49
  language: navigator.language,
50
  screenWidth: window.screen.width,
51
  screenHeight: window.screen.height
52
+ })"""
 
 
53
  page.goto("about:blank")
54
  device_info = page.evaluate(script)
55
  browser.close()
56
 
57
+ # ✅ Convert output to JSON string for compatibility
58
  result = {
59
  "webpage_content": webpage_content,
60
  "device_info": device_info
61
  }
62
+ return json.dumps(result) # ✅ Ensure return type matches "string"