From 6820b49f0e309038d88441c4b9e0c6132533552d Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 29 Apr 2023 08:44:21 -0700 Subject: [PATCH] adding in tts logic with gcp --- voice-server/app.py | 21 ++++++++++++++++++++- voice-server/tts.py | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 voice-server/tts.py diff --git a/voice-server/app.py b/voice-server/app.py index c383955..34fc68b 100644 --- a/voice-server/app.py +++ b/voice-server/app.py @@ -1,5 +1,6 @@ -from flask import Flask, request, abort +from flask import Flask, request, abort, send_file from tempfile import NamedTemporaryFile +import tts import whisper # Load the Whisper model: @@ -44,6 +45,24 @@ def transcribe(): return {"results": results} +# endpoint to convert text to speech and return the file +@app.route("/tts", methods=["GET"]) +def text_to_speech(): + # get the text from the request + text = request.form.get("text") + if not text: + abort(400, "Please provide text to convert to speech!") + + # call the text_to_wav function from tts.py + fileName = tts.text_to_wav( + voice_name="en-GB-Neural2-B", + text=text, + ) + + # return the file + return send_file(f"{fileName}.wav", mimetype="audio/wav") + + if __name__ == "__main__": # 0.0.0.0 opens up to all ipv4 addresses app.run(host="0.0.0.0", port=5001, debug=True) diff --git a/voice-server/tts.py b/voice-server/tts.py new file mode 100644 index 0000000..8442c2e --- /dev/null +++ b/voice-server/tts.py @@ -0,0 +1,24 @@ +import google.cloud.texttospeech as tts + + +def text_to_wav(voice_name: str, text: str): + language_code = "-".join(voice_name.split("-")[:2]) + text_input = tts.SynthesisInput(text=text) + voice_params = tts.VoiceSelectionParams( + language_code=language_code, name=voice_name + ) + audio_config = tts.AudioConfig(audio_encoding=tts.AudioEncoding.LINEAR16) + + client = tts.TextToSpeechClient() + response = client.synthesize_speech( + input=text_input, + voice=voice_params, + audio_config=audio_config, + ) + + filename = f"{voice_name}.wav" + with open(filename, "wb") as out: + out.write(response.audio_content) + print(f'Generated speech saved to "{filename}"') + + return filename