Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change voice id #80

Merged
merged 5 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions backend/src/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
dashboard_sink = StringIO()


def change_prompt(prompt_id: str, clear_context: bool):
logger.info(f"Setting prompt to {prompt_id}")
def change_prompt(prompt_id: str, voice_id: str, clear_context: bool):
logger.info(f"Setting prompt to {prompt_id} and voice {voice_id}")
if prompt_id not in list(prompt_manager.prompts):
return f"Prompt {prompt_id} does not exist"
global_config.elevenlabs_voice_id = voice_id
prompt_manager.set_current_prompt(prompt_id, clear_context)
r = "Prompt setted to " + prompt_id
r = "Prompt setted to " + prompt_id + " with voice " + voice_id
if clear_context:
r += " and context cleared"
return r
Expand All @@ -42,6 +43,10 @@ def get_context_as_chatbot() -> list[tuple[str, str]]:
def save_prompt(prompt_id: str, prompt: str):
logger.info(f"Saving prompt {prompt_id}")
prompt_manager.new_custom_prompt(prompt_id, prompt)
return gr.Dropdown(
label="Prompt ID",
choices=list(prompt_manager.prompts),
)


def start_dashboard(loop: asyncio.AbstractEventLoop):
Expand Down Expand Up @@ -154,7 +159,11 @@ def gen():
)

with gr.Tab("Config"):

prompts_dropdown = gr.Dropdown(
label="Prompt ID",
choices=list(prompt_manager.prompts),
render=False,
)
with gr.Tab("Global Config"):
gr.Markdown(
value=lambda: global_config.as_markdown(),
Expand All @@ -165,21 +174,22 @@ def gen():
gr.Interface(
fn=change_prompt,
inputs=[
prompts_dropdown,
gr.Textbox(
label="Prompts",
value=lambda: prompt_manager.current_prompt_id,
label="Voice ID",
value=lambda: global_config.elevenlabs_voice_id,
),
gr.Checkbox(label="Clear context", value=False),
],
outputs="text",
allow_flagging="never",
)
gr.Textbox(
value=lambda: "Current prompt: " + prompt_manager.current_prompt_id,
value=lambda: f"Current prompt: {prompt_manager.current_prompt_id}\nCurrent voice: {global_config.elevenlabs_voice_id}",
interactive=False,
every=1,
lines=1,
max_lines=1,
lines=2,
max_lines=2,
container=False,
)

Expand Down Expand Up @@ -216,6 +226,7 @@ def prompts_html():
).click(
save_prompt,
inputs=[custom_prompt_id, custom_prompt],
outputs=prompts_dropdown,
)

blocks.queue().launch(prevent_thread_lock=True, share=True, quiet=True)
Expand Down
1 change: 0 additions & 1 deletion backend/src/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def handle_config_event(self, req_config: Config):
logger.info("Updating configs received from client")
global_config.set_all(req_config)
chat.set_config(global_config)
tts.set_config(global_config)
global_config.save()


Expand Down
33 changes: 14 additions & 19 deletions backend/src/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import threading
import os
from typing import Generator, Iterator
from elevenlabs import generate, set_api_key
from elevenlabs import generate, Voice, VoiceSettings
from loguru import logger

from src.models import Action, OutgoingAction
Expand All @@ -16,22 +16,15 @@
@singleton
class TTS:
def __init__(self):
self.voice_id = ""
self.is_playing = False
self.queue: Queue[Generator] = Queue(maxsize=2)

if not os.path.isfile("mpv.exe"):
logger.warning("mpv.exe not found, TTS disabled")
global_config.tts = False

if global_config.elevenlabs_api_key != "":
set_api_key(global_config.elevenlabs_api_key)
else:
global_config.tts = False

if global_config.elevenlabs_voice_id != "":
self.voice_id = global_config.elevenlabs_voice_id
else:
if (
not os.path.isfile("mpv.exe") or
not global_config.elevenlabs_api_key or
not global_config.elevenlabs_voice_id
):
logger.warning("mpv.exe or keys not found, TTS disabled")
global_config.tts = False

def synthesize(self, gen: Generator[str, None, None], loop: asyncio.AbstractEventLoop) -> None:
Expand Down Expand Up @@ -83,9 +76,15 @@ def non_stream():
else:
elevenlabs_text = wrapped_generator()
logger.info(f"Using {global_config.elevenlabs_buffer_size} elevenlabs buffer size")
voice = Voice(
voice_id=global_config.elevenlabs_voice_id,
settings=VoiceSettings(stability=0.05, similarity_boost=0.75, style=0.75),
)

gen = generate(
text=elevenlabs_text,
voice=self.voice_id,
voice=voice,
api_key=global_config.elevenlabs_api_key,
stream=global_config.elevenlabs_streaming,
model="eleven_multilingual_v2",
stream_chunk_size=global_config.elevenlabs_buffer_size,
Expand Down Expand Up @@ -151,9 +150,5 @@ def stream(self, audio: Iterator[bytes] | bytes, loop: asyncio.AbstractEventLoop
finally:
self.finished_playing(loop)

def set_config(self, config):
self.voice_id = config.elevenlabs_voice_id
set_api_key(config.elevenlabs_api_key)


tts = TTS()
2 changes: 1 addition & 1 deletion backend/start_backend.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
call setup_windows.bat
.\python\python.exe -m poetry run python run.py
.\python\python.exe -m poetry run uvicorn "src.main:app" --port 5000
pause