Update app.py
Browse files
app.py
CHANGED
|
@@ -1,120 +1,93 @@
|
|
| 1 |
-
|
| 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 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def health():
|
| 38 |
-
return {"status": "running"}
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# ======================
|
| 42 |
-
# REST API (n8n Ready)
|
| 43 |
-
# ======================
|
| 44 |
-
|
| 45 |
-
@api.post("/generate-json")
|
| 46 |
-
async def generate_json(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 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
# mount UI at root
|
| 120 |
-
app = gr.mount_gradio_app(api, demo, path="/")
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from generate import generate_music, load_model
|
| 4 |
+
|
| 5 |
+
# ------------------------------------------------
|
| 6 |
+
# Load Model (Runs Once)
|
| 7 |
+
# ------------------------------------------------
|
| 8 |
+
print("Loading MusicGen model...")
|
| 9 |
+
model = load_model()
|
| 10 |
+
print("Model ready.")
|
| 11 |
+
|
| 12 |
+
# ------------------------------------------------
|
| 13 |
+
# Generate Handler
|
| 14 |
+
# ------------------------------------------------
|
| 15 |
+
def create_music(
|
| 16 |
+
prompt,
|
| 17 |
+
duration,
|
| 18 |
+
temperature
|
| 19 |
+
):
|
| 20 |
+
"""
|
| 21 |
+
Generates music from prompt.
|
| 22 |
+
Returns path to generated audio file.
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
if not prompt or prompt.strip() == "":
|
| 26 |
+
return None, "Prompt cannot be empty"
|
| 27 |
+
|
| 28 |
+
output_file = generate_music(
|
| 29 |
+
model=model,
|
| 30 |
+
prompt=prompt,
|
| 31 |
+
duration=int(duration),
|
| 32 |
+
temperature=float(temperature),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
+
return output_file, "Generation Complete ✅"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# ------------------------------------------------
|
| 39 |
+
# UI
|
| 40 |
+
# ------------------------------------------------
|
| 41 |
+
with gr.Blocks(title="Tier 3 AI Music Studio") as demo:
|
| 42 |
+
|
| 43 |
+
gr.Markdown("# 🎵 Tier 3 AI Music Generator")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
prompt = gr.Textbox(
|
| 47 |
+
label="Music Prompt",
|
| 48 |
+
placeholder="Epic cinematic orchestra, emotional, cinematic buildup..."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
duration = gr.Slider(
|
| 53 |
+
minimum=5,
|
| 54 |
+
maximum=30,
|
| 55 |
+
value=10,
|
| 56 |
+
step=1,
|
| 57 |
+
label="Duration (seconds)"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
temperature = gr.Slider(
|
| 61 |
+
minimum=0.1,
|
| 62 |
+
maximum=1.5,
|
| 63 |
+
value=1.0,
|
| 64 |
+
step=0.1,
|
| 65 |
+
label="Creativity"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
generate_btn = gr.Button("Generate Music")
|
| 69 |
+
|
| 70 |
+
audio_output = gr.Audio(label="Generated Audio")
|
| 71 |
+
status = gr.Textbox(label="Status")
|
| 72 |
+
|
| 73 |
+
generate_btn.click(
|
| 74 |
+
fn=create_music,
|
| 75 |
+
inputs=[
|
| 76 |
+
prompt,
|
| 77 |
+
duration,
|
| 78 |
+
temperature
|
| 79 |
+
],
|
| 80 |
+
outputs=[
|
| 81 |
+
audio_output,
|
| 82 |
+
status
|
| 83 |
+
],
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# ------------------------------------------------
|
| 87 |
+
# Launch
|
| 88 |
+
# ------------------------------------------------
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
demo.launch(
|
| 91 |
+
server_name="0.0.0.0",
|
| 92 |
+
server_port=7860
|
| 93 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|