Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,13 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
-
|
| 4 |
-
# Set a writable cache directory for torch and preload Silero VAD
|
| 5 |
-
os.environ["TORCH_HOME"] = "/tmp/torch"
|
| 6 |
-
os.makedirs("/tmp/torch", exist_ok=True)
|
| 7 |
-
|
| 8 |
-
# Preload Silero VAD to avoid runtime issues
|
| 9 |
-
# try:
|
| 10 |
-
# torch.hub.load(repo_or_dir="snakers4/silero-vad", model="silero_vad", trust_repo=True)
|
| 11 |
-
# print("Silero VAD model preloaded successfully.")
|
| 12 |
-
# except Exception as e:
|
| 13 |
-
# print(f"Failed to preload Silero VAD: {e}")
|
| 14 |
-
|
| 15 |
-
# Now proceed with the rest of the imports
|
| 16 |
-
from openvoice import se_extractor
|
| 17 |
-
from openvoice.api import ToneColorConverter
|
| 18 |
import gradio as gr
|
| 19 |
-
import
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
import time
|
| 23 |
-
import uuid
|
| 24 |
|
| 25 |
-
#
|
|
|
|
| 26 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 27 |
os.environ["HF_HUB_CACHE"] = "/tmp/huggingface"
|
| 28 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
|
@@ -30,48 +15,53 @@ os.environ["MPLCONFIGDIR"] = "/tmp"
|
|
| 30 |
os.environ["XDG_CACHE_HOME"] = "/tmp"
|
| 31 |
os.environ["XDG_CONFIG_HOME"] = "/tmp"
|
| 32 |
os.environ["NUMBA_DISABLE_CACHE"] = "1"
|
|
|
|
| 33 |
os.makedirs("/tmp/huggingface", exist_ok=True)
|
| 34 |
os.makedirs("/tmp/flagged", exist_ok=True)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
ckpt_converter = "checkpoints/converter/config.json"
|
| 38 |
output_dir = "/tmp/outputs"
|
| 39 |
os.makedirs(output_dir, exist_ok=True)
|
| 40 |
|
| 41 |
-
# Initialize
|
|
|
|
| 42 |
tone_color_converter = ToneColorConverter(ckpt_converter)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
|
| 47 |
def clone_and_speak(text, speaker_wav):
|
| 48 |
if not speaker_wav:
|
| 49 |
return "Please upload a reference .wav file."
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
#
|
| 61 |
tone_color_converter.convert(
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
output_path=output_wav,
|
| 68 |
-
top_k=10,
|
| 69 |
-
temperature=0.3
|
| 70 |
)
|
| 71 |
|
| 72 |
-
return
|
| 73 |
|
| 74 |
-
# Gradio interface
|
| 75 |
gr.Interface(
|
| 76 |
fn=clone_and_speak,
|
| 77 |
inputs=[
|
|
@@ -79,7 +69,7 @@ gr.Interface(
|
|
| 79 |
gr.Audio(type="filepath", label="Upload a Reference Voice (.wav)")
|
| 80 |
],
|
| 81 |
outputs=gr.Audio(label="Synthesized Output"),
|
| 82 |
-
flagging_dir="/tmp/flagged",
|
| 83 |
-
title="Text to Voice using OpenVoice",
|
| 84 |
-
description="
|
| 85 |
).launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import time
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
+
from melo.api import TTS
|
| 7 |
+
from openvoice.api import ToneColorConverter
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Set temporary cache locations for Hugging Face Spaces
|
| 10 |
+
os.environ["TORCH_HOME"] = "/tmp/torch"
|
| 11 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 12 |
os.environ["HF_HUB_CACHE"] = "/tmp/huggingface"
|
| 13 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
|
|
|
|
| 15 |
os.environ["XDG_CACHE_HOME"] = "/tmp"
|
| 16 |
os.environ["XDG_CONFIG_HOME"] = "/tmp"
|
| 17 |
os.environ["NUMBA_DISABLE_CACHE"] = "1"
|
| 18 |
+
os.makedirs("/tmp/torch", exist_ok=True)
|
| 19 |
os.makedirs("/tmp/huggingface", exist_ok=True)
|
| 20 |
os.makedirs("/tmp/flagged", exist_ok=True)
|
| 21 |
|
| 22 |
+
# Output folder
|
|
|
|
| 23 |
output_dir = "/tmp/outputs"
|
| 24 |
os.makedirs(output_dir, exist_ok=True)
|
| 25 |
|
| 26 |
+
# Initialize tone converter
|
| 27 |
+
ckpt_converter = "checkpoints/converter/config.json"
|
| 28 |
tone_color_converter = ToneColorConverter(ckpt_converter)
|
| 29 |
|
| 30 |
+
# Device setting
|
| 31 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
|
| 33 |
def clone_and_speak(text, speaker_wav):
|
| 34 |
if not speaker_wav:
|
| 35 |
return "Please upload a reference .wav file."
|
| 36 |
|
| 37 |
+
base_name = f"output_{int(time.time())}_{uuid.uuid4().hex[:6]}"
|
| 38 |
+
tmp_melo_path = f"{output_dir}/{base_name}_tmp.wav"
|
| 39 |
+
final_output_path = f"{output_dir}/{base_name}_converted.wav"
|
| 40 |
+
|
| 41 |
+
# Use English speaker model
|
| 42 |
+
model = TTS(language="EN", device=device)
|
| 43 |
+
speaker_ids = model.hps.data.spk2id
|
| 44 |
+
default_speaker_id = next(iter(speaker_ids.values()))
|
| 45 |
+
|
| 46 |
+
# Generate base TTS voice
|
| 47 |
+
model.tts_to_file(text, default_speaker_id, tmp_melo_path)
|
| 48 |
|
| 49 |
+
# Use speaker_wav as reference to extract style embedding
|
| 50 |
+
from openvoice import se_extractor
|
| 51 |
+
ref_se, _ = se_extractor.get_se(speaker_wav, tone_color_converter, vad=False)
|
| 52 |
|
| 53 |
+
# Run the tone conversion
|
| 54 |
tone_color_converter.convert(
|
| 55 |
+
audio_src_path=tmp_melo_path,
|
| 56 |
+
src_se=ref_se,
|
| 57 |
+
tgt_se=ref_se,
|
| 58 |
+
output_path=final_output_path,
|
| 59 |
+
message="@HuggingFace",
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
+
return final_output_path
|
| 63 |
|
| 64 |
+
# Gradio interface
|
| 65 |
gr.Interface(
|
| 66 |
fn=clone_and_speak,
|
| 67 |
inputs=[
|
|
|
|
| 69 |
gr.Audio(type="filepath", label="Upload a Reference Voice (.wav)")
|
| 70 |
],
|
| 71 |
outputs=gr.Audio(label="Synthesized Output"),
|
| 72 |
+
flagging_dir="/tmp/flagged",
|
| 73 |
+
title="Text to Voice using Melo TTS + OpenVoice",
|
| 74 |
+
description="Use Melo TTS for base synthesis and OpenVoice to apply a reference speaker's tone.",
|
| 75 |
).launch()
|