Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,87 @@
|
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
|
17 |
-
arg2: the second argument
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
-
"""
|
24 |
Args:
|
25 |
-
timezone: A
|
26 |
"""
|
27 |
try:
|
28 |
-
# Create timezone object
|
29 |
tz = pytz.timezone(timezone)
|
30 |
-
# Get current time in that timezone
|
31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
32 |
return f"The current local time in {timezone} is: {local_time}"
|
33 |
except Exception as e:
|
34 |
-
return f"Error fetching time
|
35 |
-
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
# 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:
|
40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
41 |
-
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
# Import tool from Hub
|
51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
-
|
53 |
with open("prompts.yaml", 'r') as stream:
|
54 |
prompt_templates = yaml.safe_load(stream)
|
55 |
-
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +91,4 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
-
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 bs4 import BeautifulSoup # For web scraping
|
7 |
+
from PIL import Image # For image processing
|
8 |
+
from io import BytesIO
|
9 |
from tools.final_answer import FinalAnswerTool
|
|
|
10 |
from Gradio_UI import GradioUI
|
11 |
|
12 |
+
# Weather Tool (using wttr.in API, no API key needed)
|
13 |
+
@tool
|
14 |
+
def get_weather(city: str) -> str:
|
15 |
+
"""Fetches the current weather for a given city.
|
16 |
+
Args:
|
17 |
+
city: The name of the city (e.g., 'London').
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
url = f"https://wttr.in/{city}?format=%C+%t+%w"
|
21 |
+
response = requests.get(url)
|
22 |
+
return f"Weather in {city}: {response.text}"
|
23 |
+
except Exception as e:
|
24 |
+
return f"Error fetching weather: {str(e)}"
|
25 |
+
|
26 |
+
# Web Scraping Tool (Extracts headlines and articles from a given news website)
|
27 |
@tool
|
28 |
+
def scrape_news(url: str) -> str:
|
29 |
+
"""Scrapes headlines and article text from a news website.
|
|
|
30 |
Args:
|
31 |
+
url: The URL of the news site.
|
|
|
32 |
"""
|
33 |
+
try:
|
34 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
35 |
+
response = requests.get(url, headers=headers)
|
36 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
37 |
+
|
38 |
+
# Extracting headlines
|
39 |
+
headlines = [h.get_text() for h in soup.find_all('h1')][:5]
|
40 |
+
articles = [p.get_text() for p in soup.find_all('p')][:5]
|
41 |
+
|
42 |
+
return f"Headlines:\n" + "\n".join(headlines) + "\n\nArticles:\n" + "\n".join(articles)
|
43 |
+
except Exception as e:
|
44 |
+
return f"Error scraping news: {str(e)}"
|
45 |
|
46 |
+
# Image Generation Tool (uses existing text-to-image tool)
|
47 |
+
@tool
|
48 |
+
def generate_image(prompt: str) -> str:
|
49 |
+
"""Generates an image based on the given text prompt.
|
50 |
+
Args:
|
51 |
+
prompt: A description of the image to generate.
|
52 |
+
"""
|
53 |
+
return image_generation_tool.run(prompt)
|
54 |
+
|
55 |
+
# Timezone Tool (already in your code)
|
56 |
@tool
|
57 |
def get_current_time_in_timezone(timezone: str) -> str:
|
58 |
+
"""Fetches the current local time in a specified timezone.
|
59 |
Args:
|
60 |
+
timezone: A valid timezone (e.g., 'America/New_York').
|
61 |
"""
|
62 |
try:
|
|
|
63 |
tz = pytz.timezone(timezone)
|
|
|
64 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
65 |
return f"The current local time in {timezone} is: {local_time}"
|
66 |
except Exception as e:
|
67 |
+
return f"Error fetching time: {str(e)}"
|
|
|
68 |
|
69 |
final_answer = FinalAnswerTool()
|
70 |
|
|
|
|
|
|
|
71 |
model = HfApiModel(
|
72 |
+
max_tokens=2096,
|
73 |
+
temperature=0.5,
|
74 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
75 |
+
custom_role_conversions=None,
|
76 |
)
|
77 |
|
78 |
+
# Load prompt templates
|
|
|
|
|
|
|
79 |
with open("prompts.yaml", 'r') as stream:
|
80 |
prompt_templates = yaml.safe_load(stream)
|
81 |
+
|
82 |
agent = CodeAgent(
|
83 |
model=model,
|
84 |
+
tools=[final_answer, get_weather, scrape_news, generate_image],
|
85 |
max_steps=6,
|
86 |
verbosity_level=1,
|
87 |
grammar=None,
|
|
|
91 |
prompt_templates=prompt_templates
|
92 |
)
|
93 |
|
94 |
+
GradioUI(agent).launch()
|
|