-
Notifications
You must be signed in to change notification settings - Fork 27
Add ocr-translation task type and provider #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
julien-nc
wants to merge
1
commit into
main
Choose a base branch
from
enh/noid/ocr-translation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+243
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Assistant\TaskProcessing; | ||
|
|
||
| use Exception; | ||
| use OCA\Assistant\AppInfo\Application; | ||
| use OCA\Assistant\Service\TaskProcessingService; | ||
| use OCP\Files\File; | ||
| use OCP\IL10N; | ||
| use OCP\TaskProcessing\ISynchronousProvider; | ||
| use OCP\TaskProcessing\Task; | ||
| use OCP\TaskProcessing\TaskTypes\ImageToTextOpticalCharacterRecognition; | ||
| use OCP\TaskProcessing\TaskTypes\TextToTextTranslate; | ||
| use Psr\Log\LoggerInterface; | ||
| use RuntimeException; | ||
|
|
||
| class ImageToTextTranslateProvider implements ISynchronousProvider { | ||
|
|
||
| public function __construct( | ||
| private IL10N $l, | ||
| private TaskProcessingService $taskProcessingService, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| } | ||
|
|
||
| public function getId(): string { | ||
| return Application::APP_ID . '-image2text:translate'; | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| return $this->l->t('Assistant'); | ||
| } | ||
|
|
||
| public function getTaskTypeId(): string { | ||
| return ImageToTextTranslateTaskType::ID; | ||
| } | ||
|
|
||
| public function getExpectedRuntime(): int { | ||
| return 60; | ||
| } | ||
|
|
||
| public function getInputShapeEnumValues(): array { | ||
| $translateProvider = $this->taskProcessingService->getPreferredProvider(TextToTextTranslate::ID); | ||
|
|
||
| return [ | ||
| 'origin_language' => $translateProvider->getInputShapeEnumValues()['origin_language'], | ||
| 'target_language' => $translateProvider->getInputShapeEnumValues()['target_language'], | ||
| ]; | ||
| } | ||
|
|
||
| public function getInputShapeDefaults(): array { | ||
| $translateProvider = $this->taskProcessingService->getPreferredProvider(TextToTextTranslate::ID); | ||
| return [ | ||
| 'origin_language' => $translateProvider->getInputShapeDefaults()['origin_language'], | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
| 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']) || !is_array($input['input'])) { | ||
| throw new RuntimeException('Invalid input'); | ||
| } | ||
| foreach ($input['input'] as $i => $inputImage) { | ||
| if (!($inputImage instanceof File) || !$inputImage->isReadable()) { | ||
| throw new RuntimeException('Invalid input images'); | ||
| } | ||
| } | ||
|
|
||
| if (!isset($input['origin_language']) || !is_string($input['origin_language'])) { | ||
| throw new RuntimeException('Invalid origin_language input'); | ||
| } | ||
| if (!isset($input['target_language']) || !is_string($input['target_language'])) { | ||
| throw new RuntimeException('Invalid target_language input'); | ||
| } | ||
|
|
||
| // OCR | ||
| $ocrInputs = array_map(static function (File $file) { | ||
| return $file->getId(); | ||
| }, $input['input']); | ||
| try { | ||
| $task = new Task( | ||
| ImageToTextOpticalCharacterRecognition::ID, | ||
| ['input' => $ocrInputs], | ||
| Application::APP_ID . ':internal', | ||
| $userId, | ||
| ); | ||
| $taskOutput = $this->taskProcessingService->runTaskProcessingTask($task); | ||
| $ocrOutputs = $taskOutput['output']; | ||
| } catch (Exception $e) { | ||
| $this->logger->warning('OCR sub task failed with: ' . $e->getMessage(), ['exception' => $e]); | ||
| throw new RuntimeException('OCR sub task failed with: ' . $e->getMessage()); | ||
| } | ||
|
|
||
| $translatedOutputs = []; | ||
| foreach ($ocrOutputs as $ocrOutput) { | ||
| try { | ||
| $task = new Task( | ||
| TextToTextTranslate::ID, | ||
| [ | ||
| 'input' => $ocrOutput, | ||
| 'origin_language' => $input['origin_language'], | ||
| 'target_language' => $input['target_language'], | ||
| ], | ||
| Application::APP_ID . ':internal', | ||
| $userId, | ||
| ); | ||
| $taskOutput = $this->taskProcessingService->runTaskProcessingTask($task); | ||
| $translatedOutputs[] = $taskOutput['output']; | ||
| } catch (Exception $e) { | ||
| $this->logger->warning('Translation sub task failed with: ' . $e->getMessage(), ['exception' => $e]); | ||
| throw new RuntimeException('Translation sub task failed with: ' . $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| // Translation | ||
| return [ | ||
| 'output' => $translatedOutputs, | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Assistant\TaskProcessing; | ||
|
|
||
| use OCA\Assistant\AppInfo\Application; | ||
| use OCP\IL10N; | ||
| use OCP\TaskProcessing\EShapeType; | ||
| use OCP\TaskProcessing\ITaskType; | ||
| use OCP\TaskProcessing\ShapeDescriptor; | ||
|
|
||
| class ImageToTextTranslateTaskType implements ITaskType { | ||
| public const ID = Application::APP_ID . ':image2text:translate'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we put this in server? |
||
|
|
||
| public function __construct( | ||
| private IL10N $l, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getName(): string { | ||
| return $this->l->t('Translate image'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getDescription(): string { | ||
| return $this->l->t('Translate the text content of an image'); | ||
| } | ||
|
|
||
| /** | ||
| * @return string | ||
| */ | ||
| public function getId(): string { | ||
| return self::ID; | ||
| } | ||
|
|
||
| /** | ||
| * @return ShapeDescriptor[] | ||
| */ | ||
| public function getInputShape(): array { | ||
| return [ | ||
| 'input' => new ShapeDescriptor( | ||
| $this->l->t('Input files'), | ||
| $this->l->t('The files to extract text from'), | ||
| EShapeType::ListOfFiles | ||
| ), | ||
| 'origin_language' => new ShapeDescriptor( | ||
| $this->l->t('Origin language'), | ||
| $this->l->t('The language of the origin text'), | ||
| EShapeType::Enum | ||
| ), | ||
| 'target_language' => new ShapeDescriptor( | ||
| $this->l->t('Target language'), | ||
| $this->l->t('The desired language to translate the origin text in'), | ||
| EShapeType::Enum | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return ShapeDescriptor[] | ||
| */ | ||
| public function getOutputShape(): array { | ||
| return [ | ||
| 'output' => new ShapeDescriptor( | ||
| $this->l->t('Output texts'), | ||
| $this->l->t('The texts that were extracted from the files'), | ||
| EShapeType::ListOfTexts | ||
| ), | ||
| ]; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cool to use
$reportProgresshere, so the user gets some feedback :)