-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTextToSpeechProvider.php
More file actions
170 lines (141 loc) · 5.35 KB
/
TextToSpeechProvider.php
File metadata and controls
170 lines (141 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\OpenAi\TaskProcessing;
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCA\OpenAi\Service\WatermarkingService;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\TaskProcessing\EShapeType;
use OCP\TaskProcessing\ISynchronousWatermarkingProvider;
use OCP\TaskProcessing\ShapeDescriptor;
use OCP\TaskProcessing\ShapeEnumValue;
use Psr\Log\LoggerInterface;
use RuntimeException;
class TextToSpeechProvider implements ISynchronousWatermarkingProvider {
public function __construct(
private OpenAiAPIService $openAiAPIService,
private IL10N $l,
private LoggerInterface $logger,
private IAppConfig $appConfig,
private ?string $userId,
private WatermarkingService $watermarkingService,
) {
}
public function getId(): string {
return Application::APP_ID . '-text2speech';
}
public function getName(): string {
return $this->openAiAPIService->getServiceName(Application::SERVICE_TYPE_TTS);
}
public function getTaskTypeId(): string {
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech')) {
return \OCP\TaskProcessing\TaskTypes\TextToSpeech::ID;
}
return TextToSpeechTaskType::ID;
}
public function getExpectedRuntime(): int {
return $this->openAiAPIService->getExpTextProcessingTime();
}
public function getInputShapeEnumValues(): array {
return [];
}
public function getInputShapeDefaults(): array {
return [];
}
public function getOptionalInputShape(): array {
return [
'voice' => new ShapeDescriptor(
$this->l->t('Voice'),
$this->l->t('The voice to use'),
EShapeType::Enum
),
'model' => new ShapeDescriptor(
$this->l->t('Model'),
$this->l->t('The model used to generate the speech'),
EShapeType::Enum
),
'speed' => new ShapeDescriptor(
$this->l->t('Speed'),
$this->openAiAPIService->isUsingOpenAi(Application::SERVICE_TYPE_TTS)
? $this->l->t('Speech speed modifier (Valid values: 0.25-4)')
: $this->l->t('Speech speed modifier'),
EShapeType::Number
)
];
}
public function getOptionalInputShapeEnumValues(): array {
$voices = json_decode($this->appConfig->getValueString(Application::APP_ID, 'tts_voices', lazy: true)) ?: Application::DEFAULT_SPEECH_VOICES;
return [
'voice' => array_map(function ($v) {
return new ShapeEnumValue($v, $v);
}, $voices),
'model' => $this->openAiAPIService->getModelEnumValues($this->userId, Application::SERVICE_TYPE_TTS),
];
}
public function getOptionalInputShapeDefaults(): array {
$adminVoice = $this->appConfig->getValueString(Application::APP_ID, 'default_speech_voice', lazy: true) ?: Application::DEFAULT_SPEECH_VOICE;
$adminModel = $this->appConfig->getValueString(Application::APP_ID, 'default_speech_model_id', lazy: true) ?: Application::DEFAULT_SPEECH_MODEL_ID;
return [
'voice' => $adminVoice,
'model' => $adminModel,
'speed' => 1,
];
}
public function getOutputShapeEnumValues(): array {
return [];
}
public function getOptionalOutputShape(): array {
return [];
}
public function getOptionalOutputShapeEnumValues(): array {
return [];
}
public function process(?string $userId, array $input, callable $reportProgress, bool $includeWatermark = true): array {
if (!isset($input['input']) || !is_string($input['input'])) {
throw new RuntimeException('Invalid prompt');
}
// For OpenAI the text input limit is 4096 characters (https://platform.openai.com/docs/api-reference/audio/createSpeech#audio-createspeech-input)
$prompt = $input['input'];
if ($includeWatermark) {
$prompt .= "\n\n" . $this->l->t('This was generated using Artificial Intelligence.');
}
if (isset($input['model']) && is_string($input['model'])) {
$model = $input['model'];
} else {
$model = $this->appConfig->getValueString(Application::APP_ID, 'default_speech_model_id', Application::DEFAULT_SPEECH_MODEL_ID, lazy: true) ?: Application::DEFAULT_SPEECH_MODEL_ID;
}
if (isset($input['voice']) && is_string($input['voice'])) {
$voice = $input['voice'];
} else {
$voice = $this->appConfig->getValueString(Application::APP_ID, 'default_speech_voice', Application::DEFAULT_SPEECH_VOICE, lazy: true) ?: Application::DEFAULT_SPEECH_VOICE;
}
$speed = 1;
if (isset($input['speed']) && is_numeric($input['speed'])) {
$speed = $input['speed'];
if ($this->openAiAPIService->isUsingOpenAi(Application::SERVICE_TYPE_TTS)) {
if ($speed > 4) {
$speed = 4;
} elseif ($speed < 0.25) {
$speed = 0.25;
}
}
}
try {
$apiResponse = $this->openAiAPIService->requestSpeechCreation($userId, $prompt, $model, $voice, $speed);
if (!isset($apiResponse['body'])) {
$this->logger->warning('OpenAI/LocalAI\'s text to speech generation failed: no speech returned');
throw new RuntimeException('OpenAI/LocalAI\'s text to speech generation failed: no speech returned');
}
$audio = $includeWatermark ? $this->watermarkingService->markAudio($apiResponse['body']) : $apiResponse['body'];
return ['speech' => $audio];
} catch (\Exception $e) {
$this->logger->warning('OpenAI/LocalAI\'s text to speech generation failed with: ' . $e->getMessage(), ['exception' => $e]);
throw new RuntimeException('OpenAI/LocalAI\'s text to speech generation failed with: ' . $e->getMessage());
}
}
}