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
This commit is contained in:
parent
df82567356
commit
c541cb5cba
4 changed files with 221 additions and 115 deletions
70
script/amazon_polly.py
Normal file
70
script/amazon_polly.py
Normal file
|
@ -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",
|
||||
)
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue