thecollabagepatch commited on
Commit
98b2108
·
1 Parent(s): 4f864e1

one more try

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -121,10 +121,16 @@ def generate_midi(seed, use_chords, chord_progression, bpm):
121
 
122
  @spaces.GPU(duration=120)
123
  def generate_music(midi_audio, prompt_duration, musicgen_model, num_iterations, bpm):
124
- wav_filename, sample_rate = midi_audio
 
 
 
 
 
 
 
 
125
 
126
- # Load the generated audio
127
- song, sr = torchaudio.load(wav_filename)
128
  song = song.to(device)
129
 
130
  # Use the user-provided BPM value for duration calculation
@@ -178,8 +184,10 @@ def generate_music(midi_audio, prompt_duration, musicgen_model, num_iterations,
178
  combined_audio_filename = f"combined_audio_{random.randint(1, 10000)}.mp3"
179
  combined_audio.export(combined_audio_filename, format="mp3")
180
 
 
181
  # Clean up temporary files
182
- os.remove(temp_filename)
 
183
  for filename in all_audio_files:
184
  os.remove(filename)
185
 
 
121
 
122
  @spaces.GPU(duration=120)
123
  def generate_music(midi_audio, prompt_duration, musicgen_model, num_iterations, bpm):
124
+ if isinstance(midi_audio, tuple):
125
+ wav_filename, sample_rate = midi_audio
126
+ song, sr = torchaudio.load(wav_filename)
127
+ else:
128
+ # Assuming midi_audio is a numpy array
129
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
130
+ temp_filename = temp_file.name
131
+ torchaudio.save(temp_filename, torch.from_numpy(midi_audio), sample_rate=44100)
132
+ song, sr = torchaudio.load(temp_filename)
133
 
 
 
134
  song = song.to(device)
135
 
136
  # Use the user-provided BPM value for duration calculation
 
184
  combined_audio_filename = f"combined_audio_{random.randint(1, 10000)}.mp3"
185
  combined_audio.export(combined_audio_filename, format="mp3")
186
 
187
+
188
  # Clean up temporary files
189
+ if not isinstance(midi_audio, tuple):
190
+ os.remove(temp_filename)
191
  for filename in all_audio_files:
192
  os.remove(filename)
193