FakeTextToImageProvider.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. declare(strict_types=1);
  7. namespace OCA\Testing\TaskProcessing;
  8. use OCA\Testing\AppInfo\Application;
  9. use OCP\AppFramework\Services\IAppConfig;
  10. use OCP\TaskProcessing\EShapeType;
  11. use OCP\TaskProcessing\Exception\ProcessingException;
  12. use OCP\TaskProcessing\ISynchronousProvider;
  13. use OCP\TaskProcessing\ShapeDescriptor;
  14. use OCP\TaskProcessing\TaskTypes\TextToImage;
  15. use RuntimeException;
  16. class FakeTextToImageProvider implements ISynchronousProvider {
  17. public function __construct(
  18. protected IAppConfig $appConfig,
  19. ) {
  20. }
  21. public function getId(): string {
  22. return Application::APP_ID . '-text2image';
  23. }
  24. public function getName(): string {
  25. return 'Fake text2image task processing provider';
  26. }
  27. public function getTaskTypeId(): string {
  28. return TextToImage::ID;
  29. }
  30. public function getExpectedRuntime(): int {
  31. return 1;
  32. }
  33. public function getInputShapeEnumValues(): array {
  34. return [];
  35. }
  36. public function getInputShapeDefaults(): array {
  37. return [
  38. 'numberOfImages' => 1,
  39. ];
  40. }
  41. public function getOptionalInputShape(): array {
  42. return [
  43. 'size' => new ShapeDescriptor(
  44. 'Size',
  45. 'Optional. The size of the generated images. Must be in 256x256 format.',
  46. EShapeType::Text
  47. ),
  48. ];
  49. }
  50. public function getOptionalInputShapeEnumValues(): array {
  51. return [];
  52. }
  53. public function getOptionalInputShapeDefaults(): array {
  54. return [];
  55. }
  56. public function getOutputShapeEnumValues(): array {
  57. return [];
  58. }
  59. public function getOptionalOutputShape(): array {
  60. return [];
  61. }
  62. public function getOptionalOutputShapeEnumValues(): array {
  63. return [];
  64. }
  65. public function process(?string $userId, array $input, callable $reportProgress): array {
  66. if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) {
  67. throw new ProcessingException('Failing as set by AppConfig');
  68. }
  69. if (!isset($input['input']) || !is_string($input['input'])) {
  70. throw new RuntimeException('Invalid prompt');
  71. }
  72. $prompt = $input['input'];
  73. $nbImages = 1;
  74. if (isset($input['numberOfImages']) && is_int($input['numberOfImages'])) {
  75. $nbImages = $input['numberOfImages'];
  76. }
  77. $fakeContent = file_get_contents(__DIR__ . '/../../img/logo.png');
  78. $output = ['images' => []];
  79. foreach (range(1, $nbImages) as $i) {
  80. $output['images'][] = $fakeContent;
  81. }
  82. /** @var array<string, list<numeric|string>|numeric|string> $output */
  83. return $output;
  84. }
  85. }