diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 902dbfe6..34c1941b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -11,6 +11,7 @@ use OCA\OpenAi\Notification\Notifier; use OCA\OpenAi\OldProcessing\Translation\TranslationProvider as OldTranslationProvider; use OCA\OpenAi\TaskProcessing\AudioToAudioChatProvider; +use OCA\OpenAi\TaskProcessing\AudioToTextEnhancedProvider; use OCA\OpenAi\TaskProcessing\AudioToTextProvider; use OCA\OpenAi\TaskProcessing\ChangeToneProvider; use OCA\OpenAi\TaskProcessing\ChangeToneTaskType; @@ -108,6 +109,9 @@ public function register(IRegistrationContext $context): void { } if ($this->appConfig->getValueString(Application::APP_ID, 'stt_provider_enabled', '1') === '1') { $context->registerTaskProcessingProvider(AudioToTextProvider::class); + if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToTextReformatParagraphs')) { + $context->registerTaskProcessingProvider(AudioToTextEnhancedProvider::class); + } } $serviceUrl = $this->appConfig->getValueString(Application::APP_ID, 'url'); diff --git a/lib/TaskProcessing/AudioToTextEnhancedProvider.php b/lib/TaskProcessing/AudioToTextEnhancedProvider.php new file mode 100644 index 00000000..a2dcf079 --- /dev/null +++ b/lib/TaskProcessing/AudioToTextEnhancedProvider.php @@ -0,0 +1,109 @@ +audioToTextProvider->getId() . '-enhanced'; + } + + public function getName(): string { + return $this->audioToTextProvider->getName() . ' (with paragraph reformatting)'; + } + + public function getTaskTypeId(): string { + return AudioToText::ID; + } + + public function getExpectedRuntime(): int { + // The audio to text provider may not be openai and this assumes it is + return $this->audioToTextProvider->getExpectedRuntime() + $this->openAiAPIService->getExpTextProcessingTime(); + } + + public function getInputShapeEnumValues(): array { + return $this->audioToTextProvider->getInputShapeEnumValues(); + } + + public function getInputShapeDefaults(): array { + return $this->audioToTextProvider->getInputShapeDefaults(); + } + + public function getOptionalInputShape(): array { + return $this->audioToTextProvider->getOptionalInputShape(); + } + + public function getOptionalInputShapeEnumValues(): array { + return $this->audioToTextProvider->getOptionalInputShapeEnumValues(); + } + + public function getOptionalInputShapeDefaults(): array { + return $this->audioToTextProvider->getOptionalInputShapeDefaults(); + } + + 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 { + $transcription = $this->audioToTextProvider->process($userId, $input, $reportProgress)['output']; + + // Skip reformatting if the transcription is empty + if (trim($transcription) === '') { + return ['output' => $transcription]; + } + + $reformatTask = new Task( + \OCP\TaskProcessing\TaskTypes\TextToTextReformatParagraphs::ID, + ['input' => $transcription], + Application::APP_ID, + $userId, + 'audio2text_enhanced', + ); + + try { + $finished = $this->taskProcessingManager->runTask($reformatTask); + $output = $finished->getOutput(); + if (is_array($output) && isset($output['output']) && is_string($output['output']) && $output['output'] !== '') { + return ['output' => $output['output']]; + } + $this->logger->warning('ReformatParagraphs follow-up task returned no usable output, falling back to raw transcription'); + } catch (Throwable $e) { + $this->logger->warning('ReformatParagraphs follow-up task failed, falling back to raw transcription: ' . $e->getMessage(), ['exception' => $e]); + } + + return ['output' => $transcription]; + } +}