-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(SpeechToText): Add SpeechToText OCP provider API #37674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
317521b
feat(SpeechToText): Add SpeechToText provider API
marcelklehr f2eb224
feat(SpeechToText): Add autoload changes
marcelklehr a9b5d1f
chore(coding style): Run cs:fix
marcelklehr 1833d93
chore(coding style): Run cs:fix
marcelklehr ef7ce88
ISpeechToTextProvider#transcribeFile: Pass \OCP\Files\File instead of…
marcelklehr 176f1af
ISpeechToTextManager: Take File as input, drop $context
marcelklehr a8d3fff
Split TranscriptionFinishedEvent into Successful and Failed events
marcelklehr 865ebfa
Add missing strict_types + author + copyright
marcelklehr ad66180
Update lib/private/SpeechToText/SpeechToTextManager.php
marcelklehr 71523b9
AbstractTranscriptionEvent: Add File param
marcelklehr ad1a0d8
Transcription*Event: Add doc block
marcelklehr 3779cc3
SpeechToTextManager: Deduplicate transcription jobs
marcelklehr 6e9f260
Update lib/private/SpeechToText/SpeechToTextManager.php
marcelklehr aac6a18
Update lib/public/SpeechToText/ISpeechToTextManager.php
marcelklehr b082657
Update lib/public/SpeechToText/ISpeechToTextManager.php
marcelklehr 47cff5d
Update lib/public/SpeechToText/ISpeechToTextManager.php
marcelklehr 3f57725
SpeechToTextManager#transcribeFile: Try next provider if one fails
marcelklehr bef4cf0
Fix missing import
nickvergessen 9649f91
Update lib/private/SpeechToText/SpeechToTextManager.php
marcelklehr ce651e5
Run cs:fix
marcelklehr eb996cb
TranscriptionJob: Ensure filesystem is setup before trying to retriev…
marcelklehr a8b27c9
TranscriptionJob: Add owner argument to simplify filesystem setup
marcelklehr a2f5421
SpeechToTextManager#scheduleFileTranscription: Take context params an…
marcelklehr fbcd275
Context params: Make userId nullable
marcelklehr db9901a
Fix(docs): Fix parameter type in doc block
nickvergessen ab7b63d
fix(autoloader): Rebuild
nickvergessen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2023 Julius Härtl <[email protected]> | ||
| * @copyright Copyright (c) 2023 Marcel Klehr <[email protected]> | ||
| * | ||
| * @author Julius Härtl <[email protected]> | ||
| * @author Marcel Klehr <[email protected]> | ||
| * | ||
| * @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/>. | ||
| */ | ||
|
|
||
|
|
||
| namespace OC\SpeechToText; | ||
|
|
||
| use InvalidArgumentException; | ||
| use OC\AppFramework\Bootstrap\Coordinator; | ||
| use OCP\BackgroundJob\IJobList; | ||
| use OCP\Files\File; | ||
| use OCP\Files\InvalidPathException; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\IServerContainer; | ||
| use OCP\PreConditionNotMetException; | ||
| use OCP\SpeechToText\ISpeechToTextManager; | ||
| use OCP\SpeechToText\ISpeechToTextProvider; | ||
| use Psr\Container\ContainerExceptionInterface; | ||
| use Psr\Container\NotFoundExceptionInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Throwable; | ||
|
|
||
| class SpeechToTextManager implements ISpeechToTextManager { | ||
| /** @var ?ISpeechToTextProvider[] */ | ||
| private ?array $providers = null; | ||
|
|
||
| public function __construct( | ||
| private IServerContainer $serverContainer, | ||
| private Coordinator $coordinator, | ||
| private LoggerInterface $logger, | ||
| private IJobList $jobList, | ||
| ) { | ||
| } | ||
|
|
||
| public function getProviders(): array { | ||
| $context = $this->coordinator->getRegistrationContext(); | ||
| if ($context === null) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($this->providers !== null) { | ||
| return $this->providers; | ||
| } | ||
|
|
||
| $this->providers = []; | ||
|
|
||
| foreach ($context->getSpeechToTextProviders() as $providerRegistration) { | ||
| $class = $providerRegistration->getService(); | ||
| try { | ||
| $this->providers[$class] = $this->serverContainer->get($class); | ||
| } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) { | ||
| $this->logger->error('Failed to load SpeechToText provider ' . $class, [ | ||
| 'exception' => $e | ||
nickvergessen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]); | ||
| } | ||
| } | ||
|
|
||
| return $this->providers; | ||
| } | ||
|
|
||
| public function hasProviders(): bool { | ||
| $context = $this->coordinator->getRegistrationContext(); | ||
| if ($context === null) { | ||
| return false; | ||
| } | ||
| return !empty($context->getSpeechToTextProviders()); | ||
| } | ||
|
|
||
| public function scheduleFileTranscription(File $file): void { | ||
marcelklehr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!$this->hasProviders()) { | ||
| throw new PreConditionNotMetException('No SpeechToText providers have been registered'); | ||
| } | ||
| try { | ||
| $this->jobList->add(TranscriptionJob::class, ['fileId' => $file->getId()]); | ||
nickvergessen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
nickvergessen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (NotFoundException|InvalidPathException $e) { | ||
| throw new InvalidArgumentException('Invalid file provided for file transcription: ' . $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public function transcribeFile(File $file): string { | ||
| $provider = current($this->getProviders()); | ||
nickvergessen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!$provider) { | ||
| throw new PreConditionNotMetException('No SpeechToText providers have been registered'); | ||
| } | ||
|
|
||
| try { | ||
| return $provider->transcribeFile($file); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->info('SpeechToText transcription failed', ['exception' => $e]); | ||
| throw new \RuntimeException('SpeechToText transcription failed: ' . $e->getMessage()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2023 Marcel Klehr <[email protected]> | ||
| * | ||
| * @author Marcel Klehr <[email protected]> | ||
| * | ||
| * @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/>. | ||
| */ | ||
|
|
||
|
|
||
| namespace OC\SpeechToText; | ||
marcelklehr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use OCP\EventDispatcher\IEventDispatcher; | ||
| use OCP\Files\File; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\PreConditionNotMetException; | ||
| use OCP\SpeechToText\Events\TranscriptionFailedEvent; | ||
| use OCP\SpeechToText\Events\TranscriptionSuccessfulEvent; | ||
| use OCP\SpeechToText\ISpeechToTextManager; | ||
|
|
||
| class TranscriptionJob extends QueuedJob { | ||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| private ISpeechToTextManager $speechToTextManager, | ||
| private IEventDispatcher $eventDispatcher, | ||
| private IRootFolder $rootFolder, | ||
| ) { | ||
| parent::__construct($timeFactory); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| protected function run($argument) { | ||
| $fileId = $argument['fileId']; | ||
nickvergessen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| $file = current($this->rootFolder->getById($fileId)); | ||
| if (!($file instanceof File)) { | ||
| $this->eventDispatcher->dispatchTyped( | ||
| new TranscriptionFailedEvent( | ||
| $fileId, | ||
| 'File not found', | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
| $result = $this->speechToTextManager->transcribeFile($file); | ||
| $this->eventDispatcher->dispatchTyped( | ||
| new TranscriptionSuccessfulEvent( | ||
| $fileId, | ||
| $result, | ||
| ) | ||
| ); | ||
| } catch (PreConditionNotMetException|\RuntimeException|\InvalidArgumentException $e) { | ||
marcelklehr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $this->eventDispatcher->dispatchTyped( | ||
| new TranscriptionFailedEvent( | ||
| $fileId, | ||
| $e->getMessage(), | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/public/SpeechToText/Events/AbstractTranscriptionEvent.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2023 Marcel Klehr <[email protected]> | ||
| * | ||
| * @author Marcel Klehr <[email protected]> | ||
| * | ||
| * @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/>. | ||
| * | ||
| */ | ||
| namespace OCP\SpeechToText\Events; | ||
|
|
||
| use OCP\EventDispatcher\Event; | ||
|
|
||
| /** | ||
| * @since 27.0.0 | ||
| */ | ||
| abstract class AbstractTranscriptionEvent extends Event { | ||
| /** | ||
| * @since 27.0.0 | ||
| */ | ||
| public function __construct( | ||
| private int $fileIdId | ||
marcelklehr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| /** | ||
| * @since 27.0.0 | ||
| */ | ||
| public function getFileId(): int { | ||
| return $this->fileIdId; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.