ThiSecur commited on
Commit
50584ea
·
verified ·
1 Parent(s): bc26476

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -15
app.py CHANGED
@@ -8,17 +8,61 @@ from Gradio_UI import GradioUI
8
 
9
  # Custom Tools
10
  @tool
11
- def get_current_time_in_timezone(timezone: str) -> str:
12
- """A tool that fetches the current local time in a specified timezone.
 
 
 
 
 
 
13
  Args:
14
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
 
15
  """
16
  try:
17
- tz = pytz.timezone(timezone)
18
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
19
- return f"The current local time in {timezone} is: {local_time}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  except Exception as e:
21
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
22
 
23
  @tool
24
  def weather_lookup(city: str) -> str:
@@ -74,14 +118,7 @@ with open("prompts.yaml", 'r') as stream:
74
  # Create agent with all tools
75
  agent = CodeAgent(
76
  model=model,
77
- tools=[
78
- final_answer, # Required
79
- duckduckgo_search,
80
- image_generation_tool,
81
- get_current_time_in_timezone,
82
- weather_lookup,
83
- unit_converter
84
- ],
85
  max_steps=6,
86
  verbosity_level=1,
87
  grammar=None,
 
8
 
9
  # Custom Tools
10
  @tool
11
+ @tool
12
+ def convert_time_between_timezones(
13
+ time_str: str,
14
+ from_tz: str,
15
+ to_tz: str,
16
+ time_format: str = "%Y-%m-%d %H:%M:%S"
17
+ ) -> str:
18
+ """Converts a time between two timezones, handling DST automatically.
19
  Args:
20
+ time_str: The time string to convert (e.g., '2023-12-25 16:00:00')
21
+ from_tz: Source timezone (e.g., 'Europe/Madrid', 'UTC+2', 'America/Mexico_City')
22
+ to_tz: Target timezone (e.g., 'America/Sao_Paulo', 'UTC-3')
23
+ time_format: Format of input/output time strings
24
  """
25
  try:
26
+ # Handle UTC offsets (like 'UTC+2')
27
+ if from_tz.startswith('UTC'):
28
+ from_tz = f'Etc/GMT{-int(from_tz[3:])}' if '+' in from_tz else f'Etc/GMT{+int(from_tz[3:])}'
29
+ if to_tz.startswith('UTC'):
30
+ to_tz = f'Etc/GMT{-int(to_tz[3:])}' if '+' in to_tz else f'Etc/GMT{+int(to_tz[3:])}'
31
+
32
+ # Get timezone objects
33
+ from_zone = pytz.timezone(from_tz)
34
+ to_zone = pytz.timezone(to_tz)
35
+
36
+ # Parse time (naive) and localize to source timezone
37
+ naive_time = datetime.datetime.strptime(time_str, time_format)
38
+ localized_time = from_zone.localize(naive_time)
39
+
40
+ # Convert to target timezone
41
+ converted_time = localized_time.astimezone(to_zone)
42
+
43
+ # Get current UTC offsets to show the difference
44
+ from_offset = localized_time.strftime('%z')
45
+ to_offset = converted_time.strftime('%z')
46
+
47
+ return (f"{localized_time.strftime(time_format)} {from_tz} ({from_offset}) "
48
+ f"is {converted_time.strftime(time_format)} {to_tz} ({to_offset})")
49
+
50
+ except pytz.UnknownTimeZoneError as e:
51
+ common_tz = {
52
+ 'Spain': 'Europe/Madrid',
53
+ 'São Paulo': 'America/Sao_Paulo',
54
+ 'SP': 'America/Sao_Paulo',
55
+ 'Paulo, Mexico': 'America/Mexico_City',
56
+ 'CEST': 'Europe/Madrid', # Central European Summer Time
57
+ 'CET': 'Europe/Paris', # Central European Time
58
+ 'BRT': 'America/Sao_Paulo'
59
+ }
60
+ if e.args[0] in common_tz:
61
+ return f"Note: Using '{common_tz[e.args[0]]}' for '{e.args[0]}'.\n" + \
62
+ convert_time_between_timezones(time_str, common_tz[e.args[0]], to_tz, time_format)
63
+ return f"Error: Unknown timezone - {str(e)}. Common timezones: Europe/Madrid, America/Sao_Paulo, etc."
64
  except Exception as e:
65
+ return f"Error converting time: {str(e)}"
66
 
67
  @tool
68
  def weather_lookup(city: str) -> str:
 
118
  # Create agent with all tools
119
  agent = CodeAgent(
120
  model=model,
121
+ tools=[final_answer, duckduckgo_search, image_generation_tool, convert_time_between_timezones],
 
 
 
 
 
 
 
122
  max_steps=6,
123
  verbosity_level=1,
124
  grammar=None,