SpeechToTextManager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
  5. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  6. *
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Marcel Klehr <mklehr@gmx.net>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. namespace OC\SpeechToText;
  26. use InvalidArgumentException;
  27. use OC\AppFramework\Bootstrap\Coordinator;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\Files\File;
  30. use OCP\Files\InvalidPathException;
  31. use OCP\Files\NotFoundException;
  32. use OCP\IConfig;
  33. use OCP\IServerContainer;
  34. use OCP\PreConditionNotMetException;
  35. use OCP\SpeechToText\ISpeechToTextManager;
  36. use OCP\SpeechToText\ISpeechToTextProvider;
  37. use Psr\Container\ContainerExceptionInterface;
  38. use Psr\Container\NotFoundExceptionInterface;
  39. use Psr\Log\LoggerInterface;
  40. use RuntimeException;
  41. use Throwable;
  42. class SpeechToTextManager implements ISpeechToTextManager {
  43. /** @var ?ISpeechToTextProvider[] */
  44. private ?array $providers = null;
  45. public function __construct(
  46. private IServerContainer $serverContainer,
  47. private Coordinator $coordinator,
  48. private LoggerInterface $logger,
  49. private IJobList $jobList,
  50. private IConfig $config,
  51. ) {
  52. }
  53. public function getProviders(): array {
  54. $context = $this->coordinator->getRegistrationContext();
  55. if ($context === null) {
  56. return [];
  57. }
  58. if ($this->providers !== null) {
  59. return $this->providers;
  60. }
  61. $this->providers = [];
  62. foreach ($context->getSpeechToTextProviders() as $providerServiceRegistration) {
  63. $class = $providerServiceRegistration->getService();
  64. try {
  65. $this->providers[$class] = $this->serverContainer->get($class);
  66. } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
  67. $this->logger->error('Failed to load SpeechToText provider ' . $class, [
  68. 'exception' => $e,
  69. ]);
  70. }
  71. }
  72. return $this->providers;
  73. }
  74. public function hasProviders(): bool {
  75. $context = $this->coordinator->getRegistrationContext();
  76. if ($context === null) {
  77. return false;
  78. }
  79. return !empty($context->getSpeechToTextProviders());
  80. }
  81. public function scheduleFileTranscription(File $file, ?string $userId, string $appId): void {
  82. if (!$this->hasProviders()) {
  83. throw new PreConditionNotMetException('No SpeechToText providers have been registered');
  84. }
  85. try {
  86. $this->jobList->add(TranscriptionJob::class, [
  87. 'fileId' => $file->getId(),
  88. 'owner' => $file->getOwner()->getUID(),
  89. 'userId' => $userId,
  90. 'appId' => $appId,
  91. ]);
  92. } catch (NotFoundException|InvalidPathException $e) {
  93. throw new InvalidArgumentException('Invalid file provided for file transcription: ' . $e->getMessage());
  94. }
  95. }
  96. public function transcribeFile(File $file): string {
  97. if (!$this->hasProviders()) {
  98. throw new PreConditionNotMetException('No SpeechToText providers have been registered');
  99. }
  100. $providers = $this->getProviders();
  101. $json = $this->config->getAppValue('core', 'ai.stt_provider', '');
  102. if ($json !== '') {
  103. $className = json_decode($json, true);
  104. $provider = current(array_filter($providers, fn ($provider) => $provider::class === $className));
  105. if ($provider !== false) {
  106. $providers = [$provider];
  107. }
  108. }
  109. foreach ($providers as $provider) {
  110. try {
  111. return $provider->transcribeFile($file);
  112. } catch (\Throwable $e) {
  113. $this->logger->info('SpeechToText transcription using provider ' . $provider->getName() . ' failed', ['exception' => $e]);
  114. }
  115. }
  116. throw new RuntimeException('Could not transcribe file');
  117. }
  118. }