Skip to content

Commit

Permalink
AI admin settings: Use config values in AI feature managers
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr authored and julien-nc committed Aug 2, 2023
1 parent a840e8c commit 2a3ef10
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function getForm() {
$value = $defaultValue;
$json = $this->config->getAppValue('core', $key, '');
if ($json !== '') {
$value = json_decode($json, JSON_OBJECT_AS_ARRAY);
$value = json_decode($json, true);
switch($key) {
case 'ai.textprocessing_provider_preferences':
// fill $value with $defaultValue values
Expand Down
15 changes: 14 additions & 1 deletion lib/private/SpeechToText/SpeechToTextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCP\Files\File;
use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IServerContainer;
use OCP\PreConditionNotMetException;
use OCP\SpeechToText\ISpeechToTextManager;
Expand All @@ -53,6 +54,7 @@ public function __construct(
private Coordinator $coordinator,
private LoggerInterface $logger,
private IJobList $jobList,
private IConfig $config,
) {
}

Expand Down Expand Up @@ -111,7 +113,18 @@ public function transcribeFile(File $file): string {
throw new PreConditionNotMetException('No SpeechToText providers have been registered');
}

foreach ($this->getProviders() as $provider) {
$providers = $this->getProviders();

$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));
if ($provider !== false) {
$providers = [$provider];
}
}

foreach ($providers as $provider) {
try {
return $provider->transcribeFile($file);
} catch (\Throwable $e) {
Expand Down
18 changes: 17 additions & 1 deletion lib/private/TextProcessing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use OC\AppFramework\Bootstrap\Coordinator;
use OC\TextProcessing\Db\Task as DbTask;
use OCP\IConfig;
use OCP\TextProcessing\Task as OCPTask;
use OC\TextProcessing\Db\TaskMapper;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -52,6 +53,7 @@ public function __construct(
private LoggerInterface $logger,
private IJobList $jobList,
private TaskMapper $taskMapper,
private IConfig $config,
) {
}

Expand Down Expand Up @@ -111,7 +113,21 @@ public function runTask(OCPTask $task): string {
if (!$this->canHandleTask($task)) {
throw new PreConditionNotMetException('No text processing provider is installed that can handle this task');
}
foreach ($this->getProviders() as $provider) {
$providers = $this->getProviders();
$json = $this->config->getAppValue('core', 'ai.textprocessing_provider_preferences', '');
if ($json !== '') {
$preferences = json_decode($json, true);
if (isset($preferences[$task->getType()])) {
// If a preference for this task type is set, move the preferred provider to the start
$provider = current(array_filter($providers, fn ($provider) => $provider::class === $preferences[$task->getType()]));
if ($provider !== false) {
$providers = array_filter($providers, fn ($p) => $p !== $provider);
array_unshift($providers, $provider);
}
}
}

foreach ($providers as $provider) {
if (!$task->canUseProvider($provider)) {
continue;
}
Expand Down
25 changes: 22 additions & 3 deletions lib/private/Translation/TranslationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use InvalidArgumentException;
use OC\AppFramework\Bootstrap\Coordinator;
use OCP\IConfig;
use OCP\IServerContainer;
use OCP\PreConditionNotMetException;
use OCP\Translation\CouldNotTranslateException;
Expand All @@ -48,6 +49,7 @@ public function __construct(
private IServerContainer $serverContainer,
private Coordinator $coordinator,
private LoggerInterface $logger,
private IConfig $config,
) {
}

Expand All @@ -64,8 +66,25 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua
throw new PreConditionNotMetException('No translation providers available');
}

$providers = $this->getProviders();
$json = $this->config->getAppValue('core', 'ai.translation_provider_preferences', '');

if ($json !== '') {
$precedence = json_decode($json, true);
$newProviders = [];
foreach ($precedence as $className) {
$provider = current(array_filter($providers, fn ($provider) => $provider::class === $className));
if ($provider !== false) {
$newProviders[] = $provider;
}
}
// Add all providers that haven't been added so far
$newProviders += array_udiff($providers, $newProviders, fn ($a, $b) => $a::class > $b::class ? 1 : ($a::class < $b::class ? -1 : 0));
$providers = $newProviders;
}

if ($fromLanguage === null) {
foreach ($this->getProviders() as $provider) {
foreach ($providers as $provider) {
if ($provider instanceof IDetectLanguageProvider) {
$fromLanguage = $provider->detectLanguage($text);
}
Expand All @@ -84,11 +103,11 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua
return $text;
}

foreach ($this->getProviders() as $provider) {
foreach ($providers as $provider) {
try {
return $provider->translate($fromLanguage, $toLanguage, $text);
} catch (RuntimeException $e) {
$this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]);
$this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage} using provider {$provider->getName()}", ['exception' => $e]);
}
}

Expand Down

0 comments on commit 2a3ef10

Please sign in to comment.