Skip to content

Commit

Permalink
chore: tts
Browse files Browse the repository at this point in the history
Signed-off-by: Neko Ayaka <[email protected]>
  • Loading branch information
nekomeowww committed Dec 2, 2024
1 parent be4cbce commit 8b1338c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 11 deletions.
14 changes: 11 additions & 3 deletions app/components/MainStage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const nowSpeakingAvatarBorderOpacityMax = 100
const openAiApiKey = useLocalStorage('openai-api-key', '')
const openAiApiBaseURL = useLocalStorage('openai-api-base-url', '')
const openAIModel = useLocalStorage<{ id: string, name?: string }>('openai-model', { id: 'openai/gpt-3.5-turbo', name: 'OpenAI GPT3.5 Turbo' })
const elevenLabsApiKey = useLocalStorage('elevenlabs-api-key', '')
const { setupOpenAI, streamSpeech, stream, models } = useLLM()
const { audioContext, calculateVolume } = useAudioContext()
Expand Down Expand Up @@ -102,7 +103,7 @@ const ttsQueue = useQueue<string>({
handlers: [
async (ctx) => {
const now = Date.now()
const res = await streamSpeech(ctx.data)
const res = await streamSpeech(ctx.data, elevenLabsApiKey.value)
const elapsed = Date.now() - now
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -253,6 +254,13 @@ onUnmounted(() => {
<template>
<div max-h="[100vh]" h-full p="2" flex="~ col">
<div space-x="2" flex="~ row" w-full>
<div flex="~ row" w-full>
<input
v-model="openAiApiBaseURL"
placeholder="Input your API base URL"
p="2" bg="zinc-100 dark:zinc-700" w-full rounded-lg outline-none
>
</div>
<div flex="~ row" w-full>
<input
v-model="openAiApiKey"
Expand All @@ -262,8 +270,8 @@ onUnmounted(() => {
</div>
<div flex="~ row" w-full>
<input
v-model="openAiApiBaseURL"
placeholder="Input your API base URL"
v-model="elevenLabsApiKey"
placeholder="Input your ElevenLabs API key"
p="2" bg="zinc-100 dark:zinc-700" w-full rounded-lg outline-none
>
</div>
Expand Down
3 changes: 2 additions & 1 deletion app/stores/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ export const useLLM = defineStore('llm', () => {
return await openAI.value.models.list()
}

async function streamSpeech(text: string) {
async function streamSpeech(text: string, apiKey: string) {
if (!text || !text.trim())
throw new Error('Text is required')

return await ofetch('/api/v1/llm/voice/text-to-speech', {
body: {
text,
apiKey,
},
method: 'POST',
cache: 'no-cache',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@
"vue-tsc": "^2.1.10",
"yauzl": "^3.2.0",
"zod": "^3.23.8"
},
"dependencies": {

Check failure on line 66 in package.json

View workflow job for this annotation

GitHub Actions / lint

Expected object keys to be in specified order. 'dependencies' should be before 'devDependencies'
"elevenlabs": "^0.18.1"
}
}
93 changes: 93 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions server/api/pageview.ts

This file was deleted.

30 changes: 30 additions & 0 deletions server/api/v1/llm/voice/text-to-speech.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ElevenLabsClient } from 'elevenlabs'

export default defineEventHandler(async (event) => {
const body = await readBody<{ text: string, apiKey: string }>(event)
const client = new ElevenLabsClient({
apiKey: body.apiKey,
})

const res = await client.generate({
// voice: 'ShanShan',
// Quite good for English
voice: 'Myriam',
// Beatrice is not 'childish' like the others
// voice: 'Beatrice',
text: body.text,
stream: true,
model_id: 'eleven_multilingual_v2',
voice_settings: {
stability: 0.4,
similarity_boost: 0.5,
},
})

// Set headers for streaming
event.node.res.setHeader('Content-Type', 'audio/mpeg')
event.node.res.setHeader('Transfer-Encoding', 'chunked')

// res is NodeJS.ReadableStream
return sendStream(event, res)
})

0 comments on commit 8b1338c

Please sign in to comment.