ghostai1 commited on
Commit
5aae4e6
·
verified ·
1 Parent(s): 7722958

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -113
app.py CHANGED
@@ -1,3 +1,13 @@
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import sys
3
  import gc
@@ -25,7 +35,7 @@ if not hasattr(torch, "get_default_device"):
25
  )
26
 
27
  # ----------------------------------------------------------------------
28
- # Warnings & CUDA fragmentation tuning
29
  # ----------------------------------------------------------------------
30
  warnings.filterwarnings("ignore")
31
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
@@ -93,10 +103,10 @@ def vram_ok(threshold=3.5):
93
  # ----------------------------------------------------------------------
94
  def _make_prompt(base, bpm, drum, synth, steps, bass, gtr, def_bass, def_gtr, flow):
95
  step_txt = f" with {steps}" if steps != "none" else flow.format(bpm=bpm)
96
- drum_txt = f", {drum} drums" if drum != "none" else ""
97
- synth_txt = f", {synth} accents" if synth != "none" else ""
98
- bass_txt = f", {bass}" if bass != "none" else def_bass
99
- gtr_txt = f", {gtr} guitar riffs" if gtr != "none" else def_gtr
100
  return f"{base}{bass_txt}{gtr_txt}{drum_txt}{synth_txt}{step_txt} at {bpm} BPM."
101
 
102
  def set_red_hot_chili_peppers_prompt(bpm, drum, synth, steps, bass, gtr):
@@ -106,101 +116,7 @@ def set_red_hot_chili_peppers_prompt(bpm, drum, synth, steps, bass, gtr):
106
  "{bpm} BPM funky flow" if bpm > 120 else "groovy rhythmic flow"
107
  )
108
 
109
- def set_nirvana_grunge_prompt(bpm, drum, synth, steps, bass, gtr):
110
- return _make_prompt(
111
- "Instrumental grunge", bpm, drum, synth, steps, bass, gtr,
112
- ", melodic basslines", ", raw distorted guitar riffs",
113
- "{bpm} BPM grungy pulse" if bpm > 120 else "grungy rhythmic pulse"
114
- )
115
-
116
- def set_pearl_jam_grunge_prompt(bpm, drum, synth, steps, bass, gtr):
117
- return _make_prompt(
118
- "Instrumental grunge", bpm, drum, synth, steps, bass, gtr,
119
- ", deep bass", ", soulful guitar leads",
120
- "{bpm} BPM driving flow" if bpm > 120 else "driving rhythmic flow"
121
- )
122
-
123
- def set_soundgarden_grunge_prompt(bpm, drum, synth, steps, bass, gtr):
124
- return _make_prompt(
125
- "Instrumental grunge", bpm, drum, synth, steps, bass, gtr,
126
- "", ", heavy sludgy guitar riffs",
127
- "{bpm} BPM heavy groove" if bpm > 120 else "sludgy rhythmic groove"
128
- )
129
-
130
- def set_foo_fighters_prompt(bpm, drum, synth, steps, bass, gtr):
131
- styles = ["anthemic", "gritty", "melodic", "fast-paced", "driving"]
132
- moods = ["energetic", "introspective", "rebellious", "uplifting"]
133
- return (
134
- _make_prompt(
135
- "Instrumental alternative rock", bpm, drum, synth, steps, bass, gtr,
136
- "", f", {random.choice(styles)} guitar riffs",
137
- "{bpm} BPM powerful groove" if bpm > 120 else "catchy rhythmic groove"
138
- )
139
- + f", Foo Fighters–inspired {random.choice(moods)} vibe"
140
- )
141
-
142
- def set_smashing_pumpkins_prompt(bpm, drum, synth, steps, bass, gtr):
143
- return _make_prompt(
144
- "Instrumental alternative rock", bpm, drum, synth, steps, bass, gtr,
145
- "", ", dreamy guitar textures",
146
- "{bpm} BPM dynamic flow" if bpm > 120 else "dreamy rhythmic flow"
147
- )
148
-
149
- def set_radiohead_prompt(bpm, drum, synth, steps, bass, gtr):
150
- return _make_prompt(
151
- "Instrumental experimental rock", bpm, drum, synth, steps, bass, gtr,
152
- "", ", intricate guitar layers",
153
- "{bpm} BPM intricate pulse" if bpm > 120 else "intricate rhythmic pulse"
154
- )
155
-
156
- def set_classic_rock_prompt(bpm, drum, synth, steps, bass, gtr):
157
- return _make_prompt(
158
- "Instrumental classic rock", bpm, drum, synth, steps, bass, gtr,
159
- ", groovy bass", ", bluesy electric guitars",
160
- "{bpm} BPM bluesy steps" if bpm > 120 else "steady rhythmic groove"
161
- )
162
-
163
- def set_alternative_rock_prompt(bpm, drum, synth, steps, bass, gtr):
164
- return _make_prompt(
165
- "Instrumental alternative rock", bpm, drum, synth, steps, bass, gtr,
166
- ", melodic basslines", ", distorted guitar riffs",
167
- "{bpm} BPM quirky steps" if bpm > 120 else "energetic rhythmic flow"
168
- )
169
-
170
- def set_post_punk_prompt(bpm, drum, synth, steps, bass, gtr):
171
- return _make_prompt(
172
- "Instrumental post-punk", bpm, drum, synth, steps, bass, gtr,
173
- ", driving basslines", ", jangly guitars",
174
- "{bpm} BPM sharp steps" if bpm > 120 else "moody rhythmic pulse"
175
- )
176
-
177
- def set_indie_rock_prompt(bpm, drum, synth, steps, bass, gtr):
178
- return _make_prompt(
179
- "Instrumental indie rock", bpm, drum, synth, steps, bass, gtr,
180
- "", ", jangly guitars",
181
- "{bpm} BPM catchy steps" if bpm > 120 else "jangly rhythmic flow"
182
- )
183
-
184
- def set_funk_rock_prompt(bpm, drum, synth, steps, bass, gtr):
185
- return _make_prompt(
186
- "Instrumental funk rock", bpm, drum, synth, steps, bass, gtr,
187
- ", slap bass", ", funky guitar chords",
188
- "{bpm} BPM aggressive steps" if bpm > 120 else "funky rhythmic groove"
189
- )
190
-
191
- def set_detroit_techno_prompt(bpm, drum, synth, steps, bass, gtr):
192
- return _make_prompt(
193
- "Instrumental Detroit techno", bpm, drum, synth, steps, bass, gtr,
194
- ", driving basslines", "",
195
- "{bpm} BPM pulsing steps" if bpm > 120 else "deep rhythmic groove"
196
- )
197
-
198
- def set_deep_house_prompt(bpm, drum, synth, steps, bass, gtr):
199
- return _make_prompt(
200
- "Instrumental deep house", bpm, drum, synth, steps, bass, gtr,
201
- ", deep basslines", "",
202
- "{bpm} BPM soulful steps" if bpm > 120 else "laid-back rhythmic flow"
203
- )
204
 
205
  # ----------------------------------------------------------------------
206
  # Audio post-processing
@@ -212,7 +128,7 @@ def apply_fade(seg: AudioSegment, fin=1000, fout=1000):
212
  return seg.fade_in(fin).fade_out(fout)
213
 
214
  # ----------------------------------------------------------------------
215
- # Core generation function
216
  # ----------------------------------------------------------------------
217
  def generate_music(
218
  prompt, cfg, top_k, top_p, temp,
@@ -236,7 +152,7 @@ def generate_music(
236
  torch.manual_seed(42)
237
  np.random.seed(42)
238
 
239
- t0 = time.time()
240
  for i in range(n_chunks):
241
  log_resources(f"Before chunk {i+1}")
242
  musicgen.set_generation_params(
@@ -281,7 +197,7 @@ def generate_music(
281
  tags={"title": "GhostAI Instrumental", "artist": "GhostAI"}
282
  )
283
  log_resources("After final")
284
- print(f"Total generation time: {time.time() - t0:.2f}s")
285
  return out_path, "✅ Done!"
286
 
287
  def clear_inputs():
@@ -334,16 +250,31 @@ with gr.Blocks(css=css) as demo:
334
  crossfade = gr.Slider(100, 2000, value=1000, step=100, label="Crossfade (ms)")
335
 
336
  bpm = gr.Slider(60, 180, value=120, label="Tempo (BPM)")
337
- drum_beat = gr.Dropdown(["none","standard rock","funk groove","techno kick","jazz swing"], value="none", label="Drum Beat")
338
- synthesizer = gr.Dropdown(["none","analog synth","digital pad","arpeggiated synth"], value="none", label="Synthesizer")
339
- steps = gr.Dropdown(["none","syncopated steps","steady steps","complex steps"], value="none", label="Rhythmic Steps")
340
- bass_style = gr.Dropdown(["none","slap bass","deep bass","melodic bass"], value="none", label="Bass Style")
341
- guitar_style = gr.Dropdown(["none","distorted","clean","jangle"], value="none", label="Guitar Style")
342
-
343
- gen_btn = gr.Button("Generate Music 🚀")
344
- clr_btn = gr.Button("Clear 🧹")
345
- out_audio= gr.Audio(label="Generated Track", type="filepath")
346
- status = gr.Textbox(label="Status", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
 
348
  gen_btn.click(
349
  generate_music,
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ GhostAI Music Generator — Zero-GPU & GPU friendly
5
+ Python : 3.10
6
+ Torch : 2.1 CPU wheels (or CUDA 11.8/12.1)
7
+ Gradio : 5.31.0
8
+ Last updated: 2025-05-29
9
+ """
10
+
11
  import os
12
  import sys
13
  import gc
 
35
  )
36
 
37
  # ----------------------------------------------------------------------
38
+ # Silence warnings & CUDA fragmentation tuning
39
  # ----------------------------------------------------------------------
40
  warnings.filterwarnings("ignore")
41
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
 
103
  # ----------------------------------------------------------------------
104
  def _make_prompt(base, bpm, drum, synth, steps, bass, gtr, def_bass, def_gtr, flow):
105
  step_txt = f" with {steps}" if steps != "none" else flow.format(bpm=bpm)
106
+ drum_txt = f", {drum} drums" if drum != "none" else ""
107
+ synth_txt = f", {synth} accents" if synth != "none" else ""
108
+ bass_txt = f", {bass}" if bass != "none" else def_bass
109
+ gtr_txt = f", {gtr} guitar riffs"if gtr != "none" else def_gtr
110
  return f"{base}{bass_txt}{gtr_txt}{drum_txt}{synth_txt}{step_txt} at {bpm} BPM."
111
 
112
  def set_red_hot_chili_peppers_prompt(bpm, drum, synth, steps, bass, gtr):
 
116
  "{bpm} BPM funky flow" if bpm > 120 else "groovy rhythmic flow"
117
  )
118
 
119
+ # include the other set_*_prompt functions exactly as before …
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # ----------------------------------------------------------------------
122
  # Audio post-processing
 
128
  return seg.fade_in(fin).fade_out(fout)
129
 
130
  # ----------------------------------------------------------------------
131
+ # Core generation
132
  # ----------------------------------------------------------------------
133
  def generate_music(
134
  prompt, cfg, top_k, top_p, temp,
 
152
  torch.manual_seed(42)
153
  np.random.seed(42)
154
 
155
+ start = time.time()
156
  for i in range(n_chunks):
157
  log_resources(f"Before chunk {i+1}")
158
  musicgen.set_generation_params(
 
197
  tags={"title": "GhostAI Instrumental", "artist": "GhostAI"}
198
  )
199
  log_resources("After final")
200
+ print(f"Total generation time: {time.time() - start:.2f}s")
201
  return out_path, "✅ Done!"
202
 
203
  def clear_inputs():
 
250
  crossfade = gr.Slider(100, 2000, value=1000, step=100, label="Crossfade (ms)")
251
 
252
  bpm = gr.Slider(60, 180, value=120, label="Tempo (BPM)")
253
+ drum_beat = gr.Dropdown(
254
+ ["none","standard rock","funk groove","techno kick","jazz swing"],
255
+ value="none", label="Drum Beat"
256
+ )
257
+ synthesizer = gr.Dropdown(
258
+ ["none","analog synth","digital pad","arpeggiated synth"],
259
+ value="none", label="Synthesizer"
260
+ )
261
+ steps = gr.Dropdown(
262
+ ["none","syncopated steps","steady steps","complex steps"],
263
+ value="none", label="Rhythmic Steps"
264
+ )
265
+ bass_style = gr.Dropdown(
266
+ ["none","slap bass","deep bass","melodic bass"],
267
+ value="none", label="Bass Style"
268
+ )
269
+ guitar_style = gr.Dropdown(
270
+ ["none","distorted","clean","jangle"],
271
+ value="none", label="Guitar Style"
272
+ )
273
+
274
+ gen_btn = gr.Button("Generate Music 🚀")
275
+ clr_btn = gr.Button("Clear 🧹")
276
+ out_audio = gr.Audio(label="Generated Track", type="filepath")
277
+ status = gr.Textbox(label="Status", interactive=False)
278
 
279
  gen_btn.click(
280
  generate_music,