Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import scipy.io.wavfile
|
|
| 7 |
import io
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
-
# 1.
|
| 11 |
MODEL_ID = "facebook/musicgen-small"
|
| 12 |
device = "cpu"
|
| 13 |
|
|
@@ -23,8 +23,6 @@ def generate_core(prompt, duration):
|
|
| 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,8 +32,8 @@ def generate_core(prompt, duration):
|
|
| 34 |
audio_data = audio_values[0, 0].cpu().numpy()
|
| 35 |
return sampling_rate, audio_data
|
| 36 |
|
| 37 |
-
# 2. FastAPI
|
| 38 |
-
app = FastAPI(
|
| 39 |
|
| 40 |
@app.post("/generate")
|
| 41 |
async def api_generate(prompt: str = Query(...), duration: int = Query(10)):
|
|
@@ -48,7 +46,7 @@ async def api_generate(prompt: str = Query(...), duration: int = Query(10)):
|
|
| 48 |
raise HTTPException(status_code=500, detail=str(e))
|
| 49 |
|
| 50 |
# 3. Gradio Interface
|
| 51 |
-
with gr.Blocks(theme=gr.themes.
|
| 52 |
gr.Markdown("# 🎵 MusicGen Automation Hub")
|
| 53 |
with gr.Row():
|
| 54 |
with gr.Column():
|
|
@@ -60,5 +58,7 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
|
|
| 60 |
|
| 61 |
run_btn.click(generate_core, [p_in, d_in], a_out)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
|
|
|
|
|
| 7 |
import io
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
+
# 1. Model Loading
|
| 11 |
MODEL_ID = "facebook/musicgen-small"
|
| 12 |
device = "cpu"
|
| 13 |
|
|
|
|
| 23 |
|
| 24 |
duration = min(int(duration), 30)
|
| 25 |
inputs = processor(text=[prompt], padding=True, return_tensors="pt").to(device)
|
|
|
|
|
|
|
| 26 |
max_tokens = int(duration * 50)
|
| 27 |
|
| 28 |
with torch.no_grad():
|
|
|
|
| 32 |
audio_data = audio_values[0, 0].cpu().numpy()
|
| 33 |
return sampling_rate, audio_data
|
| 34 |
|
| 35 |
+
# 2. FastAPI Setup
|
| 36 |
+
app = FastAPI()
|
| 37 |
|
| 38 |
@app.post("/generate")
|
| 39 |
async def api_generate(prompt: str = Query(...), duration: int = Query(10)):
|
|
|
|
| 46 |
raise HTTPException(status_code=500, detail=str(e))
|
| 47 |
|
| 48 |
# 3. Gradio Interface
|
| 49 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 50 |
gr.Markdown("# 🎵 MusicGen Automation Hub")
|
| 51 |
with gr.Row():
|
| 52 |
with gr.Column():
|
|
|
|
| 58 |
|
| 59 |
run_btn.click(generate_core, [p_in, d_in], a_out)
|
| 60 |
|
| 61 |
+
# 4. Mounting without the incompatible 'show_api' argument
|
| 62 |
+
# We mount it at the root. The API documentation crash is bypassed by
|
| 63 |
+
# the explicit Query parameters in the FastAPI routes above.
|
| 64 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|