aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/SpeechToText/SpeechToTextManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/SpeechToText/SpeechToTextManager.php')
-rw-r--r--lib/private/SpeechToText/SpeechToTextManager.php76
1 files changed, 55 insertions, 21 deletions
diff --git a/lib/private/SpeechToText/SpeechToTextManager.php b/lib/private/SpeechToText/SpeechToTextManager.php
index 520700cf198..d69add2d80b 100644
--- a/lib/private/SpeechToText/SpeechToTextManager.php
+++ b/lib/private/SpeechToText/SpeechToTextManager.php
@@ -3,26 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
- * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
- *
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Marcel Klehr <mklehr@gmx.net>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -36,10 +18,15 @@ use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IServerContainer;
+use OCP\IUserSession;
use OCP\PreConditionNotMetException;
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;
@@ -56,6 +43,8 @@ class SpeechToTextManager implements ISpeechToTextManager {
private LoggerInterface $logger,
private IJobList $jobList,
private IConfig $config,
+ private IUserSession $userSession,
+ private ITaskProcessingManager $taskProcessingManager,
) {
}
@@ -109,7 +98,48 @@ class SpeechToTextManager implements ISpeechToTextManager {
}
}
- public function transcribeFile(File $file): string {
+ public function cancelScheduledFileTranscription(File $file, ?string $userId, string $appId): void {
+ try {
+ $jobArguments = [
+ 'fileId' => $file->getId(),
+ 'owner' => $file->getOwner()->getUID(),
+ 'userId' => $userId,
+ 'appId' => $appId,
+ ];
+ if (!$this->jobList->has(TranscriptionJob::class, $jobArguments)) {
+ $this->logger->debug('Failed to cancel a Speech-to-text job for file ' . $file->getId() . '. No related job was found.');
+ return;
+ }
+ $this->jobList->remove(TranscriptionJob::class, $jobArguments);
+ } catch (NotFoundException|InvalidPathException $e) {
+ throw new InvalidArgumentException('Invalid file provided to cancel file transcription: ' . $e->getMessage());
+ }
+ }
+
+ 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 {
+ if (isset($this->taskProcessingManager->getAvailableTaskTypes()['core:audio2text'])) {
+ $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) {
+ throw new RuntimeException('Failed to run a Speech-to-text job from STTManager with TaskProcessing for file ' . $file->getId(), 0, $e);
+ }
+
if (!$this->hasProviders()) {
throw new PreConditionNotMetException('No SpeechToText providers have been registered');
}
@@ -132,9 +162,13 @@ class SpeechToTextManager implements ISpeechToTextManager {
foreach ($providers as $provider) {
try {
+ if ($provider instanceof ISpeechToTextProviderWithUserId) {
+ $provider->setUserId($this->userSession->getUser()?->getUID());
+ }
return $provider->transcribeFile($file);
} catch (\Throwable $e) {
$this->logger->info('SpeechToText transcription using provider ' . $provider->getName() . ' failed', ['exception' => $e]);
+ throw new RuntimeException('SpeechToText transcription using provider "' . $provider->getName() . '" failed: ' . $e->getMessage());
}
}