Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Comment thread
kyteinsky marked this conversation as resolved.
$context->registerTaskProcessingProvider(AudioToTextEnhancedProvider::class);
}
}

$serviceUrl = $this->appConfig->getValueString(Application::APP_ID, 'url');
Expand Down
109 changes: 109 additions & 0 deletions lib/TaskProcessing/AudioToTextEnhancedProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenAi\TaskProcessing;

use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\Task;
use OCP\TaskProcessing\TaskTypes\AudioToText;
use Psr\Log\LoggerInterface;
use Throwable;

class AudioToTextEnhancedProvider implements ISynchronousProvider {

public function __construct(
private AudioToTextProvider $audioToTextProvider,
private OpenAiAPIService $openAiAPIService,
private IManager $taskProcessingManager,
private LoggerInterface $logger,
) {
}

public function getId(): string {
return $this->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',
);
Comment thread
lukasdotcom marked this conversation as resolved.

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');
Comment thread
lukasdotcom marked this conversation as resolved.
} catch (Throwable $e) {
$this->logger->warning('ReformatParagraphs follow-up task failed, falling back to raw transcription: ' . $e->getMessage(), ['exception' => $e]);
}

return ['output' => $transcription];
}
}
Loading