| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package AI::Image;
|
|
2
|
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
418855
|
use strict;
|
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
126
|
|
|
4
|
3
|
|
|
3
|
|
23
|
use warnings;
|
|
|
3
|
|
|
|
|
20
|
|
|
|
3
|
|
|
|
|
186
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
20
|
use strict;
|
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
80
|
|
|
7
|
3
|
|
|
3
|
|
14
|
use warnings;
|
|
|
3
|
|
|
|
|
10
|
|
|
|
3
|
|
|
|
|
179
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
19
|
use Carp;
|
|
|
3
|
|
|
|
|
11
|
|
|
|
3
|
|
|
|
|
263
|
|
|
10
|
3
|
|
|
3
|
|
2586
|
use HTTP::Tiny;
|
|
|
3
|
|
|
|
|
192323
|
|
|
|
3
|
|
|
|
|
217
|
|
|
11
|
3
|
|
|
3
|
|
2246
|
use JSON::PP;
|
|
|
3
|
|
|
|
|
54960
|
|
|
|
3
|
|
|
|
|
1883
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.1';
|
|
14
|
|
|
|
|
|
|
$VERSION = eval $VERSION;
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $http = HTTP::Tiny->new;
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Create Image object
|
|
19
|
|
|
|
|
|
|
sub new {
|
|
20
|
3
|
|
|
3
|
1
|
191697
|
my $class = shift;
|
|
21
|
3
|
|
|
|
|
13
|
my %attr = @_;
|
|
22
|
|
|
|
|
|
|
|
|
23
|
3
|
|
|
|
|
11
|
$attr{'error'} = '';
|
|
24
|
|
|
|
|
|
|
|
|
25
|
3
|
100
|
|
|
|
15
|
$attr{'api'} = 'OpenAI' unless $attr{'api'};
|
|
26
|
3
|
100
|
|
|
|
12
|
$attr{'error'} = 'Invalid API' unless $attr{'api'} eq 'OpenAI';
|
|
27
|
3
|
100
|
|
|
|
12
|
$attr{'error'} = 'API Key missing' unless $attr{'key'};
|
|
28
|
|
|
|
|
|
|
|
|
29
|
3
|
50
|
|
|
|
12
|
$attr{'model'} = 'dall-e-2' unless $attr{'model'};
|
|
30
|
3
|
50
|
|
|
|
14
|
$attr{'size'} = '512x512' unless $attr{'size'};
|
|
31
|
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
12
|
return bless \%attr, $class;
|
|
33
|
|
|
|
|
|
|
}
|
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Define endpoints for APIs
|
|
36
|
|
|
|
|
|
|
my %url = (
|
|
37
|
|
|
|
|
|
|
'OpenAI' => 'https://api.openai.com/v1/images/generations',
|
|
38
|
|
|
|
|
|
|
);
|
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Define HTTP Headers for APIs
|
|
41
|
|
|
|
|
|
|
my %header = (
|
|
42
|
|
|
|
|
|
|
'OpenAI' => &_get_header_openai,
|
|
43
|
|
|
|
|
|
|
);
|
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Returns true if last operation was success
|
|
46
|
|
|
|
|
|
|
sub success {
|
|
47
|
3
|
|
|
3
|
1
|
1615
|
my $self = shift;
|
|
48
|
3
|
|
|
|
|
22
|
return !$self->{'error'};
|
|
49
|
|
|
|
|
|
|
}
|
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Returns error if last operation failed
|
|
52
|
|
|
|
|
|
|
sub error {
|
|
53
|
0
|
|
|
0
|
1
|
0
|
my $self = shift;
|
|
54
|
0
|
|
|
|
|
0
|
return $self->{'error'};
|
|
55
|
|
|
|
|
|
|
}
|
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Header for calling OpenAI
|
|
58
|
|
|
|
|
|
|
sub _get_header_openai {
|
|
59
|
3
|
|
|
3
|
|
6
|
my $self = shift;
|
|
60
|
3
|
50
|
|
|
|
12
|
$self->{'key'} = '' unless defined $self->{'key'};
|
|
61
|
|
|
|
|
|
|
return {
|
|
62
|
3
|
|
|
|
|
43
|
'Authorization' => 'Bearer ' . $self->{'key'},
|
|
63
|
|
|
|
|
|
|
'Content-type' => 'application/json'
|
|
64
|
|
|
|
|
|
|
};
|
|
65
|
|
|
|
|
|
|
}
|
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Get URL from image prompt
|
|
68
|
|
|
|
|
|
|
sub image {
|
|
69
|
0
|
|
|
0
|
1
|
|
my ($self, $prompt) = @_;
|
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $response = $http->post($url{$self->{'api'}}, {
|
|
72
|
|
|
|
|
|
|
'headers' => {
|
|
73
|
|
|
|
|
|
|
'Authorization' => 'Bearer ' . $self->{'key'},
|
|
74
|
|
|
|
|
|
|
'Content-type' => 'application/json'
|
|
75
|
|
|
|
|
|
|
},
|
|
76
|
|
|
|
|
|
|
content => encode_json {
|
|
77
|
|
|
|
|
|
|
model => $self->{'model'},
|
|
78
|
0
|
|
|
|
|
|
size => $self->{'size'},
|
|
79
|
|
|
|
|
|
|
prompt => $prompt,
|
|
80
|
|
|
|
|
|
|
}
|
|
81
|
|
|
|
|
|
|
});
|
|
82
|
0
|
0
|
|
|
|
|
if ($response->{'content'} =~ 'invalid_api_key') {
|
|
83
|
0
|
|
|
|
|
|
croak 'Incorrect API Key - check your API Key is correct';
|
|
84
|
|
|
|
|
|
|
}
|
|
85
|
|
|
|
|
|
|
|
|
86
|
0
|
0
|
0
|
|
|
|
if ($self->{'debug'} and !$response->{'success'}) {
|
|
87
|
0
|
0
|
|
|
|
|
croak $response if $self->{'debug'} eq 'verbose';
|
|
88
|
0
|
|
|
|
|
|
croak $response->{'content'};
|
|
89
|
|
|
|
|
|
|
}
|
|
90
|
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $reply = decode_json($response->{'content'});
|
|
92
|
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
return $reply->{'data'}[0]->{'url'};
|
|
94
|
|
|
|
|
|
|
}
|
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__
|