手順として異なる部分に絞って、アプリケーションの作成作業を紹介します。
生成した画像は表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
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を実行するボタンを作成します。
識別のボタン名はTEXT_TO_IMAGE、ラベルはText To Image、動作のアクションとして、デフォルトのページの送信を選択します。
生成された画像を表示するページ・アイテムを作成します。
識別の名前はP1_IMAGE、タイプとしてイメージの表示を選択します。ラベルは画像、設定の基準としてImage URL stored in Page Item Valueを選択します。
アイテムの配置は以上です。
ボタンTEXT_TO_IMAGEをクリックしたときに実行されるプロセスを作成します。
作成したプロセスの識別の名前はText to Imageとします。タイプはコードを実行です。ソースのPL/SQLコードとして、以下を記述します。ワークスペース名がapexdevと決め打ちにしているので、その部分は変更が必要です。
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
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; |
ページ・デザイナで対話モード・レポートのページを開き、列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のアプリケーション作成の参考になれば幸いです。
完