Spaces:
Sleeping
Sleeping
File size: 4,066 Bytes
34f3c91 b5a6c20 b0fbf41 136e469 b0fbf41 22d7d71 136e469 b0fbf41 136e469 b0fbf41 b5a6c20 b0fbf41 a71e58b b0fbf41 b5a6c20 b0fbf41 34f3c91 22d7d71 b0fbf41 22d7d71 b0fbf41 22d7d71 34f3c91 9b5b26a 136e469 b0fbf41 136e469 9b5b26a 8c01ffb 6aae614 b5a6c20 e121372 34f3c91 a71e58b b059cbd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
@tool
def travel_search(city: str) -> str:
"""A tool that searches for travel information about a city.
Args:
city: Name of the city to search for
"""
web_search = DuckDuckGoSearchTool()
try:
city_info = web_search(f"{city} tourism highlights attractions")
return f"Travel information for {city}:\n{city_info}"
except Exception as e:
return f"Error searching for {city}: {str(e)}"
@tool
def airport_time_diff(airport1: str, airport2: str) -> str:
"""Calculate time difference between two airports and show attractions at destination.
Args:
airport1: First airport (city or IATA code)
airport2: Second airport (city or IATA code)
"""
web_search = DuckDuckGoSearchTool()
def get_airport_timezone(airport):
try:
search_result = web_search(f"{airport} airport timezone")
timezone_map = {
'EST': 'America/New_York',
'EDT': 'America/New_York',
'CST': 'America/Chicago',
'CDT': 'America/Chicago',
'PST': 'America/Los_Angeles',
'PDT': 'America/Los_Angeles',
'CET': 'Europe/Berlin',
'CEST': 'Europe/Berlin',
'GMT': 'Europe/London',
'BST': 'Europe/London',
'JST': 'Asia/Tokyo'
}
for tz_code, tz_name in timezone_map.items():
if tz_code in search_result.upper():
return tz_name
return 'UTC'
except Exception as e:
return 'UTC'
# Get timezones and current times
tz1 = get_airport_timezone(airport1)
tz2 = get_airport_timezone(airport2)
time1 = datetime.datetime.now(pytz.timezone(tz1))
time2 = datetime.datetime.now(pytz.timezone(tz2))
# Get destination city attractions
try:
# Extract city name from airport2 (remove 'airport' if present)
dest_city = airport2.lower().replace('airport', '').strip()
attractions = web_search(f"{dest_city} top tourist attractions highlights")
except Exception as e:
attractions = "Could not fetch attractions information."
return f"""
Time Comparison:
🛫 {airport1}: {time1.strftime('%H:%M')} ({tz1})
🛬 {airport2}: {time2.strftime('%H:%M')} ({tz2})
Popular Attractions at {airport2}:
{attractions}
"""
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None
)
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[
final_answer,
DuckDuckGoSearchTool(),
get_current_time_in_timezone,
travel_search,
airport_time_diff,
image_generation_tool
],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
if __name__ == "__main__":
interface = GradioUI(agent)
interface.launch() |