Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import librosa
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
|
| 6 |
+
# Import SV2TTS modules (from the Real-Time-Voice-Cloning repo you’ll add as dependency)
|
| 7 |
+
from encoder import inference as encoder
|
| 8 |
+
from synthesizer.inference import Synthesizer
|
| 9 |
+
from vocoder import inference as vocoder
|
| 10 |
+
|
| 11 |
+
# Load models (make sure weights are in /saved_models in your repo)
|
| 12 |
+
encoder.load_model("saved_models/encoder/saved_model.pt")
|
| 13 |
+
synthesizer = Synthesizer("saved_models/synthesizer/saved_model.pt")
|
| 14 |
+
vocoder.load_model("saved_models/vocoder/saved_model.pt")
|
| 15 |
+
|
| 16 |
+
def clone_voice(sample_wav, text, consent):
|
| 17 |
+
if not consent:
|
| 18 |
+
return "⚠️ You must confirm consent to use this voice.", None
|
| 19 |
+
if sample_wav is None or text.strip() == "":
|
| 20 |
+
return "Please upload a sample and enter text.", None
|
| 21 |
+
|
| 22 |
+
wav, sr = librosa.load(sample_wav, sr=None)
|
| 23 |
+
wav = encoder.preprocess_wav(wav, sr)
|
| 24 |
+
embed = encoder.embed_utterance(wav)
|
| 25 |
+
|
| 26 |
+
# Generate mel spectrogram
|
| 27 |
+
specs = synthesizer.synthesize_spectrograms([text], [embed])
|
| 28 |
+
# Vocoder to waveform
|
| 29 |
+
generated_wav = vocoder.infer_waveform(specs[0])
|
| 30 |
+
|
| 31 |
+
out_path = "out.wav"
|
| 32 |
+
sf.write(out_path, generated_wav, synthesizer.sample_rate)
|
| 33 |
+
return "✅ Done", out_path
|
| 34 |
+
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("# 🎙️ SV2TTS Voice Cloning Demo")
|
| 37 |
+
sample = gr.Audio(label="Upload speaker sample (5–10s)", source="upload", type="filepath")
|
| 38 |
+
txt = gr.Textbox(label="Text to say", value="Hello, this is a test.")
|
| 39 |
+
consent = gr.Checkbox(label="I confirm I have permission to clone this voice", value=False)
|
| 40 |
+
status = gr.Textbox(label="Status")
|
| 41 |
+
out_audio = gr.Audio(label="Generated audio")
|
| 42 |
+
btn = gr.Button("Generate")
|
| 43 |
+
btn.click(fn=clone_voice, inputs=[sample, txt, consent], outputs=[status, out_audio])
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|