Spaces:
Sleeping
Sleeping
removing chrome web driver
Browse filessimplifying my function by removing the chrome web driver to eliminate the need for extra configuration of Dockerfile
app.py
CHANGED
@@ -1,60 +1,58 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
-
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
from selenium import webdriver
|
11 |
-
from selenium.webdriver.common.by import By
|
12 |
-
from selenium.webdriver.chrome.service import Service
|
13 |
from selenium.webdriver.chrome.options import Options
|
|
|
|
|
14 |
|
15 |
@tool
|
16 |
def get_zh_top_news() -> tuple[str, str]:
|
17 |
"""A tool that retrieves the current top news article's title and URL from www.zerohedge.com.
|
18 |
-
|
19 |
Returns:
|
20 |
tuple[str, str]: A tuple containing the article title (str) and its URL (str).
|
21 |
|
22 |
Raises:
|
23 |
Exception: If the page fails to load or the expected element is not found.
|
24 |
"""
|
25 |
-
# Set up Chrome options for headless browsing
|
26 |
chrome_options = Options()
|
27 |
chrome_options.add_argument("--headless") # Run without opening a browser window
|
28 |
-
chrome_options.add_argument("--disable-gpu") # Disable GPU
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
driver = webdriver.Chrome(options=chrome_options)
|
32 |
|
33 |
try:
|
34 |
# Navigate to ZeroHedge homepage
|
35 |
driver.get("https://www.zerohedge.com")
|
36 |
|
37 |
# Find the top article element using a CSS selector
|
38 |
-
# Based on ZeroHedge's structure, the top headline is typically the first prominent article
|
39 |
top_article = driver.find_element(By.CSS_SELECTOR, "article .ArticleTeaser_titleLink__mK4rX")
|
40 |
|
41 |
-
# Extract the title
|
42 |
article_title = top_article.text.strip()
|
43 |
-
|
44 |
-
# Extract the URL from the href attribute
|
45 |
article_link = top_article.get_attribute("href")
|
46 |
|
47 |
-
# Ensure the link is absolute
|
48 |
if not article_link.startswith("http"):
|
49 |
article_link = f"https://www.zerohedge.com{article_link}"
|
50 |
|
51 |
return article_title, article_link
|
52 |
|
53 |
except Exception as e:
|
|
|
54 |
return "Error: Headline not found", "https://www.zerohedge.com"
|
55 |
|
56 |
finally:
|
57 |
-
# Always close the browser session to free resources
|
58 |
driver.quit()
|
59 |
|
60 |
@tool
|
@@ -64,37 +62,29 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
64 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
65 |
"""
|
66 |
try:
|
67 |
-
# Create timezone object
|
68 |
tz = pytz.timezone(timezone)
|
69 |
-
# Get current time in that timezone
|
70 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
71 |
return f"The current local time in {timezone} is: {local_time}"
|
72 |
except Exception as e:
|
73 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
74 |
|
75 |
-
|
76 |
final_answer = FinalAnswerTool()
|
77 |
|
78 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
79 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
80 |
-
|
81 |
model = HfApiModel(
|
82 |
-
max_tokens=2096,
|
83 |
-
temperature=0.5,
|
84 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
85 |
-
custom_role_conversions=None,
|
86 |
)
|
87 |
|
88 |
-
|
89 |
-
# Import tool from Hub
|
90 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
91 |
|
92 |
with open("prompts.yaml", 'r') as stream:
|
93 |
prompt_templates = yaml.safe_load(stream)
|
94 |
-
|
95 |
agent = CodeAgent(
|
96 |
model=model,
|
97 |
-
tools=[get_zh_top_news,get_current_time_in_timezone,final_answer],
|
98 |
max_steps=6,
|
99 |
verbosity_level=1,
|
100 |
grammar=None,
|
@@ -104,5 +94,4 @@ agent = CodeAgent(
|
|
104 |
prompt_templates=prompt_templates
|
105 |
)
|
106 |
|
107 |
-
|
108 |
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
7 |
from Gradio_UI import GradioUI
|
8 |
|
9 |
from selenium import webdriver
|
|
|
|
|
10 |
from selenium.webdriver.chrome.options import Options
|
11 |
+
from selenium.webdriver.common.by import By
|
12 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
13 |
|
14 |
@tool
|
15 |
def get_zh_top_news() -> tuple[str, str]:
|
16 |
"""A tool that retrieves the current top news article's title and URL from www.zerohedge.com.
|
17 |
+
|
18 |
Returns:
|
19 |
tuple[str, str]: A tuple containing the article title (str) and its URL (str).
|
20 |
|
21 |
Raises:
|
22 |
Exception: If the page fails to load or the expected element is not found.
|
23 |
"""
|
24 |
+
# Set up Chrome options for headless browsing
|
25 |
chrome_options = Options()
|
26 |
chrome_options.add_argument("--headless") # Run without opening a browser window
|
27 |
+
chrome_options.add_argument("--disable-gpu") # Disable GPU for headless mode
|
28 |
+
chrome_options.add_argument("--no-sandbox") # Required for containerized environments
|
29 |
+
chrome_options.add_argument("--disable-dev-shm-usage") # Avoid memory issues in containers
|
30 |
|
31 |
+
# Use webdriver-manager to automatically manage ChromeDriver
|
32 |
+
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
|
33 |
|
34 |
try:
|
35 |
# Navigate to ZeroHedge homepage
|
36 |
driver.get("https://www.zerohedge.com")
|
37 |
|
38 |
# Find the top article element using a CSS selector
|
|
|
39 |
top_article = driver.find_element(By.CSS_SELECTOR, "article .ArticleTeaser_titleLink__mK4rX")
|
40 |
|
41 |
+
# Extract the title and URL
|
42 |
article_title = top_article.text.strip()
|
|
|
|
|
43 |
article_link = top_article.get_attribute("href")
|
44 |
|
45 |
+
# Ensure the link is absolute
|
46 |
if not article_link.startswith("http"):
|
47 |
article_link = f"https://www.zerohedge.com{article_link}"
|
48 |
|
49 |
return article_title, article_link
|
50 |
|
51 |
except Exception as e:
|
52 |
+
print(f"Error retrieving top headline: {e}")
|
53 |
return "Error: Headline not found", "https://www.zerohedge.com"
|
54 |
|
55 |
finally:
|
|
|
56 |
driver.quit()
|
57 |
|
58 |
@tool
|
|
|
62 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
63 |
"""
|
64 |
try:
|
|
|
65 |
tz = pytz.timezone(timezone)
|
|
|
66 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
67 |
return f"The current local time in {timezone} is: {local_time}"
|
68 |
except Exception as e:
|
69 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
70 |
|
|
|
71 |
final_answer = FinalAnswerTool()
|
72 |
|
|
|
|
|
|
|
73 |
model = HfApiModel(
|
74 |
+
max_tokens=2096,
|
75 |
+
temperature=0.5,
|
76 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
77 |
+
custom_role_conversions=None,
|
78 |
)
|
79 |
|
|
|
|
|
80 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
81 |
|
82 |
with open("prompts.yaml", 'r') as stream:
|
83 |
prompt_templates = yaml.safe_load(stream)
|
84 |
+
|
85 |
agent = CodeAgent(
|
86 |
model=model,
|
87 |
+
tools=[get_zh_top_news, get_current_time_in_timezone, final_answer],
|
88 |
max_steps=6,
|
89 |
verbosity_level=1,
|
90 |
grammar=None,
|
|
|
94 |
prompt_templates=prompt_templates
|
95 |
)
|
96 |
|
|
|
97 |
GradioUI(agent).launch()
|