SpeechToTextManager.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 OCP\SpeechToText\ISpeechToTextProviderWithId;
  38. use Psr\Container\ContainerExceptionInterface;
  39. use Psr\Container\NotFoundExceptionInterface;
  40. use Psr\Log\LoggerInterface;
  41. use RuntimeException;
  42. use Throwable;
  43. class SpeechToTextManager implements ISpeechToTextManager {
  44. /** @var ?ISpeechToTextProvider[] */
  45. private ?array $providers = null;
  46. public function __construct(
  47. private IServerContainer $serverContainer,
  48. private Coordinator $coordinator,
  49. private LoggerInterface $logger,
  50. private IJobList $jobList,
  51. private IConfig $config,
  52. ) {
  53. }
  54. public function getProviders(): array {
  55. $context = $this->coordinator->getRegistrationContext();
  56. if ($context === null) {
  57. return [];
  58. }
  59. if ($this->providers !== null) {
  60. return $this->providers;
  61. }
  62. $this->providers = [];
  63. foreach ($context->getSpeechToTextProviders() as $providerServiceRegistration) {
  64. $class = $providerServiceRegistration->getService();
  65. try {
  66. $this->providers[$class] = $this->serverContainer->get($class);
  67. } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
  68. $this->logger->error('Failed to load SpeechToText provider ' . $class, [
  69. 'exception' => $e,
  70. ]);
  71. }
  72. }
  73. return $this->providers;
  74. }
  75. public function hasProviders(): bool {
  76. $context = $this->coordinator->getRegistrationContext();
  77. if ($context === null) {
  78. return false;
  79. }
  80. return !empty($context->getSpeechToTextProviders());
  81. }
  82. public function scheduleFileTranscription(File $file, ?string $userId, string $appId): void {
  83. if (!$this->hasProviders()) {
  84. throw new PreConditionNotMetException('No SpeechToText providers have been registered');
  85. }
  86. try {
  87. $this->jobList->add(TranscriptionJob::class, [
  88. 'fileId' => $file->getId(),
  89. 'owner' => $file->getOwner()->getUID(),
  90. 'userId' => $userId,
  91. 'appId' => $appId,
  92. ]);
  93. } catch (NotFoundException|InvalidPathException $e) {
  94. throw new InvalidArgumentException('Invalid file provided for file transcription: ' . $e->getMessage());
  95. }
  96. }
  97. public function transcribeFile(File $file): string {
  98. if (!$this->hasProviders()) {
  99. throw new PreConditionNotMetException('No SpeechToText providers have been registered');
  100. }
  101. $providers = $this->getProviders();
  102. $json = $this->config->getAppValue('core', 'ai.stt_provider', '');
  103. if ($json !== '') {
  104. $classNameOrId = json_decode($json, true);
  105. $provider = current(array_filter($providers, function ($provider) use ($classNameOrId) {
  106. if ($provider instanceof ISpeechToTextProviderWithId) {
  107. return $provider->getId() === $classNameOrId;
  108. }
  109. return $provider::class === $classNameOrId;
  110. }));
  111. if ($provider !== false) {
  112. $providers = [$provider];
  113. }
  114. }
  115. foreach ($providers as $provider) {
  116. try {
  117. return $provider->transcribeFile($file);
  118. } catch (\Throwable $e) {
  119. $this->logger->info('SpeechToText transcription using provider ' . $provider->getName() . ' failed', ['exception' => $e]);
  120. }
  121. }
  122. throw new RuntimeException('Could not transcribe file');
  123. }
  124. }