|
23 | 23 |
|
24 | 24 | def run_streaming_tts_quickstart():
|
25 | 25 | # [START tts_synthezise_streaming]
|
26 |
| - """Synthesizes speech from a stream of input text. |
27 |
| - """ |
| 26 | + """Synthesizes speech from a stream of input text.""" |
28 | 27 | from google.cloud import texttospeech
|
29 |
| - import itertools |
30 | 28 |
|
31 | 29 | client = texttospeech.TextToSpeechClient()
|
32 | 30 |
|
33 | 31 | # See https://cloud.google.com/text-to-speech/docs/voices for all voices.
|
34 |
| - streaming_config = texttospeech.StreamingSynthesizeConfig(voice=texttospeech.VoiceSelectionParams(name="en-US-Journey-D", language_code="en-US")) |
| 32 | + streaming_config = texttospeech.StreamingSynthesizeConfig( |
| 33 | + voice=texttospeech.VoiceSelectionParams( |
| 34 | + name="en-US-Chirp3-HD-Charon", |
| 35 | + language_code="en-US", |
| 36 | + ) |
| 37 | + ) |
35 | 38 |
|
36 | 39 | # Set the config for your stream. The first request must contain your config, and then each subsequent request must contain text.
|
37 |
| - config_request = texttospeech.StreamingSynthesizeRequest(streaming_config=streaming_config) |
| 40 | + config_request = texttospeech.StreamingSynthesizeRequest( |
| 41 | + streaming_config=streaming_config |
| 42 | + ) |
| 43 | + |
| 44 | + text_iterator = [ |
| 45 | + "Hello there. ", |
| 46 | + "How are you ", |
| 47 | + "today? It's ", |
| 48 | + "such nice weather outside.", |
| 49 | + ] |
38 | 50 |
|
39 | 51 | # Request generator. Consider using Gemini or another LLM with output streaming as a generator.
|
40 | 52 | def request_generator():
|
41 |
| - yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="Hello there. ")) |
42 |
| - yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="How are you ")) |
43 |
| - yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="today? It's ")) |
44 |
| - yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="such nice weather outside.")) |
| 53 | + yield config_request |
| 54 | + for text in text_iterator: |
| 55 | + yield texttospeech.StreamingSynthesizeRequest( |
| 56 | + input=texttospeech.StreamingSynthesisInput(text=text) |
| 57 | + ) |
| 58 | + |
| 59 | + streaming_responses = client.streaming_synthesize(request_generator()) |
45 | 60 |
|
46 |
| - streaming_responses = client.streaming_synthesize(itertools.chain([config_request], request_generator())) |
47 | 61 | for response in streaming_responses:
|
48 | 62 | print(f"Audio content size in bytes is: {len(response.audio_content)}")
|
49 | 63 | # [END tts_synthezise_streaming]
|
|
0 commit comments