-
Notifications
You must be signed in to change notification settings - Fork 26
feat: implement generate subtitles provider #381
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
Open
edward-ly
wants to merge
3
commits into
main
Choose a base branch
from
feat/noid/stt-subtitles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| use OCA\OpenAi\TaskProcessing\AudioToAudioTranslateTaskType; | ||
| use OCA\OpenAi\TaskProcessing\AudioToTextEnhancedProvider; | ||
| use OCA\OpenAi\TaskProcessing\AudioToTextProvider; | ||
| use OCA\OpenAi\TaskProcessing\AudioToTextSubtitlesProvider; | ||
| use OCA\OpenAi\TaskProcessing\ChangeToneProvider; | ||
| use OCA\OpenAi\TaskProcessing\ContextWriteProvider; | ||
| use OCA\OpenAi\TaskProcessing\EmojiProvider; | ||
|
|
@@ -51,6 +52,10 @@ class Application extends App implements IBootstrap { | |
| 'alloy', 'ash', 'ballad', 'coral', 'echo', 'fable', | ||
| 'onyx', 'nova', 'sage', 'shimmer', 'verse' | ||
| ]; | ||
| public const DEFAULT_SUBTITLE_FORMAT = 'srt'; | ||
| public const DEFAULT_SUBTITLE_FORMATS = [ | ||
| 'srt', 'vtt' | ||
| ]; | ||
|
Comment on lines
+56
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: how does |
||
| public const DEFAULT_DEFAULT_IMAGE_SIZE = '1024x1024'; | ||
| public const MAX_GENERATION_IDLE_TIME = 60 * 60 * 24 * 10; | ||
| public const DEFAULT_CHUNK_SIZE = 10000; | ||
|
|
@@ -116,6 +121,9 @@ public function register(IRegistrationContext $context): void { | |
| } | ||
| if ($sttProviderEnabled) { | ||
| $context->registerTaskProcessingProvider(AudioToTextProvider::class); | ||
| if (class_exists('OCP\\TaskProcessing\\TaskTypes\\AudioToTextSubtitles')) { | ||
|
julien-nc marked this conversation as resolved.
|
||
| $context->registerTaskProcessingProvider(AudioToTextSubtitlesProvider::class); | ||
| } | ||
| if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToTextReformatParagraphs')) { | ||
| $context->registerTaskProcessingProvider(AudioToTextEnhancedProvider::class); | ||
| } | ||
|
|
||
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,168 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\OpenAi\TaskProcessing; | ||
|
|
||
| use OCA\OpenAi\AppInfo\Application; | ||
| use OCA\OpenAi\Service\OpenAiAPIService; | ||
| use OCP\Files\File; | ||
| use OCP\IAppConfig; | ||
| use OCP\IL10N; | ||
| use OCP\TaskProcessing\EShapeType; | ||
| use OCP\TaskProcessing\Exception\ProcessingException; | ||
| use OCP\TaskProcessing\Exception\UserFacingProcessingException; | ||
| use OCP\TaskProcessing\ISynchronousProvider; | ||
| use OCP\TaskProcessing\ShapeDescriptor; | ||
| use OCP\TaskProcessing\ShapeEnumValue; | ||
| use OCP\TaskProcessing\TaskTypes\AudioToTextSubtitles; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class AudioToTextSubtitlesProvider implements ISynchronousProvider { | ||
|
|
||
| public function __construct( | ||
| private OpenAiAPIService $openAiAPIService, | ||
| private LoggerInterface $logger, | ||
| private IAppConfig $appConfig, | ||
| private IL10N $l, | ||
| ) { | ||
| } | ||
|
|
||
| public function getId(): string { | ||
| return Application::APP_ID . '-audio2text-subtitles'; | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| return $this->openAiAPIService->getServiceName(Application::SERVICE_TYPE_STT); | ||
| } | ||
|
|
||
| public function getTaskTypeId(): string { | ||
| return AudioToTextSubtitles::ID; | ||
| } | ||
|
|
||
| public function getExpectedRuntime(): int { | ||
| return $this->openAiAPIService->getExpTextProcessingTime(); | ||
| } | ||
|
|
||
| public function getInputShapeEnumValues(): array { | ||
| return []; | ||
| } | ||
|
|
||
| public function getInputShapeDefaults(): array { | ||
| return []; | ||
| } | ||
|
|
||
| public function getOptionalInputShape(): array { | ||
| return [ | ||
| 'language' => new ShapeDescriptor( | ||
| $this->l->t('Language'), | ||
| $this->l->t('The language of the audio file'), | ||
| EShapeType::Enum | ||
| ), | ||
| 'format' => new ShapeDescriptor( | ||
| $this->l->t('File format'), | ||
| $this->l->t('The format of the subtitles file'), | ||
| EShapeType::Enum | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| public function getOptionalInputShapeEnumValues(): array { | ||
| $languageEnumValues = array_map(static function (array $language) { | ||
| return new ShapeEnumValue($language[1], $language[0]); | ||
| }, Application::LANGUAGE_CODES_AND_ENDONYMS); | ||
| $detectLanguageEnumValue = new ShapeEnumValue($this->l->t('Detect language'), 'detect_language'); | ||
| $defaultLanguageEnumValue = new ShapeEnumValue($this->l->t('Default'), 'default'); | ||
| return [ | ||
| 'language' => array_merge([$detectLanguageEnumValue, $defaultLanguageEnumValue], $languageEnumValues), | ||
| 'format' => [ | ||
| new ShapeEnumValue($this->l->t('SubRip Text'), 'srt'), | ||
| new ShapeEnumValue($this->l->t('WebVTT'), 'vtt'), | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function getOptionalInputShapeDefaults(): array { | ||
| return [ | ||
| 'language' => 'default', | ||
| 'format' => 'srt', | ||
| ]; | ||
| } | ||
|
|
||
| public function getOutputShapeEnumValues(): array { | ||
| return []; | ||
| } | ||
|
|
||
| public function getOptionalOutputShape(): array { | ||
| return []; | ||
| } | ||
|
|
||
| public function getOptionalOutputShapeEnumValues(): array { | ||
| return []; | ||
| } | ||
|
|
||
| public function process(?string $userId, array $input, callable $reportProgress): array { | ||
| if (!isset($input['input']) || !$input['input'] instanceof File || !$input['input']->isReadable()) { | ||
| throw new ProcessingException('Invalid input file'); | ||
| } | ||
|
|
||
| $fileSize = intval($input['input']->getSize()); | ||
| // Maximum file size for OpenAI is 25MB. (https://developers.openai.com/api/docs/guides/speech-to-text) | ||
| if ($fileSize > 25 * 1000 * 1000) { | ||
| throw new UserFacingProcessingException( | ||
| 'Filesize of input is too large. Max is 25MB', | ||
| 0, | ||
| null, | ||
| $this->l->t('The input file size is too large. A maximum of 25MB is allowed.'), | ||
| ); | ||
| } | ||
|
|
||
| $fileType = $input['input']->getMimeType(); | ||
| if (!str_starts_with($fileType, 'audio/')) { | ||
| throw new UserFacingProcessingException( | ||
| 'Invalid input file type ' . $fileType, | ||
| 0, | ||
| null, | ||
| $this->l->t('The input file type is invalid. Only audio files are allowed.'), | ||
| ); | ||
| } | ||
| if ($this->openAiAPIService->isUsingOpenAi()) { | ||
|
lukasdotcom marked this conversation as resolved.
|
||
| $validFileTypes = [ | ||
| 'audio/mp3', | ||
| 'audio/mp4', | ||
| 'audio/mpeg', | ||
| 'audio/mpga', | ||
| 'audio/m4a', | ||
| 'audio/wav', | ||
| 'audio/webm', | ||
| ]; | ||
| if (!in_array($fileType, $validFileTypes)) { | ||
| throw new ProcessingException('Invalid input file type for OpenAI ' . $fileType); | ||
| } | ||
| } | ||
|
|
||
| $inputFile = $input['input']; | ||
| $format = $input['format']; | ||
| $language = $input['language'] ?? 'default'; | ||
| if (!is_string($language)) { | ||
| throw new ProcessingException('Invalid language'); | ||
| } | ||
|
|
||
| $model = $this->appConfig->getValueString(Application::APP_ID, 'default_stt_model_id', Application::DEFAULT_MODEL_ID, lazy: true) ?: Application::DEFAULT_MODEL_ID; | ||
|
|
||
| try { | ||
| $transcription = $this->openAiAPIService->transcribeFile($userId, $inputFile, false, $model, $language, $format); | ||
| return ['output' => $transcription]; | ||
| } catch (UserFacingProcessingException $e) { | ||
| throw $e; | ||
| } catch (\Throwable $e) { | ||
| $this->logger->warning('OpenAI\'s Whisper transcription failed with: ' . $e->getMessage(), ['exception' => $e]); | ||
| throw new ProcessingException('OpenAI\'s Whisper transcription failed with: ' . $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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems unused