SpeechToTextManager.php 3.7 KB

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