-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.php
301 lines (274 loc) · 9.98 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version details.
*
* @package repository_txttoimg
* @copyright 2022 OpenApp By Yedidia Klein http://openapp.co.il
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
/**
* Main repository_txttoimg class.
*
* @package repository_txttoimg
* @copyright 2022
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_txttoimg extends repository {
/**
* Get Listing function
*
* This function is for init of repository.
* @param string $path
* @param int $page
*
* @return array
*/
public function get_listing($path = '', $page = '') {
return ['list' => []];
}
/**
* Search function
*
* This is the function that do the search in txttoimg and return an array of images.
* @param string $searchtext
* @param int $page
*
* @return array
*/
public function search($searchtext, $page = 0) {
global $SESSION, $CFG, $USER;
$perpage = 10;
if (($searchtext == "") && (isset($SESSION->txttoimgsearch))) {
$q = $SESSION->txttoimgsearch;
} else {
$q = $searchtext;
$SESSION->txttoimgsearch = $q;
}
if (!$page) {
$page = 1;
}
$key = get_config('txttoimg', 'key');
$images = get_config('txttoimg', 'images');
$size = get_config('txttoimg', 'size');
switch ($size) {
case '0':
$size = 256;
break;
case '1':
$size = 512;
break;
case '2':
$size = 1024;
break;
default:
$size = 512;
}
$size = $size . 'x' . $size;
$model = get_config('txttoimg', 'version');
switch ($model) {
case '0':
$model = 'dall-e-2';
break;
case '1':
$model = 'dall-e-3';
break;
default:
$model = 'dall-e-2';
}
if ($model == 'dall-e-3') {
$sizever3 = get_config('txttoimg', 'sizever3');
switch ($sizever3) {
case '0':
$size = '1024x1024';
break;
case '1':
$size = '1024x1792';
break;
case '2':
$size = '1792x1024';
break;
default:
$size = '1024x1024';
}
// Currently Dall-e 3 API force creating one image.
$images = 1;
}
$url = 'https://api.openai.com/v1/images/generations';
$authorization = "Authorization: Bearer " . $key;
$data = "{
\"model\": \"$model\",
\"prompt\": \"$q\",
\"n\": $images,
\"size\": \"$size\"}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json' , $authorization ]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = json_decode(curl_exec($ch));
curl_close($ch);
if (isset($result->error)) {
$title = 'Error.png';
$list[] = [ 'shorttitle' => $result->error->code,
'thumbnail_title' => $result->error->message,
'title' => $result->error->code,
'description' => $result->error->message,
'thumbnail' => $CFG->wwwroot . '/repository/txttoimg/pix/error.png',
'thumbnail_width' => 150,
'thumbnail_height' => 100,
'size' => 10000,
'author' => $USER->firstname . ' ' . $USER->lastname,
'source' => $CFG->wwwroot . '/repository/txttoimg/pix/error.png',
'license' => 'public',
];
} else {
$arresult = $result->data;
for ($imagecounter = 0; $imagecounter < $images; $imagecounter++) {
$title = $q . '-' . $imagecounter . '.png';
$list[] = [ 'shorttitle' => $title,
'thumbnail_title' => $title,
'title' => $title,
'description' => $title,
'thumbnail' => $arresult[$imagecounter]->url,
'thumbnail_width' => 150,
'thumbnail_height' => 100,
'size' => 10000,
'author' => $USER->firstname . ' ' . $USER->lastname,
'source' => $arresult[$imagecounter]->url,
'license' => 'public',
];
}
}
$ret = [];
$ret['nologin'] = false;
$ret['page'] = (int)$page;
if ($ret['page'] < 1) {
$ret['page'] = 1;
}
$start = 1;
$max = 10;
$ret['list'] = $list;
$ret['norefresh'] = true;
$ret['nosearch'] = false;
// If the number of results is smaller than $max, it means we reached the last page.
$ret['pages'] = (count($ret['list']) < $max) ? $ret['page'] : -1;
return $ret;
}
/**
* get type option name function
*
* This function is for module settings.
* @return array
*/
public static function get_type_option_names() {
return array_merge(parent::get_type_option_names(), ['images', 'size', 'key', 'version', 'sizever3']);
}
/**
* get type config form function
*
* This function is the form of module settings.
*
* @param object $mform
* @param string $classname
*
* @return none
*/
public static function type_config_form($mform, $classname = 'repository') {
parent::type_config_form($mform);
$version = get_config('repository_txttoimg', 'version');
$select = $mform->addElement('select', 'version', get_string('version', 'repository_txttoimg'),
[ 'Dall-e 2',
'Dall-e 3', ]);
$select->setSelected($version);
$size = get_config('repository_txttoimg', 'size');
$select = $mform->addElement('select', 'size', get_string('size', 'repository_txttoimg'), ['256', '512', '1024']);
$select->setSelected($size);
$size = get_config('repository_txttoimg', 'sizever3');
// On Dall-e 3 the sizes are 1024x1024, 1024x1792 or 1792x1024.
$select = $mform->addElement('select', 'sizever3', get_string('sizever3', 'repository_txttoimg'),
[ get_string('square', 'repository_txttoimg'),
get_string('portrait', 'repository_txttoimg'),
get_string('landscape', 'repository_txttoimg'), ]);
$select->setSelected($sizever3);
$key = get_config('repository_txttoimg', 'key');
$mform->addElement('password', 'key', get_string('api', 'repository_txttoimg') . " ("
. get_string('api_description', 'repository_txttoimg') . ")" , ['size' => '60']);
$mform->setDefault('key', $key);
$mform->setType('key', PARAM_RAW_TRIMMED);
$images = get_config('repository_txttoimg', 'images');
$select = $mform->addElement('select', 'images', get_string('images', 'repository_txttoimg') .
" (" . get_string('images_description', 'repository_txttoimg') . ")",
[1 => '1', 2 => '2', 3 => '3' , 4 => '4']);
$select->setSelected($images);
}
/**
* check login function
*
* This function help showing the search form.
* @return bool
*/
public function check_login() {
return !empty($this->keyword);
}
/**
* print login function
*
* This function generates the search form.
* @param bool $ajax
*
* @return array
*/
public function print_login($ajax = true) {
$ret = [];
$check = get_config('txttoimg', 'key');
if (trim($check) == "") {
$warning = "<p class='errorbox'>" . get_string('warning', 'repository_txttoimg') . "</p>";
} else {
$warning = "";
}
$search = new stdClass();
$search->type = 'text';
$search->id = 'txttoimg_search';
$search->name = 's';
$search->label = $warning . get_string('search', 'repository_txttoimg').': ';
$ret['login'] = [$search];
$ret['login_btn_label'] = get_string('search');
$ret['login_btn_action'] = 'search';
$ret['allowcaching'] = true; // Indicates that login form can be cached in filepicker.js.
return $ret;
}
/**
* supported returntype function
*
* plugin only return internal links, according to txttoimg term of use.
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL;
}
/**
* Is this repository accessing private data?
*
* @return bool
*/
public function contains_private_data() {
return false;
}
}