rahul7star commited on
Commit
914eb9e
·
verified ·
1 Parent(s): 8a3669f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -5,11 +5,7 @@ from src.chatterbox.mtl_tts import ChatterboxMultilingualTTS, SUPPORTED_LANGUAGE
5
  import gradio as gr
6
 
7
 
8
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
9
- print(f"🚀 Running on device: {DEVICE}")
10
 
11
- # --- Global Model Initialization ---
12
- MODEL = None
13
 
14
  LANGUAGE_CONFIG = {
15
  "ar": {
@@ -134,27 +130,50 @@ def get_supported_languages_display() -> str:
134
  """
135
 
136
 
 
 
 
 
 
137
  def get_or_load_model():
138
- """Loads the ChatterboxMultilingualTTS model if it hasn't been loaded already,
139
- and ensures it's on the correct device."""
140
  global MODEL
 
141
  if MODEL is None:
142
- print("Model not loaded, initializing...")
 
143
  try:
144
- MODEL = ChatterboxMultilingualTTS.from_pretrained(DEVICE)
145
- if hasattr(MODEL, 'to') and str(MODEL.device) != DEVICE:
146
- MODEL.to("cpu")
147
- print(f"Model loaded successfully. Internal device: {getattr(MODEL, 'device', 'N/A')}")
 
 
 
 
 
 
 
 
 
 
 
 
148
  except Exception as e:
149
- print(f"Error loading model: {e}")
150
  raise
 
151
  return MODEL
152
 
153
- # Attempt to load the model at startup.
 
154
  try:
155
  get_or_load_model()
156
  except Exception as e:
157
- print(f"CRITICAL: Failed to load model on startup. Application may not function. Error: {e}")
 
 
 
158
 
159
  def set_seed(seed: int):
160
  """Sets the random seed for reproducibility across torch, numpy, and random."""
 
5
  import gradio as gr
6
 
7
 
 
 
8
 
 
 
9
 
10
  LANGUAGE_CONFIG = {
11
  "ar": {
 
130
  """
131
 
132
 
133
+ os.environ["CUDA_VISIBLE_DEVICES"] = ""
134
+
135
+ DEVICE = "cpu"
136
+ MODEL = None
137
+
138
  def get_or_load_model():
139
+ """Loads ChatterboxMultilingualTTS strictly on CPU."""
 
140
  global MODEL
141
+
142
  if MODEL is None:
143
+ print("Model not loaded, initializing on CPU only...")
144
+
145
  try:
146
+ MODEL = ChatterboxMultilingualTTS.from_pretrained(
147
+ DEVICE,
148
+ map_location="cpu"
149
+ )
150
+
151
+ # Extra safety: force CPU
152
+ if hasattr(MODEL, "to"):
153
+ MODEL = MODEL.to("cpu")
154
+
155
+ # Disable gradients (CPU optimization)
156
+ MODEL.eval()
157
+ for p in MODEL.parameters():
158
+ p.requires_grad = False
159
+
160
+ print("✅ Model loaded successfully on CPU")
161
+
162
  except Exception as e:
163
+ print(f"Error loading model on CPU: {e}")
164
  raise
165
+
166
  return MODEL
167
 
168
+
169
+ # Load at startup
170
  try:
171
  get_or_load_model()
172
  except Exception as e:
173
+ print(
174
+ "CRITICAL: Failed to load model on startup. "
175
+ f"Application may not function. Error: {e}"
176
+ )
177
 
178
  def set_seed(seed: int):
179
  """Sets the random seed for reproducibility across torch, numpy, and random."""