multimodalart HF Staff commited on
Commit
9bb5cb0
Β·
1 Parent(s): 00270ff

[Admin maintenance] Support new ZeroGPU hardware (#2)

Browse files

- [Admin maintenance] Support new ZeroGPU hardware (aeda8bb88a02244d07835bfe924c0fb9cd945e48)

Files changed (2) hide show
  1. app.py +23 -10
  2. requirements.txt +3 -3
app.py CHANGED
@@ -1,6 +1,15 @@
1
  # ruff: noqa: E402
2
  # Above allows ruff to ignore E402: module level import not at top of file
3
 
 
 
 
 
 
 
 
 
 
4
  import gc
5
  import json
6
  import re
@@ -17,13 +26,6 @@ import torchaudio
17
  from cached_path import cached_path
18
  from transformers import AutoModelForCausalLM, AutoTokenizer
19
 
20
- try:
21
- import spaces
22
-
23
- USING_SPACES = True
24
- except ImportError:
25
- USING_SPACES = False
26
-
27
 
28
  def gpu_decorator(func):
29
  if USING_SPACES:
@@ -58,16 +60,21 @@ DEFAULT_TTS_MODEL_CFG = [
58
  vocoder = load_vocoder()
59
 
60
 
 
 
 
 
 
61
  def load_f5tts():
62
  ckpt_path = str(cached_path(DEFAULT_TTS_MODEL_CFG[0]))
63
  F5TTS_model_cfg = json.loads(DEFAULT_TTS_MODEL_CFG[2])
64
- return load_model(DiT, F5TTS_model_cfg, ckpt_path)
65
 
66
 
67
  def load_e2tts():
68
  ckpt_path = str(cached_path("hf://SWivid/E2-TTS/E2TTS_Base/model_1200000.safetensors"))
69
  E2TTS_model_cfg = dict(dim=1024, depth=24, heads=16, ff_mult=4, text_mask_padding=False, pe_attn_head=1)
70
- return load_model(UNetT, E2TTS_model_cfg, ckpt_path)
71
 
72
 
73
  def load_custom(ckpt_path: str, vocab_path="", model_cfg=None):
@@ -78,7 +85,7 @@ def load_custom(ckpt_path: str, vocab_path="", model_cfg=None):
78
  vocab_path = str(cached_path(vocab_path))
79
  if model_cfg is None:
80
  model_cfg = json.loads(DEFAULT_TTS_MODEL_CFG[2])
81
- return load_model(DiT, model_cfg, ckpt_path, vocab_file=vocab_path)
82
 
83
 
84
  F5TTS_ema_model = load_f5tts()
@@ -151,6 +158,12 @@ def infer(
151
  pre_custom_path = model[1]
152
  ema_model = custom_ema_model
153
 
 
 
 
 
 
 
154
  final_wave, final_sample_rate, combined_spectrogram = infer_process(
155
  ref_audio,
156
  ref_text,
 
1
  # ruff: noqa: E402
2
  # Above allows ruff to ignore E402: module level import not at top of file
3
 
4
+ # `spaces` must be imported before torch (or anything that touches CUDA) so
5
+ # it can patch torch.cuda.* for the ZeroGPU worker hijack.
6
+ try:
7
+ import spaces
8
+
9
+ USING_SPACES = True
10
+ except ImportError:
11
+ USING_SPACES = False
12
+
13
  import gc
14
  import json
15
  import re
 
26
  from cached_path import cached_path
27
  from transformers import AutoModelForCausalLM, AutoTokenizer
28
 
 
 
 
 
 
 
 
29
 
30
  def gpu_decorator(func):
31
  if USING_SPACES:
 
60
  vocoder = load_vocoder()
61
 
62
 
63
+ # On ZeroGPU, CUDA is not available in the main process (the GPU is only
64
+ # attached inside @spaces.GPU workers). f5_tts.load_model calls
65
+ # safetensors.load_file(..., device="cuda") directly, which cannot be deferred
66
+ # by the `spaces` patcher and raises "No CUDA GPUs are available" at import.
67
+ # Load every model on CPU here, then move it onto the GPU inside the worker.
68
  def load_f5tts():
69
  ckpt_path = str(cached_path(DEFAULT_TTS_MODEL_CFG[0]))
70
  F5TTS_model_cfg = json.loads(DEFAULT_TTS_MODEL_CFG[2])
71
+ return load_model(DiT, F5TTS_model_cfg, ckpt_path, device="cpu")
72
 
73
 
74
  def load_e2tts():
75
  ckpt_path = str(cached_path("hf://SWivid/E2-TTS/E2TTS_Base/model_1200000.safetensors"))
76
  E2TTS_model_cfg = dict(dim=1024, depth=24, heads=16, ff_mult=4, text_mask_padding=False, pe_attn_head=1)
77
+ return load_model(UNetT, E2TTS_model_cfg, ckpt_path, device="cpu")
78
 
79
 
80
  def load_custom(ckpt_path: str, vocab_path="", model_cfg=None):
 
85
  vocab_path = str(cached_path(vocab_path))
86
  if model_cfg is None:
87
  model_cfg = json.loads(DEFAULT_TTS_MODEL_CFG[2])
88
+ return load_model(DiT, model_cfg, ckpt_path, vocab_file=vocab_path, device="cpu")
89
 
90
 
91
  F5TTS_ema_model = load_f5tts()
 
158
  pre_custom_path = model[1]
159
  ema_model = custom_ema_model
160
 
161
+ # Models were loaded on CPU at import (ZeroGPU has no GPU in the main
162
+ # process); move the selected model onto the GPU now that we're inside the
163
+ # @spaces.GPU worker. The vocoder was loaded with a deferred .to("cuda") so
164
+ # `spaces` already materialized it on the GPU.
165
+ ema_model = ema_model.to("cuda")
166
+
167
  final_wave, final_sample_rate, combined_spectrogram = infer_process(
168
  ref_audio,
169
  ref_text,
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
- torch==2.4.0
2
- torchaudio
3
  accelerate>=0.33.0
4
  bitsandbytes>0.37.0
5
  cached_path
@@ -23,4 +23,4 @@ vocos
23
  wandb
24
  x_transformers>=1.31.14
25
  f5_tts
26
- detoxify @ git+https://github.com/unitaryai/detoxify
 
1
+ torch==2.8.0
2
+ torchaudio==2.8.0
3
  accelerate>=0.33.0
4
  bitsandbytes>0.37.0
5
  cached_path
 
23
  wandb
24
  x_transformers>=1.31.14
25
  f5_tts
26
+ detoxify @ git+https://github.com/unitaryai/detoxify