Image-generation

Image generation

This Markdown file can be "evaluated" with the Raku package "Text::CodeProcessing", [AAp2]. Here is a shell command:

file-code-chunks-eval Image-generation.md 

Remark: For doing image variations using image files see the document Image-variation-and-edition.md (and its woven version, Image-variation-and-edition_woven.md.)

Here we load the Raku package "WWW::OpenAI", [AAp1]:

use WWW::OpenAI;

Images can be generated with the function openai-create-image -- see the section "Images" of [OAI2].

Here is an example that returns "whole images" in Base64 format:

my @imgResB64 = |openai-create-image(
        "raccoon with a sliced onion in the style of Raphael",
        model => 'dall-e-3',
        n => 1,
        size => 'square',
        response-format => 'b64_json',
        format => 'values',
        method => 'tiny');

@imgResB64.map({ '![](data:image/png;base64,' ~ $_ ~ ')' }).join("\n\n")        

Remark: The obtained images are put into the woven document via the output of the code cell, which has the setting results=asis.

The first argument is a prompt used to generate the image(s).

Here are the descriptions of the named arguments (options):

  • model takes Whatever, or one of 'dall-e-2' or 'dall-e-3'

  • n takes a positive integer, for the number of images to be generated

  • size takes the values according to the model value:

    • 'dall-e-2' : '1024x1024', '512x512', '256x256', 'large', 'medium', 'small'

    • 'dall-e-3' : '1024x1024', '1792x1024', '1024x1792', 'square', 'portrait', 'landscape'

  • response-format takes the values "url" and "b64_json"

  • method takes the values "tiny" and "curl"

Remark: See the model-parameter breakdown in the section below.

Here we generate a few images, get their URLs, and place (embed) the image links using a table:

my @imgRes = |openai-create-image(
        "painting of a racoon and onion in the style of Rene Magritte",
        response-format => 'url',
        n => 2,
        size => 'small',
        format => 'values',
        method => 'tiny');

@imgRes.map({ '![](' ~ $_ ~ ')' }).join("\n\n")       

Parameters

As it was mentioned above, the argument "model" can be Whatever of one of "dall-e-2" or "dall-e-3".

Not all parameters that are valid for one of the models are valid or respected by the other -- see the subsection "Create image" of OpenAI's documentation.

Here is a table that shows a breakdown of the model-parameter relationships:

ParameterTypeRequired/OptionalDefaultdall-e-2dall-e-3Valid Values
promptstringRequiredN/Aāœ”ļøāœ”ļøMaximum length is 1000 characters for dall-e-2 and 4000 characters for dall-e-3
modelstringOptionaldall-e-2āœ”ļøāœ”ļøN/A
ninteger or nullOptional1āœ”ļøāœ”ļø (only n=1)Must be between 1 and 10. For dall-e-3, only n=1 is supported
qualitystringOptionalstandardāŒāœ”ļøN/A
response_formatstring or nullOptionalurlāœ”ļøāœ”ļøMust be one of url or b64_json
sizestring or nullOptional1024x1024āœ”ļø (256x256, 512x512, 1024x1024)āœ”ļø (1024x1024, 1792x1024, 1024x1792)Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models
stylestring or nullOptionalvividāŒāœ”ļøMust be one of vivid or natural

References

Articles

[AA1] Anton Antonov, "Connecting Mathematica and Raku", (2021), RakuForPrediction at WordPress.

Packages

[AAp1] Anton Antonov, WWW::OpenAI Raku package, (2023), GitHub/antononcube.

[AAp2] Anton Antonov, Text::CodeProcessing, (2021), GitHub/antononcube.

[OAI1] OpenAI Platform, OpenAI platform.

[OAI2] OpenAI Platform, OpenAI documentation.

[OAIp1] OpenAI, OpenAI Python Library, (2020), GitHub/openai.

WWW::OpenAI v0.3.20

WWW::OpenAI provides access to the OpenAI playground (https://platform.openai.com).

Authors

  • Anton Antonov

License

Artistic-2.0

Dependencies

HTTP::Tiny:ver<0.2.5+>JSON::Fast:ver<0.17+>MIME::Base64:ver<1.2.3+>:auth<zef:raku-community-modules>Image::Markup::Utilities:ver<0.1.0+>:auth<zef:antononcube>IO::Socket::SSL:ver<0.0.2+>

Provides

  • WWW::OpenAI
  • WWW::OpenAI::Audio
  • WWW::OpenAI::Batches
  • WWW::OpenAI::ChatCompletions
  • WWW::OpenAI::Embeddings
  • WWW::OpenAI::Files
  • WWW::OpenAI::ImageEdits
  • WWW::OpenAI::ImageGenerations
  • WWW::OpenAI::ImageVariations
  • WWW::OpenAI::Models
  • WWW::OpenAI::Moderations
  • WWW::OpenAI::Request
  • WWW::OpenAI::TextCompletions

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.