YongkangZOU commited on
Commit
3b389cf
Β·
verified Β·
1 Parent(s): d68bfe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -42
app.py CHANGED
@@ -14,10 +14,9 @@ load_dotenv()
14
  MUSICGEN_API_URL = os.getenv("MUSICGEN_API_URL")
15
  VEROVIO_API_URL = os.getenv("VEROVIO_API_URL")
16
 
17
- # Ensure output directory exists
18
  Path("output").mkdir(exist_ok=True)
19
 
20
- # === Tool 1: Convert WAV to MusicXML ===
21
  def wav_to_musicxml(wav_path: str, timestamp: str) -> str:
22
  output_dir = Path("output")
23
  for f in output_dir.glob("*_basic_pitch.mid"):
@@ -41,24 +40,20 @@ def wav_to_musicxml(wav_path: str, timestamp: str) -> str:
41
  score = converter.parse(midi_path)
42
  musicxml_path = output_dir / f"generated_{timestamp}.musicxml"
43
  score.write("musicxml", fp=musicxml_path)
44
-
45
  return str(musicxml_path)
46
 
47
- # === Tool 2: Render MusicXML to HTML via Verovio ===
48
  def render_musicxml_via_verovio_api(musicxml_path: str) -> str:
49
  if not VEROVIO_API_URL:
50
  return "❌ VEROVIO_API_URL is not configured"
51
-
52
  try:
53
  with open(musicxml_path, "rb") as f:
54
  files = {'file': f}
55
  response = requests.post(VEROVIO_API_URL, files=files)
56
  except Exception as e:
57
  return f"❌ Verovio API call failed: {e}"
58
-
59
  if response.status_code != 200:
60
  return f"❌ Verovio API error {response.status_code}: {response.text}"
61
-
62
  try:
63
  svg = response.json()["svg"]
64
  svg_b64 = base64.b64encode(svg.encode("utf-8")).decode("utf-8")
@@ -71,7 +66,7 @@ def render_musicxml_via_verovio_api(musicxml_path: str) -> str:
71
  except Exception as e:
72
  return f"⚠️ Failed to parse SVG: {e}"
73
 
74
- # === Tool 3: Melody + Prompt => Music + Score ===
75
  def generate_music_and_score(melody_file, prompt):
76
  if not MUSICGEN_API_URL:
77
  return None, "❌ MUSICGEN_API_URL is not configured. Please check your .env file"
@@ -101,40 +96,42 @@ def generate_music_and_score(melody_file, prompt):
101
  html = render_musicxml_via_verovio_api(musicxml_path)
102
  return wav_out_path, html
103
 
104
- # === MCP Tool App ===
105
  with gr.Blocks(title="🎡 Vibe Jamming Tools") as demo:
106
-
107
- # Tool 1: WAV to MusicXML
108
- gr.Interface(
109
- fn=wav_to_musicxml,
110
- inputs=[
111
- gr.Audio(type="filepath", label="🎀 WAV Audio File"),
112
- gr.Textbox(label="Timestamp (e.g. 20250609_123000)")
113
- ],
114
- outputs=gr.Textbox(label="πŸ“„ MusicXML File Path"),
115
- title="Tool 1: Convert WAV to MusicXML"
116
- ).render()
117
-
118
- # Tool 2: Render MusicXML to HTML
119
- gr.Interface(
120
- fn=render_musicxml_via_verovio_api,
121
- inputs=gr.Textbox(label="πŸ“„ MusicXML File Path"),
122
- outputs=gr.HTML(label="🎼 Rendered Score Preview"),
123
- title="Tool 2: Render MusicXML via Verovio"
124
- ).render()
125
-
126
- # Tool 3: Melody + Prompt β†’ Music + Score
127
- gr.Interface(
128
- fn=generate_music_and_score,
129
- inputs=[
130
- gr.Audio(type="filepath", label="Upload your humming (.wav)"),
131
- gr.Textbox(label="Describe your desired music style (prompt)")
132
- ],
133
- outputs=[
134
- gr.Audio(type="filepath", label="🎡 Generated Music"),
135
- gr.HTML(label="🎼 Score Preview (Rendered by Verovio)")
136
- ],
137
- title="Tool 3: Melody2Music+Score"
138
- ).render()
 
 
139
 
140
  demo.launch(mcp_server=True)
 
14
  MUSICGEN_API_URL = os.getenv("MUSICGEN_API_URL")
15
  VEROVIO_API_URL = os.getenv("VEROVIO_API_URL")
16
 
 
17
  Path("output").mkdir(exist_ok=True)
18
 
19
+ # === Tool 1 ===
20
  def wav_to_musicxml(wav_path: str, timestamp: str) -> str:
21
  output_dir = Path("output")
22
  for f in output_dir.glob("*_basic_pitch.mid"):
 
40
  score = converter.parse(midi_path)
41
  musicxml_path = output_dir / f"generated_{timestamp}.musicxml"
42
  score.write("musicxml", fp=musicxml_path)
 
43
  return str(musicxml_path)
44
 
45
+ # === Tool 2 ===
46
  def render_musicxml_via_verovio_api(musicxml_path: str) -> str:
47
  if not VEROVIO_API_URL:
48
  return "❌ VEROVIO_API_URL is not configured"
 
49
  try:
50
  with open(musicxml_path, "rb") as f:
51
  files = {'file': f}
52
  response = requests.post(VEROVIO_API_URL, files=files)
53
  except Exception as e:
54
  return f"❌ Verovio API call failed: {e}"
 
55
  if response.status_code != 200:
56
  return f"❌ Verovio API error {response.status_code}: {response.text}"
 
57
  try:
58
  svg = response.json()["svg"]
59
  svg_b64 = base64.b64encode(svg.encode("utf-8")).decode("utf-8")
 
66
  except Exception as e:
67
  return f"⚠️ Failed to parse SVG: {e}"
68
 
69
+ # === Tool 3 ===
70
  def generate_music_and_score(melody_file, prompt):
71
  if not MUSICGEN_API_URL:
72
  return None, "❌ MUSICGEN_API_URL is not configured. Please check your .env file"
 
96
  html = render_musicxml_via_verovio_api(musicxml_path)
97
  return wav_out_path, html
98
 
99
+ # === UI with Tabs ===
100
  with gr.Blocks(title="🎡 Vibe Jamming Tools") as demo:
101
+ gr.Markdown("## 🎹 Vibe Jamming - Multi-tool AI Music Studio")
102
+
103
+ with gr.Tabs():
104
+ with gr.TabItem("1️⃣ WAV ➜ MusicXML"):
105
+ gr.Interface(
106
+ fn=wav_to_musicxml,
107
+ inputs=[
108
+ gr.Audio(type="filepath", label="🎀 WAV Audio File"),
109
+ gr.Textbox(label="Timestamp (e.g. 20250609_123000)")
110
+ ],
111
+ outputs=gr.Textbox(label="πŸ“„ MusicXML File Path"),
112
+ live=False
113
+ ).render()
114
+
115
+ with gr.TabItem("2️⃣ MusicXML ➜ SVG Preview"):
116
+ gr.Interface(
117
+ fn=render_musicxml_via_verovio_api,
118
+ inputs=gr.Textbox(label="πŸ“„ MusicXML File Path"),
119
+ outputs=gr.HTML(label="🎼 Rendered Score"),
120
+ live=False
121
+ ).render()
122
+
123
+ with gr.TabItem("3️⃣ Melody2Music+Score"):
124
+ gr.Interface(
125
+ fn=generate_music_and_score,
126
+ inputs=[
127
+ gr.Audio(type="filepath", label="Upload your humming (.wav)"),
128
+ gr.Textbox(label="Describe your desired music style (prompt)")
129
+ ],
130
+ outputs=[
131
+ gr.Audio(type="filepath", label="🎡 Generated Music"),
132
+ gr.HTML(label="🎼 Score Preview (Rendered by Verovio)")
133
+ ],
134
+ live=False
135
+ ).render()
136
 
137
  demo.launch(mcp_server=True)