Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- sidekick_tools.py +18 -8
sidekick_tools.py
CHANGED
|
@@ -227,15 +227,20 @@ pushover_user = os.getenv("PUSHOVER_USER")
|
|
| 227 |
pushover_url = "https://api.pushover.net/1/messages.json"
|
| 228 |
serper = GoogleSerperAPIWrapper()
|
| 229 |
|
| 230 |
-
#
|
| 231 |
-
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/
|
| 232 |
|
| 233 |
async def playwright_tools():
|
| 234 |
"""Setup browser tools with Stealth mode for Hugging Face"""
|
| 235 |
playwright = await async_playwright().start()
|
| 236 |
browser = await playwright.chromium.launch(
|
| 237 |
headless=True,
|
| 238 |
-
args=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
)
|
| 240 |
# Create context with a real-world User-Agent
|
| 241 |
context = await browser.new_context(user_agent=USER_AGENT)
|
|
@@ -263,14 +268,19 @@ def take_screenshot(url: str):
|
|
| 263 |
with sync_playwright() as p:
|
| 264 |
browser = p.chromium.launch(
|
| 265 |
headless=True,
|
| 266 |
-
args=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
)
|
| 268 |
# Use Stealth Context
|
| 269 |
context = browser.new_context(user_agent=USER_AGENT)
|
| 270 |
page = context.new_page()
|
| 271 |
page.set_viewport_size({"width": 1280, "height": 720})
|
| 272 |
|
| 273 |
-
# Use 'domcontentloaded'
|
| 274 |
page.goto(url, timeout=60000, wait_until="domcontentloaded")
|
| 275 |
|
| 276 |
filename = f"screenshot_{datetime.now().strftime('%H%M%S')}.png"
|
|
@@ -327,11 +337,11 @@ async def other_tools():
|
|
| 327 |
|
| 328 |
sentiment_tool = Tool(
|
| 329 |
name="community_sentiment",
|
| 330 |
-
func=community_sentiment,
|
| 331 |
description="Search Reddit to find what real people think about a topic."
|
| 332 |
)
|
| 333 |
|
| 334 |
-
# Wikipedia Safety Wrapper (Prevents JSON Crashes)
|
| 335 |
api_wrapper = WikipediaAPIWrapper()
|
| 336 |
wiki_engine = WikipediaQueryRun(api_wrapper=api_wrapper)
|
| 337 |
|
|
@@ -339,7 +349,7 @@ async def other_tools():
|
|
| 339 |
try:
|
| 340 |
return wiki_engine.run(query)
|
| 341 |
except Exception:
|
| 342 |
-
return "Wikipedia is currently unreachable
|
| 343 |
|
| 344 |
wiki_tool = Tool(
|
| 345 |
name="wikipedia",
|
|
|
|
| 227 |
pushover_url = "https://api.pushover.net/1/messages.json"
|
| 228 |
serper = GoogleSerperAPIWrapper()
|
| 229 |
|
| 230 |
+
# Stealth configuration to bypass 403 Forbidden errors
|
| 231 |
+
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
|
| 232 |
|
| 233 |
async def playwright_tools():
|
| 234 |
"""Setup browser tools with Stealth mode for Hugging Face"""
|
| 235 |
playwright = await async_playwright().start()
|
| 236 |
browser = await playwright.chromium.launch(
|
| 237 |
headless=True,
|
| 238 |
+
args=[
|
| 239 |
+
"--no-sandbox",
|
| 240 |
+
"--disable-setuid-sandbox",
|
| 241 |
+
"--disable-dev-shm-usage",
|
| 242 |
+
"--disable-blink-features=AutomationControlled" # Hides the bot signature
|
| 243 |
+
]
|
| 244 |
)
|
| 245 |
# Create context with a real-world User-Agent
|
| 246 |
context = await browser.new_context(user_agent=USER_AGENT)
|
|
|
|
| 268 |
with sync_playwright() as p:
|
| 269 |
browser = p.chromium.launch(
|
| 270 |
headless=True,
|
| 271 |
+
args=[
|
| 272 |
+
"--no-sandbox",
|
| 273 |
+
"--disable-setuid-sandbox",
|
| 274 |
+
"--disable-dev-shm-usage",
|
| 275 |
+
"--disable-blink-features=AutomationControlled"
|
| 276 |
+
]
|
| 277 |
)
|
| 278 |
# Use Stealth Context
|
| 279 |
context = browser.new_context(user_agent=USER_AGENT)
|
| 280 |
page = context.new_page()
|
| 281 |
page.set_viewport_size({"width": 1280, "height": 720})
|
| 282 |
|
| 283 |
+
# Use 'domcontentloaded' to capture page before bot-checkers fully trigger
|
| 284 |
page.goto(url, timeout=60000, wait_until="domcontentloaded")
|
| 285 |
|
| 286 |
filename = f"screenshot_{datetime.now().strftime('%H%M%S')}.png"
|
|
|
|
| 337 |
|
| 338 |
sentiment_tool = Tool(
|
| 339 |
name="community_sentiment",
|
| 340 |
+
func=community_sentiment, # Fixed: Now correctly points to the function
|
| 341 |
description="Search Reddit to find what real people think about a topic."
|
| 342 |
)
|
| 343 |
|
| 344 |
+
# Wikipedia Safety Wrapper (Prevents JSON Crashes on Hugging Face)
|
| 345 |
api_wrapper = WikipediaAPIWrapper()
|
| 346 |
wiki_engine = WikipediaQueryRun(api_wrapper=api_wrapper)
|
| 347 |
|
|
|
|
| 349 |
try:
|
| 350 |
return wiki_engine.run(query)
|
| 351 |
except Exception:
|
| 352 |
+
return "Wikipedia is currently unreachable. PLEASE USE THE 'SEARCH' TOOL INSTEAD."
|
| 353 |
|
| 354 |
wiki_tool = Tool(
|
| 355 |
name="wikipedia",
|