ardha27 / AI-Waifu-Vtuber

AI Vtuber for Streaming on Youtube/Twitch
786 stars 126 forks source link

sorry for disturb, there is a question about the translation #29

Open wjbwnd opened 1 year ago

wjbwnd commented 1 year ago

in main.py is written: "# translating is optional" I don't really understand how to convert the code correctly so that the input and output languages are the same (Russian, I have already set up tts)

thanks

ardha27 commented 1 year ago

It depends on your case. If you write the identity.txt in russian and the questions also in russian. Then the answer from openai will be 100% russian, so you will not need any translation

If you use it on livestreams. Although you write identity.txt in russian. If there are any person who ask in other languages than russian. Sometimes the bot will answer the questions on that person languange. So you need to translate the answer from openai before you pass it to your tts

wjbwnd commented 1 year ago

so wasn't i should do word function "translate_text"? i think translate from RU to RU will be weird or something like this

wjbwnd commented 1 year ago

i tried to do some corrections and always get the same response in console run.py

error detect error translate and some errors like this

ardha27 commented 1 year ago

You need to run same version of googletrans like requirements.txt version

wjbwnd commented 1 year ago

It turns out I just need to change the language in identify.txt and then just run the file without changing the translation functions?

ardha27 commented 1 year ago

You dont need the translation, just comment all translation on run.py

ardha27 commented 1 year ago

Pass the openai answer to your tts without translating it

wjbwnd commented 1 year ago

I'll try when I get home, thank you

wjbwnd commented 1 year ago

I apologize again, but it turns out that I have commented out some of the lines and do not receive an answer image nothing happens after these lines

here is part of the changed code: image

I think I'm making a very stupid mistake somewhere, but for some reason I can't figure out what the problem is.

Abbadon999 commented 1 year ago

Just use my code, it works

A snippet of modified code in run.py:

def translate_text(text):
    global is_Speaking
    # subtitle will act as subtitle for the viewer
    # subtitle = translate_google(text, "ID")

    # tts will be the string to be converted to audio
    detect = detect_google(text)
    tts_ru = translate_google(text, f"{detect}", "RU")
    try:
        # print("ID Answer: " + subtitle)
        print("RU Answer: " + tts_ru)
    except Exception as e:
        print("Error printing text: {0}".format(e))
        return

    # Choose between the available TTS engines
    # Japanese TTS
    # voicevox_tts(tts)

    # Silero TTS, Silero TTS can generate English, Russian, French, Hindi, Spanish, German, etc. Uncomment the line below. Make sure the input is in that language
    silero_tts(tts_ru, "ru", "v3_1_ru", "baya")

You will also need to change the code in TTS.py to do this:

# https://github.com/snakers4/silero-models#text-to-speech
def silero_tts(tts, language, model, speaker):
    device = torch.device('cpu')
    torch.set_num_threads(4)
    local_file = 'model.pt'

    if not os.path.isfile(local_file):
        torch.hub.download_url_to_file(f'https://models.silero.ai/models/tts/ru/v3_1_ru.pt',
                                    local_file)  

    model = torch.package.PackageImporter(local_file).load_pickle("tts_models", "model")
    model.to(device)

    example_text = 'В недрах тундры выдры в г+етрах т+ырят в вёдра ядра кедров.'
    sample_rate = 48000
    speaker='baya'

    audio_paths = model.save_wav(text=tts,
                                speaker=speaker,
                                sample_rate=sample_rate)
wjbwnd commented 1 year ago

@Abbadon999, can you please tell me what you did with the file translate.py ? apparently I changed something wrong again and I have an error again

Abbadon999 commented 1 year ago

You don't need to change anything in translate.py, because everything works as it is. But you will need to download the voice model https://models.silero.ai/models/tts/ru/v3_1_ru.pt and put it in the folder with run.py, you probably already have a model there, but you will need to delete it and rename "v3_1_ru" to "model"

wjbwnd commented 1 year ago

omg, i'm so sorry @Abbadon999 here is my code TTS.py

import os
import torch

#https://github.com/snakers4/silero-models#text-to-speech
def silero_tts(tts, language, model, speaker):
    device = torch.device('cpu')
    torch.set_num_threads(4)
    local_file = 'model.pt'

    if not os.path.isfile(local_file):
        torch.hub.download_url_to_file(f'https://models.silero.ai/models/tts/ru/v3_1_ru.pt',
                                    local_file)  

    model = torch.package.PackageImporter(local_file).load_pickle("tts_models", "model")
    model.to(device)

    example_text = 'В недрах тундры выдры в г+етрах т+ырят в вёдра ядра кедров.'
    sample_rate = 48000
    speaker='baya'

    audio_paths = model.save_wav(text=tts,
                                speaker=speaker,
                                sample_rate=sample_rate)

for some reason, it does not overwrite tts to the test000.wav file

wjbwnd commented 1 year ago

and when i run run.py I get

openai.error.RateLimitError: Rate limit reached for default-gpt-3.5-turbo in organization org-xxx on requests per min. Limit: 20 / min. Please try again in 3s. Contact support@openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://platform.openai.com/account/billing to add a payment method

although if you look at the statistics of the account in openai, I have no restrictions at the moment (I still have a lot of free answer generation in stock)

Abbadon999 commented 1 year ago

Reinstall everything and paste this modified code into TTS.py:

import os
import torch

def download_model(model_url, local_file):
    if not os.path.isfile(local_file):
        torch.hub.download_url_to_file(model_url, local_file)

def load_model(local_file):
    return torch.package.PackageImporter(local_file).load_pickle("tts_models", "model")

def save_audio(model, text, speaker, sample_rate):
    return model.save_wav(text=text, speaker=speaker, sample_rate=sample_rate)

def silero_tts(tts, language, model, speaker):
    local_file = 'model.pt'
    model_url = f'https://models.silero.ai/models/tts/{language}/{model}'

    download_model(model_url, local_file)
    model = load_model(local_file)
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    torch.set_num_threads(torch.get_num_threads())

    audio_paths = save_audio(model, text=tts, speaker=speaker, sample_rate=48000)

if __name__ == "__main__":
    silero_tts(tts='В недрах тундры выдры в г+етрах т+ырят в вёдра ядра кедров.', language='ru', model='v3_1_ru.pt', speaker='baya')

You will also need to change the translate_text function in run.py, here is a piece of code to replace your own:

# translating is optional
def translate_text(text):
    global is_Speaking
    # subtitle will act as subtitle for the viewer
    # subtitle = translate_google(text, "ID")

    # tts will be the string to be converted to audio
    detect = detect_google(text)
    tts_ru = translate_google(text, f"{detect}", "RU")
    try:
        # print("ID Answer: " + subtitle)
        print("RU Answer: " + tts_ru)
    except Exception as e:
        print("Error printing text: {0}".format(e))
        return

    # Choose between the available TTS engines
    # Japanese TTS
    # voicevox_tts(tts)

    # Silero TTS, Silero TTS can generate English, Russian, French, Hindi, Spanish, German, etc. Uncomment the line below. Make sure the input is in that language
    silero_tts(tts_ru, "ru", "v3_1_ru", "baya")

    # Generate Subtitle
    generate_subtitle(chat_now, text)

    time.sleep(1)

    # is_Speaking is used to prevent the assistant speaking more than one audio at a time
    is_Speaking = True
    winsound.PlaySound("test.wav", winsound.SND_FILENAME)
    is_Speaking = False

    # Clear the text files after the assistant has finished speaking
    time.sleep(1)
    with open ("output.txt", "w") as f:
        f.truncate(0)
    with open ("chat.txt", "w") as f:
        f.truncate(0)

Also don't forget to insert your api_keyfrom open ai at the beginning of the code, you must also duplicate it in the config.py file. And also do not forget to download the voice model as I said earlier and put the file in the folder with the project

wjbwnd commented 1 year ago

I miskliked, sorry ))

wjbwnd commented 1 year ago

@Abbadon999, so when i run run.py I get this

Question: Hello.
Error detect
Error translate
Error printing text: can only concatenate str (not "NoneType") to str
ardha27 commented 1 year ago

You need to install this version of googletrans

pip install googletrans==4.0.0rc1

wjbwnd commented 1 year ago

here I changed the tts source code to code @Abbadon999

Traceback (most recent call last):
  File "xxx\run.py", line 245, in <module>
    record_audio()
  File "xxx\run.py", line 68, in record_audio
    transcribe_audio("input.wav")
  File "xxx\run.py", line 87, in transcribe_audio
    openai_answer()
  File "xxx\run.py", line 120, in openai_answer
    translate_text(message)
  File "xxx\run.py", line 203, in translate_text
    silero_tts(tts_ru, "ru", "v3_1_ru", "baya")
  File "xxx\utils\TTS.py", line 23, in silero_tts
    audio_paths = save_audio(model, text=tts, speaker=speaker, sample_rate=48000)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xxx\utils\TTS.py", line 12, in save_audio
    return model.save_wav(text=text, speaker=speaker, sample_rate=sample_rate)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: TTSModelAcc_v2.save_wav() got an unexpected keyword argument 'text'
Abbadon999 commented 1 year ago

So do you want Russian or English as your output?

wjbwnd commented 1 year ago

@Abbadon999, russian