kmaes commited on
Commit
3b806de
·
verified ·
1 Parent(s): 0676034

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -9,8 +9,7 @@ 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,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 (sample_rate, numpy_array)."""
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 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,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="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=[
 
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=[