Skip to content

Commit db7c024

Browse files
authored
Merge pull request #363 from nextcloud/better-transcript
Create enhanced AudioToTextEnhancedProvider that also reformats text from transcription and ReformatParagraphs provider
2 parents 2e43257 + 693e6e8 commit db7c024

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

lib/AppInfo/Application.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCA\OpenAi\Notification\Notifier;
1212
use OCA\OpenAi\OldProcessing\Translation\TranslationProvider as OldTranslationProvider;
1313
use OCA\OpenAi\TaskProcessing\AudioToAudioChatProvider;
14+
use OCA\OpenAi\TaskProcessing\AudioToTextEnhancedProvider;
1415
use OCA\OpenAi\TaskProcessing\AudioToTextProvider;
1516
use OCA\OpenAi\TaskProcessing\ChangeToneProvider;
1617
use OCA\OpenAi\TaskProcessing\ChangeToneTaskType;
@@ -108,6 +109,9 @@ public function register(IRegistrationContext $context): void {
108109
}
109110
if ($this->appConfig->getValueString(Application::APP_ID, 'stt_provider_enabled', '1') === '1') {
110111
$context->registerTaskProcessingProvider(AudioToTextProvider::class);
112+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToTextReformatParagraphs')) {
113+
$context->registerTaskProcessingProvider(AudioToTextEnhancedProvider::class);
114+
}
111115
}
112116

113117
$serviceUrl = $this->appConfig->getValueString(Application::APP_ID, 'url');
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)