This blog post consists of the slides of the presentation “Racoons playing with pearls and onions”:

Image generation with OpenAI
… using the Raku module “WWW::OpenAI”
Anton Antonov
RakuForPrediction-book at GitHub
March 2023
Presentation plan
Warm-up with text generation
- ChatGPT
Image generations
- URLs
- Base64s
Using Comma IDE and CLI
Other examples
Setup
Load the package
use WWW::OpenAI;
(*"(Any)"*)
Get the authorization key
RakuInputExecute["my $auth-key='" <> AUTHKEY <> "'"];
Warm up (completions)
my @txtRes = |openai-completion(
'which is the most successful programming language',
n=>3,
temperature=>1.3,
max-tokens=>120,
format=>'values',
:$auth-key);
@txtRes.elems
(*"3"*)
RakuInputExecute["@txtRes==>encode-to-wl"] // ResourceFunction["GridTableForm"]

Image generation (URLs)
my @imgRes = |openai-create-image(
'Racoon with pearls in the style Raphael',
n=>3,
response-format=>'url',
format=>'values',
:$auth-key);
@imgRes.elems;
(*"3"*)
Magnify[Import[#], 2.5] & /@ RakuInputExecute["@imgRes==>encode-to-wl"]

@imgRes
Image generation (Base64s)
my @imgRes2 = |openai-create-image(
'Racoon and sliced onion in the style Rene Magritte',
n=>3,
response-format=>'b64_json',
format=>'values',
:$auth-key);
@imgRes2.elems;
(*"3"*)
Magnify[ImportByteArray[BaseDecode[#]], 2.5] & /@ RakuInputExecute["@imgRes2==>encode-to-wl"]

@imgRes2
WebImageSearch["Son of Man Magritte", 3]

Using Comma IDE and CLI
“Text::CodeProcessing” used in Mathematica and executable Markdown
Some more
Monet
my @imgRes3 = |openai-create-image('Racoons playing onions and perls in the style Hannah Wilke', n=>3, size => 'medium', format=>'values', response-format=>'b64_json', :$auth-key);
@imgRes3.elems;
(*"3"*)
Magnify[ImportByteArray[BaseDecode[#]], 1.5] & /@ RakuInputExecute["@imgRes3==>encode-to-wl"]

openai-create-image('Racoons playing onions and perls in the style Monet', n=>3, size => 'largers')
(*"#ERROR: The argument $size is expected to be Whatever or one of '1024x1024, 256x256, 512x512, large, medium, small'.Nil"*)
Helmut Newton
my @imgRes3 = |openai-create-image('Racoon in the style Helmut Newton', n=>3, size => 'small', format=>'values', response-format=>'b64_json', :$auth-key);
@imgRes3.elems;
(*"3"*)
Magnify[ImportByteArray[BaseDecode[#]], 1.5] & /@ RakuInputExecute["@imgRes3==>encode-to-wl"]

Hieronymus Bosch
my @imgRes3 = |openai-create-image('how we live now in the style of Hieronymus Bosch', n=>3, size => 'small', format=>'values', response-format=>'b64_json', :$auth-key);
@imgRes3.elems;
(*"3"*)
Magnify[ImportByteArray[BaseDecode[#]], 1.5] & /@ RakuInputExecute["@imgRes3==>encode-to-wl"]

Inkblots
my @imgRes4 = |openai-create-image('Racoon in the style Roschach inkblot', :$auth-key, n=>6, format=>'values');
@imgRes4.elems;
(*"6"*)
Magnify[Import[#], 1.5] & /@ RakuInputExecute["@imgRes4==>encode-to-wl"]

Saved

Alternative
Table[ResourceFunction["RandomRorschach"]["NumberOfStrokes" -> RandomChoice[{8, 12, 20}, RandomInteger[{1, 4}]], ColorFunction -> (GrayLevel[RandomReal[{0.1, 0.2}]] &), "ImageEffects" -> {{"Jitter", 16}, {"OilPainting", 6}}], 12]

Goldberg machines
my @imgRes5 = |openai-create-image('Camels in a Rube Goldberg machine', :$auth-key, n=>6, format=>'values');
@imgRes5.elems;
(*"6"*)
Magnify[Import[#], 1.5] & /@ RakuInputExecute["@imgRes5==>encode-to-wl"]

In case it is not clear, the generated images are visual puns:
– “Racoons” = “Raku practitioners”
– “Onion” = “The Perl and Raku Foundation” activities
– See the TPF logos and “State of the Onion” speeches
– “Pearls” = “Perl language (artifacts)”
LikeLike