見出し画像

Koeiromap-freeとlangchainであそぶ

先日、rinna社から音声とフェイスモーションの生成AIサービスKoemotionが発表されていました。悲しいかなフェイスモーションを試すスキルが自分には無いので、Koemotionの音声合成機能Koeiromapの単体と、langchainを組み合わせて遊んでみました。

とにかくkoeiromapを試食!

KOEIRO_API_KEYに各自取得したKeyを設定してください。

import requests
import json

def koeiro_free(text="こんにちは", x=0.2, y=-3.1, style="talk",seed=8.06905746361858E+18):
"""
koeiromapのリクエスト用関数
KOEIRO_API_KEYに各自取得したKeyを設定してください。
https://developers.rinna.co.jp/api-details#api=koeiromap-v1&operation=koeiromap
"""
    URL = "https://api.rinna.co.jp/koeiromap/v1.0/infer"
    
    headers={
    'Content-Type':'application/json',
    'Cache-Control':'no-cache',
    'Ocp-Apim-Subscription-Key': KOEIRO_API_KEY
    }

    koeiromap_request = {
    "text": text,
    "speaker_x": x,# Koeiromap空間上のX座標(speaker_x)の値(-10.0〜10.0) , default:0.0
    "speaker_y": y,# Koeiromap空間上のY座標(speaker_y)の値(-10.0〜10.0) , default:0.0
    "style": style, # Koeiromap Freeプランの場合は'talk' or 'happy' or 'sad'
    "seed": seed,
    "output_format": "mp3"
    }    

    response = requests.post(
        URL,
        headers=headers,
        data = json.dumps(koeiromap_request)
        ).json()

    return base64.b64decode(response['audio'])
import base64
from IPython.display import Audio

audio = koeiro_free("星空が煌めく夜に、涼風がそよぐ木々の間を抜けて流れる。その静寂の中で、心が浄化される瞬間を感じる。", 7.5, 7.0)
Audio(audio)

わたしに音声の品質を論評する能力はありませんが、自然でいい感じな気がします。

LangChainと組み合わせて遊ぶ

  • koeiromap空間のy座標は「声の高さ」で、プラス側が女性的な声、マイナス側が男性的な声なのは分かったのですが、x 座標の意味するがよくわかりませんでした。

  • おそらく、単純なパラメータというわけでは無いと思うのですが、とりあえず x 軸は勝手に「声のテンション」ということにして、x軸とy軸とstyleのオプションをgpt3.5に考えてもらうことにします。

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.output_parsers import PydanticOutputParser

from pydantic import BaseModel, Field, validator
from typing import List

先日学んだ出力パーサーを使ってみます。

class KoeiroPlane(BaseModel):
    x: float = Field(description="声のテンション, 範囲[-10.0〜10.0]")
    y: float = Field(description="声の高さ, 範囲[-10.0〜10.0]")
    style: str = Field(description="talk or happy or sad")

parser = PydanticOutputParser(pydantic_object=KoeiroPlane)

prompt = PromptTemplate(
    template="Determine the coordinates (x,y) of the coordinates on the plane appropriate for the content of the text and the style in which the emotion is estimated.\n{format_instructions}\n{query}\n",
    input_variables=["query"],
    partial_variables={"format_instructions": parser.get_format_instructions()}
)
input_query = "雨が降りそうなのに、傘を持ってくるの忘れちゃった、どうしよー"

_input = prompt.format_prompt(query=ask_query)
output = model(_input.to_string())

plane_coordinates = parser.parse(output)
plane_coordinates 

KoeiroPlane(x=-5.0, y=-5.0, style='sad')

座標が正しいのか全く不明😅ですが、ひとまずkoeiromapに投入してみます。

audio = koeiro_free(input_query, plane_coordinates.x, plane_coordinates.y, plane_coordinates.style)
Audio(audio)

langchainの出力パーサーの練習みたいな感じになってしまいました。😊
koeiromap空間の扱い方をもっと知りたいなぁ。

おしまい。

この記事が気に入ったらサポートをしてみませんか?