bharatverse11 commited on
Commit
2b36725
Β·
verified Β·
1 Parent(s): cab9b9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -39
app.py CHANGED
@@ -4,95 +4,97 @@ 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()
 
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()