jonathan@tuxmani.fr commited on
Commit
533b8cb
·
1 Parent(s): 6950d8a

-Fix indent error, add shutter sound for camera

Browse files
amazon_this_object.egg-info/SOURCES.txt CHANGED
@@ -2,12 +2,23 @@ README.md
2
  pyproject.toml
3
  ./amazon_this_object/__init__.py
4
  ./amazon_this_object/main.py
 
 
 
5
  ./amazon_this_object/static/index.html
6
  ./amazon_this_object/static/main.js
7
  ./amazon_this_object/static/style.css
 
 
8
  amazon_this_object.egg-info/PKG-INFO
9
  amazon_this_object.egg-info/SOURCES.txt
10
  amazon_this_object.egg-info/dependency_links.txt
11
  amazon_this_object.egg-info/entry_points.txt
12
  amazon_this_object.egg-info/requires.txt
13
- amazon_this_object.egg-info/top_level.txt
 
 
 
 
 
 
 
2
  pyproject.toml
3
  ./amazon_this_object/__init__.py
4
  ./amazon_this_object/main.py
5
+ ./amazon_this_object/__pycache__/__init__.cpython-312.pyc
6
+ ./amazon_this_object/__pycache__/main.cpython-312.pyc
7
+ ./amazon_this_object/sound/shutter.mp3
8
  ./amazon_this_object/static/index.html
9
  ./amazon_this_object/static/main.js
10
  ./amazon_this_object/static/style.css
11
+ amazon_this_object/__init__.py
12
+ amazon_this_object/main.py
13
  amazon_this_object.egg-info/PKG-INFO
14
  amazon_this_object.egg-info/SOURCES.txt
15
  amazon_this_object.egg-info/dependency_links.txt
16
  amazon_this_object.egg-info/entry_points.txt
17
  amazon_this_object.egg-info/requires.txt
18
+ amazon_this_object.egg-info/top_level.txt
19
+ amazon_this_object/__pycache__/__init__.cpython-312.pyc
20
+ amazon_this_object/__pycache__/main.cpython-312.pyc
21
+ amazon_this_object/sound/shutter.mp3
22
+ amazon_this_object/static/index.html
23
+ amazon_this_object/static/main.js
24
+ amazon_this_object/static/style.css
amazon_this_object/__pycache__/main.cpython-312.pyc CHANGED
Binary files a/amazon_this_object/__pycache__/main.cpython-312.pyc and b/amazon_this_object/__pycache__/main.cpython-312.pyc differ
 
amazon_this_object/main.py CHANGED
@@ -1,4 +1,5 @@
1
  import threading
 
2
  import base64
3
  from datetime import datetime
4
  from reachy_mini import ReachyMini, ReachyMiniApp
@@ -10,6 +11,7 @@ import os
10
  from openai import OpenAI
11
  from pathlib import Path
12
  from fastapi import Request, Depends, Body
 
13
 
14
  class AmazonThisObject(ReachyMiniApp):
15
  custom_app_url: str | None = "http://0.0.0.0:8042"
@@ -33,156 +35,161 @@ class AmazonThisObject(ReachyMiniApp):
33
 
34
  @self.settings_app.post("/set_token")
35
  async def set_hf_token(request: Request):
36
- try:
37
- data = await request.json()
38
- token = data.get("token", "").strip()
39
 
40
- if not token:
 
 
 
 
 
 
 
 
 
41
  return {
42
- "success": False,
43
- "message": "No token provided."
 
 
 
 
 
44
  }
45
-
46
- # Erase or add new file with HF_TOKEN=ton_token
47
- with open(file_path, "w") as f:
48
- f.write(f"HF_TOKEN={token}\n")
49
- print(f"Token Hugging Face successfully saved in {file_path}")
50
- return {
51
- "success": True,
52
- "message": "Token saved."
53
- }
54
- except Exception as e:
55
- return {
56
- "success": False,
57
- "message": "Error on server side"
58
- }
59
 
60
  @self.settings_app.post("/check_token")
61
  async def hf_token_available(self_instance: AmazonThisObject = Depends(self._get_self)):
62
- file_path = Path(exec_dir) / ".env"
63
- if not file_path.exists():
64
- return {
65
- "success": False,
66
- "token": None
67
- }
68
- try:
69
- # Read all the file
70
- with open(file_path, "r", encoding="utf-8") as f:
71
- lines = f.readlines()
72
- # Find line HF_TOKEN=
73
- for line in lines:
74
- line = line.strip()
75
- if line.startswith("HF_TOKEN="):
76
- # On prend tout après le =
77
- self_instance.amaz_token = line.split("=", 1)[1].strip().strip('"').strip("'")
78
- break
79
- if self_instance.amaz_token:
80
- return {
81
- "success": True,
82
- "token": self_instance.amaz_token
83
- }
84
- else:
85
- print("No HF_TOKEN found.")
86
- return {
87
- "success": False,
88
- "token": None,
89
- "message": "HF_TOKEN missing"
90
- }
91
- except Exception as e:
92
- print(f"Errot while readind .env : {e}")
93
- return {
94
- "success": False,
95
- "token": None,
96
- "message": "Error whild reading file"
97
- }
98
 
99
  #Take photo REST POST request
100
  @self.settings_app.post("/take_photo")
101
  def take_photo(self_instance: AmazonThisObject = Depends(self._get_self)):
102
- nonlocal last_photo, last_photo_timestamp
103
-
104
- # Part for the Simulator
105
- # Initialize webcam (0 = default camera)
106
- #cam = cv2.VideoCapture(0)
107
- # Capture one frame with opencv on simulator [ need a webcam ] -> later replace by reachy robot camera
108
- #ret, frame = cam.read()
109
- #if not ret:
110
- # return {"success": False, "error": "Camera not available"}
111
-
112
- #Code for reachy camera
113
- frame = reachy_mini.media.get_frame()
114
- if frame is None:
115
- return {"success": False, "error": "Camera not available"}
116
-
117
- reachy_mini.media.play_sound("wall_E_language/sound/shutter.mp3")
118
-
119
- # Encode as JPEG
120
- success, encoded = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
121
- if not success:
122
- return {"success": False, "error": "Failed to encode image"}
123
-
124
- #Save last photo
125
- last_photo = encoded.tobytes()
126
- last_photo_timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
127
-
128
- # Return base64 for preview
129
- b64_image = base64.b64encode(last_photo).decode('utf-8')
130
- image_url = f"data:image/jpeg;base64,{b64_image}"
131
-
132
- return {
133
- "success": True,
134
- "preview": f"data:image/jpeg;base64,{b64_image}",
135
- "filename": f"reachy_photo_{last_photo_timestamp}.jpg",
136
- }
 
 
 
 
 
137
 
138
  #Get link REST POST request
139
  @self.settings_app.post("/get_link")
140
  def get_link(self_instance: AmazonThisObject = Depends(self._get_self)):
141
- #Restore image from save variable last_photo
142
- b64_image = base64.b64encode(last_photo).decode('utf-8')
143
- image_url = f"data:image/jpeg;base64,{b64_image}"
144
-
145
- #Use model zai-org/GLM-4.5V with inference novita with huggingface client.
146
- #Analyse image and get URL only -> bypass blabla from LLM by usin "no blabla".
147
- hf_client = OpenAI(
148
- base_url="https://router.huggingface.co/v1",
149
- #define token in environment variable
150
- api_key=self_instance.amaz_token
151
- )
152
- completion = hf_client.chat.completions.create(
153
- model="zai-org/GLM-4.5V:novita",
154
- messages=[
155
- {
156
- "role": "user",
157
- "content":
158
- [
159
- {
160
- "type": "text",
161
- "text": "Generate only a valid link to search object on amazon, no blabla."
162
- },
163
- {
164
- "type": "image_url",
165
- "image_url":
166
  {
167
- "url": image_url
 
 
 
 
 
 
 
 
168
  }
169
- }
170
- ]
171
- }
172
- ],
173
- )
174
- #Get link from result
175
- url_link = completion.choices[0].message.content
176
-
177
- #Clear content and keep only URL
178
- url_link = url_link.replace("<|end_of_box|>", "")
179
- url_link = url_link.replace("<|begin_of_box|>", "")
180
-
181
- #Return success + url link
182
- return {
183
- "success": True,
184
- "url": url_link
185
- }
186
 
187
  # Keep the app running
188
  while not stop_event.is_set():
 
1
  import threading
2
+ from threading import Thread
3
  import base64
4
  from datetime import datetime
5
  from reachy_mini import ReachyMini, ReachyMiniApp
 
11
  from openai import OpenAI
12
  from pathlib import Path
13
  from fastapi import Request, Depends, Body
14
+ from enum import Enum
15
 
16
  class AmazonThisObject(ReachyMiniApp):
17
  custom_app_url: str | None = "http://0.0.0.0:8042"
 
35
 
36
  @self.settings_app.post("/set_token")
37
  async def set_hf_token(request: Request):
38
+ try:
39
+ data = await request.json()
40
+ token = data.get("token", "").strip()
41
 
42
+ if not token:
43
+ return {
44
+ "success": False,
45
+ "message": "No token provided."
46
+ }
47
+
48
+ # Erase or add new file with HF_TOKEN=ton_token
49
+ with open(file_path, "w") as f:
50
+ f.write(f"HF_TOKEN={token}\n")
51
+ print(f"Token Hugging Face successfully saved in {file_path}")
52
  return {
53
+ "success": True,
54
+ "message": "Token saved."
55
+ }
56
+ except Exception as e:
57
+ return {
58
+ "success": False,
59
+ "message": "Error on server side"
60
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  @self.settings_app.post("/check_token")
63
  async def hf_token_available(self_instance: AmazonThisObject = Depends(self._get_self)):
64
+ file_path = Path(exec_dir) / ".env"
65
+ if not file_path.exists():
66
+ return {
67
+ "success": False,
68
+ "token": None
69
+ }
70
+ try:
71
+ # Read all the file
72
+ with open(file_path, "r", encoding="utf-8") as f:
73
+ lines = f.readlines()
74
+ # Find line HF_TOKEN=
75
+ for line in lines:
76
+ line = line.strip()
77
+ if line.startswith("HF_TOKEN="):
78
+ # On prend tout après le =
79
+ self_instance.amaz_token = line.split("=", 1)[1].strip().strip('"').strip("'")
80
+ break
81
+ if self_instance.amaz_token:
82
+ return {
83
+ "success": True,
84
+ "token": self_instance.amaz_token
85
+ }
86
+ else:
87
+ print("No HF_TOKEN found.")
88
+ return {
89
+ "success": False,
90
+ "token": None,
91
+ "message": "HF_TOKEN missing"
92
+ }
93
+ except Exception as e:
94
+ print(f"Errot while readind .env : {e}")
95
+ return {
96
+ "success": False,
97
+ "token": None,
98
+ "message": "Error whild reading file"
99
+ }
100
 
101
  #Take photo REST POST request
102
  @self.settings_app.post("/take_photo")
103
  def take_photo(self_instance: AmazonThisObject = Depends(self._get_self)):
104
+ nonlocal last_photo, last_photo_timestamp
105
+
106
+ # Part for the Simulator
107
+ # Initialize webcam (0 = default camera)
108
+ #cam = cv2.VideoCapture(0)
109
+ # Capture one frame with opencv on simulator [ need a webcam ] -> later replace by reachy robot camera
110
+ #ret, frame = cam.read()
111
+ #if not ret:
112
+ # return {"success": False, "error": "Camera not available"}
113
+
114
+ #Code for reachy camera
115
+ frame = reachy_mini.media.get_frame()
116
+ if frame is None:
117
+ return {"success": False, "error": "Camera not available"}
118
+
119
+ exec_dir = os.path.abspath(__file__)
120
+ exec_dir = exec_dir.replace("main.py","")
121
+
122
+ file_path = Path(exec_dir) / "sound/shutter.mp3"
123
+
124
+ reachy_mini.media.play_sound(file_path)
125
+
126
+ # Encode as JPEG
127
+ success, encoded = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 90])
128
+ if not success:
129
+ return {"success": False, "error": "Failed to encode image"}
130
+
131
+ #Save last photo
132
+ last_photo = encoded.tobytes()
133
+ last_photo_timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
134
+
135
+ # Return base64 for preview
136
+ b64_image = base64.b64encode(last_photo).decode('utf-8')
137
+ image_url = f"data:image/jpeg;base64,{b64_image}"
138
+
139
+ return {
140
+ "success": True,
141
+ "preview": f"data:image/jpeg;base64,{b64_image}",
142
+ "filename": f"reachy_photo_{last_photo_timestamp}.jpg",
143
+ }
144
 
145
  #Get link REST POST request
146
  @self.settings_app.post("/get_link")
147
  def get_link(self_instance: AmazonThisObject = Depends(self._get_self)):
148
+ #Restore image from save variable last_photo
149
+ b64_image = base64.b64encode(last_photo).decode('utf-8')
150
+ image_url = f"data:image/jpeg;base64,{b64_image}"
151
+
152
+ #Use model zai-org/GLM-4.5V with inference novita with huggingface client.
153
+ #Analyse image and get URL only -> bypass blabla from LLM by usin "no blabla".
154
+ hf_client = OpenAI(
155
+ base_url="https://router.huggingface.co/v1",
156
+ #define token in environment variable
157
+ api_key=self_instance.amaz_token
158
+ )
159
+ completion = hf_client.chat.completions.create(
160
+ model="zai-org/GLM-4.5V:novita",
161
+ messages=[
162
+ {
163
+ "role": "user",
164
+ "content":
165
+ [
 
 
 
 
 
 
 
166
  {
167
+ "type": "text",
168
+ "text": "Generate only a valid link to search object on amazon, no blabla."
169
+ },
170
+ {
171
+ "type": "image_url",
172
+ "image_url":
173
+ {
174
+ "url": image_url
175
+ }
176
  }
177
+ ]
178
+ }
179
+ ],
180
+ )
181
+ #Get link from result
182
+ url_link = completion.choices[0].message.content
183
+
184
+ #Clear content and keep only URL
185
+ url_link = url_link.replace("<|end_of_box|>", "")
186
+ url_link = url_link.replace("<|begin_of_box|>", "")
187
+
188
+ #Return success + url link
189
+ return {
190
+ "success": True,
191
+ "url": url_link
192
+ }
 
193
 
194
  # Keep the app running
195
  while not stop_event.is_set():