Skip to content

Commit 1c006fd

Browse files
authored
chore: refactored samples, to make them follow copy-paste-run approach (#12534)
1 parent a4525c6 commit 1c006fd

8 files changed

+49
-55
lines changed

texttospeech/snippets/audio_profile.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727

2828
# [START tts_synthesize_text_audio_profile]
2929
# [START tts_synthesize_text_audio_profile_beta]
30-
def synthesize_text_with_audio_profile(text, output, effects_profile_id):
30+
def synthesize_text_with_audio_profile():
3131
"""Synthesizes speech from the input string of text."""
3232
from google.cloud import texttospeech
3333

34+
text = "hello"
35+
output = "output.mp3"
36+
effects_profile_id = "telephony-class-application"
3437
client = texttospeech.TextToSpeechClient()
3538

3639
input_text = texttospeech.SynthesisInput(text=text)

texttospeech/snippets/audio_profile_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818

1919
import audio_profile
2020

21-
TEXT = "hello"
2221
OUTPUT = "output.mp3"
23-
EFFECTS_PROFILE_ID = "telephony-class-application"
2422

2523

2624
def test_audio_profile(capsys):
2725
if os.path.exists(OUTPUT):
2826
os.remove(OUTPUT)
2927
assert not os.path.exists(OUTPUT)
30-
audio_profile.synthesize_text_with_audio_profile(TEXT, OUTPUT, EFFECTS_PROFILE_ID)
28+
audio_profile.synthesize_text_with_audio_profile()
3129
out, err = capsys.readouterr()
3230

3331
assert ('Audio content written to file "%s"' % OUTPUT) in out

texttospeech/snippets/long_audio_quickstart.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
from google.cloud import texttospeech
1616

1717

18-
def synthesize_long_audio(project_id, location, output_gcs_uri):
18+
def synthesize_long_audio(project_id: str, output_gcs_uri: str) -> None:
1919
"""
2020
Synthesizes long input, writing the resulting audio to `output_gcs_uri`.
2121
22-
Example usage: synthesize_long_audio('12345', 'us-central1', 'gs://{BUCKET_NAME}/{OUTPUT_FILE_NAME}.wav')
23-
22+
Args:
23+
project_id: ID or number of the Google Cloud project you want to use.
24+
output_gcs_uri: Specifies a Cloud Storage URI for the synthesis results.
25+
Must be specified in the format:
26+
``gs://bucket_name/object_name``, and the bucket must
27+
already exist.
2428
"""
25-
# TODO(developer): Uncomment and set the following variables
26-
# project_id = 'YOUR_PROJECT_ID'
27-
# location = 'YOUR_LOCATION'
28-
# output_gcs_uri = 'YOUR_OUTPUT_GCS_URI'
2929

3030
client = texttospeech.TextToSpeechLongAudioSynthesizeClient()
3131

@@ -41,7 +41,7 @@ def synthesize_long_audio(project_id, location, output_gcs_uri):
4141
language_code="en-US", name="en-US-Standard-A"
4242
)
4343

44-
parent = f"projects/{project_id}/locations/{location}"
44+
parent = f"projects/{project_id}/locations/us-central1"
4545

4646
request = texttospeech.SynthesizeLongAudioRequest(
4747
parent=parent,

texttospeech/snippets/long_audio_quickstart_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import uuid
1716

17+
import google.auth
1818
from google.cloud import storage
1919
import pytest
2020

2121
from long_audio_quickstart import synthesize_long_audio
2222

2323

24-
PROJECT_NUMBER = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"]
25-
26-
2724
@pytest.fixture(scope="module")
2825
def test_bucket():
2926
"""Yields a bucket that is deleted after the test completes."""
@@ -39,6 +36,7 @@ def test_bucket():
3936
def test_synthesize_long_audio(capsys, test_bucket):
4037
file_name = "fake_file.wav"
4138
output_gcs_uri = f"gs://{test_bucket.name}/{file_name}"
42-
synthesize_long_audio(str(PROJECT_NUMBER), "us-central1", output_gcs_uri)
39+
_, project_id = google.auth.default()
40+
synthesize_long_audio(project_id, output_gcs_uri)
4341
out, _ = capsys.readouterr()
4442
assert "Finished processing, check your GCS bucket to find your audio file!" in out

texttospeech/snippets/ssml_addresses.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@
2525

2626

2727
# [START tts_ssml_address_audio]
28-
def ssml_to_audio(ssml_text, outfile):
29-
# Generates SSML text from plaintext.
30-
#
31-
# Given a string of SSML text and an output file name, this function
32-
# calls the Text-to-Speech API. The API returns a synthetic audio
33-
# version of the text, formatted according to the SSML commands. This
34-
# function saves the synthetic audio to the designated output file.
35-
#
36-
# Args:
37-
# ssml_text: string of SSML text
38-
# outfile: string name of file under which to save audio output
39-
#
40-
# Returns:
41-
# nothing
28+
def ssml_to_audio(ssml_text: str) -> None:
29+
"""
30+
Generates SSML text from plaintext.
31+
Given a string of SSML text and an output file name, this function
32+
calls the Text-to-Speech API. The API returns a synthetic audio
33+
version of the text, formatted according to the SSML commands. This
34+
function saves the synthetic audio to the designated output file.
35+
36+
Args:
37+
ssml_text: string of SSML text
38+
"""
4239

4340
# Instantiates a client
4441
client = texttospeech.TextToSpeechClient()
@@ -64,26 +61,26 @@ def ssml_to_audio(ssml_text, outfile):
6461
)
6562

6663
# Writes the synthetic audio to the output file.
67-
with open(outfile, "wb") as out:
64+
with open("test_example.mp3", "wb") as out:
6865
out.write(response.audio_content)
69-
print("Audio content written to file " + outfile)
66+
print("Audio content written to file " + "test_example.mp3")
7067
# [END tts_ssml_address_audio]
7168

7269

7370
# [START tts_ssml_address_ssml]
74-
def text_to_ssml(inputfile):
75-
# Generates SSML text from plaintext.
76-
# Given an input filename, this function converts the contents of the text
77-
# file into a string of formatted SSML text. This function formats the SSML
78-
# string so that, when synthesized, the synthetic audio will pause for two
79-
# seconds between each line of the text file. This function also handles
80-
# special text characters which might interfere with SSML commands.
81-
#
82-
# Args:
83-
# inputfile: string name of plaintext file
84-
#
85-
# Returns:
86-
# A string of SSML text based on plaintext input
71+
def text_to_ssml(inputfile: str) -> str:
72+
"""
73+
Generates SSML text from plaintext.
74+
Given an input filename, this function converts the contents of the text
75+
file into a string of formatted SSML text. This function formats the SSML
76+
string so that, when synthesized, the synthetic audio will pause for two
77+
seconds between each line of the text file. This function also handles
78+
special text characters which might interfere with SSML commands.
79+
80+
Args:
81+
inputfile: name of plaintext file
82+
Returns: SSML text based on plaintext input
83+
"""
8784

8885
# Parses lines of input file
8986
with open(inputfile) as f:

texttospeech/snippets/ssml_addresses_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_ssml_to_audio(capsys):
3737
input_ssml = f.read()
3838

3939
# Assert audio file generated
40-
ssml_to_audio(input_ssml, "test_example.mp3")
40+
ssml_to_audio(input_ssml)
4141
out, err = capsys.readouterr()
4242

4343
# Assert MP3 file created

texttospeech/snippets/synthesize_text.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626

2727

2828
# [START tts_synthesize_text]
29-
def synthesize_text(text):
29+
def synthesize_text():
3030
"""Synthesizes speech from the input string of text."""
3131
from google.cloud import texttospeech
3232

33+
text = "Hello there."
3334
client = texttospeech.TextToSpeechClient()
3435

3536
input_text = texttospeech.SynthesisInput(text=text)
@@ -60,16 +61,16 @@ def synthesize_text(text):
6061

6162

6263
# [START tts_synthesize_ssml]
63-
def synthesize_ssml(ssml):
64+
def synthesize_ssml():
6465
"""Synthesizes speech from the input string of ssml.
6566
6667
Note: ssml must be well-formed according to:
6768
https://www.w3.org/TR/speech-synthesis/
6869
69-
Example: <speak>Hello there.</speak>
7070
"""
7171
from google.cloud import texttospeech
7272

73+
ssml = "<speak>Hello there.</speak>"
7374
client = texttospeech.TextToSpeechClient()
7475

7576
input_text = texttospeech.SynthesisInput(ssml=ssml)

texttospeech/snippets/synthesize_text_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919

2020
import synthesize_text
2121

22-
TEXT = "Hello there."
23-
SSML = "<speak>Hello there.</speak>"
24-
2522

2623
def test_synthesize_text(capsys):
27-
synthesize_text.synthesize_text(text=TEXT)
24+
synthesize_text.synthesize_text()
2825
out, err = capsys.readouterr()
2926

3027
assert "Audio content written to file" in out
@@ -33,7 +30,7 @@ def test_synthesize_text(capsys):
3330

3431

3532
def test_synthesize_ssml(capsys):
36-
synthesize_text.synthesize_ssml(ssml=SSML)
33+
synthesize_text.synthesize_ssml()
3734
out, err = capsys.readouterr()
3835

3936
assert "Audio content written to file" in out

0 commit comments

Comments
 (0)