open-webui/src/lib/components/chat/Settings/Audio.svelte

222 lines
6.0 KiB
Svelte
Raw Normal View History

<script lang="ts">
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
2024-08-02 17:29:03 +00:00
import { createEventDispatcher, onMount, getContext } from 'svelte';
import { user, settings, config } from '$lib/stores';
import { getVoices as _getVoices } from '$lib/apis/audio';
import Switch from '$lib/components/common/Switch.svelte';
const dispatch = createEventDispatcher();
const i18n = getContext('i18n');
export let saveSettings: Function;
2024-02-11 10:21:06 +00:00
// Audio
2024-02-10 01:09:14 +00:00
let conversationMode = false;
2024-02-10 00:00:39 +00:00
let speechAutoSend = false;
let responseAutoPlayback = false;
let nonLocalVoices = false;
2024-02-10 00:00:39 +00:00
2024-06-08 03:18:48 +00:00
let STTEngine = '';
2024-02-06 05:36:03 +00:00
let voices = [];
2024-06-08 03:18:48 +00:00
let voice = '';
2024-08-02 17:29:03 +00:00
const getVoices = async () => {
if ($config.audio.tts.engine === '') {
const getVoicesLoop = setInterval(async () => {
voices = await speechSynthesis.getVoices();
2024-08-02 17:29:03 +00:00
// do your loop
if (voices.length > 0) {
clearInterval(getVoicesLoop);
}
}, 100);
} else {
const res = await _getVoices(localStorage.token).catch((e) => {
toast.error(e);
});
2024-08-02 17:29:03 +00:00
if (res) {
console.log(res);
voices = res.voices;
}
2024-08-02 17:29:03 +00:00
}
2024-02-06 06:51:08 +00:00
};
2024-02-10 00:00:39 +00:00
const toggleResponseAutoPlayback = async () => {
responseAutoPlayback = !responseAutoPlayback;
saveSettings({ responseAutoPlayback: responseAutoPlayback });
};
const toggleSpeechAutoSend = async () => {
speechAutoSend = !speechAutoSend;
saveSettings({ speechAutoSend: speechAutoSend });
};
2024-02-06 06:51:08 +00:00
onMount(async () => {
2024-05-27 05:47:42 +00:00
conversationMode = $settings.conversationMode ?? false;
speechAutoSend = $settings.speechAutoSend ?? false;
responseAutoPlayback = $settings.responseAutoPlayback ?? false;
2024-06-08 03:18:48 +00:00
STTEngine = $settings?.audio?.stt?.engine ?? '';
if ($settings?.audio?.tts?.defaultVoice === $config.audio.tts.voice) {
voice = $settings?.audio?.tts?.voice ?? $config.audio.tts.voice ?? '';
} else {
voice = $config.audio.tts.voice ?? '';
}
2024-06-08 03:18:48 +00:00
nonLocalVoices = $settings.audio?.tts?.nonLocalVoices ?? false;
2024-02-06 06:51:08 +00:00
2024-08-02 17:29:03 +00:00
await getVoices();
});
</script>
<form
class="flex flex-col h-full justify-between space-y-3 text-sm"
2024-04-20 21:00:24 +00:00
on:submit|preventDefault={async () => {
saveSettings({
2024-02-11 10:21:06 +00:00
audio: {
2024-06-08 03:18:48 +00:00
stt: {
engine: STTEngine !== '' ? STTEngine : undefined
},
tts: {
2024-06-08 03:35:50 +00:00
voice: voice !== '' ? voice : undefined,
defaultVoice: $config?.audio?.tts?.voice ?? '',
2024-06-08 03:18:48 +00:00
nonLocalVoices: $config.audio.tts.engine === '' ? nonLocalVoices : undefined
}
2024-02-06 06:51:08 +00:00
}
});
dispatch('save');
}}
>
2024-05-15 22:55:13 +00:00
<div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[25rem]">
2024-02-10 00:00:39 +00:00
<div>
<div class=" mb-1 text-sm font-medium">{$i18n.t('STT Settings')}</div>
2024-02-10 00:00:39 +00:00
2024-06-08 03:18:48 +00:00
{#if $config.audio.stt.engine !== 'web'}
<div class=" py-0.5 flex w-full justify-between">
<div class=" self-center text-xs font-medium">{$i18n.t('Speech-to-Text Engine')}</div>
<div class="flex items-center relative">
<select
class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
bind:value={STTEngine}
placeholder="Select an engine"
>
<option value="">{$i18n.t('Default')}</option>
<option value="web">{$i18n.t('Web API')}</option>
</select>
</div>
2024-02-10 00:00:39 +00:00
</div>
2024-06-08 03:18:48 +00:00
{/if}
2024-02-10 00:00:39 +00:00
2024-02-10 01:09:14 +00:00
<div class=" py-0.5 flex w-full justify-between">
<div class=" self-center text-xs font-medium">
2024-06-07 04:56:09 +00:00
{$i18n.t('Instant Auto-Send After Voice Transcription')}
</div>
2024-02-10 00:00:39 +00:00
<button
class="p-1 px-3 text-xs flex rounded transition"
on:click={() => {
toggleSpeechAutoSend();
}}
type="button"
>
{#if speechAutoSend === true}
<span class="ml-2 self-center">{$i18n.t('On')}</span>
2024-02-10 00:00:39 +00:00
{:else}
<span class="ml-2 self-center">{$i18n.t('Off')}</span>
2024-02-10 00:00:39 +00:00
{/if}
</button>
</div>
2024-02-11 10:12:49 +00:00
</div>
<div>
<div class=" mb-1 text-sm font-medium">{$i18n.t('TTS Settings')}</div>
2024-02-11 10:12:49 +00:00
2024-02-10 00:00:39 +00:00
<div class=" py-0.5 flex w-full justify-between">
<div class=" self-center text-xs font-medium">{$i18n.t('Auto-playback response')}</div>
2024-02-10 00:00:39 +00:00
<button
class="p-1 px-3 text-xs flex rounded transition"
on:click={() => {
toggleResponseAutoPlayback();
2024-02-06 05:36:03 +00:00
}}
2024-02-10 00:00:39 +00:00
type="button"
2024-02-06 05:36:03 +00:00
>
2024-02-10 00:00:39 +00:00
{#if responseAutoPlayback === true}
<span class="ml-2 self-center">{$i18n.t('On')}</span>
2024-02-10 00:00:39 +00:00
{:else}
<span class="ml-2 self-center">{$i18n.t('Off')}</span>
2024-02-10 00:00:39 +00:00
{/if}
</button>
2024-02-06 05:36:03 +00:00
</div>
</div>
2024-06-09 09:01:03 +00:00
<hr class=" dark:border-gray-850" />
2024-02-06 05:36:03 +00:00
2024-06-08 03:18:48 +00:00
{#if $config.audio.tts.engine === ''}
<div>
<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
<div class="flex w-full">
<div class="flex-1">
<select
2024-04-21 00:01:46 +00:00
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
2024-06-08 03:18:48 +00:00
bind:value={voice}
>
2024-06-08 03:18:48 +00:00
<option value="" selected={voice !== ''}>{$i18n.t('Default')}</option>
{#each voices.filter((v) => nonLocalVoices || v.localService === true) as _voice}
<option
2024-06-08 03:18:48 +00:00
value={_voice.name}
class="bg-gray-100 dark:bg-gray-700"
2024-06-08 03:18:48 +00:00
selected={voice === _voice.name}>{_voice.name}</option
>
{/each}
</select>
</div>
</div>
2024-06-05 15:48:02 +00:00
<div class="flex items-center justify-between my-1.5">
<div class="text-xs">
{$i18n.t('Allow non-local voices')}
</div>
<div class="mt-1">
<Switch bind:state={nonLocalVoices} />
</div>
</div>
</div>
2024-07-19 11:30:36 +00:00
{:else if $config.audio.tts.engine !== ''}
2024-02-06 06:51:08 +00:00
<div>
<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Set Voice')}</div>
2024-02-06 06:51:08 +00:00
<div class="flex w-full">
<div class="flex-1">
2024-04-20 21:00:24 +00:00
<input
list="voice-list"
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
2024-06-08 03:18:48 +00:00
bind:value={voice}
2024-02-06 06:51:08 +00:00
placeholder="Select a voice"
2024-04-20 21:00:24 +00:00
/>
<datalist id="voice-list">
2024-02-06 06:51:08 +00:00
{#each voices as voice}
2024-08-02 17:29:03 +00:00
<option value={voice.id}>{voice.name}</option>
2024-02-06 06:51:08 +00:00
{/each}
2024-04-20 21:00:24 +00:00
</datalist>
2024-02-06 06:51:08 +00:00
</div>
</div>
</div>
2024-02-06 05:36:03 +00:00
{/if}
</div>
2024-05-15 22:55:13 +00:00
<div class="flex justify-end text-sm font-medium">
<button
2024-03-10 06:02:27 +00:00
class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
type="submit"
>
2024-03-04 08:53:56 +00:00
{$i18n.t('Save')}
</button>
</div>
</form>