Skip to content

Commit ffba2d7

Browse files
committed
cs:fix, add quota, and default_tts model
Signed-off-by: Lukas Schaefer <[email protected]>
1 parent 0b17efc commit ffba2d7

File tree

5 files changed

+207
-191
lines changed

5 files changed

+207
-191
lines changed

lib/AppInfo/Application.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class Application extends App implements IBootstrap {
3939
public const DEFAULT_COMPLETION_MODEL_ID = 'gpt-3.5-turbo';
4040
public const DEFAULT_IMAGE_MODEL_ID = 'dall-e-2';
4141
public const DEFAULT_TRANSCRIPTION_MODEL_ID = 'whisper-1';
42-
public const DEFAULT_SPEECH_MODEL_ID = 'tts-1-hd';
43-
public const DEFAULT_SPEECH_VOICE = 'alloy';
42+
public const DEFAULT_SPEECH_MODEL_ID = 'tts-1-hd';
43+
public const DEFAULT_SPEECH_VOICE = 'alloy';
4444
public const DEFAULT_DEFAULT_IMAGE_SIZE = '1024x1024';
4545
public const MAX_GENERATION_IDLE_TIME = 60 * 60 * 24 * 10;
4646
public const DEFAULT_CHUNK_SIZE = 10000;
@@ -57,13 +57,13 @@ class Application extends App implements IBootstrap {
5757
public const QUOTA_TYPE_TEXT = 0;
5858
public const QUOTA_TYPE_IMAGE = 1;
5959
public const QUOTA_TYPE_TRANSCRIPTION = 2;
60-
public const QUOTA_TYPE_SPEECH = 3;
60+
public const QUOTA_TYPE_SPEECH = 3;
6161

6262
public const DEFAULT_QUOTAS = [
6363
self::QUOTA_TYPE_TEXT => 0, // 0 = unlimited
6464
self::QUOTA_TYPE_IMAGE => 0, // 0 = unlimited
6565
self::QUOTA_TYPE_TRANSCRIPTION => 0, // 0 = unlimited
66-
self::QUOTA_TYPE_SPEECH => 0, // 0 = unlimited
66+
self::QUOTA_TYPE_SPEECH => 0, // 0 = unlimited
6767

6868
];
6969

@@ -112,9 +112,9 @@ public function register(IRegistrationContext $context): void {
112112
$context->registerTaskProcessingProvider(\OCA\OpenAi\TaskProcessing\ProofreadProvider::class);
113113
}
114114
}
115-
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech') && $this->appConfig->getValueString(Application::APP_ID, 'tts_provider_enabled', '1') === '1') {
116-
$context->registerTaskProcessingProvider(\OCA\OpenAi\TaskProcessing\TextToSpeechProvider::class);
117-
}
115+
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech') && $this->appConfig->getValueString(Application::APP_ID, 'tts_provider_enabled', '1') === '1') {
116+
$context->registerTaskProcessingProvider(\OCA\OpenAi\TaskProcessing\TextToSpeechProvider::class);
117+
}
118118
if ($this->appConfig->getValueString(Application::APP_ID, 't2i_provider_enabled', '1') === '1') {
119119
$context->registerTaskProcessingProvider(TextToImageProvider::class);
120120
}

lib/Service/OpenAiAPIService.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public function translatedQuotaType(int $type): string {
248248
return $this->l10n->t('Image generation');
249249
case Application::QUOTA_TYPE_TRANSCRIPTION:
250250
return $this->l10n->t('Audio transcription');
251-
case Application::QUOTA_TYPE_SPEECH:
252-
return $this->l10n->t('Text to speech');
253-
default:
251+
case Application::QUOTA_TYPE_SPEECH:
252+
return $this->l10n->t('Text to speech');
253+
default:
254254
return $this->l10n->t('Unknown');
255255
}
256256
}
@@ -268,8 +268,8 @@ public function translatedQuotaUnit(int $type): string {
268268
return $this->l10n->t('images');
269269
case Application::QUOTA_TYPE_TRANSCRIPTION:
270270
return $this->l10n->t('seconds');
271-
case Application::QUOTA_TYPE_SPEECH:
272-
return $this->l10n->t('characters');
271+
case Application::QUOTA_TYPE_SPEECH:
272+
return $this->l10n->t('characters');
273273
default:
274274
return $this->l10n->t('Unknown');
275275
}
@@ -746,38 +746,38 @@ public function getImageRequestOptions(?string $userId): array {
746746
return $requestOptions;
747747
}
748748

749-
/**
750-
* @param string|null $userId
751-
* @param string $prompt
752-
* @param string $model
753-
* @param string $voice
754-
* @return array
755-
* @throws Exception
756-
*/
757-
public function requestSpeechCreation(
758-
?string $userId, string $prompt, string $model, string $voice
759-
): array {
760-
if ($this->isQuotaExceeded($userId, Application::QUOTA_TYPE_SPEECH)) {
761-
throw new Exception($this->l10n->t('Speech generation quota exceeded'), Http::STATUS_TOO_MANY_REQUESTS);
762-
}
763-
764-
$params = [
765-
'input' => $prompt,
766-
'voice' => $voice,
767-
'model' => $model === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_IMAGE_MODEL_ID : $model,
768-
'response_format' => 'wav'
769-
];
770-
771-
$apiResponse = $this->request($userId, 'audio/speech', $params, 'POST');
772-
773-
try {
774-
$charCount = mb_strlen($prompt);
775-
$this->quotaUsageMapper->createQuotaUsage($userId ?? '', Application::QUOTA_TYPE_SPEECH, $charCount);
776-
} catch (DBException $e) {
777-
$this->logger->warning('Could not create quota usage for user: ' . $userId . ' and quota type: ' . Application::QUOTA_TYPE_IMAGE . '. Error: ' . $e->getMessage(), ['app' => Application::APP_ID]);
778-
}
779-
return $apiResponse;
780-
}
749+
/**
750+
* @param string|null $userId
751+
* @param string $prompt
752+
* @param string $model
753+
* @param string $voice
754+
* @return array
755+
* @throws Exception
756+
*/
757+
public function requestSpeechCreation(
758+
?string $userId, string $prompt, string $model, string $voice,
759+
): array {
760+
if ($this->isQuotaExceeded($userId, Application::QUOTA_TYPE_SPEECH)) {
761+
throw new Exception($this->l10n->t('Speech generation quota exceeded'), Http::STATUS_TOO_MANY_REQUESTS);
762+
}
763+
764+
$params = [
765+
'input' => $prompt,
766+
'voice' => $voice,
767+
'model' => $model === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_IMAGE_MODEL_ID : $model,
768+
'response_format' => 'wav'
769+
];
770+
771+
$apiResponse = $this->request($userId, 'audio/speech', $params, 'POST');
772+
773+
try {
774+
$charCount = mb_strlen($prompt);
775+
$this->quotaUsageMapper->createQuotaUsage($userId ?? '', Application::QUOTA_TYPE_SPEECH, $charCount);
776+
} catch (DBException $e) {
777+
$this->logger->warning('Could not create quota usage for user: ' . $userId . ' and quota type: ' . Application::QUOTA_TYPE_IMAGE . '. Error: ' . $e->getMessage(), ['app' => Application::APP_ID]);
778+
}
779+
return $apiResponse;
780+
}
781781

782782
/**
783783
* @return int

lib/Service/OpenAiSettingsService.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class OpenAiSettingsService {
2323
'api_key' => 'string',
2424
'default_completion_model_id' => 'string',
2525
'default_stt_model_id' => 'string',
26+
'default_tts_model_id' => 'string',
2627
'default_image_model_id' => 'string',
2728
'default_image_size' => 'string',
2829
'image_request_auth' => 'boolean',
@@ -36,8 +37,8 @@ class OpenAiSettingsService {
3637
'llm_provider_enabled' => 'boolean',
3738
't2i_provider_enabled' => 'boolean',
3839
'stt_provider_enabled' => 'boolean',
39-
'tts_provider_exists' => 'boolean',
40-
'tts_provider_enabled' => 'boolean',
40+
'tts_provider_exists' => 'boolean',
41+
'tts_provider_enabled' => 'boolean',
4142
'chat_endpoint_enabled' => 'boolean',
4243
'basic_user' => 'string',
4344
'basic_password' => 'string',
@@ -120,6 +121,10 @@ public function getAdminDefaultImageSize(): string {
120121
return $this->appConfig->getValueString(Application::APP_ID, 'default_image_size') ?: Application::DEFAULT_DEFAULT_IMAGE_SIZE;
121122
}
122123

124+
public function getAdminDefaultTtsModelId(): string {
125+
return $this->appConfig->getValueString(Application::APP_ID, 'default_speech_model_id') ?: Application::DEFAULT_MODEL_ID;
126+
}
127+
123128
/**
124129
* @return string
125130
*/
@@ -268,6 +273,7 @@ public function getAdminConfig(): array {
268273
'api_key' => $this->getAdminApiKey(),
269274
'default_completion_model_id' => $this->getAdminDefaultCompletionModelId(),
270275
'default_stt_model_id' => $this->getAdminDefaultSttModelId(),
276+
'default_tts_model_id' => $this->getAdminDefaultTtsModelId(),
271277
'default_image_model_id' => $this->getAdminDefaultImageModelId(),
272278
'default_image_size' => $this->getAdminDefaultImageSize(),
273279
'image_request_auth' => $this->getIsImageRetrievalAuthenticated(),
@@ -284,8 +290,8 @@ public function getAdminConfig(): array {
284290
'llm_provider_enabled' => $this->getLlmProviderEnabled(),
285291
't2i_provider_enabled' => $this->getT2iProviderEnabled(),
286292
'stt_provider_enabled' => $this->getSttProviderEnabled(),
287-
'tts_provider_exists' => $this->getTtsProviderExists(),
288-
'tts_provider_enabled' => $this->getTtsProviderEnabled(),
293+
'tts_provider_exists' => $this->getTtsProviderExists(),
294+
'tts_provider_enabled' => $this->getTtsProviderEnabled(),
289295
'chat_endpoint_enabled' => $this->getChatEndpointEnabled(),
290296
'basic_user' => $this->getAdminBasicUser(),
291297
'basic_password' => $this->getAdminBasicPassword(),
@@ -358,19 +364,19 @@ public function getSttProviderEnabled(): bool {
358364
return $this->appConfig->getValueString(Application::APP_ID, 'stt_provider_enabled', '1') === '1';
359365
}
360366

361-
/**
362-
* @return bool
363-
*/
364-
public function getTtsProviderEnabled(): bool {
365-
return $this->getTtsProviderExists() && $this->appConfig->getValueString(Application::APP_ID, 'tts_provider_enabled', '1') === '1';
366-
}
367+
/**
368+
* @return bool
369+
*/
370+
public function getTtsProviderEnabled(): bool {
371+
return $this->getTtsProviderExists() && $this->appConfig->getValueString(Application::APP_ID, 'tts_provider_enabled', '1') === '1';
372+
}
367373

368-
/**
369-
* @return bool
370-
*/
371-
public function getTtsProviderExists(): bool {
372-
return class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech');
373-
}
374+
/**
375+
* @return bool
376+
*/
377+
public function getTtsProviderExists(): bool {
378+
return class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech');
379+
}
374380

375381
////////////////////////////////////////////
376382
//////////// Setters for settings //////////
@@ -443,6 +449,15 @@ public function setAdminDefaultSttModelId(string $defaultSttModelId): void {
443449
$this->appConfig->setValueString(Application::APP_ID, 'default_stt_model_id', $defaultSttModelId);
444450
}
445451

452+
/**
453+
* @param string $defaultTtsModelId
454+
* @return void
455+
*/
456+
public function setAdminDefaultTtsModelId(string $defaultTtsModelId): void {
457+
// No need to validate. As long as it's a string, we're happy campers
458+
$this->appConfig->setValueString(Application::APP_ID, 'default_speech_model_id', $defaultTtsModelId);
459+
}
460+
446461
/**
447462
* @param string $defaultImageModelId
448463
* @return void
@@ -632,6 +647,9 @@ public function setAdminConfig(array $adminConfig): void {
632647
if (isset($adminConfig['default_stt_model_id'])) {
633648
$this->setAdminDefaultSttModelId($adminConfig['default_stt_model_id']);
634649
}
650+
if (isset($adminConfig['default_tts_model_id'])) {
651+
$this->setAdminDefaultTtsModelId($adminConfig['default_tts_model_id']);
652+
}
635653
if (isset($adminConfig['default_image_model_id'])) {
636654
$this->setAdminDefaultImageModelId($adminConfig['default_image_model_id']);
637655
}
@@ -671,9 +689,9 @@ public function setAdminConfig(array $adminConfig): void {
671689
if (isset($adminConfig['stt_provider_enabled'])) {
672690
$this->setSttProviderEnabled($adminConfig['stt_provider_enabled']);
673691
}
674-
if (isset($adminConfig['tts_provider_enabled'])) {
675-
$this->setTtsProviderEnabled($adminConfig['tts_provider_enabled']);
676-
}
692+
if (isset($adminConfig['tts_provider_enabled'])) {
693+
$this->setTtsProviderEnabled($adminConfig['tts_provider_enabled']);
694+
}
677695
if (isset($adminConfig['chat_endpoint_enabled'])) {
678696
$this->setChatEndpointEnabled($adminConfig['chat_endpoint_enabled']);
679697
}
@@ -761,13 +779,13 @@ public function setSttProviderEnabled(bool $enabled): void {
761779
$this->appConfig->setValueString(Application::APP_ID, 'stt_provider_enabled', $enabled ? '1' : '0');
762780
}
763781

764-
/**
765-
* @param bool $enabled
766-
* @return void
767-
*/
768-
public function setTtsProviderEnabled(bool $enabled): void {
769-
$this->appConfig->setValueString(Application::APP_ID, 'tts_provider_enabled', $enabled ? '1' : '0');
770-
}
782+
/**
783+
* @param bool $enabled
784+
* @return void
785+
*/
786+
public function setTtsProviderEnabled(bool $enabled): void {
787+
$this->appConfig->setValueString(Application::APP_ID, 'tts_provider_enabled', $enabled ? '1' : '0');
788+
}
771789

772790
/**
773791
* @param bool $enabled

0 commit comments

Comments
 (0)