AI-Image
view release on metacpan or search on metacpan
lib/AI/Image.pm view on Meta::CPAN
our $VERSION = '0.1';
$VERSION = eval $VERSION;
my $http = HTTP::Tiny->new;
# Create Image object
sub new {
my $class = shift;
my %attr = @_;
$attr{'error'} = '';
$attr{'api'} = 'OpenAI' unless $attr{'api'};
$attr{'error'} = 'Invalid API' unless $attr{'api'} eq 'OpenAI';
$attr{'error'} = 'API Key missing' unless $attr{'key'};
$attr{'model'} = 'dall-e-2' unless $attr{'model'};
$attr{'size'} = '512x512' unless $attr{'size'};
return bless \%attr, $class;
}
# Define endpoints for APIs
my %url = (
'OpenAI' => 'https://api.openai.com/v1/images/generations',
);
# Define HTTP Headers for APIs
my %header = (
'OpenAI' => &_get_header_openai,
);
# Returns true if last operation was success
sub success {
my $self = shift;
return !$self->{'error'};
}
# Returns error if last operation failed
sub error {
my $self = shift;
return $self->{'error'};
}
# Header for calling OpenAI
sub _get_header_openai {
my $self = shift;
$self->{'key'} = '' unless defined $self->{'key'};
return {
'Authorization' => 'Bearer ' . $self->{'key'},
'Content-type' => 'application/json'
};
lib/AI/Image.pm view on Meta::CPAN
See L<https://platform.openai.com/docs/models/overview>
=item size
The size for the generated image (default: '512x512').
=item debug
Used for testing. If set to any true value, the image method
will return details of the error encountered instead of C<undef>
=back
=head2 image
my $url = $ai->image($prompt);
Generates an image based on the provided prompt and returns the URL of the generated image. The URL is valid for 1 hour.
=head3 Parameters
lib/AI/Image.pm view on Meta::CPAN
The textual description of the desired image.
=back
=head2 success
my $success = $ai->success();
Returns true if the last operation was successful.
=head2 error
my $error = $ai->error();
Returns the error message if the last operation failed.
=head1 EXAMPLE
It is common that the generated image will want to be saved as a file. This can be easily acheived
using the C<getstore> method of L<LWP::Simple>.
use strict;
use warnings;
use LWP::Simple;
( run in 1.037 second using v1.01-cache-2.11-cpan-49f99fa48dc )