Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,98 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import base64
|
| 4 |
-
import numpy as np
|
| 5 |
import io
|
| 6 |
import soundfile as sf
|
| 7 |
|
|
|
|
| 8 |
API_URL = "https://bustled-hertha-unprojective.ngrok-free.dev/generate"
|
| 9 |
|
|
|
|
| 10 |
def generate(prompt, duration, steps, cfg):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
data = res.json()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
duration = gr.Slider(1, 10, value=5, label="Duration (seconds)")
|
| 34 |
-
steps = gr.Slider(20, 80, value=40, label="Steps")
|
| 35 |
-
cfg = gr.Slider(1, 10, value=7, label="CFG Scale")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import base64
|
|
|
|
| 4 |
import io
|
| 5 |
import soundfile as sf
|
| 6 |
|
| 7 |
+
# π₯ IMPORTANT: your ngrok API endpoint
|
| 8 |
API_URL = "https://bustled-hertha-unprojective.ngrok-free.dev/generate"
|
| 9 |
|
| 10 |
+
|
| 11 |
def generate(prompt, duration, steps, cfg):
|
| 12 |
+
try:
|
| 13 |
+
# π send request to backend
|
| 14 |
+
res = requests.post(
|
| 15 |
+
API_URL,
|
| 16 |
+
json={
|
| 17 |
+
"prompt": prompt,
|
| 18 |
+
"duration": duration,
|
| 19 |
+
"steps": steps,
|
| 20 |
+
"cfg_scale": cfg
|
| 21 |
+
},
|
| 22 |
+
timeout=180 # longer timeout for generation
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
print("STATUS:", res.status_code)
|
| 26 |
+
|
| 27 |
+
# β backend error
|
| 28 |
+
if res.status_code != 200:
|
| 29 |
+
print("β Backend error:", res.text[:300])
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
# β empty response
|
| 33 |
+
if not res.text:
|
| 34 |
+
print("β Empty response")
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# β invalid JSON
|
| 38 |
+
try:
|
| 39 |
+
data = res.json()
|
| 40 |
+
except Exception:
|
| 41 |
+
print("β Invalid JSON:", res.text[:300])
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
# β missing audio
|
| 45 |
+
if "audio" not in data:
|
| 46 |
+
print("β No audio key:", data)
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
# β
decode base64 β audio
|
| 50 |
+
audio_bytes = base64.b64decode(data["audio"])
|
| 51 |
+
audio, sr = sf.read(io.BytesIO(audio_bytes))
|
| 52 |
+
|
| 53 |
+
return sr, audio
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print("β Request failed:", str(e))
|
| 57 |
+
return None
|
| 58 |
|
|
|
|
| 59 |
|
| 60 |
+
# π¨ UI
|
| 61 |
+
with gr.Blocks(title="AutoMix AI π΅") as demo:
|
| 62 |
+
gr.Markdown("# π΅ AutoMix AI Beat Generator")
|
| 63 |
+
gr.Markdown("Generate AI beats using diffusion model π")
|
| 64 |
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column():
|
| 67 |
+
prompt = gr.Textbox(
|
| 68 |
+
label="π§ Prompt",
|
| 69 |
+
placeholder="Dark trap beat, 140 BPM, heavy 808..."
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
duration = gr.Slider(
|
| 73 |
+
1, 10, value=5, step=1,
|
| 74 |
+
label="β± Duration (seconds)"
|
| 75 |
+
)
|
| 76 |
|
| 77 |
+
steps = gr.Slider(
|
| 78 |
+
20, 80, value=40, step=5,
|
| 79 |
+
label="βοΈ Diffusion Steps"
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
cfg = gr.Slider(
|
| 83 |
+
1, 10, value=7, step=0.5,
|
| 84 |
+
label="π CFG Scale"
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
btn = gr.Button("π Generate Beat", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
with gr.Column():
|
| 90 |
+
output = gr.Audio(label="π΅ Generated Beat")
|
| 91 |
|
| 92 |
+
btn.click(
|
| 93 |
+
fn=generate,
|
| 94 |
+
inputs=[prompt, duration, steps, cfg],
|
| 95 |
+
outputs=output
|
| 96 |
+
)
|
| 97 |
|
| 98 |
demo.launch()
|