File size: 10,169 Bytes
6c09f76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
## Setup

The gradio-webrtc install fails unless you have ffmpeg@6, on mac:

```
brew uninstall ffmpeg
brew install ffmpeg@6
brew link ffmpeg@6
```

Create a virtual python environment, then install the dependencies for this script:

```
pip install websockets numpy gradio-webrtc "gradio>=5.9.1"
```

If installation fails it may be

Before running this script, ensure the `GOOGLE_API_KEY` environment

```
$ export GOOGLE_API_KEY ='add your key here'
```

You can get an api-key from Google AI Studio (https://aistudio.google.com/apikey)

## Run

To run the script:

```
python gemini_gradio_audio.py
```

On the gradio page (http://127.0.0.1:7860/) click record, and talk, gemini will reply. But note that interruptions
don't work.

"""

import asyncio
import json
import os
from typing import Literal 
import base64

import gradio as gr
import numpy as np
from fastrtc import (
    AsyncStreamHandler,
    WebRTC,
    wait_for_item,
)
from jinja2 import Template
from google import genai
from google.genai.types import LiveConnectConfig, Tool, FunctionDeclaration

from google.cloud import texttospeech

from tools import FUNCTION_MAP, TOOLS

with open("questions.json", "r") as f:
    questions_dict = json.load(f)

with open("src/prompts/default_prompt.jinja2") as f:
    template_str = f.read()
    template = Template(template_str)
    system_prompt = template.render(questions=json.dumps(questions_dict, indent=4))




class TTSConfig:
    def __init__(self):
        self.client = texttospeech.TextToSpeechClient()
        self.voice = texttospeech.VoiceSelectionParams(
            name="en-US-Chirp3-HD-Charon",
            language_code="en-US"
        )
        self.audio_config = texttospeech.AudioConfig(
            audio_encoding=texttospeech.AudioEncoding.LINEAR16
        )


class AsyncGeminiHandler(AsyncStreamHandler):
    """Simple Async Gemini Handler"""

    def __init__(
            self,
            expected_layout: Literal["mono"] = "mono",
            output_sample_rate: int = 24000,
            output_frame_size: int = 480,
        ) -> None:
        super().__init__(
            expected_layout,
            output_sample_rate,
            output_frame_size,
            input_sample_rate=16000,
        )
        self.input_queue: asyncio.Queue = asyncio.Queue()
        self.output_queue: asyncio.Queue = asyncio.Queue()
        self.text_queue: asyncio.Queue = asyncio.Queue()
        self.quit: asyncio.Event = asyncio.Event()
        self.chunk_size = 1024

        self.tts_config: TTSConfig | None = TTSConfig()
        self.text_buffer = ""
    
    def copy(self) -> "AsyncGeminiHandler":
        return AsyncGeminiHandler(
            expected_layout="mono",
            output_sample_rate=self.output_sample_rate,
            output_frame_size=self.output_frame_size,
        )

    def _encode_audio(self, data: np.ndarray) -> str:
        """Encode Audio data to send to the server"""
        return base64.b64encode(data.tobytes()).decode("UTF-8")
    

    async def receive(self, frame: tuple[int, np.ndarray]) -> None:
        _, array = frame
        array = array.squeeze()
        audio_message = self._encode_audio(array)
        self.input_queue.put_nowait(audio_message)

    async def emit(self) -> tuple[int, np.ndarray] | None:
        return await wait_for_item(self.output_queue)

    async def start_up(self) -> None:
        client = genai.Client(
            api_key=os.getenv("GOOGLE_API_KEY"),
            http_options={"api_version": "v1alpha"},
        )

        
        config = LiveConnectConfig(
            system_instruction={
                        "parts": [{"text": system_prompt}],
                        "role": "user",
                    },
            tools=[Tool(function_declarations=[FunctionDeclaration(**tool) for tool in TOOLS])],
            response_modalities=["AUDIO"],
        )

        async with (
            client.aio.live.connect(model="gemini-2.0-flash-exp", config=config) as session,
            asyncio.TaskGroup() as tg
        ):
            self.session = session

            tasks = [
                tg.create_task(self.process()),
                tg.create_task(self.send_realtime()),
                tg.create_task(self.tts()),
            ]
    
    async def process(self) -> None:
        while True:
            try:
                turn = self.session.receive()
                async for response in turn:
                    if data := response.data:
                        array = np.frombuffer(data, dtype=np.int16)
                        self.output_queue.put_nowait((self.output_sample_rate, array))
                        continue

                    if text := response.text:
                        print(f"Received text: {text}")
                        self.text_buffer += text

                    if response.tool_call is not None:
                        for tool in response.tool_call.function_calls:
                            tool_response = FUNCTION_MAP[tool.name](**tool.args)
                            print(f"Calling tool: {tool.name}")
                            print(f"Tool response: {tool_response}")
                            await self.session.send(
                                input=tool_response, end_of_turn=True
                            )
                            await asyncio.sleep(0.1)

                    if sc := response.server_content:
                        if sc.turn_complete and self.text_buffer:
                            self.text_queue.put_nowait(self.text_buffer)
                            FUNCTION_MAP["store_input"](
                                role="bot",
                                input=self.text_buffer
                            )
                            self.text_buffer = ""

            except Exception as e:
                print(f"Error in processing: {e}")
                await asyncio.sleep(0.1)

    async def send_realtime(self) -> None:
        """Send real-time audio data to model."""
        while True:
            try:
                data = await self.input_queue.get()
                msg = {"data": data, "mime_type": "audio/pcm"}
                await self.session.send(input=msg)
            except Exception as e:
                print(f"Error in real-time sending: {e}")
                await asyncio.sleep(0.1) 

    async def tts(self) -> None:
            
        while True:
            try:
                text = await self.text_queue.get()
                # Get response in a single request
                if text: 
                    response = self.tts_config.client.synthesize_speech(
                        input=texttospeech.SynthesisInput(text=text),
                        voice=self.tts_config.voice,
                        audio_config=self.tts_config.audio_config
                    )
                    array = np.frombuffer(response.audio_content, dtype=np.int16)
                    self.output_queue.put_nowait((self.output_sample_rate, array))
                
            except Exception as e:
                print(f"Error in TTS: {e}")
                await asyncio.sleep(0.1)


    def shutdown(self) -> None:
        self.quit.set()


def reload_json(path):
    with open(path, "r") as f:
        return json.load(f)

# Main Gradio Interface
def registry(name: str, token: str | None = None, **kwargs):
    """Sets up and returns the Gradio interface."""

    interface = gr.Blocks()
    with interface:
        with gr.Tabs():
            with gr.TabItem("Voice Chat"):
                gr.HTML(
                    """
                    <div style='text-align: left'>
                        <h1>ML6 Voice Demo - Function Calling and Custom Output Voice</h1>
                    </div>
                    """
                )
                gemini_handler = AsyncGeminiHandler()

                with gr.Row():
                    audio = WebRTC(
                        label="Voice Chat", modality="audio", mode="send-receive"
                    )

                # Add display components for questions and answers
                with gr.Row():
                    with gr.Column():
                        gr.JSON(
                            label="Questions",
                            value=questions_dict,
                        )
                    # with gr.Column():
                    #     gr.JSON(reload_json, inputs=gr.Text(value="/Users/georgeslorre/ML6/internal/gemini-voice-agents/conversation.json", visible=False), label="Conversation", every=1)
                    with gr.Column():
                        gr.JSON(reload_json, inputs=gr.Text(value="/Users/georgeslorre/ML6/internal/gemini-voice-agents/answers.json", visible=False),label="Collected Answers", every=1)
                                               

                audio.stream(
                    gemini_handler,
                    inputs=[audio],  # Add audio_file to inputs
                    outputs=[audio],
                    time_limit=600,
                    concurrency_limit=10,
                )

    return interface

# Function to clear JSON files
def clear_json_files():
    with open("/Users/georgeslorre/ML6/internal/gemini-voice-agents/conversation.json", "w") as f:
        json.dump([], f)
    with open("/Users/georgeslorre/ML6/internal/gemini-voice-agents/answers.json", "w") as f:
        json.dump({}, f)

# Clear files before launching
clear_json_files()

# Launch the Gradio interface
gr.load(
    name="gemini-2.0-flash-exp",
    src=registry,
).launch()