Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| import boto3 | |
| import os | |
| import base64 | |
| # aws Polly keys | |
| polly = boto3.client( | |
| "polly", | |
| aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"], | |
| aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"], | |
| region_name="eu-west-1" | |
| ) | |
| app = FastAPI(title="Gaelic TTS") | |
| class TTSRequest(BaseModel): | |
| text: str | |
| ipa: str | |
| rate: str = "75%" | |
| # | |
| def root(): | |
| return {"status": "okay"} | |
| # using mix of Gaelic text and IPA | |
| def generate_tts(request: TTSRequest): | |
| ssml_text = f""" | |
| <speak> | |
| <prosody rate="{request.rate}"> | |
| <phoneme alphabet="ipa" ph="{request.ipa}">{request.text}</phoneme> | |
| </prosody> | |
| </speak> | |
| """ | |
| response = polly.synthesize_speech( | |
| Text=ssml_text, | |
| TextType="ssml", | |
| OutputFormat="mp3", | |
| VoiceId="Gwyneth", | |
| Engine="standard" | |
| ) # welsh model | |
| audio_bytes = response["AudioStream"].read() | |
| return {"audio_base64": base64.b64encode(audio_bytes).decode("utf-8")} | |
| # call | |
| def predict(request: TTSRequest): | |
| return generate_tts(request) |