Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/TaskProcessing/TextToImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
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;
use RuntimeException;

class TextToImageProvider implements ISynchronousProvider {

Expand Down Expand Up @@ -108,7 +108,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
$startTime = time();

if (!isset($input['input']) || !is_string($input['input'])) {
throw new RuntimeException('Invalid prompt');
throw new ProcessingException('Invalid prompt');
}
$prompt = $input['input'];

Expand All @@ -117,11 +117,23 @@ public function process(?string $userId, array $input, callable $reportProgress)
$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 {
Expand All @@ -148,7 +160,7 @@ public function process(?string $userId, array $input, callable $reportProgress)

if (empty($urls) && empty($b64s)) {
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed: no image returned');
throw new RuntimeException('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);
Expand All @@ -167,7 +179,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
return $output;
} catch (\Exception $e) {
$this->logger->warning('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
throw new ProcessingException('OpenAI/LocalAI\'s text to image generation failed with: ' . $e->getMessage());
}
}
}
Loading