diff options
author | Julien Veyssier <julien-nc@posteo.net> | 2024-08-28 17:26:32 +0200 |
---|---|---|
committer | Julien Veyssier <julien-nc@posteo.net> | 2024-08-30 10:07:01 +0200 |
commit | 04edeb510da454c51aaa7d91b6b2b5e6fdb77bbd (patch) | |
tree | b4750203c9df0d138db1ec9b5a6d292ea04ff209 | |
parent | 5ab0866341ac21f732117833e41b7deb12971b08 (diff) | |
download | nextcloud-server-04edeb510da454c51aaa7d91b6b2b5e6fdb77bbd.tar.gz nextcloud-server-04edeb510da454c51aaa7d91b6b2b5e6fdb77bbd.zip |
feat(speech-to-text): SpeechToTextManager::transcribeFile calls TaskProcessingManager::runTask
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
-rw-r--r-- | lib/private/SpeechToText/SpeechToTextManager.php | 27 | ||||
-rw-r--r-- | lib/private/SpeechToText/TranscriptionJob.php | 2 | ||||
-rw-r--r-- | lib/private/TaskProcessing/Manager.php | 26 | ||||
-rw-r--r-- | lib/private/TextProcessing/Manager.php | 11 | ||||
-rw-r--r-- | lib/public/SpeechToText/ISpeechToTextManager.php | 4 |
5 files changed, 61 insertions, 9 deletions
diff --git a/lib/private/SpeechToText/SpeechToTextManager.php b/lib/private/SpeechToText/SpeechToTextManager.php index d6cda473875..c4397112a18 100644 --- a/lib/private/SpeechToText/SpeechToTextManager.php +++ b/lib/private/SpeechToText/SpeechToTextManager.php @@ -24,6 +24,9 @@ use OCP\SpeechToText\ISpeechToTextManager; use OCP\SpeechToText\ISpeechToTextProvider; use OCP\SpeechToText\ISpeechToTextProviderWithId; use OCP\SpeechToText\ISpeechToTextProviderWithUserId; +use OCP\TaskProcessing\IManager as ITaskProcessingManager; +use OCP\TaskProcessing\Task; +use OCP\TaskProcessing\TaskTypes\AudioToText; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; @@ -41,6 +44,7 @@ class SpeechToTextManager implements ISpeechToTextManager { private IJobList $jobList, private IConfig $config, private IUserSession $userSession, + private ITaskProcessingManager $taskProcessingManager, ) { } @@ -112,7 +116,28 @@ class SpeechToTextManager implements ISpeechToTextManager { } } - public function transcribeFile(File $file): string { + public function transcribeFile(File $file, ?string $userId = null, string $appId = 'core'): string { + // try to run a TaskProcessing core:audio2text task + // this covers scheduling as well because OC\SpeechToText\TranscriptionJob calls this method + try { + $taskProcessingTask = new Task( + AudioToText::ID, + ['input' => $file->getId()], + $appId, + $userId, + 'from-SpeechToTextManager||' . $file->getId() . '||' . ($userId ?? '') . '||' . $appId, + ); + $resultTask = $this->taskProcessingManager->runTask($taskProcessingTask); + if ($resultTask->getStatus() === Task::STATUS_SUCCESSFUL) { + $output = $resultTask->getOutput(); + if (isset($output['output']) && is_string($output['output'])) { + return $output['output']; + } + } + } catch (Throwable $e) { + $this->logger->debug('Failed to run a Speech-to-text job from STTManager with TaskProcessing for file ' . $file->getId(), ['exception' => $e]); + } + if (!$this->hasProviders()) { throw new PreConditionNotMetException('No SpeechToText providers have been registered'); } diff --git a/lib/private/SpeechToText/TranscriptionJob.php b/lib/private/SpeechToText/TranscriptionJob.php index a46fd737865..6e899ef6e96 100644 --- a/lib/private/SpeechToText/TranscriptionJob.php +++ b/lib/private/SpeechToText/TranscriptionJob.php @@ -63,7 +63,7 @@ class TranscriptionJob extends QueuedJob { ); return; } - $result = $this->speechToTextManager->transcribeFile($file); + $result = $this->speechToTextManager->transcribeFile($file, $userId, $appId); $this->eventDispatcher->dispatchTyped( new TranscriptionSuccessfulEvent( $fileId, diff --git a/lib/private/TaskProcessing/Manager.php b/lib/private/TaskProcessing/Manager.php index 873e0720f08..fb0a4da4c4e 100644 --- a/lib/private/TaskProcessing/Manager.php +++ b/lib/private/TaskProcessing/Manager.php @@ -88,7 +88,6 @@ class Manager implements IManager { IAppDataFactory $appDataFactory, private IRootFolder $rootFolder, private \OCP\TextToImage\IManager $textToImageManager, - private \OCP\SpeechToText\ISpeechToTextManager $speechToTextManager, private IUserMountCache $userMountCache, private IClientService $clientService, private IAppManager $appManager, @@ -369,12 +368,35 @@ class Manager implements IManager { return $newProviders; } + /** + * This is almost a copy of SpeechToTextManager->getProviders + * to avoid a dependency cycle between SpeechToTextManager and TaskProcessingManager + */ + private function _getRawSpeechToTextProviders(): array { + $context = $this->coordinator->getRegistrationContext(); + if ($context === null) { + return []; + } + $providers = []; + foreach ($context->getSpeechToTextProviders() as $providerServiceRegistration) { + $class = $providerServiceRegistration->getService(); + try { + $providers[$class] = $this->serverContainer->get($class); + } catch (NotFoundExceptionInterface|ContainerExceptionInterface|\Throwable $e) { + $this->logger->error('Failed to load SpeechToText provider ' . $class, [ + 'exception' => $e, + ]); + } + } + + return $providers; + } /** * @return IProvider[] */ private function _getSpeechToTextProviders(): array { - $oldProviders = $this->speechToTextManager->getProviders(); + $oldProviders = $this->_getRawSpeechToTextProviders(); $newProviders = []; foreach ($oldProviders as $oldProvider) { $newProvider = new class($oldProvider, $this->rootFolder, $this->appData) implements IProvider, ISynchronousProvider { diff --git a/lib/private/TextProcessing/Manager.php b/lib/private/TextProcessing/Manager.php index c73a6a9ee37..52ca62eaede 100644 --- a/lib/private/TextProcessing/Manager.php +++ b/lib/private/TextProcessing/Manager.php @@ -137,10 +137,13 @@ class Manager implements IManager { $this->logger->debug('Running a TextProcessing (' . $taskTypeClass . ') task with TaskProcessing'); $taskProcessingResultTask = $this->taskProcessingManager->runTask($taskProcessingTask); if ($taskProcessingResultTask->getStatus() === \OCP\TaskProcessing\Task::STATUS_SUCCESSFUL) { - $task->setOutput($taskProcessingResultTask->getOutput()['output'] ?? ''); - $task->setStatus(OCPTask::STATUS_SUCCESSFUL); - $this->taskMapper->update(DbTask::fromPublicTask($task)); - return $task->getOutput(); + $output = $taskProcessingResultTask->getOutput(); + if (isset($output['output']) && is_string($output['output'])) { + $task->setOutput($output['output']); + $task->setStatus(OCPTask::STATUS_SUCCESSFUL); + $this->taskMapper->update(DbTask::fromPublicTask($task)); + return $output['output']; + } } } catch (\Throwable $e) { $this->logger->error('TextProcessing to TaskProcessing failed', ['exception' => $e]); diff --git a/lib/public/SpeechToText/ISpeechToTextManager.php b/lib/public/SpeechToText/ISpeechToTextManager.php index 043dac0ba14..6bd95197695 100644 --- a/lib/public/SpeechToText/ISpeechToTextManager.php +++ b/lib/public/SpeechToText/ISpeechToTextManager.php @@ -59,11 +59,13 @@ interface ISpeechToTextManager { /** * @param File $file The media file to transcribe + * @param ?string $userId The user that triggered this request + * @param string $appId The app that triggered this request * @returns string The transcription of the passed media file * @throws PreConditionNotMetException If no provider was registered but this method was still called * @throws InvalidArgumentException If the file could not be found or is not of a supported type * @throws RuntimeException If the transcription failed for other reasons * @since 27.0.0 */ - public function transcribeFile(File $file): string; + public function transcribeFile(File $file, ?string $userId, string $appId): string; } |