ArtificialIntelligence.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  5. *
  6. * @author Marcel Klehr <mklehr@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Settings\Admin;
  25. use OCP\AppFramework\Http\TemplateResponse;
  26. use OCP\AppFramework\Services\IInitialState;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\Settings\IDelegatedSettings;
  30. use OCP\SpeechToText\ISpeechToTextManager;
  31. use OCP\TextProcessing\IManager;
  32. use OCP\TextProcessing\IProvider;
  33. use OCP\TextProcessing\ITaskType;
  34. use OCP\Translation\ITranslationManager;
  35. use Psr\Container\ContainerExceptionInterface;
  36. use Psr\Container\ContainerInterface;
  37. use Psr\Container\NotFoundExceptionInterface;
  38. class ArtificialIntelligence implements IDelegatedSettings {
  39. public function __construct(
  40. private IConfig $config,
  41. private IL10N $l,
  42. private IInitialState $initialState,
  43. private ITranslationManager $translationManager,
  44. private ISpeechToTextManager $sttManager,
  45. private IManager $textProcessingManager,
  46. private ContainerInterface $container,
  47. private \OCP\TextToImage\IManager $text2imageManager,
  48. ) {
  49. }
  50. /**
  51. * @return TemplateResponse
  52. */
  53. public function getForm() {
  54. $translationProviders = [];
  55. $translationPreferences = [];
  56. foreach ($this->translationManager->getProviders() as $provider) {
  57. $translationProviders[] = [
  58. 'class' => $provider::class,
  59. 'name' => $provider->getName(),
  60. ];
  61. $translationPreferences[] = $provider::class;
  62. }
  63. $sttProviders = [];
  64. foreach ($this->sttManager->getProviders() as $provider) {
  65. $sttProviders[] = [
  66. 'class' => $provider::class,
  67. 'name' => $provider->getName(),
  68. ];
  69. }
  70. $textProcessingProviders = [];
  71. /** @var array<class-string<ITaskType>, class-string<IProvider>> $textProcessingSettings */
  72. $textProcessingSettings = [];
  73. foreach ($this->textProcessingManager->getProviders() as $provider) {
  74. $textProcessingProviders[] = [
  75. 'class' => $provider::class,
  76. 'name' => $provider->getName(),
  77. 'taskType' => $provider->getTaskType(),
  78. ];
  79. if (!isset($textProcessingSettings[$provider->getTaskType()])) {
  80. $textProcessingSettings[$provider->getTaskType()] = $provider::class;
  81. }
  82. }
  83. $textProcessingTaskTypes = [];
  84. foreach ($textProcessingSettings as $taskTypeClass => $providerClass) {
  85. /** @var ITaskType $taskType */
  86. try {
  87. $taskType = $this->container->get($taskTypeClass);
  88. } catch (NotFoundExceptionInterface $e) {
  89. continue;
  90. } catch (ContainerExceptionInterface $e) {
  91. continue;
  92. }
  93. $textProcessingTaskTypes[] = [
  94. 'class' => $taskTypeClass,
  95. 'name' => $taskType->getName(),
  96. 'description' => $taskType->getDescription(),
  97. ];
  98. }
  99. $text2imageProviders = [];
  100. foreach ($this->text2imageManager->getProviders() as $provider) {
  101. $text2imageProviders[] = [
  102. 'id' => $provider->getId(),
  103. 'name' => $provider->getName(),
  104. ];
  105. }
  106. $this->initialState->provideInitialState('ai-stt-providers', $sttProviders);
  107. $this->initialState->provideInitialState('ai-translation-providers', $translationProviders);
  108. $this->initialState->provideInitialState('ai-text-processing-providers', $textProcessingProviders);
  109. $this->initialState->provideInitialState('ai-text-processing-task-types', $textProcessingTaskTypes);
  110. $this->initialState->provideInitialState('ai-text2image-providers', $text2imageProviders);
  111. $settings = [
  112. 'ai.stt_provider' => count($sttProviders) > 0 ? $sttProviders[0]['class'] : null,
  113. 'ai.textprocessing_provider_preferences' => $textProcessingSettings,
  114. 'ai.translation_provider_preferences' => $translationPreferences,
  115. 'ai.text2image_provider' => count($text2imageProviders) > 0 ? $text2imageProviders[0]['id'] : null,
  116. ];
  117. foreach ($settings as $key => $defaultValue) {
  118. $value = $defaultValue;
  119. $json = $this->config->getAppValue('core', $key, '');
  120. if ($json !== '') {
  121. $value = json_decode($json, true);
  122. switch($key) {
  123. case 'ai.textprocessing_provider_preferences':
  124. // fill $value with $defaultValue values
  125. $value = array_merge($defaultValue, $value);
  126. break;
  127. case 'ai.translation_provider_preferences':
  128. // Only show entries from $value (saved pref list) that are in $defaultValue (enabled providers)
  129. // and add all providers that are enabled but not in the pref list
  130. if (!is_array($defaultValue)) {
  131. break;
  132. }
  133. $value = array_values(array_unique(array_merge(array_intersect($value, $defaultValue), $defaultValue), SORT_STRING));
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. $settings[$key] = $value;
  140. }
  141. $this->initialState->provideInitialState('ai-settings', $settings);
  142. return new TemplateResponse('settings', 'settings/admin/ai');
  143. }
  144. /**
  145. * @return string the section ID, e.g. 'sharing'
  146. */
  147. public function getSection() {
  148. return 'ai';
  149. }
  150. /**
  151. * @return int whether the form should be rather on the top or bottom of
  152. * the admin section. The forms are arranged in ascending order of the
  153. * priority values. It is required to return a value between 0 and 100.
  154. *
  155. * E.g.: 70
  156. */
  157. public function getPriority() {
  158. return 10;
  159. }
  160. public function getName(): ?string {
  161. return $this->l->t('Artificial Intelligence');
  162. }
  163. public function getAuthorizedAppConfig(): array {
  164. return [
  165. 'core' => ['/ai..*/'],
  166. ];
  167. }
  168. }