2023年3月10日金曜日

rinna株式会社のText to Image -v2のAPIを使用する

rinna株式会社が提供しているText to Image - v2のAPIをOracle APEXのアプリケーションから呼び出して、テキストから画像を生成してみます。音声か画像かの違いなので、以前のText to Speech - v2を呼び出すAPEXアプリケーションとほとんど同じです。


手順として異なる部分に絞って、アプリケーションの作成作業を紹介します。

生成した画像は表RINNA_TEXT_TO_IMAGESに保存します。以下のクイックSQLのモデルから作成します。
# prefix: rinna
# auditcols: true
text_to_images
    text clob
    content_type vc16
    image blob
    scale num
    nsfwContentDetected vc1
    type vc80
画像を取り出すRESTサービスのソースは以下になります。

select content_type, image from rinna_text_to_images where id = :id

テンプレートimageメソッドGETソース・タイプMedia Resourceです。完全なURLコピーしておきます。


APEXアプリケーションを作成します。

名前Text to Image、追加する対話モード・レポートページ名Imagesソースとして表RINNA_TEXT_TO_IMAGESを指定します。


ホーム・ページにテキスト(APIの引数promptsになる)を入力するページ・アイテムを作成します。

識別名前P1_TEXTタイプテキスト領域検証必須の値ONにします。


スケールを指定するページ・アイテムを作成します。

識別名前P1_SCALEタイプ数値フィールドラベルscaleとします。設定最小値0最大値20を設定し、設定必須の値ONにします。デフォルト静的値として7.5を設定します。


Text to Imageを実行するボタンを作成します。

識別ボタン名TEXT_TO_IMAGEラベルText To Image動作アクションとして、デフォルトのページの送信を選択します。


生成された画像を表示するページ・アイテムを作成します。

識別名前P1_IMAGEタイプとしてイメージの表示を選択します。ラベル画像設定基準としてImage URL stored in Page Item Valueを選択します。


アイテムの配置は以上です。

ボタンTEXT_TO_IMAGEをクリックしたときに実行されるプロセスを作成します。

作成したプロセスの識別名前Text to Imageとします。タイプコードを実行です。ソースPL/SQLコードとして、以下を記述します。ワークスペース名がapexdevと決め打ちにしているので、その部分は変更が必要です。

declare
l_request json_object_t;
l_request_clob clob;
l_response json_object_t;
l_response_clob clob;
l_id rinna_text_to_images.id%type;
l_image clob;
l_image_blob blob;
l_nsfwContentDetected rinna_text_to_images.nsfwContentDetected%type;
l_type rinna_text_to_images.type%type;
begin
/*
* Text To Image V2のリクエストを作成する。
*
* 参照: https://developers.rinna.co.jp/api-details#api=z05-text2image-jsd
*/
l_request := json_object_t();
l_request.put('prompts', :P1_TEXT);
l_request.put('scale', to_number(:P1_SCALE));
l_request_clob := l_request.to_clob();
-- apex_debug.info(l_request_clob);
/*
* Text To Image の呼び出し。
*/
apex_web_service.clear_request_headers;
apex_web_service.set_request_headers('Content-Type','application/json', p_reset => false);
apex_web_service.set_request_headers('Cache-Control','no-cache', p_reset => false);
l_response_clob := apex_web_service.make_rest_request(
p_url => 'https://api.rinna.co.jp/models/tti/v2'
,p_http_method => 'POST'
,p_body => l_request_clob
,p_credential_static_id => 'RINNA_DEVELOPER_KEY'
);
if apex_web_service.g_status_code <> 200 then
apex_debug.info('REST API Failed, %s, %s', apex_web_service.g_status_code, l_response_clob);
raise_application_error(-20001, 'Text To Image = ' || apex_web_service.g_status_code);
end if;
/*
* レスポンスから生成された画像を取得する。
*/
l_response := json_object_t(l_response_clob);
l_image := l_response.get_clob('image');
l_image_blob := null;
if instr(l_image, 'data:image/png;base64,') <> 1 then
-- image/pngではない? 記録して無視する。
apex_debug.info(l_response_clob);
else
/* APIが生成する画像はimage/pngであることを常に期待している。 */
l_image := substr(l_image, length('data:image/png;base64,')+1);
l_image_blob := apex_web_service.clobbase642blob(l_image);
end if;
l_nsfwContentDetected := 'N';
if l_response.get_boolean('nsfwContentDetected') then
l_nsfwContentDetected := 'Y';
end if;
l_type := l_response.get_string('type');
/*
* テキストと画像の両方を保存する。
*/
insert into rinna_text_to_images(text, scale, image, content_type, nsfwContentDetected, type)
values(:P1_TEXT, :P1_SCALE, l_image_blob, 'image/png', l_nsfwContentDetected, l_type)
returning id into l_id;
/* ワークスペース名はapexdevであると仮定。違う場合は変更する。 */
:P1_IMAGE := apex_util.host_url() || '/ords/apexdev/rinna/image?id=' || l_id;
end;
サーバー側の条件ボタン押下時TEXT_TO_IMAGEを選択します。


ページ・デザイナ対話モード・レポートのページを開き、列IMAGEタイプイメージの表示に変更します。BLOB属性MIMEタイプ列としてCONTENT_TYPEを指定します。


以上でアプリケーションは完成です。

アプリケーションを実行すると記事の先頭のような動作になります。

作成したAPEXアプリケーションのエクスポートを以下に置きました。
https://github.com/ujnak/apexapps/blob/master/exports/text-to-image.zip

rinna株式以外より提供されているAPIのTerms of UseおよびFAQなどは、利用者ご自身にて確認していだくようお願いします。FAQには、APIは現在、試験運用中で検証を除く業務用利用は禁止しております、と記載されています。

Oracle APEXのアプリケーション作成の参考になれば幸いです。