Joachim M. Vis commited on
Commit
b5a6c20
·
verified ·
1 Parent(s): c9330df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -7
app.py CHANGED
@@ -1,4 +1,49 @@
1
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  return None
3
 
4
  origin_info = search_airport_info(origin)
@@ -10,7 +55,6 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, t
10
  try:
11
  origin_time = datetime.datetime.now(pytz.timezone(origin_info['timezone'])).strftime("%H:%M")
12
  dest_time = datetime.datetime.now(pytz.timezone(dest_info['timezone'])).strftime("%H:%M")
13
-
14
  info = f"""
15
  Travel Information:
16
  🛫 Departure: {origin_info['name']} ({origin_info['code']})
@@ -24,7 +68,6 @@ Travel Information:
24
  ℹ️ Airport information was dynamically retrieved from the web.
25
  """
26
  return info
27
-
28
  except Exception as e:
29
  return f"Error processing timezone information: {str(e)}"
30
 
@@ -38,20 +81,18 @@ def get_current_time_in_timezone(timezone: str) -> str:
38
  except Exception as e:
39
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
40
 
41
- # Initialize tools and model
42
  final_answer = FinalAnswerTool()
 
43
  model = HfApiModel(
44
  max_tokens=2096,
45
  temperature=0.5,
46
- model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', # Using the alternative endpoint
47
  custom_role_conversions=None,
48
  )
49
 
50
- # Load prompt templates
51
  with open("prompts.yaml", 'r') as stream:
52
  prompt_templates = yaml.safe_load(stream)
53
 
54
- # Initialize agent with tools
55
  agent = CodeAgent(
56
  model=model,
57
  tools=[
@@ -69,7 +110,6 @@ agent = CodeAgent(
69
  prompt_templates=prompt_templates
70
  )
71
 
72
- # Launch Gradio interface
73
  if __name__ == "__main__":
74
  interface = GradioUI(agent)
75
  interface.launch(share=False)
 
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
+ web_search = DuckDuckGoSearchTool()
10
+
11
+ @tool
12
+ def travel_info(origin: str, destination: str) -> str:
13
+ """A tool for travel information between two cities/airports with dynamic airport search."""
14
+ def search_airport_info(location):
15
+ search_query = f"{location} airport IATA code timezone"
16
+ search_results = web_search(search_query)
17
+ try:
18
+ import re
19
+ iata_pattern = r'\(([A-Z]{3})\)'
20
+ iata_matches = re.findall(iata_pattern, search_results)
21
+ if iata_matches:
22
+ iata_code = iata_matches[0]
23
+ timezone_search = web_search(f"{location} airport timezone")
24
+ common_timezones = {
25
+ 'UTC': 'UTC',
26
+ 'EST': 'America/New_York',
27
+ 'CST': 'America/Chicago',
28
+ 'PST': 'America/Los_Angeles',
29
+ 'CET': 'Europe/Berlin',
30
+ 'GMT': 'Europe/London',
31
+ 'JST': 'Asia/Tokyo'
32
+ }
33
+ timezone = None
34
+ for tz_key in common_timezones:
35
+ if tz_key in timezone_search:
36
+ timezone = common_timezones[tz_key]
37
+ break
38
+ if not timezone:
39
+ timezone = 'UTC'
40
+ return {
41
+ 'code': iata_code,
42
+ 'name': f"{location} Airport",
43
+ 'timezone': timezone
44
+ }
45
+ except Exception as e:
46
+ print(f"Error searching for {location}: {str(e)}")
47
  return None
48
 
49
  origin_info = search_airport_info(origin)
 
55
  try:
56
  origin_time = datetime.datetime.now(pytz.timezone(origin_info['timezone'])).strftime("%H:%M")
57
  dest_time = datetime.datetime.now(pytz.timezone(dest_info['timezone'])).strftime("%H:%M")
 
58
  info = f"""
59
  Travel Information:
60
  🛫 Departure: {origin_info['name']} ({origin_info['code']})
 
68
  ℹ️ Airport information was dynamically retrieved from the web.
69
  """
70
  return info
 
71
  except Exception as e:
72
  return f"Error processing timezone information: {str(e)}"
73
 
 
81
  except Exception as e:
82
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
83
 
 
84
  final_answer = FinalAnswerTool()
85
+
86
  model = HfApiModel(
87
  max_tokens=2096,
88
  temperature=0.5,
89
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
90
  custom_role_conversions=None,
91
  )
92
 
 
93
  with open("prompts.yaml", 'r') as stream:
94
  prompt_templates = yaml.safe_load(stream)
95
 
 
96
  agent = CodeAgent(
97
  model=model,
98
  tools=[
 
110
  prompt_templates=prompt_templates
111
  )
112
 
 
113
  if __name__ == "__main__":
114
  interface = GradioUI(agent)
115
  interface.launch(share=False)