File size: 10,743 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
import asyncio
import base64
import json
import os
from typing import Literal

import gradio as gr
import numpy as np
from fastrtc import AsyncStreamHandler, WebRTC, wait_for_item
from google import genai
from google.cloud import texttospeech
from google.genai.types import FunctionDeclaration, LiveConnectConfig, Tool

import helpers.datastore as datastore
from helpers.prompts import load_prompt
from tools import FUNCTION_MAP, TOOLS

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


datastore.DATA_STORE["questions"] = questions_dict

SYSTEM_PROMPT = load_prompt(
    "src/prompts/default_prompt.jinja2", questions=questions_dict
)


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:
        """Receives and processes audio frames asynchronously."""
        _, 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:
        """Asynchronously emits items from the output queue."""
        return await wait_for_item(self.output_queue)

    async def start_up(self) -> None:
        """Initialize and start the voice agent application.

        This asynchronous method sets up the Gemini API client, configures the live connection,
        and starts three concurrent tasks for receiving, processing and sending information.

        Returns:
            None

        Raises:
            ValueError: If GEMINI_API_KEY is not provided when required.

        """
        if not os.getenv("GOOGLE_GENAI_USE_VERTEXAI") == "True":
            api_key = os.getenv("GEMINI_API_KEY")
            if not api_key:
                raise ValueError("API Key is required")

            client = genai.Client(
                api_key=api_key,
                http_options={"api_version": "v1alpha"},
            )
        else:
            client = genai.Client(http_options={"api_version": "v1beta1"})

        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,  # setup the live connection session (websocket)
            asyncio.TaskGroup() as tg,  # create a task group to run multiple tasks concurrently
        ):
            self.session = session

            # these tasks will run concurrently and continuously
            [
                tg.create_task(self.process()),
                tg.create_task(self.send_realtime()),
                tg.create_task(self.tts()),
            ]

    async def process(self) -> None:
        """Process responses from the session in a continuous loop.

        This asynchronous method handles different types of responses from the session:
        - Audio data: Processes and queues audio data with the specified sample rate
        - Text data: Accumulates received text in a buffer
        - Tool calls: Executes registered functions and sends their responses back
        - Server content: Handles turn completion and stores conversation history

        The method runs indefinitely until interrupted, handling any exceptions that occur
        during processing by logging them and continuing after a brief delay.

        Returns:
            None

        Raises:
            Exception: Any exceptions during processing are caught and logged
        """
        while True:
            try:
                turn = self.session.receive()
                async for response in turn:
                    if data := response.data:
                        # audio data
                        array = np.frombuffer(data, dtype=np.int16)
                        self.output_queue.put_nowait((self.output_sample_rate, array))
                        continue

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

                    if response.tool_call is not None:
                        # function calling
                        for tool in response.tool_call.function_calls:
                            try:
                                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)
                            except Exception as e:
                                print(f"Error in tool call: {e}")
                                await asyncio.sleep(0.1)

                    if sc := response.server_content:
                        # check if bot's turn is complete
                        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.

        This method continuously reads audio data from an input queue and sends it to a model
        session in real-time. It runs in an infinite loop until interrupted.

        The audio data is sent with mime type 'audio/pcm'. If an error occurs during sending,
        it will be printed and the method will sleep briefly before retrying.

        Returns:
            None

        Raises:
            Exception: Any exceptions during queue access or session sending will be caught and logged.
        """
        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()


# Main Gradio Interface
def registry(*args, **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</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=datastore.DATA_STORE["questions"],
                        )
                    with gr.Column():
                        gr.JSON(
                            label="Answers",
                            value=lambda: datastore.DATA_STORE["answers"],
                            every=1,
                        )

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

    return interface


# Launch the Gradio interface
gr.load(
    name="demo",
    src=registry,
).launch()