Skip to content

Commit

Permalink
Added elevenlab support for english voice
Browse files Browse the repository at this point in the history
  • Loading branch information
0Xiaohei0 committed Mar 30, 2023
1 parent 35e8e65 commit 3efa81d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
56 changes: 50 additions & 6 deletions STTSLocal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io
import os
from threading import Thread
import time
import traceback
Expand All @@ -18,6 +20,7 @@
import chatbot
import json
import streamChat
import soundfile as sf


def load_config():
Expand All @@ -33,10 +36,17 @@ def load_config():
voice_vox_api_key = data['voice_vox_api_key']
global use_cloud_voice_vox
use_cloud_voice_vox = data['use_cloud_voice_vox']
global use_elevenlab
use_elevenlab = data['use_elevenlab']
global elevenlab_api_key
elevenlab_api_key = data['elevenlab_api_key']
streamChat.twitch_access_token = data['twitch_access_token']
streamChat.twitch_channel_name = data['twitch_channel_name']
streamChat.youtube_video_id = data['youtube_video_id']

if (elevenlab_api_key == ''):
elevenlab_api_key = os.getenv("ELEVENLAB_API_KEY")

except:
print("Unable to load JSON file.")
print(traceback.format_exc())
Expand Down Expand Up @@ -99,6 +109,8 @@ def save_settings(key, value):

VOICE_OUTPUT_FILENAME = "audioResponse.wav"

use_elevenlab = False
elevenlab_api_key = ''
use_cloud_voice_vox = False
voice_vox_api_key = ''
speakersResponse = None
Expand Down Expand Up @@ -241,12 +253,15 @@ def cloud_synthesize(text, speaker_id, api_key=''):


def syntheize_audio(text, speaker_id):
global use_cloud_voice_vox
global voice_vox_api_key
if (use_cloud_voice_vox):
cloud_synthesize(text, speaker_id, api_key=voice_vox_api_key)
global use_cloud_voice_vox, voice_vox_api_key
global use_elevenlab
if (use_elevenlab):
elevenlab_synthesize(text)
else:
local_synthesize(text, speaker_id)
if (use_cloud_voice_vox):
cloud_synthesize(text, speaker_id, api_key=voice_vox_api_key)
else:
local_synthesize(text, speaker_id)


def local_synthesize(text, speaker_id):
Expand All @@ -259,6 +274,32 @@ def local_synthesize(text, speaker_id):
file.write(AudioResponse.content)


def elevenlab_synthesize(message):

global elevenlab_api_key
url = f'https://api.elevenlabs.io/v1/text-to-speech/MF3mGyEYCl7XYWbV9V6O'
headers = {
'accept': 'audio/mpeg',
'xi-api-key': elevenlab_api_key,
'Content-Type': 'application/json'
}
data = {
'text': message,
'voice_settings': {
'stability': 0.75,
'similarity_boost': 0.75
}
}
print(f"Sending POST request to: {url}")
response = requests.post(url, headers=headers, json=data, stream=True)
print(response)
audio_content = AudioSegment.from_file(
io.BytesIO(response.content), format="mp3")
audio_content.export(VOICE_OUTPUT_FILENAME, 'wav')
# with open(VOICE_OUTPUT_FILENAME, "wb") as file:
# file.write(audio_content.)


def PlayAudio():
# voiceLine = AudioSegment.from_wav(VOICE_OUTPUT_FILENAME)
# play(voiceLine)
Expand Down Expand Up @@ -421,7 +462,10 @@ def start_TTS_pipeline(input_text):
global pipeline_elapsed_time
pipeline_timer.start()
inputLanguage = language_dict[input_language_name][:2]
outputLanguage = 'ja'
if (use_elevenlab):
outputLanguage = 'en'
else:
outputLanguage = 'ja'
# print(f"inputLanguage: {inputLanguage}, outputLanguage: {outputLanguage}")
translate = inputLanguage != outputLanguage
if (translate):
Expand Down
25 changes: 24 additions & 1 deletion UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def log_message_on_console(self, message_text):
self.textbox.insert(customtkinter.INSERT, message_text+'\n')
self.textbox.configure(state="disabled")
self.textbox.see("end")



class ChatFrame(customtkinter.CTkFrame):
Expand Down Expand Up @@ -957,6 +956,21 @@ def __init__(self, *args, **kwargs):
self.openai_api_key_input.grid(
row=6, column=1, padx=10, pady=10, sticky='W')

self.use_elevenlab_var = customtkinter.BooleanVar(
self, STTS.use_elevenlab)
use_elevenlab_checkbox = customtkinter.CTkCheckBox(master=self, text="Use elevenlab on cloud (api key required)", command=self.set_use_elevenlab_var,
variable=self.use_elevenlab_var, onvalue=True, offvalue=False)
use_elevenlab_checkbox.grid(
row=7, column=0, padx=10, pady=10, sticky='W')
self.elevenlab_api_key_var = customtkinter.StringVar(
self, STTS.voice_vox_api_key)
self.elevenlab_api_key_var.trace_add(
'write', self.update_elevenlab_api_key)
self.elevenlab_api_key_input = customtkinter.CTkEntry(
master=self, textvariable=self.elevenlab_api_key_var)
self.elevenlab_api_key_input.grid(
row=7, column=1, padx=10, pady=10, sticky='W')

def input_device_index_update_callback(self, value):
STTS.input_device_id = value

Expand Down Expand Up @@ -989,6 +1003,15 @@ def update_voicevox_api_key(self, str1, str2, str3):
STTS.voice_vox_api_key = self.voicevox_api_key_var.get()
STTS.save_config('voice_vox_api_key', STTS.voice_vox_api_key)

def set_use_elevenlab_var(self):
print(f'use_elevenlab set to {self.use_voicevox_var.get()}')
STTS.use_elevenlab = self.use_elevenlab_var.get()
STTS.save_config('use_elevenlab', STTS.use_elevenlab)

def update_elevenlab_api_key(self, str1, str2, str3):
STTS.elevenlab_api_key = self.elevenlab_api_key_var.get()
STTS.save_config('elevenlab_api_key', STTS.elevenlab_api_key)

def update_openai_api_key(self, str1, str2, str3):
chatbot.openai_api_key = self.openai_api_key_var.get()
STTS.save_config('openai_api_key', chatbot.openai_api_key)
Expand Down
Binary file modified audioResponse.wav
Binary file not shown.
13 changes: 12 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{"use_deepl": false, "deepl_api_key": "", "use_cloud_voice_vox": false, "voice_vox_api_key": "", "openai_api_key": "", "youtube_video_id": "", "twitch_access_token": "", "twitch_channel_name": ""}
{
"use_deepl": false,
"deepl_api_key": "",
"use_cloud_voice_vox": false,
"voice_vox_api_key": "",
"openai_api_key": "",
"youtube_video_id": "",
"twitch_access_token": "",
"twitch_channel_name": "",
"use_elevenlab": false,
"elevenlab_api_key": ""
}

0 comments on commit 3efa81d

Please sign in to comment.