Hematej commited on
Commit
ed5d162
·
verified ·
1 Parent(s): fdd8ee2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -6
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import torch
3
  from TTS.api import TTS
4
  import os
 
5
 
6
  # Agree to Coqui Terms of Service
7
  os.environ["COQUI_TOS_AGREED"] = "1"
@@ -10,13 +11,51 @@ os.environ["COQUI_TOS_AGREED"] = "1"
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
  print(f"Running on: {device}")
12
 
 
 
 
 
 
 
 
 
13
  # Initialize TTS model (XTTS v2)
14
- tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=False, gpu=torch.cuda.is_available())
 
 
 
 
 
 
 
15
 
16
  # Define clone function
17
  def clone(text, audio):
18
- tts.tts_to_file(text=text, speaker_wav=audio, language="en", file_path="./output.wav")
19
- return "./output.wav"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Build the Gradio Interface
22
  iface = gr.Interface(
@@ -29,9 +68,7 @@ iface = gr.Interface(
29
  title='Voice Clone',
30
  description="""
31
  by [Tony Assi](https://www.tonyassi.com/)
32
-
33
  This space uses the `xtts_v2` model. **Non-commercial use only**. [Coqui Public Model License](https://coqui.ai/cpml)
34
-
35
  Please ❤️ this Space. <a href="mailto:[email protected]">Email me</a>.
36
  """,
37
  theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"),
@@ -44,7 +81,9 @@ iface = gr.Interface(
44
  ["Hey there, it's me Jeff Goldblum. Type in whatever you'd like me to say.", "./audio/Jeff-Goldblum.mp3"],
45
  ["Hey there, it's me Heath Ledger as the Joker. Type in whatever you'd like me to say.", "./audio/Heath-Ledger.mp3"]
46
  ],
47
- cache_examples=False # <--- disables caching to avoid FileNotFoundError
48
  )
49
 
 
50
  iface.launch()
 
 
2
  import torch
3
  from TTS.api import TTS
4
  import os
5
+ import time # Import time module for measuring performance
6
 
7
  # Agree to Coqui Terms of Service
8
  os.environ["COQUI_TOS_AGREED"] = "1"
 
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
  print(f"Running on: {device}")
13
 
14
+ # --- Add check for model path before initialization ---
15
+ model_path = os.path.expanduser("~/.local/share/tts/tts_models--multilingual--multi-dataset--xtts_v2")
16
+ if not os.path.exists(model_path):
17
+ print(f"Model files not found at {model_path}. Initial download will occur.")
18
+ else:
19
+ print(f"Model files found at {model_path}. Skipping initial download.")
20
+
21
+
22
  # Initialize TTS model (XTTS v2)
23
+ print("Initializing TTS model...")
24
+ try:
25
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=False, gpu=torch.cuda.is_available())
26
+ print("TTS model initialized successfully.")
27
+ except Exception as e:
28
+ print(f"Error initializing TTS model: {e}")
29
+ print("This might indicate issues with model files or environment setup.")
30
+ exit() # Exit if model fails to initialize
31
 
32
  # Define clone function
33
  def clone(text, audio):
34
+ print(f"Received request: Text='{text[:50]}...' (length {len(text)}), Audio='{audio}'") # Log input
35
+
36
+ if not os.path.exists(audio):
37
+ print(f"Error: Reference audio file not found at {audio}")
38
+ gr.Warning("Reference audio file not found. Please ensure the path is correct.")
39
+ return None # Return None or handle error appropriately
40
+
41
+ start_time = time.time()
42
+ try:
43
+ print("Starting audio generation...")
44
+ tts.tts_to_file(text=text, speaker_wav=audio, language="en", file_path="./output.wav")
45
+ end_time = time.time()
46
+ print(f"Audio generated in {end_time - start_time:.2f} seconds.")
47
+
48
+ if os.path.exists("./output.wav") and os.path.getsize("./output.wav") > 0:
49
+ print("Output file './output.wav' created successfully.")
50
+ return "./output.wav"
51
+ else:
52
+ print("Warning: Output file is empty or not created.")
53
+ gr.Warning("Audio generation completed, but output file might be empty or not created.")
54
+ return None # Indicate failure to Gradio
55
+ except Exception as e:
56
+ print(f"Error during audio generation: {e}")
57
+ gr.Error(f"An error occurred during audio generation: {e}")
58
+ return None # Indicate failure to Gradio
59
 
60
  # Build the Gradio Interface
61
  iface = gr.Interface(
 
68
  title='Voice Clone',
69
  description="""
70
  by [Tony Assi](https://www.tonyassi.com/)
 
71
  This space uses the `xtts_v2` model. **Non-commercial use only**. [Coqui Public Model License](https://coqui.ai/cpml)
 
72
  Please ❤️ this Space. <a href="mailto:[email protected]">Email me</a>.
73
  """,
74
  theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"),
 
81
  ["Hey there, it's me Jeff Goldblum. Type in whatever you'd like me to say.", "./audio/Jeff-Goldblum.mp3"],
82
  ["Hey there, it's me Heath Ledger as the Joker. Type in whatever you'd like me to say.", "./audio/Heath-Ledger.mp3"]
83
  ],
84
+ cache_examples=False
85
  )
86
 
87
+ print("\nLaunching Gradio interface...")
88
  iface.launch()
89
+ print("Gradio interface launched.")