kmaes commited on
Commit
749e66d
·
verified ·
1 Parent(s): de6a347

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +52 -36
app.py CHANGED
@@ -9,6 +9,8 @@ import os
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,52 +126,66 @@ def midi_to_wav(midi_path: str, wav_path: str, sample_rate: int = 44100) -> bool
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
  try:
132
  # Create temporary files
133
- midi_file = tempfile.NamedTemporaryFile(suffix='.mid', delete=False)
134
- midi_path = midi_file.name
135
- midi_file.close()
136
-
137
- wav_file = tempfile.NamedTemporaryFile(suffix='.wav', delete=False)
138
- wav_path = wav_file.name
139
- wav_file.close()
140
-
141
- try:
142
- # Generate MIDI
143
- if MODEL_LOADED:
144
- generate_midi_with_model(prompt, midi_path, max_len=512, temperature=0.9)
145
- else:
146
- from midiutil import MIDIFile
147
- midi = MIDIFile(1)
148
- midi.addTempo(0, 0, 120)
149
- notes = [60, 62, 64, 65, 67, 69, 71, 72]
150
- for i, note in enumerate(notes[:min(len(prompt.split()), 8)]):
151
- midi.addNote(0, 0, note, i, 1, 100)
152
- with open(midi_path, "wb") as f:
153
- midi.writeFile(f)
154
-
155
- # Convert to WAV
156
- if SOUNDFONT_PATH and midi_to_wav(midi_path, wav_path):
157
- return wav_path
158
- else:
159
- return None
160
-
161
- finally:
162
- try:
163
- os.unlink(midi_path)
164
- except:
165
- pass
166
 
167
  except Exception as e:
168
  import traceback
169
  traceback.print_exc()
170
  return None
171
 
172
- # Create simple Gradio Interface (avoids schema generation bugs in gr.Blocks)
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  demo = gr.Interface(
174
  fn=generate_music,
175
  inputs=gr.Textbox(
@@ -177,7 +193,7 @@ demo = gr.Interface(
177
  placeholder="A cheerful pop song with piano and drums in C major",
178
  lines=2
179
  ),
180
- outputs=gr.Audio(label="Generated Music", type="filepath"),
181
  title="VR Game Music Generator",
182
  description="Generate music from text descriptions using AI. Enter a prompt describing the music you want.",
183
  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 (sample_rate, numpy_array)."""
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 files
138
+ midi_fd, midi_path = tempfile.mkstemp(suffix='.mid')
139
+ os.close(midi_fd)
140
+
141
+ wav_fd, wav_path = tempfile.mkstemp(suffix='.wav')
142
+ os.close(wav_fd)
143
+
144
+ # Generate MIDI
145
+ if MODEL_LOADED:
146
+ generate_midi_with_model(prompt, midi_path, max_len=512, temperature=0.9)
147
+ else:
148
+ from midiutil import MIDIFile
149
+ midi = MIDIFile(1)
150
+ midi.addTempo(0, 0, 120)
151
+ notes = [60, 62, 64, 65, 67, 69, 71, 72]
152
+ for i, note in enumerate(notes[:min(len(prompt.split()), 8)]):
153
+ midi.addNote(0, 0, note, i, 1, 100)
154
+ with open(midi_path, "wb") as f:
155
+ midi.writeFile(f)
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
 
170
  except Exception as e:
171
  import traceback
172
  traceback.print_exc()
173
  return None
174
 
175
+ finally:
176
+ # Clean up temp files
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(
190
  fn=generate_music,
191
  inputs=gr.Textbox(
 
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=[