SerotoninRonin commited on
Commit
47c4e69
·
verified ·
1 Parent(s): ae7a494

Added nanoleaf functions

Browse files

Can turn them on/off, and set effects and colors

Files changed (1) hide show
  1. app.py +85 -18
app.py CHANGED
@@ -1,38 +1,105 @@
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
 
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
 
18
  """
19
- return "What magic will you build ?"
 
20
 
21
  @tool
22
- def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
  Args:
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
  final_answer = FinalAnswerTool()
38
 
@@ -48,14 +115,14 @@ custom_role_conversions=None,
48
 
49
 
50
  # Import tool from Hub
51
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
 
53
  with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(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, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
+ from dotenv import load_dotenv
7
+ import os
8
  from tools.final_answer import FinalAnswerTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
+ load_dotenv()
13
+
14
+ auth = os.getenv("NANOLEAF_AUTH")
15
+ ip = os.getenv("NANOLEAF_IP")
16
+ port = os.getenv("NANOLEAF_PORT")
17
+
18
+ base_url = f"http://{ip}:{port}/api/v1/{auth}"
19
+
20
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
21
+ # @tool
22
+ # def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
23
+ # #Keep this format for the description / args / args description but feel free to modify the tool
24
+ # """A tool that does nothing yet
25
+ # Args:
26
+ # arg1: the first argument
27
+ # arg2: the second argument
28
+ # """
29
+ # return "What magic will you build ?"
30
+
31
+ @tool
32
+ def nanoleaf_on() -> dict:
33
+ """Turns on the Nanoleaf"""
34
+ payload = {"on": {"value": True}}
35
+ return send_to_nanoleaf_api("state", payload, "PUT")
36
+
37
+ @tool
38
+ def nanoleaf_off() -> dict:
39
+ """Turns off the Nanoleaf"""
40
+ payload = {"on": {"value": False}}
41
+ return send_to_nanoleaf_api("state", payload, "PUT")
42
+
43
+ @tool
44
+ def set_nanoleaf_brightness(brightness: int) -> dict:
45
+ """Sets the brightness of the Nanoleaf
46
+ Args:
47
+ brightness: The brightness value between 0 and 100
48
+ """
49
+ payload = {"brightness": {"value": brightness}}
50
+ return send_to_nanoleaf_api("state", payload, "PUT")
51
+
52
+ @tool
53
+ def nanoleaf_color_temperature(temperature: int) -> dict:
54
+ """Sets the color temperature of the Nanoleaf
55
+ Args:
56
+ temperature: The color temperature value between 1200 and 6500
57
+ """
58
+ payload = {"ct": {"value": temperature}}
59
+ return send_to_nanoleaf_api("state", payload, "PUT")
60
+
61
  @tool
62
+ def set_nanoleaf_color(hue: int, saturation: int, brightness: int) -> dict:
63
+ """Sets the color of the Nanoleaf
 
64
  Args:
65
+ hue: The hue value between 0 and 360
66
+ saturation: The saturation value between 0 and 100
67
+ brightness: The brightness value between 0 and 100
68
  """
69
+ payload = {"hue": {"value": hue}, "sat": {"value": saturation}, "brightness": {"value": brightness}}
70
+ return send_to_nanoleaf_api("state", payload, "PUT")
71
 
72
  @tool
73
+ def set_nanoleaf_effect(effect: str) -> dict:
74
+ """Sets the effect of the Nanoleaf
75
  Args:
76
+ effect: The effect name
77
  """
78
+ payload = {"select": effect}
79
+ return send_to_nanoleaf_api("effects", payload, "PUT")
80
+
81
+ @tool
82
+ def get_nanoleaf_effects() -> list:
83
+ """Gets the list of effects of the Nanoleaf"""
84
+ return send_to_nanoleaf_api("effects/effectsList", {}, "GET")
85
+
86
+
87
+
88
+ def send_to_nanoleaf_api(endpoint, payload, method):
89
+ headers = {
90
+ 'Content-Type': 'application/json'
91
+ }
92
+ url = f"{base_url}/{endpoint}"
93
+ print(url)
94
  try:
95
+ response = requests.request(method, url, headers=headers, json=payload)
96
+ if response.status_code == 204:
97
+ return {"message": "Success"}
98
+ return response.json()
 
99
  except Exception as e:
100
+ return {"error": str(e)}
101
 
102
+
103
 
104
  final_answer = FinalAnswerTool()
105
 
 
115
 
116
 
117
  # Import tool from Hub
118
+ #image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
119
 
120
  with open("prompts.yaml", 'r') as stream:
121
  prompt_templates = yaml.safe_load(stream)
122
 
123
  agent = CodeAgent(
124
  model=model,
125
+ tools=[final_answer, nanoleaf_on, nanoleaf_off, nanoleaf_color_temperature, get_nanoleaf_effects, set_nanoleaf_effect, set_nanoleaf_color, set_nanoleaf_brightness], ## add your tools here (don't remove final answer)
126
  max_steps=6,
127
  verbosity_level=1,
128
  grammar=None,