annapurnapadmaprema-ji commited on
Commit
cf362cb
·
verified ·
1 Parent(s): 6474fcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -13
app.py CHANGED
@@ -1,10 +1,7 @@
1
  from audiocraft.models import MusicGen
2
  import streamlit as st
3
- import os
4
  import torch
5
  import torchaudio
6
- import numpy as np
7
- import base64
8
  from io import BytesIO
9
 
10
  @st.cache_resource
@@ -17,12 +14,11 @@ def generate_music_tensors(description, duration: int):
17
  print("Duration:", duration)
18
  model = load_model()
19
 
20
- # Experiment with different generation parameters for improved quality
21
  model.set_generation_params(
22
  use_sampling=True,
23
- top_k=300, # Increase top_k for more diversity
24
- top_p=0.85, # Probability threshold for token sampling
25
- temperature=0.8, # Control randomness; lower values = more focused output
26
  duration=duration
27
  )
28
 
@@ -35,13 +31,11 @@ def generate_music_tensors(description, duration: int):
35
 
36
  def save_audio_to_bytes(samples: torch.Tensor):
37
  sample_rate = 32000
38
- assert samples.dim() == 2 or samples.dim() == 3
 
39
  samples = samples.detach().cpu()
40
 
41
- if samples.dim() == 2:
42
- samples = samples[None, ...]
43
-
44
- # Save audio to a byte buffer instead of file for easier download
45
  byte_io = BytesIO()
46
  torchaudio.save(byte_io, samples, sample_rate=sample_rate, format="wav")
47
  byte_io.seek(0) # Reset buffer position to the beginning for reading
@@ -66,7 +60,7 @@ def main():
66
  "Description": text_area,
67
  "Selected duration": time_slider
68
  })
69
-
70
  st.write("We will back with your music....please enjoy doing the rest of your tasks while we come back in some time :)")
71
  st.subheader("Generated Music")
72
  music_tensors = generate_music_tensors(text_area, time_slider)
 
1
  from audiocraft.models import MusicGen
2
  import streamlit as st
 
3
  import torch
4
  import torchaudio
 
 
5
  from io import BytesIO
6
 
7
  @st.cache_resource
 
14
  print("Duration:", duration)
15
  model = load_model()
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
 
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
 
60
  "Description": text_area,
61
  "Selected duration": time_slider
62
  })
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
  st.subheader("Generated Music")
66
  music_tensors = generate_music_tensors(text_area, time_slider)