Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -9,8 +9,7 @@ import os
|
|
| 9 |
import sys
|
| 10 |
import pickle
|
| 11 |
import tempfile
|
| 12 |
-
import
|
| 13 |
-
import scipy.io.wavfile as wavfile
|
| 14 |
from huggingface_hub import hf_hub_download
|
| 15 |
|
| 16 |
# Add text2midi model to path
|
|
@@ -126,7 +125,7 @@ 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 as
|
| 130 |
if not prompt or not prompt.strip():
|
| 131 |
return None
|
| 132 |
|
|
@@ -156,14 +155,19 @@ def generate_music(prompt: str):
|
|
| 156 |
|
| 157 |
# Convert to WAV
|
| 158 |
if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
|
| 159 |
-
# Read WAV file and
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
else:
|
| 168 |
return None
|
| 169 |
|
|
@@ -193,7 +197,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 |
+
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 base64-encoded dict for API compatibility."""
|
| 129 |
if not prompt or not prompt.strip():
|
| 130 |
return None
|
| 131 |
|
|
|
|
| 155 |
|
| 156 |
# Convert to WAV
|
| 157 |
if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
|
| 158 |
+
# Read WAV file as bytes and encode as base64
|
| 159 |
+
with open(wav_path, 'rb') as f:
|
| 160 |
+
audio_bytes = f.read()
|
| 161 |
+
|
| 162 |
+
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 163 |
+
|
| 164 |
+
# Return as dict with base64 data - this format works with Gradio's Audio component
|
| 165 |
+
# and doesn't require file download from the client
|
| 166 |
+
return {
|
| 167 |
+
"name": "generated_audio.wav",
|
| 168 |
+
"data": f"data:audio/wav;base64,{audio_base64}",
|
| 169 |
+
"is_file": False
|
| 170 |
+
}
|
| 171 |
else:
|
| 172 |
return None
|
| 173 |
|
|
|
|
| 197 |
placeholder="A cheerful pop song with piano and drums in C major",
|
| 198 |
lines=2
|
| 199 |
),
|
| 200 |
+
outputs=gr.Audio(label="Generated Music", type="filepath"),
|
| 201 |
title="VR Game Music Generator",
|
| 202 |
description="Generate music from text descriptions using AI. Enter a prompt describing the music you want.",
|
| 203 |
examples=[
|