Spaces:
Sleeping
Sleeping
meirk-brd
commited on
Commit
·
f268ab5
1
Parent(s):
f860ade
add parse file
Browse files
tool.py
CHANGED
|
@@ -60,8 +60,16 @@ class BrightDataScraperTool(Tool):
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if isinstance(raw, dict):
|
| 67 |
orig_name = raw.get("orig_name")
|
|
@@ -80,3 +88,12 @@ class BrightDataScraperTool(Tool):
|
|
| 80 |
if url.startswith(("http://", "https://")):
|
| 81 |
return url
|
| 82 |
return f"https://{url}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return json.dumps({"error": str(exc), "details": details})
|
| 61 |
|
| 62 |
def _coerce_url_input(self, raw) -> Optional[str]:
|
| 63 |
+
# Gradio may pass a plain URL string, or a stringified dict representing an upload, or a dict itself.
|
| 64 |
if isinstance(raw, str):
|
| 65 |
+
if raw.strip().startswith("{") and "orig_name" in raw:
|
| 66 |
+
parsed = self._parse_file_dict_string(raw)
|
| 67 |
+
if parsed:
|
| 68 |
+
raw = parsed
|
| 69 |
+
else:
|
| 70 |
+
return self._ensure_scheme(raw)
|
| 71 |
+
else:
|
| 72 |
+
return self._ensure_scheme(raw)
|
| 73 |
|
| 74 |
if isinstance(raw, dict):
|
| 75 |
orig_name = raw.get("orig_name")
|
|
|
|
| 88 |
if url.startswith(("http://", "https://")):
|
| 89 |
return url
|
| 90 |
return f"https://{url}"
|
| 91 |
+
|
| 92 |
+
def _parse_file_dict_string(self, value: str) -> Optional[dict]:
|
| 93 |
+
import ast
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
parsed = ast.literal_eval(value)
|
| 97 |
+
return parsed if isinstance(parsed, dict) else None
|
| 98 |
+
except (ValueError, SyntaxError):
|
| 99 |
+
return None
|