yasuogg commited on
Commit
7efa79e
·
verified ·
1 Parent(s): a6b8646

ZeroGPU: load model on CPU at startup, move to GPU inside @spaces.GPU

Browse files
Files changed (2) hide show
  1. __pycache__/app.cpython-312.pyc +0 -0
  2. app.py +33 -18
__pycache__/app.cpython-312.pyc ADDED
Binary file (9.66 kB). View file
 
app.py CHANGED
@@ -46,13 +46,16 @@ MODEL_REPO = "MenaVoice/KasbahTTS-V0"
46
  CKPT_FILE = "ALGERIA.safetensors"
47
  V1_CFG = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4)
48
 
49
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
50
 
51
  ckpt_path = hf_hub_download(MODEL_REPO, CKPT_FILE)
52
  vocab_path = hf_hub_download(MODEL_REPO, "vocab.txt")
53
 
54
- vocoder = load_vocoder(vocoder_name="vocos", is_local=False, device=DEVICE)
55
- model = load_model(DiT, V1_CFG, ckpt_path, vocab_file=vocab_path, device=DEVICE)
56
 
57
  EXAMPLE_REF = os.path.join(os.path.dirname(__file__), "examples", "ALG.wav")
58
  EXAMPLE_REF_TEXT = "أنيا هكا باغية ناكل هكا أني ن نشوف فيها الحاجة هذيكا."
@@ -94,21 +97,33 @@ def generate(
94
  # Leaving ref_text blank triggers Whisper auto-transcription inside f5-tts.
95
  ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, ref_text or "", show_info=gr.Info)
96
 
97
- # KasbahTTS is a *specialized* single-dialect checkpoint, so no dialect token
98
- # is prepended — infer_process defaults dialect_id to None.
99
- wave, sample_rate, _ = infer_process(
100
- ref_audio,
101
- ref_text,
102
- gen_text,
103
- model,
104
- vocoder,
105
- cross_fade_duration=cross_fade,
106
- nfe_step=int(nfe_step),
107
- cfg_strength=cfg_strength,
108
- speed=speed,
109
- show_info=gr.Info,
110
- progress=progress,
111
- )
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  if remove_sil:
114
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
 
46
  CKPT_FILE = "ALGERIA.safetensors"
47
  V1_CFG = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4)
48
 
49
+ # On ZeroGPU, CUDA exists only *inside* a @spaces.GPU call — never at import time.
50
+ # So load everything on CPU here and move to GPU inside generate().
51
+ ON_ZEROGPU = USING_ZEROGPU and bool(os.environ.get("SPACES_ZERO_GPU"))
52
+ GPU_DEVICE = "cuda" if (ON_ZEROGPU or torch.cuda.is_available()) else "cpu"
53
 
54
  ckpt_path = hf_hub_download(MODEL_REPO, CKPT_FILE)
55
  vocab_path = hf_hub_download(MODEL_REPO, "vocab.txt")
56
 
57
+ vocoder = load_vocoder(vocoder_name="vocos", is_local=False, device="cpu")
58
+ model = load_model(DiT, V1_CFG, ckpt_path, vocab_file=vocab_path, device="cpu")
59
 
60
  EXAMPLE_REF = os.path.join(os.path.dirname(__file__), "examples", "ALG.wav")
61
  EXAMPLE_REF_TEXT = "أنيا هكا باغية ناكل هكا أني ن نشوف فيها الحاجة هذيكا."
 
97
  # Leaving ref_text blank triggers Whisper auto-transcription inside f5-tts.
98
  ref_audio, ref_text = preprocess_ref_audio_text(ref_audio_orig, ref_text or "", show_info=gr.Info)
99
 
100
+ # Move to GPU now that we're inside the @spaces.GPU context (loaded on CPU at startup).
101
+ model.to(GPU_DEVICE)
102
+ vocoder.to(GPU_DEVICE)
103
+ try:
104
+ # KasbahTTS is a *specialized* single-dialect checkpoint, so no dialect
105
+ # token is prepended — infer_process defaults dialect_id to None.
106
+ wave, sample_rate, _ = infer_process(
107
+ ref_audio,
108
+ ref_text,
109
+ gen_text,
110
+ model,
111
+ vocoder,
112
+ cross_fade_duration=cross_fade,
113
+ nfe_step=int(nfe_step),
114
+ cfg_strength=cfg_strength,
115
+ speed=speed,
116
+ show_info=gr.Info,
117
+ progress=progress,
118
+ device=GPU_DEVICE,
119
+ )
120
+ finally:
121
+ # ZeroGPU tears down CUDA after the call — return the model to CPU so the
122
+ # next call starts from a valid copy.
123
+ if ON_ZEROGPU:
124
+ model.to("cpu")
125
+ vocoder.to("cpu")
126
+ torch.cuda.empty_cache()
127
 
128
  if remove_sil:
129
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f: