Update app.py
Browse files
app.py
CHANGED
|
@@ -7,28 +7,26 @@ import scipy.io.wavfile
|
|
| 7 |
import io
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
-
#
|
| 11 |
MODEL_ID = "facebook/musicgen-small"
|
| 12 |
device = "cpu"
|
| 13 |
|
| 14 |
-
print(f"DEBUG:
|
| 15 |
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 16 |
model = MusicgenForConditionalGeneration.from_pretrained(MODEL_ID, torch_dtype=torch.float32)
|
| 17 |
model.to(device)
|
| 18 |
-
print("DEBUG: Model loaded
|
| 19 |
|
| 20 |
def generate_core(prompt, duration):
|
|
|
|
| 21 |
if not prompt:
|
| 22 |
-
print("DEBUG: Request received with no prompt.")
|
| 23 |
return None
|
| 24 |
|
| 25 |
-
print(f"DEBUG: Starting generation for: '{prompt}' ({duration}s)")
|
| 26 |
-
|
| 27 |
try:
|
| 28 |
duration = min(int(duration), 30)
|
| 29 |
inputs = processor(text=[prompt], padding=True, return_tensors="pt").to(device)
|
| 30 |
|
| 31 |
-
# 50 tokens
|
| 32 |
max_tokens = int(duration * 50)
|
| 33 |
|
| 34 |
with torch.no_grad():
|
|
@@ -41,46 +39,39 @@ def generate_core(prompt, duration):
|
|
| 41 |
|
| 42 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 43 |
audio_data = audio_values[0, 0].cpu().numpy()
|
| 44 |
-
|
| 45 |
print("DEBUG: Generation successful.")
|
| 46 |
return sampling_rate, audio_data
|
| 47 |
except Exception as e:
|
| 48 |
-
print(f"
|
| 49 |
return None
|
| 50 |
|
| 51 |
-
#
|
| 52 |
app = FastAPI()
|
| 53 |
|
| 54 |
@app.post("/generate")
|
| 55 |
async def api_generate(prompt: str = Query(...), duration: int = Query(10)):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return Response(content=byte_io.getvalue(), media_type="audio/wav")
|
| 65 |
-
except Exception as e:
|
| 66 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 67 |
|
| 68 |
-
#
|
| 69 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
gr.Markdown("# 🎵 MusicGen Automation Hub")
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column():
|
| 73 |
-
p_in = gr.Textbox(label="Prompt", placeholder="
|
| 74 |
d_in = gr.Slider(1, 30, value=10, label="Duration (sec)")
|
| 75 |
run_btn = gr.Button("Generate", variant="primary")
|
| 76 |
with gr.Column():
|
| 77 |
a_out = gr.Audio(label="Output")
|
| 78 |
|
| 79 |
-
run_btn.click(generate_core, [p_in, d_in], a_out)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
# This prevents the UI from "hanging" during the 60-120s CPU generation time.
|
| 83 |
demo.queue()
|
| 84 |
-
|
| 85 |
-
# Mount Gradio
|
| 86 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 7 |
import io
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
+
# --- INITIALIZATION ---
|
| 11 |
MODEL_ID = "facebook/musicgen-small"
|
| 12 |
device = "cpu"
|
| 13 |
|
| 14 |
+
print(f"DEBUG: System boot. Loading {MODEL_ID}...")
|
| 15 |
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 16 |
model = MusicgenForConditionalGeneration.from_pretrained(MODEL_ID, torch_dtype=torch.float32)
|
| 17 |
model.to(device)
|
| 18 |
+
print("DEBUG: Model loaded successfully.")
|
| 19 |
|
| 20 |
def generate_core(prompt, duration):
|
| 21 |
+
print(f"!!! TRIGGERED !!! Prompt: {prompt} | Duration: {duration}")
|
| 22 |
if not prompt:
|
|
|
|
| 23 |
return None
|
| 24 |
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
duration = min(int(duration), 30)
|
| 27 |
inputs = processor(text=[prompt], padding=True, return_tensors="pt").to(device)
|
| 28 |
|
| 29 |
+
# 50 tokens/sec. Reducing guidance for CPU speed.
|
| 30 |
max_tokens = int(duration * 50)
|
| 31 |
|
| 32 |
with torch.no_grad():
|
|
|
|
| 39 |
|
| 40 |
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 41 |
audio_data = audio_values[0, 0].cpu().numpy()
|
|
|
|
| 42 |
print("DEBUG: Generation successful.")
|
| 43 |
return sampling_rate, audio_data
|
| 44 |
except Exception as e:
|
| 45 |
+
print(f"ERROR: {str(e)}")
|
| 46 |
return None
|
| 47 |
|
| 48 |
+
# --- FASTAPI FOR N8N ---
|
| 49 |
app = FastAPI()
|
| 50 |
|
| 51 |
@app.post("/generate")
|
| 52 |
async def api_generate(prompt: str = Query(...), duration: int = Query(10)):
|
| 53 |
+
res = generate_core(prompt, duration)
|
| 54 |
+
if res is None:
|
| 55 |
+
raise HTTPException(status_code=500, detail="Generation failed")
|
| 56 |
+
|
| 57 |
+
sr, audio = res
|
| 58 |
+
byte_io = io.BytesIO()
|
| 59 |
+
scipy.io.wavfile.write(byte_io, rate=sr, data=audio)
|
| 60 |
+
return Response(content=byte_io.getvalue(), media_type="audio/wav")
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# --- GRADIO UI ---
|
| 63 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 64 |
gr.Markdown("# 🎵 MusicGen Automation Hub")
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column():
|
| 67 |
+
p_in = gr.Textbox(label="Prompt", placeholder="Lofi hip hop...")
|
| 68 |
d_in = gr.Slider(1, 30, value=10, label="Duration (sec)")
|
| 69 |
run_btn = gr.Button("Generate", variant="primary")
|
| 70 |
with gr.Column():
|
| 71 |
a_out = gr.Audio(label="Output")
|
| 72 |
|
| 73 |
+
run_btn.click(fn=generate_core, inputs=[p_in, d_in], outputs=a_out)
|
| 74 |
|
| 75 |
+
# Enable the queue and mount
|
|
|
|
| 76 |
demo.queue()
|
|
|
|
|
|
|
| 77 |
app = gr.mount_gradio_app(app, demo, path="/")
|