Spaces:
Sleeping
Sleeping
meirk-brd
commited on
Commit
·
59e2cd4
1
Parent(s):
810f177
fix coerce url
Browse files
tool.py
CHANGED
|
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
from typing import Optional
|
| 6 |
|
| 7 |
import requests
|
| 8 |
from smolagents.tools import Tool
|
|
@@ -27,7 +27,7 @@ class BrightDataScraperTool(Tool):
|
|
| 27 |
super().__init__()
|
| 28 |
|
| 29 |
def forward(self, url) -> str:
|
| 30 |
-
url_str
|
| 31 |
|
| 32 |
if not url_str:
|
| 33 |
return json.dumps({"error": "No valid URL provided"})
|
|
@@ -59,20 +59,22 @@ class BrightDataScraperTool(Tool):
|
|
| 59 |
details = exc.response.text if getattr(exc, "response", None) is not None else ""
|
| 60 |
return json.dumps({"error": str(exc), "details": details})
|
| 61 |
|
| 62 |
-
def _coerce_url_input(self, raw) ->
|
| 63 |
if isinstance(raw, str):
|
| 64 |
-
return raw
|
| 65 |
|
| 66 |
if isinstance(raw, dict):
|
| 67 |
orig_name = raw.get("orig_name")
|
| 68 |
if isinstance(orig_name, str) and orig_name:
|
| 69 |
-
return self._ensure_scheme(orig_name)
|
| 70 |
|
| 71 |
url_value = raw.get("url")
|
| 72 |
if isinstance(url_value, str):
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
return None
|
| 76 |
|
| 77 |
def _parse_file_dict_string(self, value: str) -> Optional[dict]:
|
| 78 |
import ast
|
|
|
|
| 2 |
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
from typing import Optional
|
| 6 |
|
| 7 |
import requests
|
| 8 |
from smolagents.tools import Tool
|
|
|
|
| 27 |
super().__init__()
|
| 28 |
|
| 29 |
def forward(self, url) -> str:
|
| 30 |
+
url_str = self._coerce_url_input(url)
|
| 31 |
|
| 32 |
if not url_str:
|
| 33 |
return json.dumps({"error": "No valid URL provided"})
|
|
|
|
| 59 |
details = exc.response.text if getattr(exc, "response", None) is not None else ""
|
| 60 |
return json.dumps({"error": str(exc), "details": details})
|
| 61 |
|
| 62 |
+
def _coerce_url_input(self, raw) -> Optional[str]:
|
| 63 |
if isinstance(raw, str):
|
| 64 |
+
return self._ensure_scheme(raw)
|
| 65 |
|
| 66 |
if isinstance(raw, dict):
|
| 67 |
orig_name = raw.get("orig_name")
|
| 68 |
if isinstance(orig_name, str) and orig_name:
|
| 69 |
+
return self._ensure_scheme(orig_name)
|
| 70 |
|
| 71 |
url_value = raw.get("url")
|
| 72 |
if isinstance(url_value, str):
|
| 73 |
+
if url_value.startswith(("http://", "https://")):
|
| 74 |
+
return url_value
|
| 75 |
+
return None
|
| 76 |
|
| 77 |
+
return None
|
| 78 |
|
| 79 |
def _parse_file_dict_string(self, value: str) -> Optional[dict]:
|
| 80 |
import ast
|