annapurnapadmaprema-ji commited on
Commit
0b3a9f2
1 Parent(s): 4d80d41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from audiocraft.models import MusicGen
2
  import streamlit as st
 
3
  import torch
4
  import torchaudio
5
  from io import BytesIO
@@ -16,9 +17,7 @@ def generate_music_tensors(description, duration: int):
16
 
17
  model.set_generation_params(
18
  use_sampling=True,
19
- top_k=300,
20
- top_p=0.85,
21
- temperature=0.8,
22
  duration=duration
23
  )
24
 
@@ -31,15 +30,16 @@ def generate_music_tensors(description, duration: int):
31
 
32
  def save_audio_to_bytes(samples: torch.Tensor):
33
  sample_rate = 32000
34
- assert samples.dim() == 3 # Expecting (batch, channels, samples)
35
- samples = samples[0] # Take the first batch item
36
  samples = samples.detach().cpu()
37
 
38
- # Save audio to a byte buffer instead of a file for easier download
39
- byte_io = BytesIO()
40
- torchaudio.save(byte_io, samples, sample_rate=sample_rate, format="wav")
41
- byte_io.seek(0) # Reset buffer position to the beginning for reading
42
- return byte_io
 
 
43
 
44
  st.set_page_config(
45
  page_icon=":musical_note:",
@@ -50,29 +50,32 @@ def main():
50
  st.title("Your Music")
51
 
52
  with st.expander("See Explanation"):
53
- st.write("App is developed using Meta's Audiocraft Music Gen model. Write a description and we will generate audio.")
54
 
55
  text_area = st.text_area("Enter description")
56
- time_slider = st.slider("Select time duration (seconds)", 2, 20, 10)
57
 
58
  if text_area and time_slider:
59
- st.json({
60
- "Description": text_area,
61
- "Selected duration": time_slider
62
- })
63
-
64
- st.write("We will be back with your music....please enjoy doing the rest of your tasks while we come back in some time :)")
 
65
  st.subheader("Generated Music")
66
  music_tensors = generate_music_tensors(text_area, time_slider)
67
-
68
- # Save to byte buffer for download
69
- audio_file = save_audio_to_bytes(music_tensors)
70
-
71
- # Play and download audio
72
- st.audio(audio_file, format="audio/wav")
 
 
73
  st.download_button(
74
  label="Download Audio",
75
- data=audio_file,
76
  file_name="generated_music.wav",
77
  mime="audio/wav"
78
  )
 
1
  from audiocraft.models import MusicGen
2
  import streamlit as st
3
+ import os
4
  import torch
5
  import torchaudio
6
  from io import BytesIO
 
17
 
18
  model.set_generation_params(
19
  use_sampling=True,
20
+ top_k=250,
 
 
21
  duration=duration
22
  )
23
 
 
30
 
31
  def save_audio_to_bytes(samples: torch.Tensor):
32
  sample_rate = 32000
33
+ assert samples.dim() == 2 or samples.dim() == 3
 
34
  samples = samples.detach().cpu()
35
 
36
+ if samples.dim() == 2:
37
+ samples = samples[None, ...] # Add batch dimension if missing
38
+
39
+ audio_buffer = BytesIO()
40
+ torchaudio.save(audio_buffer, samples[0], sample_rate=sample_rate, format="wav")
41
+ audio_buffer.seek(0) # Move to the start of the buffer
42
+ return audio_buffer
43
 
44
  st.set_page_config(
45
  page_icon=":musical_note:",
 
50
  st.title("Your Music")
51
 
52
  with st.expander("See Explanation"):
53
+ st.write("This app uses Meta's Audiocraft Music Gen model to generate audio based on your description.")
54
 
55
  text_area = st.text_area("Enter description")
56
+ time_slider = st.slider("Select time duration (seconds)", 2, 20, 5)
57
 
58
  if text_area and time_slider:
59
+ st.json(
60
+ {
61
+ "Description": text_area,
62
+ "Selected duration": time_slider
63
+ }
64
+ st.write("We will back with your music....please enjoy doing the rest of your tasks while we come back in some time :)")
65
+ )
66
  st.subheader("Generated Music")
67
  music_tensors = generate_music_tensors(text_area, time_slider)
68
+
69
+ # Convert audio to bytes for playback and download
70
+ audio_buffer = save_audio_to_bytes(music_tensors)
71
+
72
+ # Play audio
73
+ st.audio(audio_buffer, format="audio/wav")
74
+
75
+ # Download button for audio
76
  st.download_button(
77
  label="Download Audio",
78
+ data=audio_buffer,
79
  file_name="generated_music.wav",
80
  mime="audio/wav"
81
  )