Bazingaaax commited on
Commit
2f7ba87
·
verified ·
1 Parent(s): c2dc148

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -88
app.py CHANGED
@@ -1,109 +1,40 @@
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
- # Custom tool for fetching interesting facts about a topic
10
- @tool
11
- def my_custom_tool(topic: str) -> str:
12
- """A tool that fetches interesting facts about a given topic.
13
- Args:
14
- topic: The topic to fetch facts about.
15
- """
16
- url = f"https://api.api-ninjas.com/v1/facts?limit=1"
17
- headers = {"X-Api-Key": "YOUR_API_KEY"} # Replace with a valid API key
18
- try:
19
- response = requests.get(url, headers=headers)
20
- if response.status_code == 200:
21
- facts = response.json()
22
- return f"Here's a fun fact about {topic}: {facts[0]['fact']}"
23
- else:
24
- return "Couldn't fetch facts at the moment. Try again later!"
25
- except Exception as e:
26
- return f"Error fetching facts: {str(e)}"
27
 
 
 
 
 
 
 
 
28
 
29
- # Tool to fetch the current time in a specified place
30
  @tool
31
  def get_current_time_in_timezone(timezone: str) -> str:
32
- """A tool that fetches the current local time in a specified timezone.
33
- Args:
34
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
35
- """
36
  try:
37
  tz = pytz.timezone(timezone)
38
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
39
  return f"The current local time in {timezone} is: {local_time}"
40
  except Exception as e:
41
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
42
-
43
-
44
- # Tool to look up the best time to visit monuments in a given place
45
- @tool
46
- def best_time_to_visit_monuments(place: str) -> str:
47
- """A tool that finds the best time to visit monuments in a given place.
48
- Args:
49
- place: The name of the city or country.
50
- """
51
- search_tool = DuckDuckGoSearchTool()
52
- search_query = f"best time to visit monuments in {place}"
53
- search_results = search_tool(search_query)
54
-
55
- if search_results:
56
- return f"According to sources, the best time to visit monuments in {place} is:\n{search_results[0]['snippet']}"
57
- else:
58
- return f"Couldn't find specific information on the best time to visit {place}. Try a different location."
59
 
60
-
61
- # Tool to fetch the timezone of a given place and provide the best visiting time for monuments
62
  @tool
63
- def get_timezone_and_visit_info(place: str) -> str:
64
- """Fetches the timezone of a given place and suggests the best time to visit its monuments.
65
- Args:
66
- place: The name of the city or country.
67
- """
68
- search_tool = DuckDuckGoSearchTool()
69
- timezone_query = f"time zone of {place}"
70
- timezone_results = search_tool(timezone_query)
71
-
72
- if timezone_results:
73
- timezone_info = timezone_results[0]["snippet"]
74
- visit_info = best_time_to_visit_monuments(place)
75
- return f"Timezone info: {timezone_info}\n\n{visit_info}"
76
- else:
77
- return f"Could not find timezone information for {place}."
78
 
79
-
80
- # Load image generation tool
81
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
82
-
83
- # Load prompt templates
84
- with open("prompts.yaml", "r") as stream:
85
- prompt_templates = yaml.safe_load(stream)
86
-
87
- # Initialize agent
88
- final_answer = FinalAnswerTool()
89
- model = HfApiModel(
90
- max_tokens=2096,
91
- temperature=0.5,
92
- model_id='meta-llama/Llama-3.2-1B-Instruct',
93
- custom_role_conversions=None,
94
- )
95
 
96
  agent = CodeAgent(
97
- model=model,
98
- tools=[final_answer, my_custom_tool, get_current_time_in_timezone, best_time_to_visit_monuments, get_timezone_and_visit_info, image_generation_tool],
99
  max_steps=6,
100
  verbosity_level=1,
101
- grammar=None,
102
- planning_interval=None,
103
- name="Travel & Info Agent",
104
- description="An AI agent that provides facts, timezone info, travel recommendations, and generates images.",
105
- prompt_templates=prompt_templates,
106
  )
107
 
108
- # Launch Gradio UI
109
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
2
  import datetime
 
3
  import pytz
4
+ import requests
5
  import yaml
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Predefined answers
8
+ PREDEFINED_RESPONSES = {
9
+ "tokyo": "The best time to visit Tokyo is during spring (March to May) or autumn (September to November).",
10
+ "eiffel tower": "The best time to visit the Eiffel Tower is early morning or late evening to avoid crowds.",
11
+ "rome": "The best time to visit the Colosseum is in the early morning or late afternoon to skip long lines.",
12
+ "machu picchu": "The dry season from May to October is the best time to visit Machu Picchu."
13
+ }
14
 
 
15
  @tool
16
  def get_current_time_in_timezone(timezone: str) -> str:
17
+ """Fetches the current local time in a specified timezone."""
 
 
 
18
  try:
19
  tz = pytz.timezone(timezone)
20
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
21
  return f"The current local time in {timezone} is: {local_time}"
22
  except Exception as e:
23
+ return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
 
25
  @tool
26
+ def get_best_time_to_visit(place: str) -> str:
27
+ """Fetches the best time to visit a place based on predefined knowledge."""
28
+ place = place.lower()
29
+ return PREDEFINED_RESPONSES.get(place, f"Sorry, I don't have predefined info for {place}. Try a web search.")
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ web_search_tool = DuckDuckGoSearchTool()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  agent = CodeAgent(
34
+ tools=[get_current_time_in_timezone, get_best_time_to_visit, web_search_tool],
 
35
  max_steps=6,
36
  verbosity_level=1,
37
+ name="TravelInfoAgent",
38
+ description="Provides travel and time zone information using predefined answers and web search."
 
 
 
39
  )
40