Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -9,8 +9,6 @@ import os
|
|
| 9 |
import sys
|
| 10 |
import pickle
|
| 11 |
import tempfile
|
| 12 |
-
import numpy as np
|
| 13 |
-
import scipy.io.wavfile as wavfile
|
| 14 |
from huggingface_hub import hf_hub_download
|
| 15 |
|
| 16 |
# Add text2midi model to path
|
|
@@ -126,24 +124,25 @@ def midi_to_wav(midi_path: str, wav_path: str, sample_rate: int = 44100) -> bool
|
|
| 126 |
return os.path.exists(wav_path)
|
| 127 |
|
| 128 |
def generate_music(prompt: str):
|
| 129 |
-
"""Generate music from text prompt. Returns audio
|
| 130 |
if not prompt or not prompt.strip():
|
| 131 |
return None
|
| 132 |
|
| 133 |
midi_path = None
|
| 134 |
-
wav_path = None
|
| 135 |
|
| 136 |
try:
|
| 137 |
-
# Create temporary
|
| 138 |
midi_fd, midi_path = tempfile.mkstemp(suffix='.mid')
|
| 139 |
os.close(midi_fd)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
os.
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
# Generate MIDI
|
| 145 |
if MODEL_LOADED:
|
| 146 |
-
generate_midi_with_model(prompt, midi_path, max_len=
|
| 147 |
else:
|
| 148 |
from midiutil import MIDIFile
|
| 149 |
midi = MIDIFile(1)
|
|
@@ -156,14 +155,8 @@ def generate_music(prompt: str):
|
|
| 156 |
|
| 157 |
# Convert to WAV
|
| 158 |
if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
|
| 159 |
-
#
|
| 160 |
-
|
| 161 |
-
# Convert to float32 for Gradio compatibility
|
| 162 |
-
if audio_data.dtype == np.int16:
|
| 163 |
-
audio_data = audio_data.astype(np.float32) / 32768.0
|
| 164 |
-
elif audio_data.dtype == np.int32:
|
| 165 |
-
audio_data = audio_data.astype(np.float32) / 2147483648.0
|
| 166 |
-
return (sample_rate, audio_data)
|
| 167 |
else:
|
| 168 |
return None
|
| 169 |
|
|
@@ -173,17 +166,12 @@ def generate_music(prompt: str):
|
|
| 173 |
return None
|
| 174 |
|
| 175 |
finally:
|
| 176 |
-
#
|
| 177 |
if midi_path and os.path.exists(midi_path):
|
| 178 |
try:
|
| 179 |
os.unlink(midi_path)
|
| 180 |
except:
|
| 181 |
pass
|
| 182 |
-
if wav_path and os.path.exists(wav_path):
|
| 183 |
-
try:
|
| 184 |
-
os.unlink(wav_path)
|
| 185 |
-
except:
|
| 186 |
-
pass
|
| 187 |
|
| 188 |
# Create simple Gradio Interface
|
| 189 |
demo = gr.Interface(
|
|
@@ -193,7 +181,7 @@ demo = gr.Interface(
|
|
| 193 |
placeholder="A cheerful pop song with piano and drums in C major",
|
| 194 |
lines=2
|
| 195 |
),
|
| 196 |
-
outputs=gr.Audio(label="Generated Music", type="
|
| 197 |
title="VR Game Music Generator",
|
| 198 |
description="Generate music from text descriptions using AI. Enter a prompt describing the music you want.",
|
| 199 |
examples=[
|
|
|
|
| 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 |
return os.path.exists(wav_path)
|
| 125 |
|
| 126 |
def generate_music(prompt: str):
|
| 127 |
+
"""Generate music from text prompt. Returns audio file path."""
|
| 128 |
if not prompt or not prompt.strip():
|
| 129 |
return None
|
| 130 |
|
| 131 |
midi_path = None
|
|
|
|
| 132 |
|
| 133 |
try:
|
| 134 |
+
# Create temporary MIDI file
|
| 135 |
midi_fd, midi_path = tempfile.mkstemp(suffix='.mid')
|
| 136 |
os.close(midi_fd)
|
| 137 |
|
| 138 |
+
# Create WAV in Gradio's temp directory for proper file serving
|
| 139 |
+
gradio_temp = os.path.join(tempfile.gettempdir(), "gradio")
|
| 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:
|
| 145 |
+
generate_midi_with_model(prompt, midi_path, max_len=128, temperature=0.9)
|
| 146 |
else:
|
| 147 |
from midiutil import MIDIFile
|
| 148 |
midi = MIDIFile(1)
|
|
|
|
| 155 |
|
| 156 |
# Convert to WAV
|
| 157 |
if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
|
| 158 |
+
# Return file path - don't delete, let Gradio serve it
|
| 159 |
+
return wav_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
else:
|
| 161 |
return None
|
| 162 |
|
|
|
|
| 166 |
return None
|
| 167 |
|
| 168 |
finally:
|
| 169 |
+
# Only clean up MIDI file, keep WAV for Gradio to serve
|
| 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(
|
|
|
|
| 181 |
placeholder="A cheerful pop song with piano and drums in C major",
|
| 182 |
lines=2
|
| 183 |
),
|
| 184 |
+
outputs=gr.Audio(label="Generated Music", type="filepath"),
|
| 185 |
title="VR Game Music Generator",
|
| 186 |
description="Generate music from text descriptions using AI. Enter a prompt describing the music you want.",
|
| 187 |
examples=[
|