Browse Source

🤩 Added custom openai tts models and role variables

Yanyutin753 1 year ago
parent
commit
761c66a8d8
34 changed files with 89 additions and 7 deletions
  1. 12 0
      backend/apps/audio/main.py
  2. 2 0
      backend/config.py
  3. 5 2
      src/lib/apis/audio/index.ts
  4. 3 2
      src/lib/apis/openai/index.ts
  5. 2 1
      src/lib/components/chat/Messages/ResponseMessage.svelte
  6. 37 2
      src/lib/components/chat/Settings/Audio.svelte
  7. 1 0
      src/lib/i18n/locales/ar-BH/translation.json
  8. 1 0
      src/lib/i18n/locales/bg-BG/translation.json
  9. 1 0
      src/lib/i18n/locales/bn-BD/translation.json
  10. 1 0
      src/lib/i18n/locales/ca-ES/translation.json
  11. 1 0
      src/lib/i18n/locales/de-DE/translation.json
  12. 1 0
      src/lib/i18n/locales/dg-DG/translation.json
  13. 1 0
      src/lib/i18n/locales/en-GB/translation.json
  14. 1 0
      src/lib/i18n/locales/en-US/translation.json
  15. 1 0
      src/lib/i18n/locales/es-ES/translation.json
  16. 1 0
      src/lib/i18n/locales/fa-IR/translation.json
  17. 1 0
      src/lib/i18n/locales/fr-CA/translation.json
  18. 1 0
      src/lib/i18n/locales/fr-FR/translation.json
  19. 1 0
      src/lib/i18n/locales/it-IT/translation.json
  20. 1 0
      src/lib/i18n/locales/ja-JP/translation.json
  21. 1 0
      src/lib/i18n/locales/ka-GE/translation.json
  22. 1 0
      src/lib/i18n/locales/ko-KR/translation.json
  23. 1 0
      src/lib/i18n/locales/nl-NL/translation.json
  24. 1 0
      src/lib/i18n/locales/pl-PL/translation.json
  25. 1 0
      src/lib/i18n/locales/pt-BR/translation.json
  26. 1 0
      src/lib/i18n/locales/pt-PT/translation.json
  27. 1 0
      src/lib/i18n/locales/ru-RU/translation.json
  28. 1 0
      src/lib/i18n/locales/sv-SE/translation.json
  29. 1 0
      src/lib/i18n/locales/tr-TR/translation.json
  30. 1 0
      src/lib/i18n/locales/uk-UA/translation.json
  31. 1 0
      src/lib/i18n/locales/vi-VN/translation.json
  32. 1 0
      src/lib/i18n/locales/zh-CN/translation.json
  33. 1 0
      src/lib/i18n/locales/zh-TW/translation.json
  34. 1 0
      src/lib/stores/index.ts

+ 12 - 0
backend/apps/audio/main.py

@@ -43,6 +43,8 @@ from config import (
     DEVICE_TYPE,
     AUDIO_OPENAI_API_BASE_URL,
     AUDIO_OPENAI_API_KEY,
+    AUDIO_OPENAI_API_MODEL,
+    AUDIO_OPENAI_API_SPEAKER
 )
 
 log = logging.getLogger(__name__)
@@ -60,6 +62,8 @@ app.add_middleware(
 
 app.state.OPENAI_API_BASE_URL = AUDIO_OPENAI_API_BASE_URL
 app.state.OPENAI_API_KEY = AUDIO_OPENAI_API_KEY
+app.state.OPENAI_API_MODEL = AUDIO_OPENAI_API_MODEL
+app.state.OPENAI_API_SPEAKER = AUDIO_OPENAI_API_SPEAKER
 
 # setting device type for whisper model
 whisper_device_type = DEVICE_TYPE if DEVICE_TYPE and DEVICE_TYPE == "cuda" else "cpu"
@@ -72,6 +76,8 @@ SPEECH_CACHE_DIR.mkdir(parents=True, exist_ok=True)
 class OpenAIConfigUpdateForm(BaseModel):
     url: str
     key: str
+    model: str
+    speaker: str
 
 
 @app.get("/config")
@@ -79,6 +85,8 @@ async def get_openai_config(user=Depends(get_admin_user)):
     return {
         "OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL,
         "OPENAI_API_KEY": app.state.OPENAI_API_KEY,
+        "OPENAI_API_MODEL": app.state.OPENAI_API_MODEL,
+        "OPENAI_API_SPEAKER": app.state.OPENAI_API_SPEAKER
     }
 
 
@@ -91,11 +99,15 @@ async def update_openai_config(
 
     app.state.OPENAI_API_BASE_URL = form_data.url
     app.state.OPENAI_API_KEY = form_data.key
+    app.state.OPENAI_API_MODEL = form_data.model
+    app.state.OPENAI_API_SPEAKER = form_data.speaker
 
     return {
         "status": True,
         "OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL,
         "OPENAI_API_KEY": app.state.OPENAI_API_KEY,
+        "OPENAI_API_MODEL": app.state.OPENAI_API_MODEL,
+        "OPENAI_API_SPEAKER": app.state.OPENAI_API_SPEAKER,
     }
 
 

+ 2 - 0
backend/config.py

@@ -574,6 +574,8 @@ IMAGE_GENERATION_MODEL = os.getenv("IMAGE_GENERATION_MODEL", "")
 
 AUDIO_OPENAI_API_BASE_URL = os.getenv("AUDIO_OPENAI_API_BASE_URL", OPENAI_API_BASE_URL)
 AUDIO_OPENAI_API_KEY = os.getenv("AUDIO_OPENAI_API_KEY", OPENAI_API_KEY)
+AUDIO_OPENAI_API_MODEL = os.getenv("AUDIO_OPENAI_API_MODEL", "tts-1")
+AUDIO_OPENAI_API_SPEAKER = os.getenv("AUDIO_OPENAI_API_SPEAKER", "alloy")
 
 ####################################
 # LiteLLM

+ 5 - 2
src/lib/apis/audio/index.ts

@@ -30,6 +30,8 @@ export const getAudioConfig = async (token: string) => {
 type OpenAIConfigForm = {
 	url: string;
 	key: string;
+	model: string;
+	speaker: string;
 };
 
 export const updateAudioConfig = async (token: string, payload: OpenAIConfigForm) => {
@@ -95,7 +97,8 @@ export const transcribeAudio = async (token: string, file: File) => {
 export const synthesizeOpenAISpeech = async (
 	token: string = '',
 	speaker: string = 'alloy',
-	text: string = ''
+	text: string = '',
+	OpenAIModel: string = 'tts-1'
 ) => {
 	let error = null;
 
@@ -106,7 +109,7 @@ export const synthesizeOpenAISpeech = async (
 			'Content-Type': 'application/json'
 		},
 		body: JSON.stringify({
-			model: 'tts-1',
+			model: OpenAIModel,
 			input: text,
 			voice: speaker
 		})

+ 3 - 2
src/lib/apis/openai/index.ts

@@ -239,7 +239,8 @@ export const generateOpenAIChatCompletion = async (
 export const synthesizeOpenAISpeech = async (
 	token: string = '',
 	speaker: string = 'alloy',
-	text: string = ''
+	text: string = '',
+	model: string = 'tts-1'
 ) => {
 	let error = null;
 
@@ -250,7 +251,7 @@ export const synthesizeOpenAISpeech = async (
 			'Content-Type': 'application/json'
 		},
 		body: JSON.stringify({
-			model: 'tts-1',
+			model: model,
 			input: text,
 			voice: speaker
 		})

+ 2 - 1
src/lib/components/chat/Messages/ResponseMessage.svelte

@@ -223,7 +223,8 @@
 					const res = await synthesizeOpenAISpeech(
 						localStorage.token,
 						$settings?.audio?.speaker,
-						sentence
+						sentence,
+						$settings?.audio?.OpenAIModel
 					).catch((error) => {
 						toast.error(error);
 

+ 37 - 2
src/lib/components/chat/Settings/Audio.svelte

@@ -26,6 +26,8 @@
 
 	let voices = [];
 	let speaker = '';
+	let models = [];
+	let OpenAIModel = '';
 
 	const getOpenAIVoices = () => {
 		voices = [
@@ -38,6 +40,10 @@
 		];
 	};
 
+	const getOpenAIVoicesModel = () => {
+		models = [{ name: 'tts-1' }, { name: 'tts-1-hd' }];
+	};
+
 	const getWebAPIVoices = () => {
 		const getVoicesLoop = setInterval(async () => {
 			voices = await speechSynthesis.getVoices();
@@ -78,12 +84,16 @@
 		if (TTSEngine === 'openai') {
 			const res = await updateAudioConfig(localStorage.token, {
 				url: OpenAIUrl,
-				key: OpenAIKey
+				key: OpenAIKey,
+				model: OpenAIModel,
+				speaker: speaker,
 			});
 
 			if (res) {
 				OpenAIUrl = res.OPENAI_API_BASE_URL;
 				OpenAIKey = res.OPENAI_API_KEY;
+				OpenAIModel = res.OPENAI_API_MODEL;
+				speaker = res.OPENAI_API_SPEAKER;
 			}
 		}
 	};
@@ -98,9 +108,11 @@
 		STTEngine = settings?.audio?.STTEngine ?? '';
 		TTSEngine = settings?.audio?.TTSEngine ?? '';
 		speaker = settings?.audio?.speaker ?? '';
+		OpenAIModel = settings?.audio?.OpenAIModel ?? '';
 
 		if (TTSEngine === 'openai') {
 			getOpenAIVoices();
+			getOpenAIVoicesModel();
 		} else {
 			getWebAPIVoices();
 		}
@@ -111,6 +123,8 @@
 			if (res) {
 				OpenAIUrl = res.OPENAI_API_BASE_URL;
 				OpenAIKey = res.OPENAI_API_KEY;
+				OpenAIModel = res.OPENAI_API_MODEL;
+				speaker = res.OPENAI_API_SPEAKER;
 			}
 		}
 	});
@@ -126,7 +140,8 @@
 			audio: {
 				STTEngine: STTEngine !== '' ? STTEngine : undefined,
 				TTSEngine: TTSEngine !== '' ? TTSEngine : undefined,
-				speaker: speaker !== '' ? speaker : undefined
+				speaker: speaker !== '' ? speaker : undefined,
+				OpenAIModel: OpenAIModel !== '' ? OpenAIModel : undefined
 			}
 		});
 		dispatch('save');
@@ -215,6 +230,7 @@
 							if (e.target.value === 'openai') {
 								getOpenAIVoices();
 								speaker = 'alloy';
+								OpenAIModel = 'tts-1';
 							} else {
 								getWebAPIVoices();
 								speaker = '';
@@ -307,6 +323,25 @@
 					</div>
 				</div>
 			</div>
+			<div>
+				<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Model')}</div>
+				<div class="flex w-full">
+					<div class="flex-1">
+						<input
+							list="model-list"
+							class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
+							bind:value={OpenAIModel}
+							placeholder="Select a model"
+						/>
+
+						<datalist id="model-list">
+							{#each models as OpenAIMode}
+								<option value={OpenAIMode.name} />
+							{/each}
+						</datalist>
+					</div>
+				</div>
+			</div>
 		{/if}
 	</div>
 

+ 1 - 0
src/lib/i18n/locales/ar-BH/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "ضبط الخطوات",
 	"Set Title Auto-Generation Model": "قم بتعيين نموذج إنشاء العنوان تلقائيًا",
 	"Set Voice": "ضبط الصوت",
+	"Set Model": "ضبط النموذج",
 	"Settings": "الاعدادات",
 	"Settings saved successfully!": "تم حفظ الاعدادات بنجاح",
 	"Share": "كشاركة",

+ 1 - 0
src/lib/i18n/locales/bg-BG/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Задай Стъпки",
 	"Set Title Auto-Generation Model": "Задай Модел за Автоматично Генериране на Заглавие",
 	"Set Voice": "Задай Глас",
+	"Set Model": "Задай Модел",
 	"Settings": "Настройки",
 	"Settings saved successfully!": "Настройките са запазени успешно!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/bn-BD/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "পরবর্তী ধাপসমূহ",
 	"Set Title Auto-Generation Model": "শিরোনাম অটোজেনারেশন মডেন নির্ধারণ করুন",
 	"Set Voice": "কন্ঠস্বর নির্ধারণ করুন",
+	"Set Model": "মডেল নির্ধারণ করুন",
 	"Settings": "সেটিংসমূহ",
 	"Settings saved successfully!": "সেটিংগুলো সফলভাবে সংরক্ষিত হয়েছে",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/ca-ES/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Estableix Passos",
 	"Set Title Auto-Generation Model": "Estableix Model d'Auto-Generació de Títol",
 	"Set Voice": "Estableix Veu",
+	"Set Model": "Estableix Model",
 	"Settings": "Configuracions",
 	"Settings saved successfully!": "Configuracions guardades amb èxit!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/de-DE/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Schritte festlegen",
 	"Set Title Auto-Generation Model": "Modell für automatische Titelgenerierung festlegen",
 	"Set Voice": "Stimme festlegen",
+	"Set Model": "Modell festlegen",
 	"Settings": "Einstellungen",
 	"Settings saved successfully!": "Einstellungen erfolgreich gespeichert!",
 	"Share": "Teilen",

+ 1 - 0
src/lib/i18n/locales/dg-DG/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Set Steps so many steps",
 	"Set Title Auto-Generation Model": "Set Title Auto-Generation Model very auto-generate",
 	"Set Voice": "Set Voice so speak",
+	"Set Model": "Set Model so speak",
 	"Settings": "Settings much settings",
 	"Settings saved successfully!": "Settings saved successfully! Very success!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/en-GB/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "",
 	"Set Title Auto-Generation Model": "",
 	"Set Voice": "",
+	"Set Model": "",
 	"Settings": "",
 	"Settings saved successfully!": "",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/en-US/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "",
 	"Set Title Auto-Generation Model": "",
 	"Set Voice": "",
+	"Set Model": "",
 	"Settings": "",
 	"Settings saved successfully!": "",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/es-ES/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Establecer Pasos",
 	"Set Title Auto-Generation Model": "Establecer modelo de generación automática de títulos",
 	"Set Voice": "Establecer la voz",
+	"Set Model": "Establecer el modelo",
 	"Settings": "Configuración",
 	"Settings saved successfully!": "¡Configuración guardada exitosamente!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/fa-IR/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "تنظیم گام\u200cها",
 	"Set Title Auto-Generation Model": "تنظیم مدل تولید خودکار عنوان",
 	"Set Voice": "تنظیم صدا",
+	"Set Model": "تنظیم مدل",
 	"Settings": "تنظیمات",
 	"Settings saved successfully!": "تنظیمات با موفقیت ذخیره شد!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/fr-CA/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Définir les étapes",
 	"Set Title Auto-Generation Model": "Définir le modèle de génération automatique de titre",
 	"Set Voice": "Définir la voix",
+	"Set Model": "Configurer le modèle",
 	"Settings": "Paramètres",
 	"Settings saved successfully!": "Paramètres enregistrés avec succès !",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/fr-FR/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Définir les étapes",
 	"Set Title Auto-Generation Model": "Définir le modèle de génération automatique de titre",
 	"Set Voice": "Définir la voix",
+	"Set Model": "Définir le modèle",
 	"Settings": "Paramètres",
 	"Settings saved successfully!": "Paramètres enregistrés avec succès !",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/it-IT/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Imposta passaggi",
 	"Set Title Auto-Generation Model": "Imposta modello di generazione automatica del titolo",
 	"Set Voice": "Imposta voce",
+	"Set Model": "Imposta modello",
 	"Settings": "Impostazioni",
 	"Settings saved successfully!": "Impostazioni salvate con successo!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/ja-JP/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "ステップを設定",
 	"Set Title Auto-Generation Model": "タイトル自動生成モデルを設定",
 	"Set Voice": "音声を設定",
+	"Set Model": "モデルを設定",
 	"Settings": "設定",
 	"Settings saved successfully!": "設定が正常に保存されました!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/ka-GE/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "ნაბიჯების დაყენება",
 	"Set Title Auto-Generation Model": "სათაურის ავტომატური გენერაციის მოდელის დაყენება",
 	"Set Voice": "ხმის დაყენება",
+	"Set Model": "მოდელის დაყენება",
 	"Settings": "ხელსაწყოები",
 	"Settings saved successfully!": "პარამეტრები წარმატებით განახლდა!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/ko-KR/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "단계 설정",
 	"Set Title Auto-Generation Model": "제목 자동 생성 모델 설정",
 	"Set Voice": "음성 설정",
+	"Set Model": "모델 설정",
 	"Settings": "설정",
 	"Settings saved successfully!": "설정이 성공적으로 저장되었습니다!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/nl-NL/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Stel Stappen in",
 	"Set Title Auto-Generation Model": "Stel Titel Auto-Generatie Model in",
 	"Set Voice": "Stel Stem in",
+	"Set Model": "Stel die model op",
 	"Settings": "Instellingen",
 	"Settings saved successfully!": "Instellingen succesvol opgeslagen!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/pl-PL/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Ustaw kroki",
 	"Set Title Auto-Generation Model": "Ustaw model automatycznego generowania tytułów",
 	"Set Voice": "Ustaw głos",
+	"Set Model": "Ustaw model",
 	"Settings": "Ustawienia",
 	"Settings saved successfully!": "Ustawienia zapisane pomyślnie!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/pt-BR/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Definir Etapas",
 	"Set Title Auto-Generation Model": "Definir Modelo de Geração Automática de Título",
 	"Set Voice": "Definir Voz",
+	"Set Model": "Definir Modelo",
 	"Settings": "Configurações",
 	"Settings saved successfully!": "Configurações salvas com sucesso!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/pt-PT/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Definir Etapas",
 	"Set Title Auto-Generation Model": "Definir Modelo de Geração Automática de Título",
 	"Set Voice": "Definir Voz",
+	"Set Model": "Definir Modelo",
 	"Settings": "Configurações",
 	"Settings saved successfully!": "Configurações salvas com sucesso!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/ru-RU/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Установить шаги",
 	"Set Title Auto-Generation Model": "Установить модель автогенерации заголовков",
 	"Set Voice": "Установить голос",
+	"Set Model": "Установить модель",
 	"Settings": "Настройки",
 	"Settings saved successfully!": "Настройки успешно сохранены!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/sv-SE/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Ange steg",
 	"Set Title Auto-Generation Model": "Ange modell för automatisk generering av titel",
 	"Set Voice": "Ange röst",
+	"Set Model": "Ställ in modell",
 	"Settings": "Inställningar",
 	"Settings saved successfully!": "Inställningar sparades framgångsrikt!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/tr-TR/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Adımları Ayarla",
 	"Set Title Auto-Generation Model": "Otomatik Başlık Oluşturma Modelini Ayarla",
 	"Set Voice": "Ses Ayarla",
+	"Set Model": "Model Ayarla",
 	"Settings": "Ayarlar",
 	"Settings saved successfully!": "Ayarlar başarıyla kaydedildi!",
 	"Share": "Paylaş",

+ 1 - 0
src/lib/i18n/locales/uk-UA/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Встановити кроки",
 	"Set Title Auto-Generation Model": "Встановити модель автогенерації заголовків",
 	"Set Voice": "Встановити голос",
+	"Set Model": "Встановити модель",
 	"Settings": "Налаштування",
 	"Settings saved successfully!": "Налаштування успішно збережено!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/vi-VN/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "Đặt Số Bước",
 	"Set Title Auto-Generation Model": "Đặt tiêu đề tự động",
 	"Set Voice": "Đặt Giọng nói",
+	"Set Model": "Thiết lập mô hình",
 	"Settings": "Cài đặt",
 	"Settings saved successfully!": "Cài đặt đã được lưu thành công!",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/zh-CN/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "设置步骤",
 	"Set Title Auto-Generation Model": "设置标题自动生成模型",
 	"Set Voice": "设置声音",
+	"Set Model": "设置模型",
 	"Settings": "设置",
 	"Settings saved successfully!": "设置已保存",
 	"Share": "",

+ 1 - 0
src/lib/i18n/locales/zh-TW/translation.json

@@ -350,6 +350,7 @@
 	"Set Steps": "設定步數",
 	"Set Title Auto-Generation Model": "設定自動生成標題用模型",
 	"Set Voice": "設定語音",
+	"Set Model": "設定模型",
 	"Settings": "設定",
 	"Settings saved successfully!": "成功儲存設定",
 	"Share": "",

+ 1 - 0
src/lib/stores/index.ts

@@ -102,6 +102,7 @@ type AudioSettings = {
 	STTEngine?: string;
 	TTSEngine?: string;
 	speaker?: string;
+	OpenAIModel?: string;
 };
 
 type TitleSettings = {