feat(taskprocessing): add text2text, text2image, contextWrite, transcribe and translate providers in the testing app
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>pull/47177/head
parent
dbab2a825d
commit
475d0089c2
@ -0,0 +1,121 @@ |
||||
<?php |
||||
/** |
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OCA\Testing\TaskProcessing; |
||||
|
||||
use OCA\Testing\AppInfo\Application; |
||||
use OCP\TaskProcessing\EShapeType; |
||||
use OCP\TaskProcessing\ISynchronousProvider; |
||||
use OCP\TaskProcessing\ShapeDescriptor; |
||||
use OCP\TaskProcessing\ShapeEnumValue; |
||||
use OCP\TaskProcessing\TaskTypes\ContextWrite; |
||||
use RuntimeException; |
||||
|
||||
class FakeContextWriteProvider implements ISynchronousProvider { |
||||
|
||||
public function __construct() { |
||||
} |
||||
|
||||
public function getId(): string { |
||||
return Application::APP_ID . '-contextwrite'; |
||||
} |
||||
|
||||
public function getName(): string { |
||||
return 'Fake context write task processing provider'; |
||||
} |
||||
|
||||
public function getTaskTypeId(): string { |
||||
return ContextWrite::ID; |
||||
} |
||||
|
||||
public function getExpectedRuntime(): int { |
||||
return 1; |
||||
} |
||||
|
||||
public function getInputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getInputShapeDefaults(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalInputShape(): array { |
||||
return [ |
||||
'max_tokens' => new ShapeDescriptor( |
||||
'Maximum output words', |
||||
'The maximum number of words/tokens that can be generated in the completion.', |
||||
EShapeType::Number |
||||
), |
||||
'model' => new ShapeDescriptor( |
||||
'Model', |
||||
'The model used to generate the completion', |
||||
EShapeType::Enum |
||||
), |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShapeEnumValues(): array { |
||||
return [ |
||||
'model' => [ |
||||
new ShapeEnumValue('Model 1', 'model_1'), |
||||
new ShapeEnumValue('Model 2', 'model_2'), |
||||
new ShapeEnumValue('Model 3', 'model_3'), |
||||
], |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShapeDefaults(): array { |
||||
return [ |
||||
'max_tokens' => 4321, |
||||
'model' => 'model_2', |
||||
]; |
||||
} |
||||
|
||||
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 { |
||||
if ( |
||||
!isset($input['style_input']) || !is_string($input['style_input']) |
||||
|| !isset($input['source_input']) || !is_string($input['source_input']) |
||||
) { |
||||
throw new RuntimeException('Invalid inputs'); |
||||
} |
||||
$writingStyle = $input['style_input']; |
||||
$sourceMaterial = $input['source_input']; |
||||
|
||||
if (isset($input['model']) && is_string($input['model'])) { |
||||
$model = $input['model']; |
||||
} else { |
||||
$model = 'unknown model'; |
||||
} |
||||
|
||||
$maxTokens = null; |
||||
if (isset($input['max_tokens']) && is_int($input['max_tokens'])) { |
||||
$maxTokens = $input['max_tokens']; |
||||
} |
||||
|
||||
$fakeResult = 'This is a fake result: ' |
||||
. "\n\n- Style input: " . $writingStyle |
||||
. "\n- Source input: " . $sourceMaterial |
||||
. "\n- Model: " . $model |
||||
. "\n- Maximum number of words: " . $maxTokens; |
||||
|
||||
return ['output' => $fakeResult]; |
||||
} |
||||
} |
||||
@ -0,0 +1,99 @@ |
||||
<?php |
||||
/** |
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OCA\Testing\TaskProcessing; |
||||
|
||||
use OCA\Testing\AppInfo\Application; |
||||
use OCP\TaskProcessing\EShapeType; |
||||
use OCP\TaskProcessing\ISynchronousProvider; |
||||
use OCP\TaskProcessing\ShapeDescriptor; |
||||
use OCP\TaskProcessing\TaskTypes\TextToImage; |
||||
use RuntimeException; |
||||
|
||||
class FakeTextToImageProvider implements ISynchronousProvider { |
||||
|
||||
public function __construct() { |
||||
} |
||||
|
||||
public function getId(): string { |
||||
return Application::APP_ID . '-text2image'; |
||||
} |
||||
|
||||
public function getName(): string { |
||||
return 'Fake text2image task processing provider'; |
||||
} |
||||
|
||||
public function getTaskTypeId(): string { |
||||
return TextToImage::ID; |
||||
} |
||||
|
||||
public function getExpectedRuntime(): int { |
||||
return 1; |
||||
} |
||||
|
||||
public function getInputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getInputShapeDefaults(): array { |
||||
return [ |
||||
'numberOfImages' => 1, |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShape(): array { |
||||
return [ |
||||
'size' => new ShapeDescriptor( |
||||
'Size', |
||||
'Optional. The size of the generated images. Must be in 256x256 format.', |
||||
EShapeType::Text |
||||
), |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalInputShapeDefaults(): array { |
||||
return []; |
||||
} |
||||
|
||||
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 { |
||||
if (!isset($input['input']) || !is_string($input['input'])) { |
||||
throw new RuntimeException('Invalid prompt'); |
||||
} |
||||
$prompt = $input['input']; |
||||
|
||||
$nbImages = 1; |
||||
if (isset($input['numberOfImages']) && is_int($input['numberOfImages'])) { |
||||
$nbImages = $input['numberOfImages']; |
||||
} |
||||
|
||||
$fakeContent = file_get_contents(__DIR__ . '/../../img/logo.png'); |
||||
|
||||
$output = ['images' => []]; |
||||
foreach (range(1, $nbImages) as $i) { |
||||
$output['images'][] = $fakeContent; |
||||
} |
||||
/** @var array<string, list<numeric|string>|numeric|string> $output */ |
||||
return $output; |
||||
} |
||||
} |
||||
@ -0,0 +1,113 @@ |
||||
<?php |
||||
/** |
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OCA\Testing\TaskProcessing; |
||||
|
||||
use OCA\Testing\AppInfo\Application; |
||||
use OCP\TaskProcessing\EShapeType; |
||||
use OCP\TaskProcessing\ISynchronousProvider; |
||||
use OCP\TaskProcessing\ShapeDescriptor; |
||||
use OCP\TaskProcessing\ShapeEnumValue; |
||||
use OCP\TaskProcessing\TaskTypes\TextToText; |
||||
use RuntimeException; |
||||
|
||||
class FakeTextToTextProvider implements ISynchronousProvider { |
||||
|
||||
public function __construct() { |
||||
} |
||||
|
||||
public function getId(): string { |
||||
return Application::APP_ID . '-text2text'; |
||||
} |
||||
|
||||
public function getName(): string { |
||||
return 'Fake text2text task processing provider'; |
||||
} |
||||
|
||||
public function getTaskTypeId(): string { |
||||
return TextToText::ID; |
||||
} |
||||
|
||||
public function getExpectedRuntime(): int { |
||||
return 1; |
||||
} |
||||
|
||||
public function getInputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getInputShapeDefaults(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalInputShape(): array { |
||||
return [ |
||||
'max_tokens' => new ShapeDescriptor( |
||||
'Maximum output words', |
||||
'The maximum number of words/tokens that can be generated in the completion.', |
||||
EShapeType::Number |
||||
), |
||||
'model' => new ShapeDescriptor( |
||||
'Model', |
||||
'The model used to generate the completion', |
||||
EShapeType::Enum |
||||
), |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShapeEnumValues(): array { |
||||
return [ |
||||
'model' => [ |
||||
new ShapeEnumValue('Model 1', 'model_1'), |
||||
new ShapeEnumValue('Model 2', 'model_2'), |
||||
new ShapeEnumValue('Model 3', 'model_3'), |
||||
], |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShapeDefaults(): array { |
||||
return [ |
||||
'max_tokens' => 1234, |
||||
'model' => 'model_2', |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalOutputShape(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOutputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalOutputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function process(?string $userId, array $input, callable $reportProgress): array { |
||||
if (isset($input['model']) && is_string($input['model'])) { |
||||
$model = $input['model']; |
||||
} else { |
||||
$model = 'unknown model'; |
||||
} |
||||
|
||||
if (!isset($input['input']) || !is_string($input['input'])) { |
||||
throw new RuntimeException('Invalid prompt'); |
||||
} |
||||
$prompt = $input['input']; |
||||
|
||||
$maxTokens = null; |
||||
if (isset($input['max_tokens']) && is_int($input['max_tokens'])) { |
||||
$maxTokens = $input['max_tokens']; |
||||
} |
||||
|
||||
return [ |
||||
'output' => 'This is a fake result: ' . "\n\n- Prompt: " . $prompt . "\n- Model: " . $model . "\n- Maximum number of words: " . $maxTokens, |
||||
]; |
||||
} |
||||
} |
||||
@ -0,0 +1,80 @@ |
||||
<?php |
||||
/** |
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OCA\Testing\TaskProcessing; |
||||
|
||||
use OCA\Testing\AppInfo\Application; |
||||
use OCP\Files\File; |
||||
use OCP\TaskProcessing\ISynchronousProvider; |
||||
use OCP\TaskProcessing\TaskTypes\AudioToText; |
||||
use RuntimeException; |
||||
|
||||
class FakeTranscribeProvider implements ISynchronousProvider { |
||||
|
||||
public function __construct( |
||||
) { |
||||
} |
||||
|
||||
public function getId(): string { |
||||
return Application::APP_ID . '-audio2text'; |
||||
} |
||||
|
||||
public function getName(): string { |
||||
return 'Fake audio2text task processing provider'; |
||||
} |
||||
|
||||
public function getTaskTypeId(): string { |
||||
return AudioToText::ID; |
||||
} |
||||
|
||||
public function getExpectedRuntime(): int { |
||||
return 1; |
||||
} |
||||
|
||||
public function getInputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getInputShapeDefaults(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalInputShape(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalInputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalInputShapeDefaults(): array { |
||||
return []; |
||||
} |
||||
|
||||
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 { |
||||
if (!isset($input['input']) || !$input['input'] instanceof File || !$input['input']->isReadable()) { |
||||
throw new RuntimeException('Invalid input file'); |
||||
} |
||||
$inputFile = $input['input']; |
||||
$transcription = 'Fake transcription result'; |
||||
|
||||
return ['output' => $transcription]; |
||||
} |
||||
} |
||||
@ -0,0 +1,146 @@ |
||||
<?php |
||||
/** |
||||
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OCA\Testing\TaskProcessing; |
||||
|
||||
use OCA\Testing\AppInfo\Application; |
||||
use OCP\L10N\IFactory; |
||||
use OCP\TaskProcessing\EShapeType; |
||||
use OCP\TaskProcessing\ISynchronousProvider; |
||||
use OCP\TaskProcessing\ShapeDescriptor; |
||||
use OCP\TaskProcessing\ShapeEnumValue; |
||||
use OCP\TaskProcessing\TaskTypes\TextToTextTranslate; |
||||
use RuntimeException; |
||||
|
||||
class FakeTranslateProvider implements ISynchronousProvider { |
||||
|
||||
public function __construct( |
||||
private IFactory $l10nFactory, |
||||
) { |
||||
} |
||||
|
||||
public function getId(): string { |
||||
return Application::APP_ID . '-translate'; |
||||
} |
||||
|
||||
public function getName(): string { |
||||
return 'Fake translate task processing provider'; |
||||
} |
||||
|
||||
public function getTaskTypeId(): string { |
||||
return TextToTextTranslate::ID; |
||||
} |
||||
|
||||
public function getExpectedRuntime(): int { |
||||
return 1; |
||||
} |
||||
|
||||
public function getInputShapeEnumValues(): array { |
||||
$coreL = $this->l10nFactory->getLanguages(); |
||||
$languages = array_merge($coreL['commonLanguages'], $coreL['otherLanguages']); |
||||
$languageEnumValues = array_map(static function (array $language) { |
||||
return new ShapeEnumValue($language['name'], $language['code']); |
||||
}, $languages); |
||||
$detectLanguageEnumValue = new ShapeEnumValue('Detect language', 'detect_language'); |
||||
return [ |
||||
'origin_language' => array_merge([$detectLanguageEnumValue], $languageEnumValues), |
||||
'target_language' => $languageEnumValues, |
||||
]; |
||||
} |
||||
|
||||
public function getInputShapeDefaults(): array { |
||||
return [ |
||||
'origin_language' => 'detect_language', |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShape(): array { |
||||
return [ |
||||
'max_tokens' => new ShapeDescriptor( |
||||
'Maximum output words', |
||||
'The maximum number of words/tokens that can be generated in the completion.', |
||||
EShapeType::Number |
||||
), |
||||
'model' => new ShapeDescriptor( |
||||
'Model', |
||||
'The model used to generate the completion', |
||||
EShapeType::Enum |
||||
), |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShapeEnumValues(): array { |
||||
return [ |
||||
'model' => [ |
||||
new ShapeEnumValue('Model 1', 'model_1'), |
||||
new ShapeEnumValue('Model 2', 'model_2'), |
||||
new ShapeEnumValue('Model 3', 'model_3'), |
||||
], |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalInputShapeDefaults(): array { |
||||
return [ |
||||
'max_tokens' => 200, |
||||
'model' => 'model_3', |
||||
]; |
||||
} |
||||
|
||||
public function getOptionalOutputShape(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOutputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
public function getOptionalOutputShapeEnumValues(): array { |
||||
return []; |
||||
} |
||||
|
||||
private function getCoreLanguagesByCode(): array { |
||||
$coreL = $this->l10nFactory->getLanguages(); |
||||
$coreLanguages = array_reduce(array_merge($coreL['commonLanguages'], $coreL['otherLanguages']), function ($carry, $val) { |
||||
$carry[$val['code']] = $val['name']; |
||||
return $carry; |
||||
}); |
||||
return $coreLanguages; |
||||
} |
||||
|
||||
public function process(?string $userId, array $input, callable $reportProgress): array { |
||||
if (isset($input['model']) && is_string($input['model'])) { |
||||
$model = $input['model']; |
||||
} else { |
||||
$model = 'model_3'; |
||||
} |
||||
|
||||
if (!isset($input['input']) || !is_string($input['input'])) { |
||||
throw new RuntimeException('Invalid input text'); |
||||
} |
||||
$inputText = $input['input']; |
||||
|
||||
$maxTokens = null; |
||||
if (isset($input['max_tokens']) && is_int($input['max_tokens'])) { |
||||
$maxTokens = $input['max_tokens']; |
||||
} |
||||
|
||||
$coreLanguages = $this->getCoreLanguagesByCode(); |
||||
|
||||
$toLanguage = $coreLanguages[$input['target_language']] ?? $input['target_language']; |
||||
if ($input['origin_language'] !== 'detect_language') { |
||||
$fromLanguage = $coreLanguages[$input['origin_language']] ?? $input['origin_language']; |
||||
$prompt = 'Fake translation from ' . $fromLanguage . ' to ' . $toLanguage . ': ' . $inputText; |
||||
} else { |
||||
$prompt = 'Fake Translation to ' . $toLanguage . ': ' . $inputText; |
||||
} |
||||
|
||||
$fakeResult = $prompt . "\n\nModel: " . $model . "\nMax tokens: " . $maxTokens; |
||||
|
||||
return ['output' => $fakeResult]; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue