sudoping01 commited on
Commit
ed72200
·
verified ·
1 Parent(s): 3447474

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -11,7 +11,7 @@ import numpy as np
11
  import spaces
12
  import logging
13
  from huggingface_hub import login
14
-
15
 
16
  torch._dynamo.config.disable = True
17
  torch._dynamo.config.suppress_errors = True
@@ -28,36 +28,39 @@ if hf_token:
28
  tts_model = None
29
  speakers_dict = None
30
  model_initialized = False
 
31
 
32
  @spaces.GPU()
33
  def initialize_model():
34
  """Initialize the TTS model and speakers - called once with GPU context"""
35
- global tts_model, speakers_dict, model_initialized
36
 
37
- if not model_initialized:
38
- logger.info("Initializing Bambara TTS model...")
39
-
40
- try:
41
- from maliba_ai.tts.inference import BambaraTTSInference
42
- from maliba_ai.config.speakers import Adame, Moussa, Bourama, Modibo, Seydou
43
-
44
-
45
- tts_model = BambaraTTSInference()
46
-
47
- speakers_dict = {
48
- "Adame": Adame,
49
- "Moussa": Moussa,
50
- "Bourama": Bourama,
51
- "Modibo": Modibo,
52
- "Seydou": Seydou
53
- }
54
 
55
- model_initialized = True
56
- logger.info("Model initialized successfully!")
57
-
58
- except Exception as e:
59
- logger.error(f"Failed to initialize model: {e}")
60
- raise e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  return tts_model, speakers_dict
63
 
 
11
  import spaces
12
  import logging
13
  from huggingface_hub import login
14
+ import threading
15
 
16
  torch._dynamo.config.disable = True
17
  torch._dynamo.config.suppress_errors = True
 
28
  tts_model = None
29
  speakers_dict = None
30
  model_initialized = False
31
+ model_initialized_lock = threading.Lock()
32
 
33
  @spaces.GPU()
34
  def initialize_model():
35
  """Initialize the TTS model and speakers - called once with GPU context"""
36
+ global tts_model, speakers_dict, model_initialized, model_initialized_lock
37
 
38
+ # Always acquire lock first for async safety
39
+ with model_initialized_lock:
40
+ if not model_initialized:
41
+ logger.info("Initializing Bambara TTS model...")
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ try:
44
+ from maliba_ai.tts.inference import BambaraTTSInference
45
+ from maliba_ai.config.speakers import Adame, Moussa, Bourama, Modibo, Seydou
46
+
47
+ # All initialization inside the lock
48
+ tts_model = BambaraTTSInference()
49
+ speakers_dict = {
50
+ "Adame": Adame,
51
+ "Moussa": Moussa,
52
+ "Bourama": Bourama,
53
+ "Modibo": Modibo,
54
+ "Seydou": Seydou
55
+ }
56
+
57
+ # Set flag last, after everything is ready
58
+ model_initialized = True
59
+ logger.info("Model initialized successfully!")
60
+
61
+ except Exception as e:
62
+ logger.error(f"Failed to initialize model: {e}")
63
+ raise e
64
 
65
  return tts_model, speakers_dict
66