gangel commited on
Commit
e95d98f
·
1 Parent(s): 881628d

Fix async code

Browse files
Files changed (2) hide show
  1. app.py +32 -22
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,35 +1,44 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, VisitWebpageTool, HfApiModel,load_tool,tool
2
  import datetime
3
- import requests
 
 
 
4
  import pytz
5
  import yaml
6
- from tools.final_answer import FinalAnswerTool
7
- import python_weather
8
-
9
 
10
  from Gradio_UI import GradioUI
 
 
 
11
 
12
 
13
  @tool
14
  def get_weather(location: str) -> str:
15
- """Tool for checking the weather at specific location
16
  Args:
17
  location: name of the location for checking the weather, mandatory parameter
18
  Return:
19
  Description of the weather
20
  """
21
- await async with python_weather.Client(unit=python_weather.METRIC) as client:
22
- # fetch a weather forecast from a city
23
- weather = await client.get(location)
24
- if weather is None:
25
- return "Problem: unknown location or weather"
 
 
 
 
 
 
 
 
 
26
 
27
- output = [f"Current temperature for {location} is {weather.temperature}"]
28
- # get the weather forecast for a few days
29
- for daily in weather:
30
- output.append(daily)
31
 
32
- return ' '.join(output)
33
 
34
  @tool
35
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,20 +64,22 @@ final_answer = FinalAnswerTool()
55
  model = HfApiModel(
56
  max_tokens=2096,
57
  temperature=0.5,
58
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
 
59
  custom_role_conversions=None,
 
60
  )
61
 
62
-
63
  # Import tool from Hub
64
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
65
 
66
  with open("prompts.yaml", 'r') as stream:
67
  prompt_templates = yaml.safe_load(stream)
68
-
69
  agent = CodeAgent(
70
  model=model,
71
- tools=[final_answer, get_current_time_in_timezone, get_weather, DuckDuckGoSearchTool(), VisitWebpageTool()], ## add your tools here (don't remove final answer)
 
72
  max_steps=6,
73
  verbosity_level=1,
74
  grammar=None,
@@ -78,5 +89,4 @@ agent = CodeAgent(
78
  prompt_templates=prompt_templates
79
  )
80
 
81
-
82
- GradioUI(agent).launch()
 
1
+ import asyncio
2
  import datetime
3
+ import os
4
+
5
+ import dotenv
6
+ import python_weather
7
  import pytz
8
  import yaml
9
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, HfApiModel, load_tool, tool
 
 
10
 
11
  from Gradio_UI import GradioUI
12
+ from tools.final_answer import FinalAnswerTool
13
+
14
+ dotenv.load_dotenv(override=True)
15
 
16
 
17
  @tool
18
  def get_weather(location: str) -> str:
19
+ """Tool for checking the full description of the weather for a specific location
20
  Args:
21
  location: name of the location for checking the weather, mandatory parameter
22
  Return:
23
  Description of the weather
24
  """
25
+ async def fetch_weather():
26
+ async with python_weather.Client(unit=python_weather.METRIC) as client:
27
+ # fetch a weather forecast from a city
28
+ weather = await client.get(location)
29
+ if weather is None:
30
+ return "Problem: unknown location or weather"
31
+
32
+ output = [f"Current temperature for {location} is {weather.temperature}"]
33
+ # get the weather forecast for a few days
34
+ for daily in weather:
35
+ desc = f"Weather for day {str(daily.date)}. Temperature high: {daily.highest_temperature}, low: {daily.lowest_temperature}. avg: {daily.temperature}."
36
+ output.append(desc)
37
+
38
+ return '\n'.join(output)
39
 
40
+ return asyncio.run(fetch_weather())
 
 
 
41
 
 
42
 
43
  @tool
44
  def get_current_time_in_timezone(timezone: str) -> str:
 
64
  model = HfApiModel(
65
  max_tokens=2096,
66
  temperature=0.5,
67
+ # model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
68
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
69
  custom_role_conversions=None,
70
+ token=os.getenv("HF_KEY")
71
  )
72
 
 
73
  # Import tool from Hub
74
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
75
 
76
  with open("prompts.yaml", 'r') as stream:
77
  prompt_templates = yaml.safe_load(stream)
78
+
79
  agent = CodeAgent(
80
  model=model,
81
+ tools=[final_answer, get_current_time_in_timezone, get_weather, DuckDuckGoSearchTool(), VisitWebpageTool()],
82
+ ## add your tools here (don't remove final answer)
83
  max_steps=6,
84
  verbosity_level=1,
85
  grammar=None,
 
89
  prompt_templates=prompt_templates
90
  )
91
 
92
+ GradioUI(agent).launch()
 
requirements.txt CHANGED
@@ -1,5 +1,7 @@
1
  markdownify
2
- smolagents
3
  requests
4
  duckduckgo_search
5
  pandas
 
 
 
1
  markdownify
2
+ smolagents[gradio]
3
  requests
4
  duckduckgo_search
5
  pandas
6
+ python-weather
7
+ python-dotenv