feat(speech-to-text): SpeechToTextManager::transcribeFile calls TaskProcessingManager::runTask

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
pull/47601/head
Julien Veyssier 1 year ago
parent 5ab0866341
commit 04edeb510d
No known key found for this signature in database
GPG Key ID: 4141FEE162030638
  1. 27
      lib/private/SpeechToText/SpeechToTextManager.php
  2. 2
      lib/private/SpeechToText/TranscriptionJob.php
  3. 26
      lib/private/TaskProcessing/Manager.php
  4. 11
      lib/private/TextProcessing/Manager.php
  5. 4
      lib/public/SpeechToText/ISpeechToTextManager.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');
}

@ -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,

@ -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 {

@ -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]);

@ -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;
}

Loading…
Cancel
Save