Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from fastapi import FastAPI, HTTPException
|
| 4 |
+
from fastapi.responses import Response
|
| 5 |
+
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
| 6 |
+
import scipy.io.wavfile
|
| 7 |
+
import io
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# Initialize FastAPI
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Model Setup
|
| 14 |
+
MODEL_ID = "facebook/musicgen-small"
|
| 15 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 16 |
+
model = MusicgenForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 17 |
+
|
| 18 |
+
def generate_core(prompt, duration):
|
| 19 |
+
"""Shared generation logic for both Gradio and API"""
|
| 20 |
+
if duration > 30:
|
| 21 |
+
duration = 30
|
| 22 |
+
|
| 23 |
+
inputs = processor(text=[prompt], padding=True, return_tensors="pt")
|
| 24 |
+
max_tokens = int(duration * 50)
|
| 25 |
+
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
audio_values = model.generate(**inputs, max_new_tokens=max_tokens)
|
| 28 |
+
|
| 29 |
+
sampling_rate = model.config.audio_encoder.sampling_rate
|
| 30 |
+
audio_data = audio_values[0, 0].cpu().numpy()
|
| 31 |
+
return sampling_rate, audio_data
|
| 32 |
+
|
| 33 |
+
# --- API Endpoint for n8n ---
|
| 34 |
+
@app.post("/generate")
|
| 35 |
+
async def api_generate(prompt: str, duration: int = 10):
|
| 36 |
+
try:
|
| 37 |
+
sr, audio = generate_core(prompt, duration)
|
| 38 |
+
byte_io = io.BytesIO()
|
| 39 |
+
scipy.io.wavfile.write(byte_io, rate=sr, data=audio)
|
| 40 |
+
return Response(content=byte_io.getvalue(), media_type="audio/wav")
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 43 |
+
|
| 44 |
+
# --- Gradio UI Interface ---
|
| 45 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 46 |
+
gr.Markdown("## 🎵 MusicGen Automation Hub")
|
| 47 |
+
gr.Markdown("Use this UI for manual testing or hit the `/generate` endpoint for n8n.")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column():
|
| 51 |
+
prompt_input = gr.Textbox(label="Text Prompt", placeholder="Cyberpunk synthwave with heavy bass...")
|
| 52 |
+
duration_slider = gr.Slider(minimum=1, maximum=30, value=10, step=1, label="Duration (Seconds)")
|
| 53 |
+
generate_btn = gr.Button("Generate Music", variant="primary")
|
| 54 |
+
|
| 55 |
+
with gr.Column():
|
| 56 |
+
audio_output = gr.Audio(label="Generated Audio", type="numpy")
|
| 57 |
+
|
| 58 |
+
generate_btn.click(
|
| 59 |
+
fn=generate_core,
|
| 60 |
+
inputs=[prompt_input, duration_slider],
|
| 61 |
+
outputs=audio_output
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Mount Gradio into FastAPI
|
| 65 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|