Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,100 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# ======================
|
| 6 |
-
#
|
| 7 |
# ======================
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@api.get("/health")
|
| 12 |
def health():
|
| 13 |
return {"status": "running"}
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@api.post("/generate")
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
# ======================
|
| 22 |
# GRADIO UI
|
| 23 |
# ======================
|
| 24 |
|
| 25 |
-
def ui_generate(prompt, duration):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
with gr.Blocks(
|
| 31 |
-
title="AI Background Music Generator",
|
| 32 |
-
theme=gr.themes.Soft()
|
| 33 |
-
) as demo:
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
""")
|
| 39 |
|
| 40 |
prompt = gr.Textbox(
|
| 41 |
-
label="
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
duration = gr.Slider(
|
|
@@ -47,7 +102,7 @@ Generate royalty-free background music instantly.
|
|
| 47 |
maximum=30,
|
| 48 |
value=10,
|
| 49 |
step=1,
|
| 50 |
-
label="Duration
|
| 51 |
)
|
| 52 |
|
| 53 |
btn = gr.Button("Generate")
|
|
@@ -56,14 +111,10 @@ Generate royalty-free background music instantly.
|
|
| 56 |
|
| 57 |
btn.click(
|
| 58 |
fn=ui_generate,
|
| 59 |
-
inputs=[prompt, duration],
|
| 60 |
outputs=output
|
| 61 |
)
|
| 62 |
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
# IMPORTANT FIX
|
| 66 |
-
# ======================
|
| 67 |
-
# Mount GRADIO as ROOT APP
|
| 68 |
-
|
| 69 |
app = gr.mount_gradio_app(api, demo, path="/")
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
+
|
| 8 |
+
from generate import generate_music, build_prompt, load_model
|
| 9 |
+
|
| 10 |
+
# preload model (removes cold start)
|
| 11 |
+
load_model()
|
| 12 |
+
|
| 13 |
+
api = FastAPI(title="Basyx Music Generator")
|
| 14 |
+
|
| 15 |
+
# serve local outputs
|
| 16 |
+
api.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")
|
| 17 |
+
|
| 18 |
|
| 19 |
# ======================
|
| 20 |
+
# API MODEL
|
| 21 |
# ======================
|
| 22 |
|
| 23 |
+
class GenerateRequest(BaseModel):
|
| 24 |
+
prompt: str
|
| 25 |
+
duration: int = 10
|
| 26 |
+
style: str | None = "nasheed"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
BASE_URL = "https://basyx-musicgen.hf.space"
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# ======================
|
| 33 |
+
# HEALTH CHECK
|
| 34 |
+
# ======================
|
| 35 |
|
| 36 |
@api.get("/health")
|
| 37 |
def health():
|
| 38 |
return {"status": "running"}
|
| 39 |
|
| 40 |
+
|
| 41 |
+
# ======================
|
| 42 |
+
# REST API (n8n Ready)
|
| 43 |
+
# ======================
|
| 44 |
+
|
| 45 |
@api.post("/generate")
|
| 46 |
+
async def generate(req: GenerateRequest):
|
| 47 |
+
|
| 48 |
+
if req.duration > 30:
|
| 49 |
+
raise HTTPException(400, "Duration too long")
|
| 50 |
+
|
| 51 |
+
if len(req.prompt) > 200:
|
| 52 |
+
raise HTTPException(400, "Prompt too long")
|
| 53 |
+
|
| 54 |
+
final_prompt = build_prompt(req.prompt, req.style)
|
| 55 |
+
|
| 56 |
+
local_path, public_url = generate_music(
|
| 57 |
+
final_prompt,
|
| 58 |
+
req.duration
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
filename = local_path.split("/")[-1]
|
| 62 |
+
|
| 63 |
+
return JSONResponse({
|
| 64 |
+
"status": "success",
|
| 65 |
+
"local_url": f"{BASE_URL}/outputs/{filename}",
|
| 66 |
+
"public_url": public_url
|
| 67 |
+
})
|
| 68 |
|
| 69 |
|
| 70 |
# ======================
|
| 71 |
# GRADIO UI
|
| 72 |
# ======================
|
| 73 |
|
| 74 |
+
def ui_generate(prompt, duration, style):
|
| 75 |
+
|
| 76 |
+
final_prompt = build_prompt(prompt, style)
|
| 77 |
|
| 78 |
+
local_path, _ = generate_music(
|
| 79 |
+
final_prompt,
|
| 80 |
+
duration
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
return local_path
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
with gr.Blocks(title="AI Background Music Generator") as demo:
|
| 87 |
+
|
| 88 |
+
gr.Markdown("# 🎵 AI Background Music Generator")
|
|
|
|
| 89 |
|
| 90 |
prompt = gr.Textbox(
|
| 91 |
+
label="Describe your music"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
style = gr.Dropdown(
|
| 95 |
+
choices=["nasheed", "cinematic", "lofi"],
|
| 96 |
+
value="nasheed",
|
| 97 |
+
label="Style"
|
| 98 |
)
|
| 99 |
|
| 100 |
duration = gr.Slider(
|
|
|
|
| 102 |
maximum=30,
|
| 103 |
value=10,
|
| 104 |
step=1,
|
| 105 |
+
label="Duration"
|
| 106 |
)
|
| 107 |
|
| 108 |
btn = gr.Button("Generate")
|
|
|
|
| 111 |
|
| 112 |
btn.click(
|
| 113 |
fn=ui_generate,
|
| 114 |
+
inputs=[prompt, duration, style],
|
| 115 |
outputs=output
|
| 116 |
)
|
| 117 |
|
| 118 |
|
| 119 |
+
# mount UI at root
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
app = gr.mount_gradio_app(api, demo, path="/")
|