remiai3 commited on
Commit
e5fa7fa
Β·
verified Β·
1 Parent(s): ad8ff68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -38
app.py CHANGED
@@ -1,46 +1,38 @@
1
- import gradio as gr
2
- import torch
3
- from TTS.api import TTS
4
- from pydub import AudioSegment
5
  import os
 
 
6
 
7
- # Load XTTS model (runs on CPU if no GPU available)
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
- tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to(device)
10
 
11
- # Convert mp3 to wav if needed
12
- def convert_to_wav(file_path):
13
- if file_path.endswith(".mp3"):
14
- sound = AudioSegment.from_mp3(file_path)
15
- wav_path = file_path.replace(".mp3", ".wav")
16
- sound.export(wav_path, format="wav")
17
- return wav_path
18
- return file_path
19
 
20
- # Text β†’ Speech cloning
21
- def clone_from_text(sample_voice, text):
22
- if not sample_voice or not text.strip():
23
- return None
24
- sample_voice = convert_to_wav(sample_voice)
25
- output_path = "output.wav"
26
 
27
- tts.tts_to_file(
28
- text=text,
29
- file_path=output_path,
30
- speaker_wav=sample_voice,
31
- language="en"
32
- )
33
- return output_path
 
 
 
34
 
35
  # Gradio UI
36
  with gr.Blocks() as demo:
37
- gr.Markdown("## 🎀 Voice Cloning App (XTTS-v2 on CPU)\nUpload a sample voice and enter text.")
38
-
39
- sample_voice_input = gr.Audio(type="filepath", label="Upload Sample Voice (.mp3/.wav)")
40
- text_input = gr.Textbox(label="Enter Text")
41
- tts_output = gr.Audio(label="Generated Speech")
42
- btn1 = gr.Button("Generate")
43
-
44
- btn1.click(fn=clone_from_text, inputs=[sample_voice_input, text_input], outputs=tts_output)
45
-
46
- demo.launch()
 
 
 
 
 
 
 
1
  import os
2
+ from TTS.api import TTS
3
+ import gradio as gr
4
 
5
+ # βœ… Auto-accept Coqui license (non-commercial CPML)
6
+ os.environ["COQUI_TOS_AGREED"] = "1"
 
7
 
8
+ # Pick device
9
+ device = "cpu"
 
 
 
 
 
 
10
 
11
+ # Load XTTS-v2
12
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to(device)
 
 
 
 
13
 
14
+ def clone_voice(sample_audio, text):
15
+ if sample_audio is None:
16
+ return "Please upload a voice sample.", None
17
+
18
+ output_path = "output.wav"
19
+ tts.tts_to_file(text=text,
20
+ speaker_wav=sample_audio,
21
+ language="en",
22
+ file_path=output_path)
23
+ return f"Generated speech for: {text}", output_path
24
 
25
  # Gradio UI
26
  with gr.Blocks() as demo:
27
+ gr.Markdown("## 🎀 Voice Cloning with XTTS-v2")
28
+ with gr.Row():
29
+ sample = gr.Audio(label="Upload a sample voice", type="filepath")
30
+ text = gr.Textbox(label="Enter text to speak")
31
+ btn = gr.Button("Generate Voice")
32
+ output_text = gr.Textbox(label="Status")
33
+ output_audio = gr.Audio(label="Cloned Voice", type="filepath")
34
+
35
+ btn.click(fn=clone_voice, inputs=[sample, text], outputs=[output_text, output_audio])
36
+
37
+ if __name__ == "__main__":
38
+ demo.launch()