mugwaneza commited on
Commit
c4422ae
·
verified ·
1 Parent(s): d60a922

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -64
app.py CHANGED
@@ -1,100 +1,83 @@
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
- #These are waether specific imports
8
  import openmeteo_requests
9
  import pandas as pd
10
  import requests_cache
11
  from retry_requests import retry
12
-
13
  from Gradio_UI import GradioUI
14
 
15
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
16
  @tool
17
- def houry_templature(arg1:str, arg2:int)-> 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 does nothing yet
20
  Args:
21
- arg1: the first argument
22
- arg2: the second argument
23
  """
24
- # Setup the Open-Meteo API client with cache and retry on error
25
- cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
26
- retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
27
- openmeteo = openmeteo_requests.Client(session = retry_session)
28
-
29
- # Make sure all required weather variables are listed here
30
- # The order of variables in hourly or daily is important to assign them correctly below
31
- url = "https://api.open-meteo.com/v1/forecast"
32
- params = {
33
- "latitude": arg1,
34
- "longitude": arg2,
35
- "hourly": "temperature_2m",
36
- }
37
- responses = openmeteo.weather_api(url, params=params)
38
-
39
- hourly = response.Hourly()
40
- hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
41
-
42
- hourly_data["temperature_2m"] = hourly_temperature_2m
43
-
44
-
45
- return hourly_data["temperature_2m"]
 
 
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.
50
- Args:
51
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
52
- """
53
  try:
54
- # Create timezone object
55
  tz = pytz.timezone(timezone)
56
- # Get current time in that timezone
57
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
58
  return f"The current local time in {timezone} is: {local_time}"
59
  except Exception as e:
60
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
61
 
62
 
 
63
  final_answer = FinalAnswerTool()
64
 
65
- # 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:
66
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
67
-
68
- model = HfApiModel(
69
- max_tokens=2096,
70
- temperature=0.5,
71
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
72
- custom_role_conversions=None,
73
- )
74
-
75
 
76
- # Import tool from Hub
77
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
78
 
 
79
  with open("prompts.yaml", 'r') as stream:
80
  prompt_templates = yaml.safe_load(stream)
81
-
 
82
  agent = CodeAgent(
83
  model=model,
84
- tools=[final_answer], ## add your tools here (don't remove final answer)
85
  max_steps=6,
86
  verbosity_level=1,
87
- grammar=None,
88
- planning_interval=None,
89
- name=None,
90
- description=None,
91
  prompt_templates=prompt_templates
92
  )
93
 
94
-
95
- houry_templature(32.9,15,7)
96
- get_current_time_in_timezone('Africa/Kigali')
97
-
98
-
99
-
100
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfAgent, load_tool, tool
2
  import datetime
 
3
  import pytz
4
  import yaml
 
 
5
  import openmeteo_requests
6
  import pandas as pd
7
  import requests_cache
8
  from retry_requests import retry
9
+ from tools.final_answer import FinalAnswerTool
10
  from Gradio_UI import GradioUI
11
 
12
+
13
+ # Weather tool
14
  @tool
15
+ def hourly_temperature(latitude: float, longitude: float) -> str:
16
+ """Fetch hourly temperature data using Open-Meteo API.
 
17
  Args:
18
+ latitude: Latitude of the location
19
+ longitude: Longitude of the location
20
  """
21
+ try:
22
+ cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
23
+ retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
24
+ openmeteo = openmeteo_requests.Client(session=retry_session)
25
+
26
+ url = "https://api.open-meteo.com/v1/forecast"
27
+ params = {
28
+ "latitude": latitude,
29
+ "longitude": longitude,
30
+ "hourly": "temperature_2m",
31
+ }
32
+
33
+ responses = openmeteo.weather_api(url, params=params)
34
+ response = responses[0] # Get first response object
35
+
36
+ hourly = response.Hourly()
37
+ hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
38
+
39
+ # Prepare readable output
40
+ temps = hourly_temperature_2m[:5] # Show first 5 hourly values
41
+ return f"Next hours' temperatures (°C): {temps.tolist()}"
42
+
43
+ except Exception as e:
44
+ return f"Error fetching weather: {str(e)}"
45
 
46
+
47
+ # Time tool
48
  @tool
49
  def get_current_time_in_timezone(timezone: str) -> str:
50
+ """Fetch the current local time in a specified timezone."""
 
 
 
51
  try:
 
52
  tz = pytz.timezone(timezone)
 
53
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
54
  return f"The current local time in {timezone} is: {local_time}"
55
  except Exception as e:
56
+ return f"Error fetching time: {str(e)}"
57
 
58
 
59
+ # Final answer tool
60
  final_answer = FinalAnswerTool()
61
 
62
+ # Initialize model (new API)
63
+ model = HfAgent("Qwen/Qwen2.5-Coder-32B-Instruct")
 
 
 
 
 
 
 
 
64
 
65
+ # Optional: load text-to-image tool from Hugging Face Hub
66
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
67
 
68
+ # Load custom prompts if you have them
69
  with open("prompts.yaml", 'r') as stream:
70
  prompt_templates = yaml.safe_load(stream)
71
+
72
+ # Define your agent
73
  agent = CodeAgent(
74
  model=model,
75
+ tools=[final_answer, hourly_temperature, get_current_time_in_timezone],
76
  max_steps=6,
77
  verbosity_level=1,
 
 
 
 
78
  prompt_templates=prompt_templates
79
  )
80
 
81
+ # Run Gradio UI
82
+ if __name__ == "__main__":
83
+ GradioUI(agent).launch()