Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import os
|
|
| 9 |
import sys
|
| 10 |
import pickle
|
| 11 |
import tempfile
|
|
|
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
|
| 14 |
# Add text2midi model to path
|
|
@@ -124,21 +125,20 @@ def midi_to_wav(midi_path: str, wav_path: str, sample_rate: int = 44100) -> bool
|
|
| 124 |
return os.path.exists(wav_path)
|
| 125 |
|
| 126 |
def generate_music(prompt: str):
|
| 127 |
-
"""Generate music from text prompt. Returns audio
|
| 128 |
if not prompt or not prompt.strip():
|
| 129 |
return None
|
| 130 |
|
| 131 |
midi_path = None
|
|
|
|
| 132 |
|
| 133 |
try:
|
| 134 |
-
# Create temporary
|
| 135 |
midi_fd, midi_path = tempfile.mkstemp(suffix='.mid')
|
| 136 |
os.close(midi_fd)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
os.makedirs(gradio_temp, exist_ok=True)
|
| 141 |
-
wav_path = os.path.join(gradio_temp, f"audio_{os.urandom(8).hex()}.wav")
|
| 142 |
|
| 143 |
# Generate MIDI (reduced max_len for faster testing)
|
| 144 |
if MODEL_LOADED:
|
|
@@ -155,8 +155,12 @@ def generate_music(prompt: str):
|
|
| 155 |
|
| 156 |
# Convert to WAV
|
| 157 |
if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
|
| 158 |
-
#
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
else:
|
| 161 |
return None
|
| 162 |
|
|
@@ -166,12 +170,17 @@ def generate_music(prompt: str):
|
|
| 166 |
return None
|
| 167 |
|
| 168 |
finally:
|
| 169 |
-
#
|
| 170 |
if midi_path and os.path.exists(midi_path):
|
| 171 |
try:
|
| 172 |
os.unlink(midi_path)
|
| 173 |
except:
|
| 174 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
# Create simple Gradio Interface
|
| 177 |
demo = gr.Interface(
|
|
|
|
| 9 |
import sys
|
| 10 |
import pickle
|
| 11 |
import tempfile
|
| 12 |
+
import base64
|
| 13 |
from huggingface_hub import hf_hub_download
|
| 14 |
|
| 15 |
# Add text2midi model to path
|
|
|
|
| 125 |
return os.path.exists(wav_path)
|
| 126 |
|
| 127 |
def generate_music(prompt: str):
|
| 128 |
+
"""Generate music from text prompt. Returns audio as data URI for API compatibility."""
|
| 129 |
if not prompt or not prompt.strip():
|
| 130 |
return None
|
| 131 |
|
| 132 |
midi_path = None
|
| 133 |
+
wav_path = None
|
| 134 |
|
| 135 |
try:
|
| 136 |
+
# Create temporary files
|
| 137 |
midi_fd, midi_path = tempfile.mkstemp(suffix='.mid')
|
| 138 |
os.close(midi_fd)
|
| 139 |
|
| 140 |
+
wav_fd, wav_path = tempfile.mkstemp(suffix='.wav')
|
| 141 |
+
os.close(wav_fd)
|
|
|
|
|
|
|
| 142 |
|
| 143 |
# Generate MIDI (reduced max_len for faster testing)
|
| 144 |
if MODEL_LOADED:
|
|
|
|
| 155 |
|
| 156 |
# Convert to WAV
|
| 157 |
if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
|
| 158 |
+
# Read and encode as base64 data URI
|
| 159 |
+
with open(wav_path, 'rb') as f:
|
| 160 |
+
audio_bytes = f.read()
|
| 161 |
+
audio_b64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 162 |
+
# Return as data URI - works with Gradio Audio component
|
| 163 |
+
return f"data:audio/wav;base64,{audio_b64}"
|
| 164 |
else:
|
| 165 |
return None
|
| 166 |
|
|
|
|
| 170 |
return None
|
| 171 |
|
| 172 |
finally:
|
| 173 |
+
# Clean up temp files
|
| 174 |
if midi_path and os.path.exists(midi_path):
|
| 175 |
try:
|
| 176 |
os.unlink(midi_path)
|
| 177 |
except:
|
| 178 |
pass
|
| 179 |
+
if wav_path and os.path.exists(wav_path):
|
| 180 |
+
try:
|
| 181 |
+
os.unlink(wav_path)
|
| 182 |
+
except:
|
| 183 |
+
pass
|
| 184 |
|
| 185 |
# Create simple Gradio Interface
|
| 186 |
demo = gr.Interface(
|