callmyname commited on
Commit
9dedd3b
·
verified ·
1 Parent(s): 1be09ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -35
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
@@ -7,32 +7,40 @@ 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 get_weather(latitude:float, longitude:float)-> str:
13
- """A tool returns description of weather by given coordinances
14
- Args:
15
- latitude: float value of latitute 49.4310
16
- longitude: float value of longitude 18.4843
17
- """
18
 
19
- api_url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&current_weather=true"
20
- response = requests.get(api_url)
21
- if response.status_code == 200:
22
- data = response.json()
23
- return data.get("current_weather", "No weather information available")
24
- else:
25
- return "Error: Unable to fetch weather data."
26
-
27
- @tool
28
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
29
- #Keep this format for the description / args / args description but feel free to modify the tool
30
- """A tool that does nothing yet
31
  Args:
32
- arg1: the first argument
33
- arg2: the second argument
 
 
34
  """
35
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  @tool
38
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -49,29 +57,28 @@ def get_current_time_in_timezone(timezone: str) -> str:
49
  except Exception as e:
50
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
51
 
52
-
53
  final_answer = FinalAnswerTool()
54
 
55
- # 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:
56
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
57
-
58
- model = HfApiModel(
59
- max_tokens=2096,
60
- temperature=0.5,
61
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
62
- custom_role_conversions=None,
63
  )
64
 
65
-
66
  # Import tool from Hub
67
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
68
 
 
69
  with open("prompts.yaml", 'r') as stream:
70
  prompt_templates = yaml.safe_load(stream)
71
 
 
72
  agent = CodeAgent(
73
  model=model,
74
- tools=[final_answer,get_weather], ## add your tools here (don't remove final answer)
75
  max_steps=6,
76
  verbosity_level=1,
77
  grammar=None,
@@ -81,5 +88,5 @@ agent = CodeAgent(
81
  prompt_templates=prompt_templates
82
  )
83
 
84
-
85
  GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
+ # Weather search tool using DuckDuckGo
11
  @tool
12
+ def get_weather_info(city: str) -> str:
13
+ """Searches for current weather information for a given city using DuckDuckGo.
 
 
 
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  Args:
16
+ city: Name of the city to get weather information for (e.g., 'Warsaw', 'New York')
17
+
18
+ Returns:
19
+ A string containing weather information for the specified city
20
  """
21
+ try:
22
+ # Initialize DuckDuckGo search tool
23
+ search_tool = DuckDuckGoSearchTool()
24
+
25
+ # Create weather-specific search query
26
+ search_query = f"current weather {city} temperature conditions forecast"
27
+
28
+ # Perform search
29
+ search_results = search_tool(search_query)
30
+
31
+ # Extract weather information from results
32
+ if search_results and len(search_results) > 0:
33
+ # Combine relevant snippets
34
+ weather_info = f"Weather information for {city}:\n"
35
+ for i, result in enumerate(search_results[:3]): # Take top 3 results
36
+ if 'snippet' in result:
37
+ weather_info += f"\n{i+1}. {result['snippet']}"
38
+ return weather_info
39
+ else:
40
+ return f"Could not find weather information for {city}. Please check the city name and try again."
41
+
42
+ except Exception as e:
43
+ return f"Error searching for weather in {city}: {str(e)}"
44
 
45
  @tool
46
  def get_current_time_in_timezone(timezone: str) -> str:
 
57
  except Exception as e:
58
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
59
 
60
+ # Initialize tools
61
  final_answer = FinalAnswerTool()
62
 
63
+ # Initialize model
64
+ model = InferenceClientModel(
65
+ max_tokens=2096,
66
+ temperature=0.5,
67
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
68
+ custom_role_conversions=None,
 
 
69
  )
70
 
 
71
  # Import tool from Hub
72
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
73
 
74
+ # Load system prompt from prompt.yaml file
75
  with open("prompts.yaml", 'r') as stream:
76
  prompt_templates = yaml.safe_load(stream)
77
 
78
+ # Create agent with all tools including weather search
79
  agent = CodeAgent(
80
  model=model,
81
+ tools=[get_weather_info, get_current_time_in_timezone, image_generation_tool, final_answer],
82
  max_steps=6,
83
  verbosity_level=1,
84
  grammar=None,
 
88
  prompt_templates=prompt_templates
89
  )
90
 
91
+ # Launch Gradio UI
92
  GradioUI(agent).launch()