CR commited on
Commit
45a2756
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files

adding temperature in fahrenheit

Files changed (1) hide show
  1. app.py +26 -0
app.py CHANGED
@@ -1,13 +1,38 @@
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
 
8
  from Gradio_UI import GradioUI
9
 
 
 
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  @tool
12
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
  #Keep this format for the description / args / args description but feel free to modify the tool
@@ -18,6 +43,7 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
+ import httpx
4
  import requests
5
  import pytz
6
  import yaml
7
  from tools.final_answer import FinalAnswerTool
8
+ import os
9
 
10
  from Gradio_UI import GradioUI
11
 
12
+ # Import OpenWeather API key
13
+ OpenWeather_API_key = os.environ["OPEN_WEATHER_API"]
14
+
15
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
16
+ @tool
17
+ def get_current_temperature(location: str)-> str: #it's import to specify the return type
18
+ #Keep this format for the description / args / args description but feel free to modify the tool
19
+ """A tool that fetches the current temperature in a specified location.
20
+ Args:
21
+ location: A string representing a valid location (e.g. 'San Francisco, CA', 'Madrid, Spain')
22
+ """
23
+ try:
24
+ # First we get the geocoding data to get the latitude and longitude of a location
25
+ geocoding_data = httpx.get('http://api.openweathermap.org/geo/1.0/direct?q={location}&limit=1&appid={OpenWeather_API_key}')
26
+ latitude = geocoding_data.lat
27
+ longitude = geocoding_data.lon
28
+ # Then we use that to get the current temperature in Fahrenheit
29
+ current_data = httpx.get('https://api.openweathermap.org/data/3.0/onecall?lat={latitude}&lon={longitude}&units={imperial}&appid={OpenWeather_API_key}')
30
+ temperature = current_data.current.temp
31
+ return f"The current temperature in {location} is {temperature} degrees Fahrenheit."
32
+ except Exception as e:
33
+ return f"Error fetching data for location'{location}': {str(e)}"
34
+
35
+
36
  @tool
37
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
38
  #Keep this format for the description / args / args description but feel free to modify the tool
 
43
  """
44
  return "What magic will you build ?"
45
 
46
+
47
  @tool
48
  def get_current_time_in_timezone(timezone: str) -> str:
49
  """A tool that fetches the current local time in a specified timezone.