-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTextToImageProvider.php
More file actions
185 lines (157 loc) · 5.91 KB
/
TextToImageProvider.php
File metadata and controls
185 lines (157 loc) · 5.91 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\OpenAi\TaskProcessing;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCP\Http\Client\IClientService;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\Exception\ProcessingException;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\ShapeDescriptor;
use OCP\TaskProcessing\TaskTypes\TextToImage;
use Psr\Log\LoggerInterface;
class TextToImageProvider implements ISynchronousProvider {
public function __construct(
private OpenAiAPIService $openAiAPIService,
private IL10N $l,
private LoggerInterface $logger,
private IClientService $clientService,
private IAppConfig $appConfig,
private ?string $userId,
) {
}
public function getId(): string {
return Application::APP_ID . '-text2image';
}
public function getName(): string {
return $this->openAiAPIService->isUsingOpenAi()
? $this->l->t('OpenAI\'s DALL-E 2')
: $this->openAiAPIService->getServiceName();
}
public function getTaskTypeId(): string {
return TextToImage::ID;
}
public function getExpectedRuntime(): int {
return $this->openAiAPIService->getExpTextProcessingTime();
}
public function getInputShapeEnumValues(): array {
return [];
}
public function getInputShapeDefaults(): array {
return [
'numberOfImages' => 1,
];
}
public function getOptionalInputShape(): array {
$defaultImageSize = $this->appConfig->getValueString(Application::APP_ID, 'default_image_size', lazy: true) ?: Application::DEFAULT_DEFAULT_IMAGE_SIZE;
return [
'size' => new ShapeDescriptor(
$this->l->t('Size'),
$this->l->t('Optional. The size of the generated images. Must be in 256x256 format. Default is %s', [$defaultImageSize]),
EShapeType::Text
),
'model' => new ShapeDescriptor(
$this->l->t('Model'),
$this->l->t('The model used to generate the images'),
EShapeType::Enum
),
];
}
public function getOptionalInputShapeEnumValues(): array {
return [
'model' => $this->openAiAPIService->getModelEnumValues($this->userId),
];
}
public function getOptionalInputShapeDefaults(): array {
$adminModel = $this->openAiAPIService->isUsingOpenAi()
? ($this->appConfig->getValueString(Application::APP_ID, 'default_image_model_id', Application::DEFAULT_MODEL_ID, lazy: true) ?: Application::DEFAULT_MODEL_ID)
: $this->appConfig->getValueString(Application::APP_ID, 'default_image_model_id', lazy: true);
return [
'model' => $adminModel,
];
}
public function getOutputShapeEnumValues(): array {
return [];
}
public function getOptionalOutputShape(): array {
return [];
}
public function getOptionalOutputShapeEnumValues(): array {
return [];
}
public function process(?string $userId, array $input, callable $reportProgress): array {
$startTime = time();
if (!isset($input['input']) || !is_string($input['input'])) {
throw new ProcessingException('Invalid prompt');
}
$prompt = $input['input'];
$nbImages = 1;
if (isset($input['numberOfImages']) && is_int($input['numberOfImages'])) {
$nbImages = $input['numberOfImages'];
}
if ($nbImages > 12) {
throw new ProcessingException('numberOfImages is out of bounds: Cannot generate more than 12 images');
}
if ($nbImages < 1) {
throw new ProcessingException('numberOfImages is out of bounds: Cannot generate less than 1 image');
}
$size = $this->appConfig->getValueString(Application::APP_ID, 'default_image_size', lazy: true) ?: Application::DEFAULT_DEFAULT_IMAGE_SIZE;
if (isset($input['size']) && is_string($input['size']) && preg_match('/^\d+x\d+$/', $input['size'])) {
$size = trim($input['size']);
}
[$x, $y] = explode('x', $size, 2);
if ((int)$x > 4096 || (int)$y > 4096) {
throw new ProcessingException('size is out of bounds: should be within 4096x4096');
}
if (isset($input['model']) && is_string($input['model'])) {
$model = $input['model'];
} else {
$model = $this->appConfig->getValueString(Application::APP_ID, 'default_image_model_id', Application::DEFAULT_MODEL_ID, lazy: true) ?: Application::DEFAULT_MODEL_ID;
}
try {
$apiResponse = $this->openAiAPIService->requestImageCreation($userId, $prompt, $model, $nbImages, $size);
$b64s = array_map(static function (array $result) {
return $result['b64_json'] ?? null;
}, $apiResponse['data']);
$b64s = array_filter($b64s, static function (?string $b64) {
return $b64 !== null;
});
$b64s = array_values($b64s);
$urls = array_map(static function (array $result) {
return $result['url'] ?? null;
}, $apiResponse['data']);
$urls = array_filter($urls, static function (?string $url) {
return $url !== null;
});
$urls = array_values($urls);
if (empty($urls) && empty($b64s)) {
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed: no image returned');
throw new ProcessingException('OpenAI/LocalAI\'s text to image generation failed: no image returned');
}
$client = $this->clientService->newClient();
$requestOptions = $this->openAiAPIService->getImageRequestOptions($userId);
$output = ['images' => []];
foreach ($urls as $url) {
$imageResponse = $client->get($url, $requestOptions);
$output['images'][] = $imageResponse->getBody();
}
foreach ($b64s as $b64) {
$imagePayload = base64_decode($b64);
$output['images'][] = $imagePayload;
}
$endTime = time();
$this->openAiAPIService->updateExpImgProcessingTime($endTime - $startTime);
/** @var array<string, list<numeric|string>|numeric|string> $output */
return $output;
} catch (\Exception $e) {
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new ProcessingException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
}
}
}