in genai-for-marketing/backend_apis/app/main.py [0:0]
def text_to_speech(data: TexttoSpeechRequest
) -> TexttoSpeechResponse:
""" Text to Speech
Body:
text:str
prefix: str
language_code:str | None = None
language_name: str | None = None
Returns:
audio_uri :str
"""
output_gcs_uri = "gs://"+bucket_name+"/"+data.prefix+".wav"
try:
input = texttospeech.SynthesisInput(
text=data.text
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.LINEAR16
)
if data.language_code != None and data.language_name != None:
voice = texttospeech.VoiceSelectionParams(
language_code=data.language_code, name=data.language_name
)
else:
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", name="en-US-Standard-A"
)
parent = f"projects/{project_id}/locations/{location}"
request = texttospeech.SynthesizeLongAudioRequest(
parent=parent,
input=input,
audio_config=audio_config,
voice=voice,
output_gcs_uri=output_gcs_uri,
)
operation = texttospeech_client.synthesize_long_audio(request=request)
# Set a deadline for your LRO to finish. 300 seconds is reasonable,
# but can be adjusted depending on the length of the input.
# If the operation times out, that likely means there was an error.
# In that case, inspect the error, and try again.
result = operation.result(timeout=300)
except Exception as e:
raise HTTPException(
status_code=400,
detail="Something went wrong. Please try again."+str(e)
)
return TexttoSpeechResponse(
audio_uri=output_gcs_uri
)