Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -25,68 +25,72 @@ if hf_token:
|
|
25 |
login(token=hf_token)
|
26 |
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
if cls._instance is None:
|
36 |
-
cls._instance = super(ModelSingleton, cls).__new__(cls)
|
37 |
-
cls._instance.initialized = False
|
38 |
-
cls._instance.tts_model = None
|
39 |
-
cls._instance.speakers_dict = None
|
40 |
-
cls._instance.init_lock = threading.RLock()
|
41 |
-
return cls._instance
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
"""Thread-safe initialization with singleton pattern"""
|
46 |
-
if self.initialized:
|
47 |
-
logger.info("Model already initialized, skipping...")
|
48 |
-
return self.tts_model, self.speakers_dict
|
49 |
-
|
50 |
-
with self.init_lock:
|
51 |
-
# Double-check pattern
|
52 |
-
if self.initialized:
|
53 |
-
logger.info("Model already initialized (double-check), skipping...")
|
54 |
-
return self.tts_model, self.speakers_dict
|
55 |
-
|
56 |
-
logger.info("Initializing Bambara TTS model...")
|
57 |
-
start_time = time.time()
|
58 |
-
|
59 |
-
try:
|
60 |
-
from maliba_ai.tts.inference import BambaraTTSInference
|
61 |
-
from maliba_ai.config.speakers import Adame, Moussa, Bourama, Modibo, Seydou
|
62 |
-
|
63 |
-
self.tts_model = BambaraTTSInference()
|
64 |
-
self.speakers_dict = {
|
65 |
-
"Adama": Adame,
|
66 |
-
"Moussa": Moussa,
|
67 |
-
"Bourama": Bourama,
|
68 |
-
"Modibo": Modibo,
|
69 |
-
"Seydou": Seydou
|
70 |
-
}
|
71 |
-
|
72 |
-
self.initialized = True
|
73 |
-
elapsed = time.time() - start_time
|
74 |
-
logger.info(f"Model initialized successfully in {elapsed:.2f} seconds!")
|
75 |
-
|
76 |
-
except Exception as e:
|
77 |
-
logger.error(f"Failed to initialize model: {e}")
|
78 |
-
raise e
|
79 |
-
|
80 |
-
return self.tts_model, self.speakers_dict
|
81 |
-
|
82 |
-
def get_model(self):
|
83 |
-
"""Get the model, initializing if needed"""
|
84 |
-
if not self.initialized:
|
85 |
-
return self.initialize()
|
86 |
-
return self.tts_model, self.speakers_dict
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
def validate_inputs(text, temperature, top_k, top_p, max_tokens):
|
92 |
if not text or not text.strip():
|
@@ -109,10 +113,17 @@ def generate_speech(text, speaker_name, use_advanced, temperature, top_k, top_p,
|
|
109 |
return None, "Please enter some Bambara text."
|
110 |
|
111 |
try:
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
speaker = speakers[speaker_name]
|
|
|
116 |
|
117 |
if use_advanced:
|
118 |
is_valid, error_msg = validate_inputs(text, temperature, top_k, top_p, max_tokens)
|
@@ -133,30 +144,20 @@ def generate_speech(text, speaker_name, use_advanced, temperature, top_k, top_p,
|
|
133 |
speaker_id=speaker
|
134 |
)
|
135 |
|
136 |
-
if waveform.size == 0:
|
137 |
return None, "Failed to generate audio. Please try again."
|
138 |
|
139 |
sample_rate = 16000
|
140 |
-
return (sample_rate, waveform), f"✅ Audio generated successfully"
|
141 |
|
142 |
except Exception as e:
|
143 |
logger.error(f"Speech generation failed: {e}")
|
144 |
return None, f"❌ Error: {str(e)}"
|
145 |
|
146 |
-
|
147 |
-
def preload_model():
|
148 |
-
"""Preload the model when the app starts"""
|
149 |
-
try:
|
150 |
-
logger.info("Preloading model...")
|
151 |
-
model_singleton.initialize()
|
152 |
-
logger.info("Model preloaded successfully!")
|
153 |
-
except Exception as e:
|
154 |
-
logger.error(f"Failed to preload model: {e}")
|
155 |
-
|
156 |
-
SPEAKER_NAMES = ["Adame", "Moussa", "Bourama", "Modibo", "Seydou"]
|
157 |
|
158 |
examples = [
|
159 |
-
["Aw ni ce", "
|
160 |
["Mali bɛna diya kɔsɛbɛ, ka a da a kan baara bɛ ka kɛ.", "Moussa"],
|
161 |
["Ne bɛ se ka sɛbɛnni yɛlɛma ka kɛ kuma ye", "Bourama"],
|
162 |
["I ka kɛnɛ wa?", "Modibo"],
|
@@ -277,7 +278,7 @@ def build_interface():
|
|
277 |
- **Speakers**: 5 different voice options
|
278 |
- **Sample Rate**: 16kHz
|
279 |
|
280 |
-
**
|
281 |
""")
|
282 |
|
283 |
def toggle_advanced(use_adv):
|
@@ -309,8 +310,7 @@ def main():
|
|
309 |
"""Main function to launch the Gradio interface"""
|
310 |
logger.info("Starting Bambara TTS Gradio interface.")
|
311 |
|
312 |
-
|
313 |
-
|
314 |
interface = build_interface()
|
315 |
interface.launch(
|
316 |
server_name="0.0.0.0",
|
|
|
25 |
login(token=hf_token)
|
26 |
|
27 |
|
28 |
+
_tts_model = None
|
29 |
+
_speakers_dict = None
|
30 |
+
_model_initialized = False
|
31 |
+
_initialization_in_progress = False
|
32 |
+
|
33 |
+
def get_speakers_dict():
|
34 |
+
"""Get speakers dictionary - moved to function to avoid import issues"""
|
35 |
+
try:
|
36 |
+
from maliba_ai.config.speakers import Adame, Moussa, Bourama, Modibo, Seydou
|
37 |
+
return {
|
38 |
+
"Adama": Adame,
|
39 |
+
"Moussa": Moussa,
|
40 |
+
"Bourama": Bourama,
|
41 |
+
"Modibo": Modibo,
|
42 |
+
"Seydou": Seydou
|
43 |
+
}
|
44 |
+
except Exception as e:
|
45 |
+
logger.error(f"Failed to import speakers: {e}")
|
46 |
+
return {}
|
47 |
+
|
48 |
+
@spaces.GPU()
|
49 |
+
def initialize_model_once():
|
50 |
+
global _tts_model, _speakers_dict, _model_initialized, _initialization_in_progress
|
51 |
|
52 |
+
if _model_initialized:
|
53 |
+
logger.info("Model already initialized, returning existing instance")
|
54 |
+
return _tts_model, _speakers_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
if _initialization_in_progress:
|
57 |
+
logger.info("Initialization already in progress, waiting...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
for _ in range(50):
|
60 |
+
time.sleep(0.1)
|
61 |
+
if _model_initialized:
|
62 |
+
return _tts_model, _speakers_dict
|
63 |
+
|
64 |
+
|
65 |
+
_initialization_in_progress = True
|
66 |
+
|
67 |
+
try:
|
68 |
+
logger.info("Initializing Bambara TTS model...")
|
69 |
+
start_time = time.time()
|
70 |
+
|
71 |
+
from maliba_ai.tts.inference import BambaraTTSInference
|
72 |
+
|
73 |
+
model = BambaraTTSInference()
|
74 |
+
speakers = get_speakers_dict()
|
75 |
+
|
76 |
+
if not speakers:
|
77 |
+
raise ValueError("Failed to load speakers dictionary")
|
78 |
+
|
79 |
+
_tts_model = model
|
80 |
+
_speakers_dict = speakers
|
81 |
+
_model_initialized = True
|
82 |
+
|
83 |
+
elapsed = time.time() - start_time
|
84 |
+
logger.info(f"Model initialized successfully in {elapsed:.2f} seconds!")
|
85 |
+
|
86 |
+
return _tts_model, _speakers_dict
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
logger.error(f"Failed to initialize model: {e}")
|
90 |
+
_initialization_in_progress = False
|
91 |
+
raise e
|
92 |
+
finally:
|
93 |
+
_initialization_in_progress = False
|
94 |
|
95 |
def validate_inputs(text, temperature, top_k, top_p, max_tokens):
|
96 |
if not text or not text.strip():
|
|
|
113 |
return None, "Please enter some Bambara text."
|
114 |
|
115 |
try:
|
116 |
+
tts, speakers = initialize_model_once()
|
117 |
+
|
118 |
+
if not tts or not speakers:
|
119 |
+
return None, "❌ Model not properly initialized"
|
120 |
+
|
121 |
+
if speaker_name not in speakers:
|
122 |
+
available_speakers = list(speakers.keys())
|
123 |
+
return None, f"❌ Speaker '{speaker_name}' not found. Available: {available_speakers}"
|
124 |
|
125 |
speaker = speakers[speaker_name]
|
126 |
+
logger.info(f"Using speaker: {speaker_name}")
|
127 |
|
128 |
if use_advanced:
|
129 |
is_valid, error_msg = validate_inputs(text, temperature, top_k, top_p, max_tokens)
|
|
|
144 |
speaker_id=speaker
|
145 |
)
|
146 |
|
147 |
+
if waveform is None or waveform.size == 0:
|
148 |
return None, "Failed to generate audio. Please try again."
|
149 |
|
150 |
sample_rate = 16000
|
151 |
+
return (sample_rate, waveform), f"✅ Audio generated successfully for speaker {speaker_name}"
|
152 |
|
153 |
except Exception as e:
|
154 |
logger.error(f"Speech generation failed: {e}")
|
155 |
return None, f"❌ Error: {str(e)}"
|
156 |
|
157 |
+
SPEAKER_NAMES = ["Adama", "Moussa", "Bourama", "Modibo", "Seydou"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
examples = [
|
160 |
+
["Aw ni ce", "Adama"],
|
161 |
["Mali bɛna diya kɔsɛbɛ, ka a da a kan baara bɛ ka kɛ.", "Moussa"],
|
162 |
["Ne bɛ se ka sɛbɛnni yɛlɛma ka kɛ kuma ye", "Bourama"],
|
163 |
["I ka kɛnɛ wa?", "Modibo"],
|
|
|
278 |
- **Speakers**: 5 different voice options
|
279 |
- **Sample Rate**: 16kHz
|
280 |
|
281 |
+
**Model loads once on first request and stays in memory**
|
282 |
""")
|
283 |
|
284 |
def toggle_advanced(use_adv):
|
|
|
310 |
"""Main function to launch the Gradio interface"""
|
311 |
logger.info("Starting Bambara TTS Gradio interface.")
|
312 |
|
313 |
+
# DO NOT preload - let it initialize on first request only
|
|
|
314 |
interface = build_interface()
|
315 |
interface.launch(
|
316 |
server_name="0.0.0.0",
|