Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,95 +4,97 @@ import base64
|
|
| 4 |
import io
|
| 5 |
import soundfile as sf
|
| 6 |
|
| 7 |
-
# π₯
|
| 8 |
API_URL = "https://bustled-hertha-unprojective.ngrok-free.dev/generate"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def generate(prompt, duration, steps, cfg):
|
| 12 |
try:
|
| 13 |
-
|
|
|
|
| 14 |
res = requests.post(
|
| 15 |
API_URL,
|
| 16 |
json={
|
| 17 |
-
"prompt":
|
| 18 |
-
"duration":
|
| 19 |
-
"steps":
|
| 20 |
-
"cfg_scale": cfg
|
| 21 |
},
|
| 22 |
-
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
-
print("STATUS:
|
| 26 |
|
| 27 |
-
# β backend error
|
| 28 |
if res.status_code != 200:
|
| 29 |
-
print("β Backend error:
|
| 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:
|
| 42 |
return None
|
| 43 |
|
| 44 |
-
# β missing audio
|
| 45 |
if "audio" not in data:
|
| 46 |
-
print("β No audio key:
|
| 47 |
return None
|
| 48 |
|
| 49 |
-
#
|
| 50 |
audio_bytes = base64.b64decode(data["audio"])
|
| 51 |
-
audio, sr
|
| 52 |
|
|
|
|
| 53 |
return sr, audio
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
-
print("β Request failed:
|
| 57 |
return None
|
| 58 |
|
| 59 |
|
| 60 |
-
#
|
| 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 |
-
|
| 68 |
label="π§ Prompt",
|
| 69 |
-
placeholder="
|
|
|
|
| 70 |
)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
label="β± Duration (seconds)"
|
| 75 |
)
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
label="βοΈ Diffusion Steps"
|
| 80 |
)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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=[
|
| 95 |
-
outputs=output
|
| 96 |
)
|
| 97 |
|
| 98 |
demo.launch()
|
|
|
|
| 4 |
import io
|
| 5 |
import soundfile as sf
|
| 6 |
|
| 7 |
+
# π₯ Your ngrok API endpoint β update this each time you restart Colab
|
| 8 |
API_URL = "https://bustled-hertha-unprojective.ngrok-free.dev/generate"
|
| 9 |
|
| 10 |
+
# Required to bypass ngrok browser warning page (fixes Invalid JSON error)
|
| 11 |
+
HEADERS = {"ngrok-skip-browser-warning": "true"}
|
| 12 |
+
|
| 13 |
|
| 14 |
def generate(prompt, duration, steps, cfg):
|
| 15 |
try:
|
| 16 |
+
print(f"π€ Sending request β prompt='{prompt}' duration={duration}s steps={steps}")
|
| 17 |
+
|
| 18 |
res = requests.post(
|
| 19 |
API_URL,
|
| 20 |
json={
|
| 21 |
+
"prompt": prompt,
|
| 22 |
+
"duration": float(duration),
|
| 23 |
+
"steps": int(steps),
|
| 24 |
+
"cfg_scale": float(cfg),
|
| 25 |
},
|
| 26 |
+
headers=HEADERS,
|
| 27 |
+
timeout=300, # 5 min β generation can be slow
|
| 28 |
)
|
| 29 |
|
| 30 |
+
print(f"STATUS: {res.status_code}")
|
| 31 |
|
|
|
|
| 32 |
if res.status_code != 200:
|
| 33 |
+
print(f"β Backend error: {res.text[:500]}")
|
| 34 |
return None
|
| 35 |
|
|
|
|
| 36 |
if not res.text:
|
| 37 |
+
print("β Empty response from backend")
|
| 38 |
return None
|
| 39 |
|
|
|
|
| 40 |
try:
|
| 41 |
data = res.json()
|
| 42 |
except Exception:
|
| 43 |
+
print(f"β Invalid JSON (got HTML?): {res.text[:300]}")
|
| 44 |
return None
|
| 45 |
|
|
|
|
| 46 |
if "audio" not in data:
|
| 47 |
+
print(f"β No audio key in response: {data}")
|
| 48 |
return None
|
| 49 |
|
| 50 |
+
# Decode base64 WAV β numpy
|
| 51 |
audio_bytes = base64.b64decode(data["audio"])
|
| 52 |
+
audio, sr = sf.read(io.BytesIO(audio_bytes))
|
| 53 |
|
| 54 |
+
print(f"β
Got audio: {len(audio)/sr:.2f}s @ {sr}Hz")
|
| 55 |
return sr, audio
|
| 56 |
|
| 57 |
+
except requests.exceptions.Timeout:
|
| 58 |
+
print("β Request timed out β try fewer steps or shorter duration")
|
| 59 |
+
return None
|
| 60 |
except Exception as e:
|
| 61 |
+
print(f"β Request failed: {e}")
|
| 62 |
return None
|
| 63 |
|
| 64 |
|
| 65 |
+
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 66 |
with gr.Blocks(title="AutoMix AI π΅") as demo:
|
| 67 |
gr.Markdown("# π΅ AutoMix AI Beat Generator")
|
| 68 |
+
gr.Markdown("Generate AI beats using a diffusion model fine-tuned on trap/rap/R&B π")
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column():
|
| 72 |
+
prompt_in = gr.Textbox(
|
| 73 |
label="π§ Prompt",
|
| 74 |
+
placeholder="A dark trap beat at 140 BPM in C minor, featuring 808 bass and synth bells.",
|
| 75 |
+
lines=3,
|
| 76 |
)
|
| 77 |
+
duration_in = gr.Slider(
|
| 78 |
+
minimum=5, maximum=47, value=30, step=1,
|
| 79 |
+
label="β± Duration (seconds)",
|
|
|
|
| 80 |
)
|
| 81 |
+
steps_in = gr.Slider(
|
| 82 |
+
minimum=20, maximum=200, value=100, step=10,
|
| 83 |
+
label="βοΈ Diffusion Steps (more = better quality, slower)",
|
|
|
|
| 84 |
)
|
| 85 |
+
cfg_in = gr.Slider(
|
| 86 |
+
minimum=1.0, maximum=15.0, value=7.0, step=0.5,
|
| 87 |
+
label="π CFG Scale",
|
|
|
|
| 88 |
)
|
|
|
|
| 89 |
btn = gr.Button("π Generate Beat", variant="primary")
|
| 90 |
|
| 91 |
with gr.Column():
|
| 92 |
+
output = gr.Audio(label="π΅ Generated Beat", type="numpy")
|
| 93 |
|
| 94 |
btn.click(
|
| 95 |
fn=generate,
|
| 96 |
+
inputs=[prompt_in, duration_in, steps_in, cfg_in],
|
| 97 |
+
outputs=output,
|
| 98 |
)
|
| 99 |
|
| 100 |
demo.launch()
|