kmaes commited on
Commit
1b26622
·
verified ·
1 Parent(s): 5151779

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -9,7 +9,8 @@ import os
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,7 +126,7 @@ def midi_to_wav(midi_path: str, wav_path: str, sample_rate: int = 44100) -> bool
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
 
@@ -155,12 +156,14 @@ def generate_music(prompt: str):
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
 
@@ -190,7 +193,7 @@ demo = gr.Interface(
190
  placeholder="A cheerful pop song with piano and drums in C major",
191
  lines=2
192
  ),
193
- outputs=gr.Audio(label="Generated Music", type="filepath"),
194
  title="VR Game Music Generator",
195
  description="Generate music from text descriptions using AI. Enter a prompt describing the music you want.",
196
  examples=[
 
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
  return os.path.exists(wav_path)
127
 
128
  def generate_music(prompt: str):
129
+ """Generate music from text prompt. Returns audio as numpy array."""
130
  if not prompt or not prompt.strip():
131
  return None
132
 
 
156
 
157
  # Convert to WAV
158
  if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
159
+ # Read WAV file and return as numpy array
160
+ sample_rate, audio_data = wavfile.read(wav_path)
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
 
 
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="numpy"),
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=[