Skip to content

Commit

Permalink
Merge pull request #47177 from nextcloud/fix/noid/preferred-taskproce…
Browse files Browse the repository at this point in the history
…ssing-providers

[Task processing] Fix preferred providers
  • Loading branch information
julien-nc authored Aug 12, 2024
2 parents b34edf2 + 475d008 commit 735e04e
Show file tree
Hide file tree
Showing 11 changed files with 602 additions and 15 deletions.
5 changes: 5 additions & 0 deletions apps/testing/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
'OCA\\Testing\\Provider\\FakeTextProcessingProviderSync' => $baseDir . '/../lib/Provider/FakeTextProcessingProviderSync.php',
'OCA\\Testing\\Provider\\FakeTranslationProvider' => $baseDir . '/../lib/Provider/FakeTranslationProvider.php',
'OCA\\Testing\\Settings\\DeclarativeSettingsForm' => $baseDir . '/../lib/Settings/DeclarativeSettingsForm.php',
'OCA\\Testing\\TaskProcessing\\FakeContextWriteProvider' => $baseDir . '/../lib/TaskProcessing/FakeContextWriteProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTextToImageProvider' => $baseDir . '/../lib/TaskProcessing/FakeTextToImageProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTextToTextProvider' => $baseDir . '/../lib/TaskProcessing/FakeTextToTextProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTranscribeProvider' => $baseDir . '/../lib/TaskProcessing/FakeTranscribeProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTranslateProvider' => $baseDir . '/../lib/TaskProcessing/FakeTranslateProvider.php',
);
5 changes: 5 additions & 0 deletions apps/testing/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class ComposerStaticInitTesting
'OCA\\Testing\\Provider\\FakeTextProcessingProviderSync' => __DIR__ . '/..' . '/../lib/Provider/FakeTextProcessingProviderSync.php',
'OCA\\Testing\\Provider\\FakeTranslationProvider' => __DIR__ . '/..' . '/../lib/Provider/FakeTranslationProvider.php',
'OCA\\Testing\\Settings\\DeclarativeSettingsForm' => __DIR__ . '/..' . '/../lib/Settings/DeclarativeSettingsForm.php',
'OCA\\Testing\\TaskProcessing\\FakeContextWriteProvider' => __DIR__ . '/..' . '/../lib/TaskProcessing/FakeContextWriteProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTextToImageProvider' => __DIR__ . '/..' . '/../lib/TaskProcessing/FakeTextToImageProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTextToTextProvider' => __DIR__ . '/..' . '/../lib/TaskProcessing/FakeTextToTextProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTranscribeProvider' => __DIR__ . '/..' . '/../lib/TaskProcessing/FakeTranscribeProvider.php',
'OCA\\Testing\\TaskProcessing\\FakeTranslateProvider' => __DIR__ . '/..' . '/../lib/TaskProcessing/FakeTranslateProvider.php',
);

public static function getInitializer(ClassLoader $loader)
Expand Down
17 changes: 15 additions & 2 deletions apps/testing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
use OCA\Testing\Provider\FakeTextProcessingProviderSync;
use OCA\Testing\Provider\FakeTranslationProvider;
use OCA\Testing\Settings\DeclarativeSettingsForm;
use OCA\Testing\TaskProcessing\FakeContextWriteProvider;
use OCA\Testing\TaskProcessing\FakeTextToImageProvider;
use OCA\Testing\TaskProcessing\FakeTextToTextProvider;
use OCA\Testing\TaskProcessing\FakeTranscribeProvider;
use OCA\Testing\TaskProcessing\FakeTranslateProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
Expand All @@ -24,8 +29,10 @@
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent;

class Application extends App implements IBootstrap {
public const APP_ID = 'testing';

public function __construct(array $urlParams = []) {
parent::__construct('testing', $urlParams);
parent::__construct(self::APP_ID, $urlParams);
}

public function register(IRegistrationContext $context): void {
Expand All @@ -34,6 +41,12 @@ public function register(IRegistrationContext $context): void {
$context->registerTextProcessingProvider(FakeTextProcessingProviderSync::class);
$context->registerTextToImageProvider(FakeText2ImageProvider::class);

$context->registerTaskProcessingProvider(FakeTextToTextProvider::class);
$context->registerTaskProcessingProvider(FakeTextToImageProvider::class);
$context->registerTaskProcessingProvider(FakeTranslateProvider::class);
$context->registerTaskProcessingProvider(FakeTranscribeProvider::class);
$context->registerTaskProcessingProvider(FakeContextWriteProvider::class);

$context->registerDeclarativeSettings(DeclarativeSettingsForm::class);
$context->registerEventListener(DeclarativeSettingsRegisterFormEvent::class, RegisterDeclarativeSettingsListener::class);
$context->registerEventListener(DeclarativeSettingsGetValueEvent::class, GetDeclarativeSettingsValueListener::class);
Expand All @@ -43,7 +56,7 @@ public function register(IRegistrationContext $context): void {
public function boot(IBootContext $context): void {
$server = $context->getServerContainer();
$config = $server->getConfig();
if ($config->getAppValue('testing', 'enable_alt_user_backend', 'no') === 'yes') {
if ($config->getAppValue(self::APP_ID, 'enable_alt_user_backend', 'no') === 'yes') {
$userManager = $server->getUserManager();

// replace all user backends with this one
Expand Down
121 changes: 121 additions & 0 deletions apps/testing/lib/TaskProcessing/FakeContextWriteProvider.php
Original file line number Diff line number Diff line change
@@ -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];
}
}
99 changes: 99 additions & 0 deletions apps/testing/lib/TaskProcessing/FakeTextToImageProvider.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 735e04e

Please sign in to comment.