Spaces:
Sleeping
Sleeping
Add custom tools.
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -33,6 +34,45 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
@@ -40,10 +80,10 @@ final_answer = FinalAnswerTool()
|
|
| 40 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
|
|
@@ -55,7 +95,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
+
import os
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
+
@tool
|
| 38 |
+
def get_aqi(city:str, state:str, country:str)-> str:
|
| 39 |
+
"""A tool that fetches the specified city's air quality data.
|
| 40 |
+
Args:
|
| 41 |
+
city: city's English name
|
| 42 |
+
state: state's English name
|
| 43 |
+
country: country's English name
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
IQAIR_API_KEY = os.environ["IQAIR_API_KEY"]
|
| 47 |
+
url = f"http://api.airvisual.com/v2/city?city={city}&state={state}&country={country}&key={IQAIR_API_KEY}"
|
| 48 |
+
payload={}
|
| 49 |
+
headers = {}
|
| 50 |
+
|
| 51 |
+
response = requests.request("GET", url, headers=headers, data=payload)
|
| 52 |
+
|
| 53 |
+
data_json = response.json
|
| 54 |
+
if data_json.status == "success":
|
| 55 |
+
return f"The current air quality in {city} is {data_json.current.pollution.aqius}."
|
| 56 |
+
else:
|
| 57 |
+
return "No air quality data was fetched.";
|
| 58 |
+
|
| 59 |
+
except requests.exceptions.Timeout:
|
| 60 |
+
return "The request timed out. Please try again later or check the URL."
|
| 61 |
+
except RequestException as e:
|
| 62 |
+
return f"Error fetching the IQAir API: {str(e)}"
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"An unexpected error occurred: {str(e)}"
|
| 65 |
+
|
| 66 |
+
@tool
|
| 67 |
+
def air_purifier_switch(cmd:str) -> str:
|
| 68 |
+
"""A tool to turn the air purifier on/off.
|
| 69 |
+
Args:
|
| 70 |
+
cmd: Switch command (on/off). on - turn on; off - turn off.
|
| 71 |
+
"""
|
| 72 |
+
if cmd != "on" and cmd != "off":
|
| 73 |
+
return "Illegal air purifier switch command.";
|
| 74 |
+
|
| 75 |
+
return f"The air purifier has been successfully turned {cmd}."
|
| 76 |
|
| 77 |
final_answer = FinalAnswerTool()
|
| 78 |
|
|
|
|
| 80 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 81 |
|
| 82 |
model = HfApiModel(
|
| 83 |
+
max_tokens=2096,
|
| 84 |
+
temperature=0.5,
|
| 85 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 86 |
+
custom_role_conversions=None,
|
| 87 |
)
|
| 88 |
|
| 89 |
|
|
|
|
| 95 |
|
| 96 |
agent = CodeAgent(
|
| 97 |
model=model,
|
| 98 |
+
tools=[get_aqi, air_purifier_switch, final_answer], ## add your tools here (don't remove final answer)
|
| 99 |
max_steps=6,
|
| 100 |
verbosity_level=1,
|
| 101 |
grammar=None,
|