basyx commited on
Commit
feb79b6
·
verified ·
1 Parent(s): 87807e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -1,26 +1,30 @@
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():
@@ -30,9 +34,11 @@ def generate_core(prompt, duration):
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()
@@ -41,25 +47,18 @@ async def api_generate(prompt: str, duration: int = 10):
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="/")
 
1
  import torch
2
  import gradio as gr
3
+ from fastapi import FastAPI, HTTPException, Query
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
+ # 1. Resource-Optimized Model Loading
 
 
 
11
  MODEL_ID = "facebook/musicgen-small"
12
+ device = "cpu"
13
+
14
+ print(f"Loading model {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("Model loaded successfully.")
19
 
20
  def generate_core(prompt, duration):
21
+ if not prompt:
22
+ return None
 
23
 
24
+ duration = min(int(duration), 30)
25
+ inputs = processor(text=[prompt], padding=True, return_tensors="pt").to(device)
26
+
27
+ # 50 tokens = 1 second
28
  max_tokens = int(duration * 50)
29
 
30
  with torch.no_grad():
 
34
  audio_data = audio_values[0, 0].cpu().numpy()
35
  return sampling_rate, audio_data
36
 
37
+ # 2. FastAPI Engine
38
+ app = FastAPI(title="MusicGen Automation API")
39
+
40
  @app.post("/generate")
41
+ async def api_generate(prompt: str = Query(...), duration: int = Query(10)):
42
  try:
43
  sr, audio = generate_core(prompt, duration)
44
  byte_io = io.BytesIO()
 
47
  except Exception as e:
48
  raise HTTPException(status_code=500, detail=str(e))
49
 
50
+ # 3. Gradio Interface
51
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
52
+ gr.Markdown("# 🎵 MusicGen Automation Hub")
 
 
53
  with gr.Row():
54
  with gr.Column():
55
+ p_in = gr.Textbox(label="Prompt", placeholder="Upbeat synthwave...")
56
+ d_in = gr.Slider(1, 30, value=10, label="Duration (sec)")
57
+ run_btn = gr.Button("Generate", variant="primary")
 
58
  with gr.Column():
59
+ a_out = gr.Audio(label="Output")
60
+
61
+ run_btn.click(generate_core, [p_in, d_in], a_out)
 
 
 
 
62
 
63
+ # Mount Gradio and KILL the API schema generator that causes the 500 error
64
+ app = gr.mount_gradio_app(app, demo, path="/", show_api=False)