-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically generate Amazon Polly list of voices and regions (#119198)
* Automatically generate list of voices and regions. Requires AWS credentials. * add missing commit * replace pydantic with dataclass * dictionary values are strings or list of strings * also generated set of supported engines * use sets for amazon polly parameters * move default for readability
- Loading branch information
Showing
4 changed files
with
221 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
"""Automatically generated file. | ||
To update, run python3 -m script.amazon_polly | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Final | ||
|
||
SUPPORTED_ENGINES: Final[set[str]] = { | ||
"generative", | ||
"long-form", | ||
"neural", | ||
"standard", | ||
} | ||
|
||
SUPPORTED_REGIONS: Final[set[str]] = { | ||
"af-south-1", | ||
"ap-east-1", | ||
"ap-northeast-1", | ||
"ap-northeast-2", | ||
"ap-northeast-3", | ||
"ap-south-1", | ||
"ap-southeast-1", | ||
"ap-southeast-2", | ||
"ca-central-1", | ||
"eu-central-1", | ||
"eu-north-1", | ||
"eu-west-1", | ||
"eu-west-2", | ||
"eu-west-3", | ||
"me-south-1", | ||
"sa-east-1", | ||
"us-east-1", | ||
"us-east-2", | ||
"us-west-1", | ||
"us-west-2", | ||
} | ||
|
||
SUPPORTED_VOICES: Final[set[str]] = { | ||
"Aditi", | ||
"Adriano", | ||
"Amy", | ||
"Andres", | ||
"Aria", | ||
"Arlet", | ||
"Arthur", | ||
"Astrid", | ||
"Ayanda", | ||
"Bianca", | ||
"Brian", | ||
"Burcu", | ||
"Camila", | ||
"Carla", | ||
"Carmen", | ||
"Celine", | ||
"Chantal", | ||
"Conchita", | ||
"Cristiano", | ||
"Daniel", | ||
"Danielle", | ||
"Dora", | ||
"Elin", | ||
"Emma", | ||
"Enrique", | ||
"Ewa", | ||
"Filiz", | ||
"Gabrielle", | ||
"Geraint", | ||
"Giorgio", | ||
"Gregory", | ||
"Gwyneth", | ||
"Hala", | ||
"Hannah", | ||
"Hans", | ||
"Hiujin", | ||
"Ida", | ||
"Ines", | ||
"Isabelle", | ||
"Ivy", | ||
"Jacek", | ||
"Jan", | ||
"Joanna", | ||
"Joey", | ||
"Justin", | ||
"Kajal", | ||
"Karl", | ||
"Kazuha", | ||
"Kendra", | ||
"Kevin", | ||
"Kimberly", | ||
"Laura", | ||
"Lea", | ||
"Liam", | ||
"Lisa", | ||
"Liv", | ||
"Lotte", | ||
"Lucia", | ||
"Lupe", | ||
"Mads", | ||
"Maja", | ||
"Marlene", | ||
"Mathieu", | ||
"Matthew", | ||
"Maxim", | ||
"Mia", | ||
"Miguel", | ||
"Mizuki", | ||
"Naja", | ||
"Niamh", | ||
"Nicole", | ||
"Ola", | ||
"Olivia", | ||
"Pedro", | ||
"Penelope", | ||
"Raveena", | ||
"Remi", | ||
"Ricardo", | ||
"Ruben", | ||
"Russell", | ||
"Ruth", | ||
"Salli", | ||
"Seoyeon", | ||
"Sergio", | ||
"Sofie", | ||
"Stephen", | ||
"Suvi", | ||
"Takumi", | ||
"Tatyana", | ||
"Thiago", | ||
"Tomoko", | ||
"Vicki", | ||
"Vitoria", | ||
"Zayd", | ||
"Zeina", | ||
"Zhiyu", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Helper script to update supported languages for Amazone Polly text-to-speech (TTS). | ||
N.B. This script requires AWS credentials. | ||
""" | ||
|
||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import Self | ||
|
||
import boto3 | ||
|
||
from .hassfest.serializer import format_python_namespace | ||
|
||
|
||
@dataclass(frozen=True) | ||
class AmazonPollyVoice: | ||
"""Amazon Polly Voice.""" | ||
|
||
id: str | ||
name: str | ||
gender: str | ||
language_name: str | ||
language_code: str | ||
supported_engines: set[str] | ||
additional_language_codes: set[str] | ||
|
||
@classmethod | ||
def validate(cls, model: dict[str, str | list[str]]) -> Self: | ||
"""Validate data model.""" | ||
return cls( | ||
id=model["Id"], | ||
name=model["Name"], | ||
gender=model["Gender"], | ||
language_name=model["LanguageName"], | ||
language_code=model["LanguageCode"], | ||
supported_engines=set(model["SupportedEngines"]), | ||
additional_language_codes=set(model.get("AdditionalLanguageCodes", [])), | ||
) | ||
|
||
|
||
def get_all_voices(client: boto3.client) -> list[AmazonPollyVoice]: | ||
"""Get list of all supported voices from Amazon Polly.""" | ||
response = client.describe_voices() | ||
return [AmazonPollyVoice.validate(voice) for voice in response["Voices"]] | ||
|
||
|
||
supported_regions = set( | ||
boto3.session.Session().get_available_regions(service_name="polly") | ||
) | ||
|
||
polly_client = boto3.client(service_name="polly", region_name="us-east-1") | ||
voices = get_all_voices(polly_client) | ||
supported_voices = set({v.id for v in voices}) | ||
supported_engines = set().union(*[v.supported_engines for v in voices]) | ||
|
||
Path("homeassistant/generated/amazon_polly.py").write_text( | ||
format_python_namespace( | ||
{ | ||
"SUPPORTED_VOICES": supported_voices, | ||
"SUPPORTED_REGIONS": supported_regions, | ||
"SUPPORTED_ENGINES": supported_engines, | ||
}, | ||
annotations={ | ||
"SUPPORTED_VOICES": "Final[set[str]]", | ||
"SUPPORTED_REGIONS": "Final[set[str]]", | ||
"SUPPORTED_ENGINES": "Final[set[str]]", | ||
}, | ||
generator="script.amazon_polly", | ||
) | ||
) |