adding in tts logic with gcp

This commit is contained in:
talksik
2023-04-29 08:44:21 -07:00
parent cd4c26966a
commit 6820b49f0e
2 changed files with 44 additions and 1 deletions
+20 -1
View File
@@ -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)
+24
View File
@@ -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