kmaes commited on
Commit
276a9a0
·
verified ·
1 Parent(s): 1ebc467

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +11 -15
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
@@ -155,19 +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 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,7 +193,7 @@ demo = gr.Interface(
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=[
 
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
 
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=[