modified: app.py
Browse files
app.py
CHANGED
@@ -76,36 +76,33 @@ MAX_SEQ_LEN = 16384
|
|
76 |
# --------------------------
|
77 |
# Preload Models with KV Cache Initialization
|
78 |
# --------------------------
|
79 |
-
@spaces.GPU
|
80 |
-
def preload_models():
|
81 |
-
global model, mmtokenizer, codec_model, codectool, vocal_decoder, inst_decoder
|
82 |
-
|
83 |
-
# Text generation model with KV cache support
|
84 |
-
model = AutoModelForCausalLM.from_pretrained(
|
85 |
-
"m-a-p/YuE-s1-7B-anneal-en-cot",
|
86 |
-
torch_dtype=TORCH_DTYPE,
|
87 |
-
attn_implementation="flash_attention_2",
|
88 |
-
use_cache=True # Enable KV caching
|
89 |
-
).to(DEVICE).eval()
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
codec_model.load_state_dict(
|
99 |
-
torch.load(MODEL_DIR/"final_ckpt/ckpt_00360000.pth", map_location='cpu')['codec_model']
|
100 |
-
)
|
101 |
-
codec_model.eval()
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# --------------------------
|
111 |
# Optimized Generation with KV Cache Management
|
@@ -237,9 +234,6 @@ def infer(genre, lyrics, num_segments=2, max_tokens=2000):
|
|
237 |
with tempfile.TemporaryDirectory() as tmpdir:
|
238 |
return generate_music(genre, lyrics, num_segments, max_tokens)
|
239 |
|
240 |
-
# Initialize models at startup
|
241 |
-
preload_models()
|
242 |
-
|
243 |
# Gradio UI
|
244 |
with gr.Blocks() as demo:
|
245 |
gr.Markdown("# YuE Music Generator with KV Cache Optimization")
|
|
|
76 |
# --------------------------
|
77 |
# Preload Models with KV Cache Initialization
|
78 |
# --------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# Text generation model with KV cache support
|
81 |
+
model = AutoModelForCausalLM.from_pretrained(
|
82 |
+
"m-a-p/YuE-s1-7B-anneal-en-cot",
|
83 |
+
torch_dtype=TORCH_DTYPE,
|
84 |
+
attn_implementation="flash_attention_2",
|
85 |
+
use_cache=True # Enable KV caching
|
86 |
+
).to(DEVICE).eval()
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
# Tokenizer and codec tools
|
89 |
+
mmtokenizer = _MMSentencePieceTokenizer("./mm_tokenizer_v0.2_hf/tokenizer.model")
|
90 |
+
codectool = CodecManipulator("xcodec", 0, 1)
|
91 |
+
|
92 |
+
# Audio codec model
|
93 |
+
model_config = OmegaConf.load(MODEL_DIR/"final_ckpt/config.yaml")
|
94 |
+
codec_model = SoundStream(**model_config.generator.config).to(DEVICE)
|
95 |
+
codec_model.load_state_dict(
|
96 |
+
torch.load(MODEL_DIR/"final_ckpt/ckpt_00360000.pth", map_location='cpu')['codec_model']
|
97 |
+
)
|
98 |
+
codec_model.eval()
|
99 |
+
|
100 |
+
# Vocoders
|
101 |
+
vocal_decoder, inst_decoder = build_codec_model(
|
102 |
+
MODEL_DIR/"decoders/config.yaml",
|
103 |
+
MODEL_DIR/"decoders/decoder_131000.pth",
|
104 |
+
MODEL_DIR/"decoders/decoder_151000.pth"
|
105 |
+
)
|
106 |
|
107 |
# --------------------------
|
108 |
# Optimized Generation with KV Cache Management
|
|
|
234 |
with tempfile.TemporaryDirectory() as tmpdir:
|
235 |
return generate_music(genre, lyrics, num_segments, max_tokens)
|
236 |
|
|
|
|
|
|
|
237 |
# Gradio UI
|
238 |
with gr.Blocks() as demo:
|
239 |
gr.Markdown("# YuE Music Generator with KV Cache Optimization")
|