FabienDanieau commited on
Commit
ed1bcc7
·
1 Parent(s): c582573

enhancement #12: display chatbox with image and tool calling

Browse files
src/reachy_mini_conversation_demo/camera_worker.py CHANGED
@@ -61,7 +61,12 @@ class CameraWorker:
61
  def get_latest_frame(self) -> Optional[np.ndarray]:
62
  """Get the latest frame (thread-safe)."""
63
  with self.frame_lock:
64
- return self.latest_frame.copy() if self.latest_frame is not None else None
 
 
 
 
 
65
 
66
  def get_face_tracking_offsets(
67
  self,
 
61
  def get_latest_frame(self) -> Optional[np.ndarray]:
62
  """Get the latest frame (thread-safe)."""
63
  with self.frame_lock:
64
+ if self.latest_frame is None:
65
+ return None
66
+ else:
67
+ frame = self.latest_frame.copy()
68
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
69
+ return frame
70
 
71
  def get_face_tracking_offsets(
72
  self,
src/reachy_mini_conversation_demo/main.py CHANGED
@@ -1,5 +1,7 @@
1
  from threading import Thread # noqa: D100
2
 
 
 
3
  from fastrtc import Stream
4
  from reachy_mini import ReachyMini
5
 
@@ -11,16 +13,23 @@ from reachy_mini_conversation_demo.utils import (
11
  AioTaskThread,
12
  handle_vision_stuff,
13
  parse_args,
14
- # setup_logger,
15
  )
16
 
17
 
 
 
 
 
 
 
18
  def main():
19
  """Entrypoint for the Reachy Mini conversation demo."""
20
  args = parse_args()
21
 
22
- # logger = setup_logger(args.debug)
23
-
 
24
  robot = ReachyMini()
25
 
26
  camera, camera_worker, head_tracker, vision_manager = handle_vision_stuff(
@@ -45,9 +54,20 @@ def main():
45
  head_wobbler=head_wobbler,
46
  )
47
 
 
 
48
  handler = OpenaiRealtimeHandler(deps)
49
- stream = Stream(handler=handler, mode="send-receive", modality="audio")
 
 
 
 
 
 
 
50
 
 
 
51
  # UI bloquante → thread standard
52
  ui_thread = Thread(target=stream.ui.launch, daemon=True)
53
  ui_thread.start()
 
1
  from threading import Thread # noqa: D100
2
 
3
+ import gradio as gr
4
+ from fastapi import FastAPI
5
  from fastrtc import Stream
6
  from reachy_mini import ReachyMini
7
 
 
13
  AioTaskThread,
14
  handle_vision_stuff,
15
  parse_args,
16
+ setup_logger,
17
  )
18
 
19
 
20
+ def update_chatbot(chatbot: list[dict], response: dict):
21
+ print("Updating chatbot with response:", response)
22
+ chatbot.append(response)
23
+ return chatbot
24
+
25
+
26
  def main():
27
  """Entrypoint for the Reachy Mini conversation demo."""
28
  args = parse_args()
29
 
30
+ logger = setup_logger(args.debug)
31
+ logger.info("Starting Reachy Mini Conversation Demo")
32
+
33
  robot = ReachyMini()
34
 
35
  camera, camera_worker, head_tracker, vision_manager = handle_vision_stuff(
 
54
  head_wobbler=head_wobbler,
55
  )
56
 
57
+ chatbot = gr.Chatbot(type="messages", resizable=True)
58
+
59
  handler = OpenaiRealtimeHandler(deps)
60
+ stream = Stream(
61
+ handler=handler,
62
+ mode="send-receive",
63
+ modality="audio",
64
+ additional_inputs=[chatbot],
65
+ additional_outputs=[chatbot],
66
+ additional_outputs_handler=update_chatbot,
67
+ )
68
 
69
+ app = FastAPI()
70
+ app = gr.mount_gradio_app(app, stream.ui, path="/")
71
  # UI bloquante → thread standard
72
  ui_thread = Thread(target=stream.ui.launch, daemon=True)
73
  ui_thread.start()
src/reachy_mini_conversation_demo/openai_realtime.py CHANGED
@@ -4,6 +4,7 @@ import json
4
  import logging
5
  from datetime import datetime
6
 
 
7
  import numpy as np
8
  from fastrtc import AdditionalOutputs, AsyncStreamHandler, wait_for_item
9
  from openai import AsyncOpenAI
@@ -51,10 +52,10 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
51
  "turn_detection": {
52
  "type": "server_vad",
53
  },
54
- # "input_audio_transcription": {
55
- # "model": "whisper-1",
56
- # "language": "en",
57
- # },
58
  "voice": "ballad",
59
  "instructions": "On parle en francais",
60
  "tools": ALL_TOOL_SPECS,
@@ -89,20 +90,22 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
89
  pass
90
  # self.deps.head_wobbler.reset()
91
 
92
- # if (
93
- # event.type
94
- # == "conversation.item.input_audio_transcription.completed"
95
- # ):
96
- # await self.output_queue.put(
97
- # AdditionalOutputs({"role": "user", "content": event.transcript})
98
- # )
99
-
100
- # if event.type == "response.audio_transcript.done":
101
- # await self.output_queue.put(
102
- # AdditionalOutputs(
103
- # {"role": "assistant", "content": event.transcript}
104
- # )
105
- # )
 
 
106
 
107
  if event.type == "response.audio.delta":
108
  self.deps.head_wobbler.feed(event.delta)
@@ -166,6 +169,16 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
166
  "output": json.dumps(tool_result),
167
  }
168
  )
 
 
 
 
 
 
 
 
 
 
169
  if tool_name == "camera":
170
  b64_im = json.dumps(tool_result["b64_im"])
171
  await self.connection.conversation.item.create(
@@ -180,6 +193,19 @@ class OpenaiRealtimeHandler(AsyncStreamHandler):
180
  ],
181
  }
182
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
  if not self.is_idle_tool_call:
185
  await self.connection.response.create(
 
4
  import logging
5
  from datetime import datetime
6
 
7
+ import gradio as gr
8
  import numpy as np
9
  from fastrtc import AdditionalOutputs, AsyncStreamHandler, wait_for_item
10
  from openai import AsyncOpenAI
 
52
  "turn_detection": {
53
  "type": "server_vad",
54
  },
55
+ "input_audio_transcription": {
56
+ "model": "whisper-1",
57
+ "language": "fr",
58
+ },
59
  "voice": "ballad",
60
  "instructions": "On parle en francais",
61
  "tools": ALL_TOOL_SPECS,
 
90
  pass
91
  # self.deps.head_wobbler.reset()
92
 
93
+ if (
94
+ event.type
95
+ == "conversation.item.input_audio_transcription.completed"
96
+ ):
97
+ logger.debug(f"user transcript: {event.transcript}")
98
+ await self.output_queue.put(
99
+ AdditionalOutputs({"role": "user", "content": event.transcript})
100
+ )
101
+
102
+ if event.type == "response.audio_transcript.done":
103
+ logger.debug(f"assistant transcript: {event.transcript}")
104
+ await self.output_queue.put(
105
+ AdditionalOutputs(
106
+ {"role": "assistant", "content": event.transcript}
107
+ )
108
+ )
109
 
110
  if event.type == "response.audio.delta":
111
  self.deps.head_wobbler.feed(event.delta)
 
169
  "output": json.dumps(tool_result),
170
  }
171
  )
172
+
173
+ await self.output_queue.put(
174
+ AdditionalOutputs(
175
+ {
176
+ "role": "assistant",
177
+ "content": "**Using tool: " + tool_name + "**",
178
+ }
179
+ )
180
+ )
181
+
182
  if tool_name == "camera":
183
  b64_im = json.dumps(tool_result["b64_im"])
184
  await self.connection.conversation.item.create(
 
193
  ],
194
  }
195
  )
196
+ logger.info("additional input camera")
197
+
198
+ np_img = self.deps.camera_worker.get_latest_frame()
199
+ img = gr.Image(value=np_img)
200
+
201
+ await self.output_queue.put(
202
+ AdditionalOutputs(
203
+ {
204
+ "role": "assistant",
205
+ "content": img,
206
+ }
207
+ )
208
+ )
209
 
210
  if not self.is_idle_tool_call:
211
  await self.connection.response.create(
src/reachy_mini_conversation_demo/utils.py CHANGED
@@ -38,13 +38,15 @@ def handle_vision_stuff(args, current_robot):
38
  head_tracker = None
39
  vision_manager = None
40
  if not args.no_camera:
 
 
41
  if not args.sim:
42
  camera = find_camera()
43
  else:
44
  import cv2
45
-
46
  camera = cv2.VideoCapture(0)
47
-
48
  if args.head_tracker is not None:
49
  if args.head_tracker == "yolo":
50
  from reachy_mini_conversation_demo.vision.yolo_head_tracker import (
@@ -120,6 +122,8 @@ def setup_logger(debug):
120
  logging.getLogger("aiortc").setLevel(logging.INFO)
121
  logging.getLogger("fastrtc").setLevel(logging.INFO)
122
  logging.getLogger("aioice").setLevel(logging.INFO)
 
 
123
  else:
124
  logging.getLogger("aiortc").setLevel(logging.ERROR)
125
  logging.getLogger("fastrtc").setLevel(logging.ERROR)
 
38
  head_tracker = None
39
  vision_manager = None
40
  if not args.no_camera:
41
+ camera = find_camera()
42
+ """
43
  if not args.sim:
44
  camera = find_camera()
45
  else:
46
  import cv2
47
+
48
  camera = cv2.VideoCapture(0)
49
+ """
50
  if args.head_tracker is not None:
51
  if args.head_tracker == "yolo":
52
  from reachy_mini_conversation_demo.vision.yolo_head_tracker import (
 
122
  logging.getLogger("aiortc").setLevel(logging.INFO)
123
  logging.getLogger("fastrtc").setLevel(logging.INFO)
124
  logging.getLogger("aioice").setLevel(logging.INFO)
125
+ logging.getLogger("openai").setLevel(logging.INFO)
126
+ logging.getLogger("websockets").setLevel(logging.INFO)
127
  else:
128
  logging.getLogger("aiortc").setLevel(logging.ERROR)
129
  logging.getLogger("fastrtc").setLevel(logging.ERROR)