Audio.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { createEventDispatcher, onMount, getContext } from 'svelte';
  4. const dispatch = createEventDispatcher();
  5. import { getBackendConfig } from '$lib/apis';
  6. import {
  7. getAudioConfig,
  8. updateAudioConfig,
  9. getModels as _getModels,
  10. getVoices as _getVoices
  11. } from '$lib/apis/audio';
  12. import { config } from '$lib/stores';
  13. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  14. import { TTS_RESPONSE_SPLIT } from '$lib/types';
  15. import type { Writable } from 'svelte/store';
  16. import type { i18n as i18nType } from 'i18next';
  17. const i18n = getContext<Writable<i18nType>>('i18n');
  18. export let saveHandler: () => void;
  19. // Audio
  20. let TTS_OPENAI_API_BASE_URL = '';
  21. let TTS_OPENAI_API_KEY = '';
  22. let TTS_API_KEY = '';
  23. let TTS_ENGINE = '';
  24. let TTS_MODEL = '';
  25. let TTS_VOICE = '';
  26. let TTS_SPLIT_ON: TTS_RESPONSE_SPLIT = TTS_RESPONSE_SPLIT.PUNCTUATION;
  27. let STT_OPENAI_API_BASE_URL = '';
  28. let STT_OPENAI_API_KEY = '';
  29. let STT_ENGINE = '';
  30. let STT_MODEL = '';
  31. // eslint-disable-next-line no-undef
  32. let voices: SpeechSynthesisVoice[] = [];
  33. let models: Awaited<ReturnType<typeof _getModels>>['models'] = [];
  34. const getModels = async () => {
  35. if (TTS_ENGINE === '') {
  36. models = [];
  37. } else {
  38. const res = await _getModels(localStorage.token).catch((e) => {
  39. toast.error(e);
  40. });
  41. if (res) {
  42. console.log(res);
  43. models = res.models;
  44. }
  45. }
  46. };
  47. const getVoices = async () => {
  48. if (TTS_ENGINE === '') {
  49. const getVoicesLoop = setInterval(() => {
  50. voices = speechSynthesis.getVoices();
  51. // do your loop
  52. if (voices.length > 0) {
  53. clearInterval(getVoicesLoop);
  54. }
  55. }, 100);
  56. } else {
  57. const res = await _getVoices(localStorage.token).catch((e) => {
  58. toast.error(e);
  59. });
  60. if (res) {
  61. console.log(res);
  62. voices = res.voices;
  63. }
  64. }
  65. };
  66. const updateConfigHandler = async () => {
  67. const res = await updateAudioConfig(localStorage.token, {
  68. tts: {
  69. OPENAI_API_BASE_URL: TTS_OPENAI_API_BASE_URL,
  70. OPENAI_API_KEY: TTS_OPENAI_API_KEY,
  71. API_KEY: TTS_API_KEY,
  72. ENGINE: TTS_ENGINE,
  73. MODEL: TTS_MODEL,
  74. VOICE: TTS_VOICE,
  75. SPLIT_ON: TTS_SPLIT_ON
  76. },
  77. stt: {
  78. OPENAI_API_BASE_URL: STT_OPENAI_API_BASE_URL,
  79. OPENAI_API_KEY: STT_OPENAI_API_KEY,
  80. ENGINE: STT_ENGINE,
  81. MODEL: STT_MODEL
  82. }
  83. });
  84. if (res) {
  85. saveHandler();
  86. getBackendConfig()
  87. .then(config.set)
  88. .catch(() => {});
  89. }
  90. };
  91. onMount(async () => {
  92. const res = await getAudioConfig(localStorage.token);
  93. if (res) {
  94. console.log(res);
  95. TTS_OPENAI_API_BASE_URL = res.tts.OPENAI_API_BASE_URL;
  96. TTS_OPENAI_API_KEY = res.tts.OPENAI_API_KEY;
  97. TTS_API_KEY = res.tts.API_KEY;
  98. TTS_ENGINE = res.tts.ENGINE;
  99. TTS_MODEL = res.tts.MODEL;
  100. TTS_VOICE = res.tts.VOICE;
  101. TTS_SPLIT_ON = res.tts.SPLIT_ON || TTS_RESPONSE_SPLIT.PUNCTUATION;
  102. STT_OPENAI_API_BASE_URL = res.stt.OPENAI_API_BASE_URL;
  103. STT_OPENAI_API_KEY = res.stt.OPENAI_API_KEY;
  104. STT_ENGINE = res.stt.ENGINE;
  105. STT_MODEL = res.stt.MODEL;
  106. }
  107. await getVoices();
  108. await getModels();
  109. });
  110. </script>
  111. <form
  112. class="flex flex-col h-full justify-between space-y-3 text-sm"
  113. on:submit|preventDefault={async () => {
  114. await updateConfigHandler();
  115. dispatch('save');
  116. }}
  117. >
  118. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  119. <div class="flex flex-col gap-3">
  120. <div>
  121. <div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
  122. <div class=" py-0.5 flex w-full justify-between">
  123. <div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
  124. <div class="flex items-center relative">
  125. <select
  126. class="dark:bg-gray-900 cursor-pointer w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  127. bind:value={STT_ENGINE}
  128. placeholder="Select an engine"
  129. >
  130. <option value="">{$i18n.t('Whisper (Local)')}</option>
  131. <option value="openai">OpenAI</option>
  132. <option value="web">{$i18n.t('Web API')}</option>
  133. </select>
  134. </div>
  135. </div>
  136. {#if STT_ENGINE === 'openai'}
  137. <div>
  138. <div class="mt-1 flex gap-2 mb-1">
  139. <input
  140. class="flex-1 w-full rounded-lg py-2 pl-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  141. placeholder={$i18n.t('API Base URL')}
  142. bind:value={STT_OPENAI_API_BASE_URL}
  143. required
  144. />
  145. <SensitiveInput placeholder={$i18n.t('API Key')} bind:value={STT_OPENAI_API_KEY} />
  146. </div>
  147. </div>
  148. <hr class=" dark:border-gray-850 my-2" />
  149. <div>
  150. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('STT Model')}</div>
  151. <div class="flex w-full">
  152. <div class="flex-1">
  153. <input
  154. list="model-list"
  155. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  156. bind:value={STT_MODEL}
  157. placeholder="Select a model"
  158. />
  159. <datalist id="model-list">
  160. <option value="whisper-1" />
  161. </datalist>
  162. </div>
  163. </div>
  164. </div>
  165. {/if}
  166. </div>
  167. <hr class=" dark:border-gray-800" />
  168. <div>
  169. <div class=" mb-1 text-sm font-medium">{$i18n.t('TTS Settings')}</div>
  170. <div class=" py-0.5 flex w-full justify-between">
  171. <div class=" self-center text-xs font-medium">{$i18n.t('Text-to-Speech Engine')}</div>
  172. <div class="flex items-center relative">
  173. <select
  174. class=" dark:bg-gray-900 w-fit pr-8 cursor-pointer rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  175. bind:value={TTS_ENGINE}
  176. placeholder="Select a mode"
  177. on:change={async (e) => {
  178. await updateConfigHandler();
  179. await getVoices();
  180. await getModels();
  181. if (e.target?.value === 'openai') {
  182. TTS_VOICE = 'alloy';
  183. TTS_MODEL = 'tts-1';
  184. } else {
  185. TTS_VOICE = '';
  186. TTS_MODEL = '';
  187. }
  188. }}
  189. >
  190. <option value="">{$i18n.t('Web API')}</option>
  191. <option value="openai">{$i18n.t('OpenAI')}</option>
  192. <option value="elevenlabs">{$i18n.t('ElevenLabs')}</option>
  193. </select>
  194. </div>
  195. </div>
  196. {#if TTS_ENGINE === 'openai'}
  197. <div>
  198. <div class="mt-1 flex gap-2 mb-1">
  199. <input
  200. class="flex-1 w-full rounded-lg py-2 pl-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  201. placeholder={$i18n.t('API Base URL')}
  202. bind:value={TTS_OPENAI_API_BASE_URL}
  203. required
  204. />
  205. <SensitiveInput placeholder={$i18n.t('API Key')} bind:value={TTS_OPENAI_API_KEY} />
  206. </div>
  207. </div>
  208. {:else if TTS_ENGINE === 'elevenlabs'}
  209. <div>
  210. <div class="mt-1 flex gap-2 mb-1">
  211. <input
  212. class="flex-1 w-full rounded-lg py-2 pl-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  213. placeholder={$i18n.t('API Key')}
  214. bind:value={TTS_API_KEY}
  215. required
  216. />
  217. </div>
  218. </div>
  219. {/if}
  220. <hr class=" dark:border-gray-850 my-2" />
  221. {#if TTS_ENGINE === ''}
  222. <div>
  223. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Voice')}</div>
  224. <div class="flex w-full">
  225. <div class="flex-1">
  226. <select
  227. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  228. bind:value={TTS_VOICE}
  229. >
  230. <option value="" selected={TTS_VOICE !== ''}>{$i18n.t('Default')}</option>
  231. {#each voices as voice}
  232. <option
  233. value={voice.voiceURI}
  234. class="bg-gray-100 dark:bg-gray-700"
  235. selected={TTS_VOICE === voice.voiceURI}>{voice.name}</option
  236. >
  237. {/each}
  238. </select>
  239. </div>
  240. </div>
  241. </div>
  242. {:else if TTS_ENGINE === 'openai'}
  243. <div class=" flex gap-2">
  244. <div class="w-full">
  245. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Voice')}</div>
  246. <div class="flex w-full">
  247. <div class="flex-1">
  248. <input
  249. list="voice-list"
  250. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  251. bind:value={TTS_VOICE}
  252. placeholder="Select a voice"
  253. />
  254. <datalist id="voice-list">
  255. {#each voices as voice}
  256. <option value={voice.id}>{voice.name}</option>
  257. {/each}
  258. </datalist>
  259. </div>
  260. </div>
  261. </div>
  262. <div class="w-full">
  263. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Model')}</div>
  264. <div class="flex w-full">
  265. <div class="flex-1">
  266. <input
  267. list="tts-model-list"
  268. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  269. bind:value={TTS_MODEL}
  270. placeholder="Select a model"
  271. />
  272. <datalist id="tts-model-list">
  273. {#each models as model}
  274. <option value={model.id} />
  275. {/each}
  276. </datalist>
  277. </div>
  278. </div>
  279. </div>
  280. </div>
  281. {:else if TTS_ENGINE === 'elevenlabs'}
  282. <div class=" flex gap-2">
  283. <div class="w-full">
  284. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Voice')}</div>
  285. <div class="flex w-full">
  286. <div class="flex-1">
  287. <input
  288. list="voice-list"
  289. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  290. bind:value={TTS_VOICE}
  291. placeholder="Select a voice"
  292. />
  293. <datalist id="voice-list">
  294. {#each voices as voice}
  295. <option value={voice.id}>{voice.name}</option>
  296. {/each}
  297. </datalist>
  298. </div>
  299. </div>
  300. </div>
  301. <div class="w-full">
  302. <div class=" mb-1.5 text-sm font-medium">{$i18n.t('TTS Model')}</div>
  303. <div class="flex w-full">
  304. <div class="flex-1">
  305. <input
  306. list="tts-model-list"
  307. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  308. bind:value={TTS_MODEL}
  309. placeholder="Select a model"
  310. />
  311. <datalist id="tts-model-list">
  312. {#each models as model}
  313. <option value={model.id} />
  314. {/each}
  315. </datalist>
  316. </div>
  317. </div>
  318. </div>
  319. </div>
  320. {/if}
  321. <hr class="dark:border-gray-850 my-2" />
  322. <div class="pt-0.5 flex w-full justify-between">
  323. <div class="self-center text-xs font-medium">{$i18n.t('Response splitting')}</div>
  324. <div class="flex items-center relative">
  325. <select
  326. class="dark:bg-gray-900 w-fit pr-8 cursor-pointer rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  327. aria-label="Select how to split message text for TTS requests"
  328. bind:value={TTS_SPLIT_ON}
  329. >
  330. {#each Object.values(TTS_RESPONSE_SPLIT) as split}
  331. <option value={split}
  332. >{$i18n.t(split.charAt(0).toUpperCase() + split.slice(1))}</option
  333. >
  334. {/each}
  335. </select>
  336. </div>
  337. </div>
  338. <div class="mt-2 mb-1 text-xs text-gray-400 dark:text-gray-500">
  339. {$i18n.t(
  340. "Control how message text is split for TTS requests. 'Punctuation' splits into sentences, 'paragraphs' splits into paragraphs, and 'none' keeps the message as a single string."
  341. )}
  342. </div>
  343. </div>
  344. </div>
  345. </div>
  346. <div class="flex justify-end text-sm font-medium">
  347. <button
  348. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  349. type="submit"
  350. >
  351. {$i18n.t('Save')}
  352. </button>
  353. </div>
  354. </form>