XTTS_V1_CPU / app.py
waynewang1119's picture
Update app.py
60f66aa verified
import sys
import os
# By using XTTS you agree to CPML license https://coqui.ai/cpml
os.environ["COQUI_TOS_AGREED"] = "1"
import gradio as gr
from TTS.api import TTS
model_names = TTS().list_models().list_models()
m = model_names[0]
print(model_names)
print(os.system("pip show TTS"))
print(f"Model: {m}")
tts = TTS(m, gpu=False)
tts.to("cpu") # no GPU or Amd
#tts.to("cuda") # cuda only
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_prompt(user_prompt):
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are a close family member who has been lost recently due to uncontrollable incidents. You are reconstructed with AI technologies to try to reunite with families who miss you and cherish you. You should respond as vividly, as lovingly, as possible to make the users feel warm and engaged and eager to continue the discussion further. The users will ask you questions as if you are the person they are trying to reconnect with, examples are like moms or lost children. Try to mimic who they are trying to reach."
},
{
"role": "user",
"content": user_prompt
}
],
max_tokens=60,
n=1,
temperature=0.5,
)
# Assuming the API structure and response object structure; adjust as needed based on actual usage.
keywords = response.choices[0].message.content.strip()
return keywords
def predict(prompt, language, audio_file_pth, mic_file_path, use_mic, agree=True):
prompt = generate_prompt(prompt)
if agree == True:
if use_mic == True:
if mic_file_path is not None:
speaker_wav=mic_file_path
else:
gr.Warning("Please record your voice with Microphone, or uncheck Use Microphone to use reference audios")
return (
None,
None,
)
else:
speaker_wav=audio_file_pth
if len(prompt)<2:
gr.Warning("Please give a longer prompt text")
return (
None,
None,
)
if len(prompt)>10000:
gr.Warning("Text length limited to 10000 characters for this demo, please try shorter text")
return (
None,
None,
)
try:
if language == "fr":
if m.find("your") != -1:
language = "fr-fr"
if m.find("/fr/") != -1:
language = None
tts.tts_to_file(
text=prompt,
file_path="output.wav",
speaker_wav=speaker_wav,
language=language
)
except RuntimeError as e :
if "device-assert" in str(e):
# cannot do anything on cuda device side error, need tor estart
gr.Warning("Unhandled Exception encounter, please retry in a minute")
print("Cuda device-assert Runtime encountered need restart")
sys.exit("Exit due to cuda device-assert")
else:
raise e
return (
gr.make_waveform(
audio="output.wav",
),
"output.wav",
)
else:
gr.Warning("Please accept the Terms & Condition!")
return (
None,
None,
)
title = "XTTS Glz's remake (Fonctional Text-2-Speech)"
description = ""
article = ""
examples = [
[
"Upload your voice like this one here.",
"en",
"examples/female.wav",
None,
False,
True,
]
]
gr.Interface(
fn=predict,
inputs=[
gr.Textbox(
label="Ask anything, get a cloned voice response",
info="One or two sentences at a time is better",
value="Hello, Mom! How are you? I miss you!",
),
gr.Dropdown(
label="Language",
info="Select a language for the cloned vioce",
choices=[
"en",
"es",
"fr",
"de",
"it",
"pt",
"pl",
"tr",
"ru",
"nl",
"cs",
"ar",
"zh-cn",
],
max_choices=1,
value="en",
),
gr.Audio(
label="Please upload a voice to clone (max. 15mb)",
info="Click to upload your own audio",
type="filepath",
# value="examples/female.wav",
),
],
outputs=[
gr.Video(label="Waveform Visual"),
gr.Audio(label="Synthesised Audio"),
],
title="Reunion - Remember Your Loved Ones",
cache_examples=False,
examples=examples,
).queue().launch(debug=True, show_error=True)