-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtechcps.sh
109 lines (88 loc) · 3.16 KB
/
techcps.sh
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
# ARC132!
export PROJECT_ID=$(gcloud config get-value project)
source venv/bin/activate
cat > synthesize-text.json <<EOF
{
'input':{
'text':'Cloud Text-to-Speech API allows developers to include
natural-sounding, synthetic human speech as playable audio in
their applications. The Text-to-Speech API converts text or
Speech Synthesis Markup Language (SSML) input into audio data
like MP3 or LINEAR16 (the encoding used in WAV files).'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}
EOF
curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @synthesize-text.json "https://texttospeech.googleapis.com/v1/text:synthesize" \
> "$file_cp2"
cat > tts_decode.py <<EOF
import argparse
from base64 import decodebytes
import json
"""
Usage:
python tts_decode.py --input "synthesize-text.txt" \
--output "synthesize-text-audio.mp3"
"""
def decode_tts_output(input_file, output_file):
""" Decode output from Cloud Text-to-Speech.
input_file: the response from Cloud Text-to-Speech
output_file: the name of the audio file to create
"""
with open(input_file) as input:
response = json.load(input)
audio_data = response['audioContent']
with open(output_file, "wb") as new_file:
new_file.write(decodebytes(audio_data.encode('utf-8')))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Decode output from Cloud Text-to-Speech",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--input',
help='The response from the Text-to-Speech API.',
required=True)
parser.add_argument('--output',
help='The name of the audio file to create',
required=True)
args = parser.parse_args()
decode_tts_output(args.input, args.output)
EOF
python tts_decode.py --input "$file_cp2" --output "synthesize-text-audio.mp3"
audio_uri="gs://cloud-samples-data/speech/corbeau_renard.flac"
cat > "$request_cp3" <<EOF
{
"config": {
"encoding": "FLAC",
"sampleRateHertz": 44100,
"languageCode": "fr-FR"
},
"audio": {
"uri": "$audio_uri"
}
}
EOF
curl -s -X POST -H "Content-Type: application/json" \
--data-binary @"$request_cp3" \
"https://speech.googleapis.com/v1/speech:recognize?key=${API_KEY}" \
-o "$response_cp3"
response=$(curl -s -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "{\"q\": \"$sentence_cp4\"}" \
"https://translation.googleapis.com/language/translate/v2?key=${API_KEY}&source=ja&target=en")
echo "$response" > "$file_cp4"
curl -s -X POST \
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "{\"q\": [\"$sentence_cp5\"]}" \
"https://translation.googleapis.com/language/translate/v2/detect?key=${API_KEY}" \
-o "$file_cp5"