Skip to content

Commit

Permalink
enh(SpeechToText): Allow providers to declare a dynamic ID instead of…
Browse files Browse the repository at this point in the history
… using className

this allows AppAPI to register anonymous classes as SpeechToText providers

Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Oct 24, 2023
1 parent 7a55ea7 commit 1c5411e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion apps/settings/lib/Settings/Admin/ArtificialIntelligence.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\IL10N;
use OCP\Settings\IDelegatedSettings;
use OCP\SpeechToText\ISpeechToTextManager;
use OCP\SpeechToText\ISpeechToTextProviderWithId;
use OCP\TextProcessing\IManager;
use OCP\TextProcessing\IProvider;
use OCP\TextProcessing\ITaskType;
Expand Down Expand Up @@ -68,7 +69,7 @@ public function getForm() {
$sttProviders = [];
foreach ($this->sttManager->getProviders() as $provider) {
$sttProviders[] = [
'class' => $provider::class,
'class' => $provider instanceof ISpeechToTextProviderWithId ? $provider->getId() : $provider::class,
'name' => $provider->getName(),
];
}
Expand Down
10 changes: 8 additions & 2 deletions lib/private/SpeechToText/SpeechToTextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCP\PreConditionNotMetException;
use OCP\SpeechToText\ISpeechToTextManager;
use OCP\SpeechToText\ISpeechToTextProvider;
use OCP\SpeechToText\ISpeechToTextProviderWithId;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -117,8 +118,13 @@ public function transcribeFile(File $file): string {

$json = $this->config->getAppValue('core', 'ai.stt_provider', '');
if ($json !== '') {
$className = json_decode($json, true);
$provider = current(array_filter($providers, fn ($provider) => $provider::class === $className));
$classNameOrId = json_decode($json, true);
$provider = current(array_filter($providers, function ($provider) use ($classNameOrId) {
if ($provider instanceof ISpeechToTextProviderWithId) {
return $provider->getId() === $classNameOrId;
}
return $provider::class === $classNameOrId;
}));
if ($provider !== false) {
$providers = [$provider];
}
Expand Down
14 changes: 14 additions & 0 deletions lib/public/SpeechToText/ISpeechToTextProviderWithId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace OCP\SpeechToText;

/**
* @since 28.0.0
*/
interface ISpeechToTextProviderWithId extends ISpeechToTextProvider {

/**
* @since 28.0.0
*/
public function getId(): string;
}

0 comments on commit 1c5411e

Please sign in to comment.