sperera commited on
Commit
e8e3ab1
·
verified ·
1 Parent(s): 40889fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -30
app.py CHANGED
@@ -1,17 +1,15 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, InferenceClientModel,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
14
- """A tool that does nothing yet
15
  Args:
16
  arg1: the first argument
17
  arg2: the second argument
@@ -25,72 +23,60 @@ def get_current_time_in_timezone(timezone: str) -> str:
25
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
27
  try:
28
- # Create timezone object
29
  tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
  return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
-
37
  @tool
38
  def get_current_weather(city: str) -> str:
39
  """Get the current weather conditions and temperature for a given city.
40
-
41
- Args:
42
  city: The name of the city to look up (e.g., 'San Francisco', 'Tokyo').
43
  """
44
- # Using a free public weather API endpoint (Open-Meteo as an example)
45
- # First, get coordinates for the city using a geocoding lookup
46
  geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
47
  geo_res = requests.get(geo_url).json()
48
-
49
  if not geo_res.get("results"):
50
  return f"Could not find coordinates for city: {city}"
51
-
52
  lat = geo_res["results"][0]["latitude"]
53
  lon = geo_res["results"][0]["longitude"]
54
-
55
- # Fetch current weather data
56
  weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current=temperature_2m,weather_code"
57
  data = requests.get(weather_url).json()
58
-
59
  current = data.get("current", {})
60
  temp = current.get("temperature_2m")
61
-
62
  return f"The current temperature in {city} is {temp}°C."
63
 
64
  final_answer = FinalAnswerTool()
65
 
66
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
67
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
68
-
69
  model = InferenceClientModel(
70
- max_tokens=2096,
71
- temperature=0.5,
72
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
73
- custom_role_conversions=None,
74
  )
75
 
76
-
77
  # Import tool from Hub
78
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
79
 
80
  with open("prompts.yaml", 'r') as stream:
81
  prompt_templates = yaml.safe_load(stream)
82
-
83
  agent = CodeAgent(
84
  model=model,
85
- tools=[final_answer, get_current_time_in_timezone, get_current_weather], ## add your tools here (don't remove final answer)
86
  max_steps=6,
87
  verbosity_level=1,
88
- grammar=None,
89
  planning_interval=None,
90
  name=None,
91
  description=None,
92
  prompt_templates=prompt_templates
93
  )
94
 
95
-
96
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, 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
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
10
  @tool
11
+ def my_custom_tool(arg1: str, arg2: int) -> str:
12
+ """A tool that does nothing yet
 
13
  Args:
14
  arg1: the first argument
15
  arg2: the second argument
 
23
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
24
  """
25
  try:
 
26
  tz = pytz.timezone(timezone)
 
27
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
28
  return f"The current local time in {timezone} is: {local_time}"
29
  except Exception as e:
30
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
31
 
 
32
  @tool
33
  def get_current_weather(city: str) -> str:
34
  """Get the current weather conditions and temperature for a given city.
35
+ Args:
 
36
  city: The name of the city to look up (e.g., 'San Francisco', 'Tokyo').
37
  """
 
 
38
  geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
39
  geo_res = requests.get(geo_url).json()
40
+
41
  if not geo_res.get("results"):
42
  return f"Could not find coordinates for city: {city}"
43
+
44
  lat = geo_res["results"][0]["latitude"]
45
  lon = geo_res["results"][0]["longitude"]
46
+
 
47
  weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&current=temperature_2m,weather_code"
48
  data = requests.get(weather_url).json()
49
+
50
  current = data.get("current", {})
51
  temp = current.get("temperature_2m")
52
+
53
  return f"The current temperature in {city} is {temp}°C."
54
 
55
  final_answer = FinalAnswerTool()
56
 
 
 
 
57
  model = InferenceClientModel(
58
+ max_tokens=2096,
59
+ temperature=0.5,
60
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
61
+ custom_role_conversions=None,
62
  )
63
 
 
64
  # Import tool from Hub
65
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
66
 
67
  with open("prompts.yaml", 'r') as stream:
68
  prompt_templates = yaml.safe_load(stream)
69
+
70
  agent = CodeAgent(
71
  model=model,
72
+ tools=[final_answer, get_current_time_in_timezone, get_current_weather],
73
  max_steps=6,
74
  verbosity_level=1,
75
+ # grammar=None, <-- Removed because it causes a TypeError in this smolagents version
76
  planning_interval=None,
77
  name=None,
78
  description=None,
79
  prompt_templates=prompt_templates
80
  )
81
 
 
82
  GradioUI(agent).launch()