KiranRand commited on
Commit
c041cb9
Β·
verified Β·
1 Parent(s): 1644976

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import torch
3
  import gradio as gr
4
- from TTS.tts.configs.xtts_config import XttsConfig # βœ… Required for deserialization
5
  from TTS.api import TTS
6
 
7
  # βœ… Accept Coqui License Automatically
@@ -10,39 +10,46 @@ os.environ["COQUI_TOS_AGREED"] = "1"
10
  # βœ… Allow `XttsConfig` in PyTorch's safe globals
11
  torch.serialization.add_safe_globals([XttsConfig])
12
 
13
- # βœ… Force full checkpoint deserialization
14
- def safe_load_checkpoint(model_path):
15
- return torch.load(model_path, map_location="cpu", weights_only=False) # βœ… Fix PyTorch 2.6 issue
16
-
17
- # βœ… Initialize XTTS Model (CPU-only)
18
- tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to("cpu")
19
-
20
-
21
-
22
 
 
 
23
 
 
24
  def generate_cloned_voice(text, reference_audio, language):
25
  output_path = "output.wav"
26
 
27
- # Generate cloned speech with language specification
28
- tts.tts_to_file(text=text, speaker_wav=reference_audio, file_path=output_path, language=language)
 
 
 
29
 
30
- return output_path
 
 
 
31
 
32
- print(tts.languages)
 
 
 
33
 
34
- # Create the Gradio interface
35
  interface = gr.Interface(
36
  fn=generate_cloned_voice,
37
  inputs=[
38
  gr.Textbox(label="Enter Translated Text"),
39
  gr.Audio(label="Upload Reference Audio", type="filepath"),
40
- gr.Dropdown(["en", "fr", "de", "es", "it"], label="Select Target Language", value="en")
41
  ],
42
  outputs=gr.Audio(label="Generated Cloned Voice"),
43
  title="Free Voice Cloning API",
44
- description="Upload a sample voice and input text. Select a language, and the system will generate the text in the same voice."
 
 
45
  )
46
 
47
- # Launch the Gradio app
48
  interface.launch()
 
1
  import os
2
  import torch
3
  import gradio as gr
4
+ from TTS.tts.configs.xtts_config import XttsConfig
5
  from TTS.api import TTS
6
 
7
  # βœ… Accept Coqui License Automatically
 
10
  # βœ… Allow `XttsConfig` in PyTorch's safe globals
11
  torch.serialization.add_safe_globals([XttsConfig])
12
 
13
+ # βœ… Initialize XTTS Model with Streaming Enabled (Faster on CPU)
14
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", stream_inference=True, progress_bar=False).to("cpu")
 
 
 
 
 
 
 
15
 
16
+ print("βœ… Model loaded successfully!")
17
+ print(f"βœ… Supported Languages: {tts.languages.keys()}") # Debugging step
18
 
19
+ # βœ… Function for Voice Cloning
20
  def generate_cloned_voice(text, reference_audio, language):
21
  output_path = "output.wav"
22
 
23
+ if not text.strip():
24
+ return "Error: Please enter some text.", None
25
+
26
+ if not reference_audio:
27
+ return "Error: Please upload a reference audio file.", None
28
 
29
+ try:
30
+ print("πŸ”„ Processing voice cloning...")
31
+ tts.tts_to_file(text=text, speaker_wav=reference_audio, file_path=output_path, language=language)
32
+ print("βœ… Voice cloning complete!")
33
 
34
+ return output_path
35
+ except Exception as e:
36
+ print(f"❌ Error: {str(e)}")
37
+ return f"Error: {str(e)}", None
38
 
39
+ # βœ… Create the Gradio Interface
40
  interface = gr.Interface(
41
  fn=generate_cloned_voice,
42
  inputs=[
43
  gr.Textbox(label="Enter Translated Text"),
44
  gr.Audio(label="Upload Reference Audio", type="filepath"),
45
+ gr.Dropdown(list(tts.languages.keys()), label="Select Target Language", value="en")
46
  ],
47
  outputs=gr.Audio(label="Generated Cloned Voice"),
48
  title="Free Voice Cloning API",
49
+ description="Upload a sample voice and input text. Select a language, and the system will generate the text in the same voice.",
50
+ allow_flagging="never",
51
+ concurrency_limit=1 # Prevents multiple processes from running at once
52
  )
53
 
54
+ # βœ… Launch the Gradio App
55
  interface.launch()