Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,28 @@
|
|
| 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 get_weather(city: str, country_code: str = "US") -> str:
|
| 13 |
"""Fetches current weather information for a given city and country.
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
city: The name of the city (e.g., 'New York').
|
| 17 |
country_code: The 2-letter country code (default is 'US').
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
response = requests.get(url)
|
| 24 |
data = response.json()
|
| 25 |
|
|
@@ -40,45 +43,49 @@ def get_weather(city: str, country_code: str = "US") -> str:
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"An error occurred: {str(e)}"
|
| 42 |
|
|
|
|
| 43 |
@tool
|
| 44 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 45 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 46 |
Args:
|
| 47 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
-
# Create timezone object
|
| 51 |
tz = pytz.timezone(timezone)
|
| 52 |
-
# Get current time in that timezone
|
| 53 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 54 |
-
return f"The current local time in {timezone} is:
|
| 55 |
-
{local_time}"
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 58 |
|
| 59 |
-
|
| 60 |
final_answer = FinalAnswerTool()
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 64 |
-
|
| 65 |
model = HfApiModel(
|
| 66 |
-
max_tokens=2096,
|
| 67 |
-
temperature=0.5,
|
| 68 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 69 |
-
custom_role_conversions=None,
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
# Import tool from Hub
|
| 74 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 75 |
|
|
|
|
| 76 |
with open("prompts.yaml", 'r') as stream:
|
| 77 |
prompt_templates = yaml.safe_load(stream)
|
| 78 |
-
|
|
|
|
| 79 |
agent = CodeAgent(
|
| 80 |
model=model,
|
| 81 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
max_steps=6,
|
| 83 |
verbosity_level=1,
|
| 84 |
grammar=None,
|
|
@@ -88,5 +95,5 @@ agent = CodeAgent(
|
|
| 88 |
prompt_templates=prompt_templates
|
| 89 |
)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
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 |
+
# 🔑 Define your API key here
|
| 10 |
+
OPENWEATHER_API_KEY = Weatherapi # Replace with your real OpenWeatherMap API key
|
| 11 |
+
|
| 12 |
+
# 🌤️ Weather Tool
|
| 13 |
@tool
|
| 14 |
def get_weather(city: str, country_code: str = "US") -> str:
|
| 15 |
"""Fetches current weather information for a given city and country.
|
| 16 |
+
|
| 17 |
Args:
|
| 18 |
city: The name of the city (e.g., 'New York').
|
| 19 |
country_code: The 2-letter country code (default is 'US').
|
| 20 |
"""
|
| 21 |
try:
|
| 22 |
+
url = (
|
| 23 |
+
f"http://api.openweathermap.org/data/2.5/weather?"
|
| 24 |
+
f"q={city},{country_code}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 25 |
+
)
|
| 26 |
response = requests.get(url)
|
| 27 |
data = response.json()
|
| 28 |
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
return f"An error occurred: {str(e)}"
|
| 45 |
|
| 46 |
+
# 🕒 Timezone Tool
|
| 47 |
@tool
|
| 48 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 49 |
"""A tool that fetches the current local time in a specified timezone.
|
| 50 |
+
|
| 51 |
Args:
|
| 52 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 53 |
"""
|
| 54 |
try:
|
|
|
|
| 55 |
tz = pytz.timezone(timezone)
|
|
|
|
| 56 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 57 |
+
return f"The current local time in {timezone} is: {local_time}"
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 60 |
|
| 61 |
+
# 🧠 Final answer tool (required)
|
| 62 |
final_answer = FinalAnswerTool()
|
| 63 |
|
| 64 |
+
# 🤖 Hugging Face model
|
|
|
|
|
|
|
| 65 |
model = HfApiModel(
|
| 66 |
+
max_tokens=2096,
|
| 67 |
+
temperature=0.5,
|
| 68 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 69 |
+
custom_role_conversions=None,
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# 🎨 Image generation tool
|
|
|
|
| 73 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 74 |
|
| 75 |
+
# 📑 Load prompt templates
|
| 76 |
with open("prompts.yaml", 'r') as stream:
|
| 77 |
prompt_templates = yaml.safe_load(stream)
|
| 78 |
+
|
| 79 |
+
# 🧩 Build agent with all tools
|
| 80 |
agent = CodeAgent(
|
| 81 |
model=model,
|
| 82 |
+
tools=[
|
| 83 |
+
final_answer,
|
| 84 |
+
get_weather,
|
| 85 |
+
get_current_time_in_timezone,
|
| 86 |
+
image_generation_tool,
|
| 87 |
+
DuckDuckGoSearchTool(), # <== Included as you requested
|
| 88 |
+
],
|
| 89 |
max_steps=6,
|
| 90 |
verbosity_level=1,
|
| 91 |
grammar=None,
|
|
|
|
| 95 |
prompt_templates=prompt_templates
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# 🖼️ Launch Gradio UI
|
| 99 |
+
GradioUI(agent).launch()
|