|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\OpenAi\TaskProcessing; |
| 11 | + |
| 12 | +use OCA\OpenAi\AppInfo\Application; |
| 13 | +use OCA\OpenAi\Service\OpenAiAPIService; |
| 14 | +use OCP\TaskProcessing\IManager; |
| 15 | +use OCP\TaskProcessing\ISynchronousProvider; |
| 16 | +use OCP\TaskProcessing\Task; |
| 17 | +use OCP\TaskProcessing\TaskTypes\AudioToText; |
| 18 | +use Psr\Log\LoggerInterface; |
| 19 | +use Throwable; |
| 20 | + |
| 21 | +class AudioToTextEnhancedProvider implements ISynchronousProvider { |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private AudioToTextProvider $audioToTextProvider, |
| 25 | + private OpenAiAPIService $openAiAPIService, |
| 26 | + private IManager $taskProcessingManager, |
| 27 | + private LoggerInterface $logger, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function getId(): string { |
| 32 | + return $this->audioToTextProvider->getId() . '-enhanced'; |
| 33 | + } |
| 34 | + |
| 35 | + public function getName(): string { |
| 36 | + return $this->audioToTextProvider->getName() . ' (with paragraph reformatting)'; |
| 37 | + } |
| 38 | + |
| 39 | + public function getTaskTypeId(): string { |
| 40 | + return AudioToText::ID; |
| 41 | + } |
| 42 | + |
| 43 | + public function getExpectedRuntime(): int { |
| 44 | + // The audio to text provider may not be openai and this assumes it is |
| 45 | + return $this->audioToTextProvider->getExpectedRuntime() + $this->openAiAPIService->getExpTextProcessingTime(); |
| 46 | + } |
| 47 | + |
| 48 | + public function getInputShapeEnumValues(): array { |
| 49 | + return $this->audioToTextProvider->getInputShapeEnumValues(); |
| 50 | + } |
| 51 | + |
| 52 | + public function getInputShapeDefaults(): array { |
| 53 | + return $this->audioToTextProvider->getInputShapeDefaults(); |
| 54 | + } |
| 55 | + |
| 56 | + public function getOptionalInputShape(): array { |
| 57 | + return $this->audioToTextProvider->getOptionalInputShape(); |
| 58 | + } |
| 59 | + |
| 60 | + public function getOptionalInputShapeEnumValues(): array { |
| 61 | + return $this->audioToTextProvider->getOptionalInputShapeEnumValues(); |
| 62 | + } |
| 63 | + |
| 64 | + public function getOptionalInputShapeDefaults(): array { |
| 65 | + return $this->audioToTextProvider->getOptionalInputShapeDefaults(); |
| 66 | + } |
| 67 | + |
| 68 | + public function getOutputShapeEnumValues(): array { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + public function getOptionalOutputShape(): array { |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + public function getOptionalOutputShapeEnumValues(): array { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + public function process(?string $userId, array $input, callable $reportProgress): array { |
| 81 | + $transcription = $this->audioToTextProvider->process($userId, $input, $reportProgress)['output']; |
| 82 | + |
| 83 | + // Skip reformatting if the transcription is empty |
| 84 | + if (trim($transcription) === '') { |
| 85 | + return ['output' => $transcription]; |
| 86 | + } |
| 87 | + |
| 88 | + $reformatTask = new Task( |
| 89 | + \OCP\TaskProcessing\TaskTypes\TextToTextReformatParagraphs::ID, |
| 90 | + ['input' => $transcription], |
| 91 | + Application::APP_ID, |
| 92 | + $userId, |
| 93 | + 'audio2text_enhanced', |
| 94 | + ); |
| 95 | + |
| 96 | + try { |
| 97 | + $finished = $this->taskProcessingManager->runTask($reformatTask); |
| 98 | + $output = $finished->getOutput(); |
| 99 | + if (is_array($output) && isset($output['output']) && is_string($output['output']) && $output['output'] !== '') { |
| 100 | + return ['output' => $output['output']]; |
| 101 | + } |
| 102 | + $this->logger->warning('ReformatParagraphs follow-up task returned no usable output, falling back to raw transcription'); |
| 103 | + } catch (Throwable $e) { |
| 104 | + $this->logger->warning('ReformatParagraphs follow-up task failed, falling back to raw transcription: ' . $e->getMessage(), ['exception' => $e]); |
| 105 | + } |
| 106 | + |
| 107 | + return ['output' => $transcription]; |
| 108 | + } |
| 109 | +} |
0 commit comments