FakeTextToImageProvider.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\TaskProcessing\EShapeType;
  10. use OCP\TaskProcessing\ISynchronousProvider;
  11. use OCP\TaskProcessing\ShapeDescriptor;
  12. use OCP\TaskProcessing\TaskTypes\TextToImage;
  13. use RuntimeException;
  14. class FakeTextToImageProvider implements ISynchronousProvider {
  15. public function __construct() {
  16. }
  17. public function getId(): string {
  18. return Application::APP_ID . '-text2image';
  19. }
  20. public function getName(): string {
  21. return 'Fake text2image task processing provider';
  22. }
  23. public function getTaskTypeId(): string {
  24. return TextToImage::ID;
  25. }
  26. public function getExpectedRuntime(): int {
  27. return 1;
  28. }
  29. public function getInputShapeEnumValues(): array {
  30. return [];
  31. }
  32. public function getInputShapeDefaults(): array {
  33. return [
  34. 'numberOfImages' => 1,
  35. ];
  36. }
  37. public function getOptionalInputShape(): array {
  38. return [
  39. 'size' => new ShapeDescriptor(
  40. 'Size',
  41. 'Optional. The size of the generated images. Must be in 256x256 format.',
  42. EShapeType::Text
  43. ),
  44. ];
  45. }
  46. public function getOptionalInputShapeEnumValues(): array {
  47. return [];
  48. }
  49. public function getOptionalInputShapeDefaults(): array {
  50. return [];
  51. }
  52. public function getOutputShapeEnumValues(): array {
  53. return [];
  54. }
  55. public function getOptionalOutputShape(): array {
  56. return [];
  57. }
  58. public function getOptionalOutputShapeEnumValues(): array {
  59. return [];
  60. }
  61. public function process(?string $userId, array $input, callable $reportProgress): array {
  62. if (!isset($input['input']) || !is_string($input['input'])) {
  63. throw new RuntimeException('Invalid prompt');
  64. }
  65. $prompt = $input['input'];
  66. $nbImages = 1;
  67. if (isset($input['numberOfImages']) && is_int($input['numberOfImages'])) {
  68. $nbImages = $input['numberOfImages'];
  69. }
  70. $fakeContent = file_get_contents(__DIR__ . '/../../img/logo.png');
  71. $output = ['images' => []];
  72. foreach (range(1, $nbImages) as $i) {
  73. $output['images'][] = $fakeContent;
  74. }
  75. /** @var array<string, list<numeric|string>|numeric|string> $output */
  76. return $output;
  77. }
  78. }