Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Amazon Polly

livebooks/aws/polly.livemd

Amazon Polly

Mix.install([
  {:aws, "~> 0.13"},
  {:hackney, "~> 1.20"},
  {:kino, "~> 0.14"}
])

クライアントの準備

access_key_id_input = Kino.Input.password("ACCESS_KEY_ID")
secret_access_key_input = Kino.Input.password("SECRET_ACCESS_KEY")
region_input = Kino.Input.text("REGION")

[
  access_key_id_input,
  secret_access_key_input,
  region_input
]
|> Kino.Layout.grid(columns: 3)
client =
  AWS.Client.create(
    Kino.Input.read(access_key_id_input),
    Kino.Input.read(secret_access_key_input),
    Kino.Input.read(region_input)
  )

音声合成

voice_list =
  client
  |> AWS.Polly.describe_voices("standard", nil, "ja-JP")
  |> elem(1)
  |> Map.get("Voices")
  |> Enum.map(fn voice ->
    Map.take(voice, ["Id", "Gender", "Name", "LanguageName"])
  end)

Kino.DataTable.new(voice_list)
text_input = Kino.Input.textarea("TEXT")
result =
  client
  |> AWS.Polly.synthesize_speech(
    %{
      "Engine" => "standard",
      "LanguageCode" => "ja-JP",
      "VoiceId" => "Takumi",
      "OutputFormat" => "mp3",
      "Text" => Kino.Input.read(text_input)
    },
    receive_body_as_binary?: true
  )
  |> elem(1)
  |> Map.get("Body")
result
|> Base.encode64()
|> then(fn base64 ->
  Kino.HTML.new("""
  
  
  """)
end)
voice_list =
  client
  |> AWS.Polly.describe_voices("neural", nil, "ja-JP")
  |> elem(1)
  |> Map.get("Voices")
  |> Enum.map(fn voice ->
    Map.take(voice, ["Id", "Gender", "Name", "LanguageName"])
  end)

Kino.DataTable.new(voice_list)
client
|> AWS.Polly.synthesize_speech(
  %{
    "Engine" => "neural",
    "LanguageCode" => "ja-JP",
    "VoiceId" => "Takumi",
    "OutputFormat" => "mp3",
    "Text" => Kino.Input.read(text_input)
  },
  receive_body_as_binary?: true
)
|> elem(1)
|> Map.get("Body")
|> Base.encode64()
|> then(fn base64 ->
  Kino.HTML.new("""
  
  
  """)
end)