Spaces:
Runtime error
Runtime error
Joachim M. Vis commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,88 +6,79 @@ import yaml
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
-
web_search = DuckDuckGoSearchTool()
|
| 10 |
-
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
"""A tool
|
| 14 |
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
'timezone': timezone
|
| 52 |
-
}
|
| 53 |
except Exception as e:
|
| 54 |
-
|
| 55 |
-
return None
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
🛫 Departure: {origin_info['name']} ({origin_info['code']})
|
| 69 |
-
Local Time: {origin_time}
|
| 70 |
-
Timezone: {origin_info['timezone']}
|
| 71 |
-
|
| 72 |
-
🛬 Arrival: {dest_info['name']} ({dest_info['code']})
|
| 73 |
-
Local Time: {dest_time}
|
| 74 |
-
Timezone: {dest_info['timezone']}
|
| 75 |
-
|
| 76 |
-
ℹ️ Airport information was dynamically retrieved from the web.
|
| 77 |
"""
|
| 78 |
-
return info
|
| 79 |
-
except Exception as e:
|
| 80 |
-
return f"Error processing timezone information: {str(e)}"
|
| 81 |
|
| 82 |
@tool
|
| 83 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 84 |
"""A tool that fetches the current local time in a specified timezone.
|
| 85 |
-
|
| 86 |
Args:
|
| 87 |
-
timezone
|
| 88 |
-
|
| 89 |
-
Returns:
|
| 90 |
-
str: A formatted string containing the current time in the specified timezone.
|
| 91 |
"""
|
| 92 |
try:
|
| 93 |
tz = pytz.timezone(timezone)
|
|
@@ -96,39 +87,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 98 |
|
| 99 |
-
# Initialize tools and model
|
| 100 |
final_answer = FinalAnswerTool()
|
| 101 |
|
| 102 |
model = HfApiModel(
|
| 103 |
max_tokens=2096,
|
| 104 |
-
temperature=0.5,
|
| 105 |
-
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 106 |
-
custom_role_conversions=None,
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
# Load prompt templates
|
| 110 |
-
with open("prompts.yaml", 'r') as stream:
|
| 111 |
-
prompt_templates = yaml.safe_load(stream)
|
| 112 |
-
|
| 113 |
-
# Initialize agent with tools
|
| 114 |
-
agent = CodeAgent(
|
| 115 |
-
model=model,
|
| 116 |
-
tools=[
|
| 117 |
-
final_answer,
|
| 118 |
-
web_search,
|
| 119 |
-
get_current_time_in_timezone,
|
| 120 |
-
travel_info
|
| 121 |
-
],
|
| 122 |
-
max_steps=6,
|
| 123 |
-
verbosity_level=2, # Increased for better debugging
|
| 124 |
-
grammar=None,
|
| 125 |
-
planning_interval=None,
|
| 126 |
-
name=None,
|
| 127 |
-
description=None,
|
| 128 |
-
prompt_templates=prompt_templates
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
# Launch Gradio interface
|
| 132 |
-
if __name__ == "__main__":
|
| 133 |
-
interface = GradioUI(agent)
|
| 134 |
interface.launch(share=False)
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
|
|
|
| 9 |
@tool
|
| 10 |
+
def travel_search(city: str) -> str:
|
| 11 |
+
"""A tool that searches for travel information about a city.
|
| 12 |
|
| 13 |
Args:
|
| 14 |
+
city: Name of the city to search for
|
| 15 |
+
"""
|
| 16 |
+
web_search = DuckDuckGoSearchTool()
|
| 17 |
+
try:
|
| 18 |
+
city_info = web_search(f"{city} tourism highlights attractions")
|
| 19 |
+
return f"Travel information for {city}:\n{city_info}"
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error searching for {city}: {str(e)}"
|
| 22 |
+
|
| 23 |
+
@tool
|
| 24 |
+
def airport_time_diff(airport1: str, airport2: str) -> str:
|
| 25 |
+
"""Calculate time difference between two airports.
|
| 26 |
|
| 27 |
+
Args:
|
| 28 |
+
airport1: First airport (city or IATA code)
|
| 29 |
+
airport2: Second airport (city or IATA code)
|
| 30 |
"""
|
| 31 |
+
web_search = DuckDuckGoSearchTool()
|
| 32 |
+
|
| 33 |
+
def get_airport_timezone(airport):
|
| 34 |
try:
|
| 35 |
+
# Search for airport timezone
|
| 36 |
+
search_result = web_search(f"{airport} airport timezone")
|
| 37 |
+
|
| 38 |
+
# Common timezone keywords
|
| 39 |
+
timezone_map = {
|
| 40 |
+
'EST': 'America/New_York',
|
| 41 |
+
'EDT': 'America/New_York',
|
| 42 |
+
'CST': 'America/Chicago',
|
| 43 |
+
'CDT': 'America/Chicago',
|
| 44 |
+
'PST': 'America/Los_Angeles',
|
| 45 |
+
'PDT': 'America/Los_Angeles',
|
| 46 |
+
'CET': 'Europe/Berlin',
|
| 47 |
+
'CEST': 'Europe/Berlin',
|
| 48 |
+
'GMT': 'Europe/London',
|
| 49 |
+
'BST': 'Europe/London',
|
| 50 |
+
'JST': 'Asia/Tokyo'
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Check for timezone in search result
|
| 54 |
+
for tz_code, tz_name in timezone_map.items():
|
| 55 |
+
if tz_code in search_result.upper():
|
| 56 |
+
return tz_name
|
| 57 |
+
|
| 58 |
+
return 'UTC' # Default to UTC if no timezone found
|
| 59 |
+
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
+
return 'UTC'
|
|
|
|
| 62 |
|
| 63 |
+
# Get timezones for both airports
|
| 64 |
+
tz1 = get_airport_timezone(airport1)
|
| 65 |
+
tz2 = get_airport_timezone(airport2)
|
| 66 |
|
| 67 |
+
# Get current times
|
| 68 |
+
time1 = datetime.datetime.now(pytz.timezone(tz1))
|
| 69 |
+
time2 = datetime.datetime.now(pytz.timezone(tz2))
|
| 70 |
+
|
| 71 |
+
return f"""
|
| 72 |
+
Time Comparison:
|
| 73 |
+
🛫 {airport1}: {time1.strftime('%H:%M')} ({tz1})
|
| 74 |
+
🛬 {airport2}: {time2.strftime('%H:%M')} ({tz2})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
"""
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
@tool
|
| 78 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 79 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 80 |
Args:
|
| 81 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
|
|
|
|
|
|
|
|
|
| 82 |
"""
|
| 83 |
try:
|
| 84 |
tz = pytz.timezone(timezone)
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 89 |
|
|
|
|
| 90 |
final_answer = FinalAnswerTool()
|
| 91 |
|
| 92 |
model = HfApiModel(
|
| 93 |
max_tokens=2096,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
interface.launch(share=False)
|