File size: 8,088 Bytes
1be5829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed12c40
1be5829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import base64
import os
from pathlib import Path
from typing import AsyncGenerator

import librosa
import numpy as np
from dotenv import load_dotenv
from fastrtc import (
    AsyncStreamHandler,
    Stream,
    get_tts_model,
    wait_for_item,
)
from fastrtc.utils import audio_to_int16
from google import genai
from google.genai.types import (
    Content,
    LiveConnectConfig,
    Part,
    PrebuiltVoiceConfig,
    SpeechConfig,
    VoiceConfig,
)

load_dotenv()

cur_dir = Path(__file__).parent

SAMPLE_RATE = 24000

tts_model = get_tts_model()


class GeminiHandler(AsyncStreamHandler):
    """Handler for the Gemini API"""

    def __init__(
        self,
    ) -> None:
        super().__init__(
            expected_layout="mono",
            output_sample_rate=24000,
            output_frame_size=480,
            input_sample_rate=24000,
        )
        self.input_queue: asyncio.Queue = asyncio.Queue()
        self.output_queue: asyncio.Queue = asyncio.Queue()
        self.quit: asyncio.Event = asyncio.Event()

    def copy(self) -> "GeminiHandler":
        return GeminiHandler()

    async def start_up(self):
        voice_name = "Charon"
        client = genai.Client(
            api_key=os.getenv("GEMINI_API_KEY"),
            http_options={"api_version": "v1alpha"},
        )

        config = LiveConnectConfig(
            response_modalities=["AUDIO"],  # type: ignore
            speech_config=SpeechConfig(
                voice_config=VoiceConfig(
                    prebuilt_voice_config=PrebuiltVoiceConfig(
                        voice_name=voice_name,
                    )
                )
            ),
            system_instruction=Content(
                parts=[Part(text="You are a helpful assistant.")],
                role="system",
            ),
        )
        async with client.aio.live.connect(
            model="gemini-2.0-flash-exp", config=config
        ) as session:
            async for audio in session.start_stream(
                stream=self.stream(), mime_type="audio/pcm"
            ):
                if audio.data:
                    array = np.frombuffer(audio.data, dtype=np.int16)
                    self.output_queue.put_nowait((self.output_sample_rate, array))

    async def stream(self) -> AsyncGenerator[bytes, None]:
        while not self.quit.is_set():
            try:
                audio = await asyncio.wait_for(self.input_queue.get(), 0.1)
                yield audio
            except (asyncio.TimeoutError, TimeoutError):
                pass

    async def receive(self, frame: tuple[int, np.ndarray]) -> None:
        _, array = frame
        array = array.squeeze()
        audio_message = base64.b64encode(array.tobytes()).decode("UTF-8")
        self.input_queue.put_nowait(audio_message)

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

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


class GeminiHandler2(GeminiHandler):
    async def start_up(self):
        starting_message = tts_model.tts("Can you help me make an omelette?")
        starting_message = librosa.resample(
            starting_message[1],
            orig_sr=starting_message[0],
            target_sr=self.output_sample_rate,
        )
        starting_message = audio_to_int16((self.output_sample_rate, starting_message))
        await self.output_queue.put((self.output_sample_rate, starting_message))
        voice_name = "Puck"
        client = genai.Client(
            api_key=os.getenv("GEMINI_API_KEY"),
            http_options={"api_version": "v1alpha"},
        )

        config = LiveConnectConfig(
            response_modalities=["AUDIO"],  # type: ignore
            speech_config=SpeechConfig(
                voice_config=VoiceConfig(
                    prebuilt_voice_config=PrebuiltVoiceConfig(
                        voice_name=voice_name,
                    )
                )
            ),
            system_instruction=Content(
                parts=[
                    Part(
                        text="You are a cooking student who wants to learn how to make an omelette."
                    ),
                    Part(
                        text="You are currently in the kitchen with a teacher who is helping you make an omelette."
                    ),
                    Part(
                        text="Please wait for the teacher to tell you what to do next. Follow the teacher's instructions carefully."
                    ),
                ],
                role="system",
            ),
        )
        async with client.aio.live.connect(
            model="gemini-2.0-flash-exp", config=config
        ) as session:
            async for audio in session.start_stream(
                stream=self.stream(), mime_type="audio/pcm"
            ):
                if audio.data:
                    array = np.frombuffer(audio.data, dtype=np.int16)
                    self.output_queue.put_nowait((self.output_sample_rate, array))

    def copy(self) -> "GeminiHandler2":
        return GeminiHandler2()


gemini_stream = Stream(
    GeminiHandler(),
    modality="audio",
    mode="send-receive",
    ui_args={
        "title": "Gemini Teacher",
        "icon": "https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png",
        "pulse_color": "rgb(74, 138, 213)",
        "icon_button_color": "rgb(255, 255, 255)",
    },
)

gemini_stream_2 = Stream(
    GeminiHandler2(),
    modality="audio",
    mode="send-receive",
    ui_args={
        "title": "Gemini Student",
        "icon": "https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png",
        "pulse_color": "rgb(132, 112, 196)",
        "icon_button_color": "rgb(255, 255, 255)",
    },
)

if __name__ == "__main__":
    from gradio.utils import get_space
    import gradio as gr

    if get_space():
        with gr.Blocks() as demo:
            gr.HTML(
                """
                <div style="display: flex; justify-content: center; align-items: center;">
                <h1>Gemini Conversation</h1>                
                </div>
                """
            )
            gr.Markdown(
                """# How to run this demo
                
                - Clone the repo - top right of the page click the vertical three dots and select "Clone repository"
                - Open the repo in a terminal and install the dependencies
                - Get a gemini API key [here](https://ai.google.dev/gemini-api/docs/api-key)
                - Create a `.env` file in the root of the repo and add the following:
                ```
                GEMINI_API_KEY=<your_gemini_api_key>
                ```
                - Run the app with `python app.py`
                - This will print the two URLs of the agents running locally
                - Use ngrok to exponse one agent to the internet. This is so that you can acces it from your phone
                - Use the ngrok URL to access the agent from your phone
                - Now, start the "teacher gemini" agent first. Then, start the "student gemini" agent. The student gemini will start talking to the teacher gemini. And the teacher gemini will respond!

                Important:
                - Make sure the audio sources are not too close to each other or too loud. Sometimes that causes them to talk over each other..
                - Feel free to modify the `system_instruction` to change the behavior of the agents.
                - You can also modify the `voice_name` to change the voice of the agents.
                - Have fun!
                """
            )
        demo.launch()

    import time

    _ = gemini_stream.ui.launch(server_port=7860, prevent_thread_lock=True)
    _ = gemini_stream_2.ui.launch(server_port=7861, prevent_thread_lock=True)
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        gemini_stream.ui.close()
        gemini_stream_2.ui.close()