Joachim M. Vis commited on
Commit
b0fbf41
·
verified ·
1 Parent(s): 136e469

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -98
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 travel_info(origin: str, destination: str) -> str:
13
- """A tool for retrieving travel information between two airports or cities.
14
 
15
  Args:
16
- origin (str): The departure location. Can be a city name (e.g., 'Berlin') or IATA code (e.g., 'BER').
17
- destination (str): The arrival location. Can be a city name (e.g., 'London') or IATA code (e.g., 'LHR').
 
 
 
 
 
 
 
 
 
 
18
 
19
- Returns:
20
- str: A formatted string containing travel information including airport details, local times, and timezone information.
 
21
  """
22
- def search_airport_info(location):
23
- search_query = f"{location} airport IATA code timezone"
24
- search_results = web_search(search_query)
25
  try:
26
- import re
27
- iata_pattern = r'\(([A-Z]{3})\)'
28
- iata_matches = re.findall(iata_pattern, search_results)
29
- if iata_matches:
30
- iata_code = iata_matches[0]
31
- timezone_search = web_search(f"{location} airport timezone")
32
- common_timezones = {
33
- 'UTC': 'UTC',
34
- 'EST': 'America/New_York',
35
- 'CST': 'America/Chicago',
36
- 'PST': 'America/Los_Angeles',
37
- 'CET': 'Europe/Berlin',
38
- 'GMT': 'Europe/London',
39
- 'JST': 'Asia/Tokyo'
40
- }
41
- timezone = None
42
- for tz_key in common_timezones:
43
- if tz_key in timezone_search:
44
- timezone = common_timezones[tz_key]
45
- break
46
- if not timezone:
47
- timezone = 'UTC'
48
- return {
49
- 'code': iata_code,
50
- 'name': f"{location} Airport",
51
- 'timezone': timezone
52
- }
53
  except Exception as e:
54
- print(f"Error searching for {location}: {str(e)}")
55
- return None
56
 
57
- origin_info = search_airport_info(origin)
58
- dest_info = search_airport_info(destination)
 
59
 
60
- if not origin_info or not dest_info:
61
- return "Sorry, I couldn't find complete information for one or both airports."
62
-
63
- try:
64
- origin_time = datetime.datetime.now(pytz.timezone(origin_info['timezone'])).strftime("%H:%M")
65
- dest_time = datetime.datetime.now(pytz.timezone(dest_info['timezone'])).strftime("%H:%M")
66
- info = f"""
67
- Travel Information:
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 (str): A string representing a valid timezone (e.g., 'America/New_York', 'Europe/London').
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)