-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtextToSpeech.py
More file actions
92 lines (64 loc) · 3 KB
/
textToSpeech.py
File metadata and controls
92 lines (64 loc) · 3 KB
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
from flask import Flask, request, jsonify
import io
from google.oauth2 import service_account
from google.cloud import texttospeech
import os
app = Flask(__name__)
@app.route('/text-to-speech/instruction', methods=['POST'])
def text_to_speech():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
textBlock = file.read()
synthesisInput = texttospeech.SynthesisInput(text=textBlock)
audioOut = client.synthesize_speech(input=synthesisInput, voice=voice, audio_config=audio_config)
os.makedirs(r"speechOuts", exist_ok=True)
with open(r"speechOuts/speechOutput.mp3", "wb") as outFile:
outFile.write(audioOut.audio_content)
return jsonify({'message': 'instruction audio generated'}), 200
@app.route('/text-to-speech/timer', methods=['POST'])
def text_to_speech2():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
textBlock = file.read()
synthesisInput = texttospeech.SynthesisInput(text=textBlock)
audioOut = client.synthesize_speech(input=synthesisInput, voice=voice, audio_config=audio_config)
os.makedirs(r"speechOuts", exist_ok=True)
with open(r"speechOuts/timer.mp3", "wb") as outFile:
outFile.write(audioOut.audio_content)
return jsonify({'message': 'Timer alert audio generated'}), 200
@app.route('/text-to-speech/temperature', methods=['POST'])
def text_to_speech3():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
textBlock = file.read()
synthesisInput = texttospeech.SynthesisInput(text=textBlock)
audioOut = client.synthesize_speech(input=synthesisInput, voice=voice, audio_config=audio_config)
os.makedirs(r"speechOuts", exist_ok=True)
with open(r"speechOuts/temperature.mp3", "wb") as outFile:
outFile.write(audioOut.audio_content)
return jsonify({'message': 'Temperature alert audio generated'}), 200
client_file = "serviceAccKey.json"
credentials = service_account.Credentials.from_service_account_file(client_file)
client = texttospeech.TextToSpeechClient(credentials=credentials)
voice = texttospeech.VoiceSelectionParams(
language_code='en-US',
name='en-US-Journey-F' # studio al: 'en-US-Studio-O'
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
effects_profile_id=['small-bluetooth-speaker-class-device'], #medium-bluetooth-speaker-class-device if we use a bigger speaker
# not supported for journey:
#speaking_rate=1,
#pitch=1
)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001) # changes values