Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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
|
| 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
|
| 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 |
-
# ===
|
| 105 |
with gr.Blocks(title="π΅ Vibe Jamming Tools") as demo:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
gr.
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
| 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)
|