thecollabagepatch commited on
Commit
ff54f69
1 Parent(s): cf7f846

added tempfile

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -10,6 +10,7 @@ from audiocraft.models import MusicGen
10
  from audiocraft.data.audio import audio_write
11
  from pydub import AudioSegment
12
  import spaces
 
13
 
14
  # Check if CUDA is available
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -120,10 +121,12 @@ def generate_midi(seed, use_chords, chord_progression, bpm):
120
 
121
  @spaces.GPU(duration=120)
122
  def generate_music(midi_audio, prompt_duration, musicgen_model, num_iterations, bpm):
123
- wav_filename = midi_audio[0] # Extract the file path from the tuple
 
 
124
 
125
  # Load the generated audio
126
- song, sr = torchaudio.load(wav_filename)
127
  song = song.to(device)
128
 
129
  # Use the user-provided BPM value for duration calculation
@@ -178,7 +181,7 @@ def generate_music(midi_audio, prompt_duration, musicgen_model, num_iterations,
178
  combined_audio.export(combined_audio_filename, format="mp3")
179
 
180
  # Clean up temporary files
181
- os.remove(wav_filename)
182
  for filename in all_audio_files:
183
  os.remove(filename)
184
 
 
10
  from audiocraft.data.audio import audio_write
11
  from pydub import AudioSegment
12
  import spaces
13
+ import tempfile
14
 
15
  # Check if CUDA is available
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
121
 
122
  @spaces.GPU(duration=120)
123
  def generate_music(midi_audio, prompt_duration, musicgen_model, num_iterations, bpm):
124
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
125
+ temp_filename = temp_file.name
126
+ torchaudio.save(temp_filename, midi_audio, sample_rate=44100)
127
 
128
  # Load the generated audio
129
+ song, sr = torchaudio.load(temp_filename)
130
  song = song.to(device)
131
 
132
  # Use the user-provided BPM value for duration calculation
 
181
  combined_audio.export(combined_audio_filename, format="mp3")
182
 
183
  # Clean up temporary files
184
+ os.remove(temp_filename)
185
  for filename in all_audio_files:
186
  os.remove(filename)
187