-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain_mac.py
More file actions
394 lines (327 loc) · 15.4 KB
/
Copy pathmain_mac.py
File metadata and controls
394 lines (327 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os, asyncio, base64, io, traceback, json
from pynput import keyboard as pynput_keyboard
from dotenv import load_dotenv
import cv2, pyaudio, PIL.Image, mss, argparse
from google import genai
from google.genai import types
from tools import get_tool_declarations, function_map
load_dotenv() # Added to load .env file
FORMAT = pyaudio.paInt16
CHANNELS = 1
SEND_SAMPLE_RATE = 16000
RECEIVE_SAMPLE_RATE = 24000
CHUNK_SIZE = 1024
MODEL = "models/gemini-2.5-flash-preview-native-audio-dialog"
# MODEL = "models/gemini-2.0-flash-live-001"
DEFAULT_MODE = "none"
client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
)
# For LiveConnectConfig, tools need to be a list of dictionaries with function_declarations inside
# We can combine custom function declarations with built-in tools like Google Search
tools=[
{"function_declarations": get_tool_declarations()}, # Your custom functions
{"google_search": types.GoogleSearch()} # Built-in Google Search tool
]
CONFIG = types.LiveConnectConfig(
response_modalities=[
"AUDIO",
],
media_resolution="MEDIA_RESOLUTION_MEDIUM",
speech_config=types.SpeechConfig(
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name="Kore")
)
),
context_window_compression=types.ContextWindowCompressionConfig(
trigger_tokens=25600,
sliding_window=types.SlidingWindow(target_tokens=12800),
),
system_instruction=types.Content(
parts=[types.Part.from_text(text=os.environ.get("PERSONALIZED_PROMPT", "You are a helpful assistant.") + """
You have access to the following tools:
1. get_reminders: Gets the user's saved reminders from the reminders.json file
2. set_reminder: Saves a new reminder with optional reminder time (e.g., 'tomorrow at 3pm')
3. manage_reminder: Manages existing reminders - can edit or delete specific reminders or delete all reminders
4. get_secret_key: Gets the user's secret key (it's not actually a secret key, it's just a test for function calling)
5. get calendar events: Gets the user's calendar events
6. Control Home:
6.1 control home entity: Controls a home entity (e.g., turn on a light)
6.2 control home climate: Controls a home climate (e.g., set the temperature)
6.3 get home entities in room: Gets the entities in a specific room
6.4 find home entities by name: Finds entities by name
7. format_linkedin_post: Generates a LinkedIn post from provided context. The function will automatically extract a topic from the context and create a professionally formatted LinkedIn post in a viral, engaging style. Always ensure you search the latest news from the web before giving the context to the linkedin formatter.
You also have access to Google Search to find information online.
Don't mention your origins or google.
""")],
role="user"
),
tools=tools,
realtime_input_config=types.RealtimeInputConfig(
automatic_activity_detection=types.AutomaticActivityDetection(disabled=True)
)
)
pya = pyaudio.PyAudio()
class AudioLoop:
def __init__(self, video_mode=DEFAULT_MODE):
self.video_mode = video_mode
self.audio_in_queue = None
self.out_queue = None # Used for video/screen frames
self.session = None
self.send_text_task = None
self.receive_audio_task = None
self.play_audio_task = None
self.is_recording = False # Added for push-to-talk
self.main_event_loop = None # Modified for keyboard listener, will be set in run()
async def toggle_recording(self): # Added for push-to-talk
self.is_recording = not self.is_recording
if self.is_recording:
print("\n🎤 Recording started... (Press 't' to stop)")
if self.session:
await self.session.send_realtime_input(activity_start=types.ActivityStart())
else:
print("\n🛑 Recording stopped. (Press 't' to start)")
if self.session:
await self.session.send_realtime_input(activity_end=types.ActivityEnd())
async def handle_function_call(self, response_text, tool_call):
if tool_call and hasattr(tool_call, 'function_calls') and tool_call.function_calls:
function_responses = []
for fc in tool_call.function_calls:
print(f"\n🔧 Function call detected: {fc.name}")
# Get the actual function implementation from our map
if fc.name in function_map:
# Get the function to execute
func = function_map[fc.name]
# Parse the arguments if any
args = {}
if hasattr(fc, 'args') and fc.args:
args = fc.args
# Execute the function
try:
result = func(**args)
print(f"Function result: {result}")
# Create a function response
function_response = types.FunctionResponse(
id=fc.id, # Important: Include the ID from the function call
name=fc.name,
response=result
)
function_responses.append(function_response)
except Exception as e:
print(f"Error executing function: {e}")
else:
print(f"Unknown function: {fc.name}")
# Send all function responses back to the model
if function_responses:
try:
await self.session.send_tool_response(function_responses=function_responses)
return True
except Exception as e:
print(f"Error sending function responses: {e}")
return False
def _on_press(self, key): # Changed for pynput
try:
if key == pynput_keyboard.KeyCode.from_char('t'):
asyncio.run_coroutine_threadsafe(self.toggle_recording(), self.main_event_loop)
except AttributeError:
# Special keys (like shift, alt, etc.) don't have a char attribute
pass
except Exception as e:
print(f"Error in _on_press: {e}")
def _blocking_listen_for_toggle_key(self): # Changed for pynput
# pynput listener runs in its own thread.
# The listener will automatically stop if this function's thread is stopped or if an error occurs.
with pynput_keyboard.Listener(on_press=self._on_press) as listener:
try:
print("Push-to-talk enabled (using pynput). Press 't' to toggle recording.")
listener.join() # This blocks until the listener stops
except Exception as e:
print(f"Pynput listener error: {e}")
finally:
print("Pynput listener stopped.")
async def handle_keyboard_input(self): # Changed for pynput
# Run the blocking pynput listener in a separate thread
# The listener itself manages its own thread for event listening.
await self.main_event_loop.run_in_executor(None, self._blocking_listen_for_toggle_key)
async def send_text(self):
while True:
text = await asyncio.to_thread(
input,
"message > ",
)
if text.lower() == "q":
break
await self.session.send(input=text or ".", end_of_turn=True)
def _get_frame(self, cap):
# Read the frameq
ret, frame = cap.read()
# Check if the frame was read successfully
if not ret:
return None
# Fix: Convert BGR to RGB color space
# OpenCV captures in BGR but PIL expects RGB format
# This prevents the blue tint in the video feed
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = PIL.Image.fromarray(frame_rgb) # Now using RGB frame
img.thumbnail([1024, 1024])
image_io = io.BytesIO()
img.save(image_io, format="jpeg")
image_io.seek(0)
mime_type = "image/jpeg"
image_bytes = image_io.read()
return {"mime_type": mime_type, "data": base64.b64encode(image_bytes).decode()}
async def get_frames(self):
# This takes about a second, and will block the whole program
# causing the audio pipeline to overflow if you don't to_thread it.
cap = await asyncio.to_thread(
cv2.VideoCapture, 0
) # 0 represents the default camera
while True:
frame = await asyncio.to_thread(self._get_frame, cap)
if frame is None:
break
await asyncio.sleep(1.0)
await self.out_queue.put(frame)
# Release the VideoCapture object
cap.release()
def _get_screen(self):
sct = mss.mss()
monitor = sct.monitors[0]
i = sct.grab(monitor)
mime_type = "image/jpeg"
image_bytes = mss.tools.to_png(i.rgb, i.size)
img = PIL.Image.open(io.BytesIO(image_bytes))
image_io = io.BytesIO()
img.save(image_io, format="jpeg")
image_io.seek(0)
image_bytes = image_io.read()
return {"mime_type": mime_type, "data": base64.b64encode(image_bytes).decode()}
async def get_screen(self):
while True:
frame = await asyncio.to_thread(self._get_screen)
if frame is None:
break
await asyncio.sleep(1.0)
await self.out_queue.put(frame)
async def send_realtime(self):
while True:
msg = await self.out_queue.get()
await self.session.send(input=msg)
async def listen_audio(self):
mic_info = pya.get_default_input_device_info()
self.audio_stream = await asyncio.to_thread(
pya.open,
format=FORMAT,
channels=CHANNELS,
rate=SEND_SAMPLE_RATE,
input=True,
input_device_index=mic_info["index"],
frames_per_buffer=CHUNK_SIZE,
)
if __debug__:
kwargs = {"exception_on_overflow": False}
else:
kwargs = {}
while True:
if self.is_recording: # Modified for push-to-talk
try:
data = await asyncio.to_thread(self.audio_stream.read, CHUNK_SIZE, **kwargs)
if self.session:
# Send audio data using send_realtime_input as per docs for manual VAD
await self.session.send_realtime_input(
audio=types.Blob(data=data, mime_type=f"audio/pcm;rate={SEND_SAMPLE_RATE}")
)
except pyaudio.paInputOverflowed:
if __debug__:
print("Input overflowed. Skipping frame.")
continue # Skip this frame and continue
except Exception as e:
print(f"Error reading audio stream: {e}")
await asyncio.sleep(0.1) # Avoid tight loop on continuous error
else:
# Sleep briefly when not recording to avoid busy-waiting
await asyncio.sleep(0.01)
async def receive_audio(self):
"Background task to reads from the websocket and write pcm chunks to the output queue"
# Initialize a list to store AI responses if it doesn't exist
if not hasattr(self, 'ai_responses'):
self.ai_responses = []
while True:
turn = self.session.receive()
current_text = ""
async for chunk in turn:
# Handle audio data
if hasattr(chunk, 'data') and chunk.data:
self.audio_in_queue.put_nowait(chunk.data)
continue
# Handle text responses from server content
if hasattr(chunk, 'server_content') and chunk.server_content:
if hasattr(chunk, 'text') and chunk.text is not None:
current_text += chunk.text
# Store the response
self.ai_responses.append(chunk.text)
# Print with AI: prefix for clarity
print(f"AI: {chunk.text}", end="")
# Check for tool calls
if hasattr(chunk, 'tool_call') and chunk.tool_call:
print(f"\nDetected tool call")
await self.handle_function_call(current_text, chunk.tool_call)
# If you interrupt the model, it sends a turn_complete.
# For interruptions to work, we need to stop playback.
# So empty out the audio queue because it may have loaded
# much more audio than has played yet.
while not self.audio_in_queue.empty():
self.audio_in_queue.get_nowait()
async def play_audio(self):
stream = await asyncio.to_thread(
pya.open,
format=FORMAT,
channels=CHANNELS,
rate=RECEIVE_SAMPLE_RATE,
output=True,
)
while True:
bytestream = await self.audio_in_queue.get()
await asyncio.to_thread(stream.write, bytestream)
async def run(self):
try:
async with (
client.aio.live.connect(model=MODEL, config=CONFIG) as session,
asyncio.TaskGroup() as tg,
):
self.session = session
self.main_event_loop = asyncio.get_running_loop() # Set event loop here
self.audio_in_queue = asyncio.Queue()
self.out_queue = asyncio.Queue(maxsize=5) # For video/screen frames
send_text_task = tg.create_task(self.send_text())
tg.create_task(self.send_realtime()) # For video/screen frames
tg.create_task(self.listen_audio()) # Now handles its own sending for audio
tg.create_task(self.handle_keyboard_input()) # Added for push-to-talk
if self.video_mode == "camera":
tg.create_task(self.get_frames())
elif self.video_mode == "screen":
tg.create_task(self.get_screen())
tg.create_task(self.receive_audio())
tg.create_task(self.play_audio())
await send_text_task
raise asyncio.CancelledError("User requested exit")
except asyncio.CancelledError:
pass
except ExceptionGroup as EG:
self.audio_stream.close()
traceback.print_exception(EG)
if __name__ == "__main__":
print("🤖Starting AI Assistant...")
print("ℹ️ Press 't' to toggle audio recording for voice input.")
print("ℹ️ Type 'q' and press Enter in the 'message >' prompt to quit.")
parser = argparse.ArgumentParser()
parser.add_argument(
"--mode",
type=str,
default=DEFAULT_MODE,
help="pixels to stream from",
choices=["camera", "screen", "none"],
)
args = parser.parse_args()
main = AudioLoop(video_mode=args.mode)
asyncio.run(main.run())