Veena commited on
Commit
a596bee
·
1 Parent(s): 002a88c

Update Maya1 Gradio app with preset characters

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -45,34 +45,37 @@ model = None
45
  prompt_builder = None
46
  snac_decoder = None
47
  pipeline = None
 
48
 
49
- @spaces.GPU
50
- async def load_models():
51
  """Load Maya1 vLLM model and pipeline (runs once)."""
52
- global model, prompt_builder, snac_decoder, pipeline
53
 
54
- if model is None:
55
- print("Loading Maya1 model with vLLM...")
56
- model = Maya1Model(
57
- model_path="maya-research/maya1",
58
- dtype="bfloat16",
59
- max_model_len=8192,
60
- gpu_memory_utilization=0.85,
61
- )
62
-
63
- print("Initializing prompt builder...")
64
- prompt_builder = Maya1PromptBuilder(model.tokenizer, model)
65
-
66
- print("Loading SNAC decoder...")
67
- snac_decoder = SNACDecoder(
68
- device="cuda",
69
- enable_batching=False,
70
- )
71
-
72
- print("Initializing pipeline...")
73
- pipeline = Maya1Pipeline(model, prompt_builder, snac_decoder)
74
-
75
- print("Models loaded successfully!")
 
 
 
76
 
77
  def preset_selected(preset_name):
78
  """Update description and text when preset is selected."""
@@ -86,7 +89,7 @@ def generate_speech(preset_name, description, text, temperature, max_tokens):
86
  """Generate emotional speech from description and text using vLLM."""
87
  try:
88
  # Load models if not already loaded
89
- asyncio.run(load_models())
90
 
91
  # If using preset, override description
92
  if preset_name and preset_name in PRESET_CHARACTERS:
@@ -98,8 +101,10 @@ def generate_speech(preset_name, description, text, temperature, max_tokens):
98
 
99
  print(f"Generating with temperature={temperature}, max_tokens={max_tokens}...")
100
 
101
- # Generate audio using vLLM pipeline
102
- audio_bytes = asyncio.run(
 
 
103
  pipeline.generate_speech(
104
  description=description,
105
  text=text,
@@ -110,6 +115,7 @@ def generate_speech(preset_name, description, text, temperature, max_tokens):
110
  seed=None,
111
  )
112
  )
 
113
 
114
  if audio_bytes is None:
115
  return None, "Error: Audio generation failed. Try different text or increase max_tokens."
 
45
  prompt_builder = None
46
  snac_decoder = None
47
  pipeline = None
48
+ models_loaded = False
49
 
50
+ def load_models():
 
51
  """Load Maya1 vLLM model and pipeline (runs once)."""
52
+ global model, prompt_builder, snac_decoder, pipeline, models_loaded
53
 
54
+ if models_loaded:
55
+ return
56
+
57
+ print("Loading Maya1 model with vLLM...")
58
+ model = Maya1Model(
59
+ model_path="maya-research/maya1",
60
+ dtype="bfloat16",
61
+ max_model_len=8192,
62
+ gpu_memory_utilization=0.85,
63
+ )
64
+
65
+ print("Initializing prompt builder...")
66
+ prompt_builder = Maya1PromptBuilder(model.tokenizer, model)
67
+
68
+ print("Loading SNAC decoder...")
69
+ snac_decoder = SNACDecoder(
70
+ device="cuda",
71
+ enable_batching=False,
72
+ )
73
+
74
+ print("Initializing pipeline...")
75
+ pipeline = Maya1Pipeline(model, prompt_builder, snac_decoder)
76
+
77
+ models_loaded = True
78
+ print("Models loaded successfully!")
79
 
80
  def preset_selected(preset_name):
81
  """Update description and text when preset is selected."""
 
89
  """Generate emotional speech from description and text using vLLM."""
90
  try:
91
  # Load models if not already loaded
92
+ load_models()
93
 
94
  # If using preset, override description
95
  if preset_name and preset_name in PRESET_CHARACTERS:
 
101
 
102
  print(f"Generating with temperature={temperature}, max_tokens={max_tokens}...")
103
 
104
+ # Generate audio using vLLM pipeline (async wrapper)
105
+ loop = asyncio.new_event_loop()
106
+ asyncio.set_event_loop(loop)
107
+ audio_bytes = loop.run_until_complete(
108
  pipeline.generate_speech(
109
  description=description,
110
  text=text,
 
115
  seed=None,
116
  )
117
  )
118
+ loop.close()
119
 
120
  if audio_bytes is None:
121
  return None, "Error: Audio generation failed. Try different text or increase max_tokens."