Khaled21 commited on
Commit
c0cb2dc
·
1 Parent(s): 536ca29

Audio display

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -4,50 +4,51 @@ import scipy.io.wavfile
4
  import tempfile
5
  import numpy as np
6
 
7
-
8
  # -----------------------------------------------------------
9
  def load_css(file_path):
10
  with open(file_path) as f:
11
  css_to_load = f.read()
12
  st.markdown(f"<style>{css_to_load}</style>", unsafe_allow_html=True)
13
 
14
-
15
  # -----------------------------------------------------------
16
  def load_model():
 
17
  return pipeline("text-to-audio", model="facebook/musicgen-small")
18
 
19
-
20
  # -----------------------------------------------------------
21
  def main():
22
  try:
23
- st.set_page_config(page_title="Text to Music", page_icon="Music")
24
  load_css("styles/styles.css")
25
  st.title("Turn Your Text Into Music")
26
 
 
27
  synthesizer = load_model()
28
  user_text = st.text_area("What type of beat would you like to hear: ")
29
 
 
30
  if user_text:
 
31
  music = synthesizer(user_text, forward_params={"do_sample": True})
32
 
 
33
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
34
  print(f"Temporary file created at: {tmp.name}")
35
- # Ensure the audio data is in the correct format (int16 for WAV)
 
36
  if music["audio"].dtype != np.int16:
37
- # Assuming the audio is normalized between -1 and 1, scale to int16
38
  audio_data = np.int16(music["audio"] / np.max(np.abs(music["audio"])) * 32767)
39
  else:
40
  audio_data = music["audio"]
41
 
42
- # Write the audio data to the temporary file
43
  scipy.io.wavfile.write(tmp.name, rate=music["sampling_rate"], data=audio_data)
44
 
45
- # Display the audio player in Streamlit
46
- st.audio(tmp.name, format="audio/wav", start_time=0)
47
 
48
  except Exception as e:
49
- st.error(e)
50
-
51
 
52
  # -----------------------------------------------------------
53
 
 
4
  import tempfile
5
  import numpy as np
6
 
 
7
  # -----------------------------------------------------------
8
  def load_css(file_path):
9
  with open(file_path) as f:
10
  css_to_load = f.read()
11
  st.markdown(f"<style>{css_to_load}</style>", unsafe_allow_html=True)
12
 
 
13
  # -----------------------------------------------------------
14
  def load_model():
15
+ # Initializes and returns the pipeline for text-to-audio generation
16
  return pipeline("text-to-audio", model="facebook/musicgen-small")
17
 
 
18
  # -----------------------------------------------------------
19
  def main():
20
  try:
21
+ st.set_page_config(page_title="Text to Music", page_icon="🎵")
22
  load_css("styles/styles.css")
23
  st.title("Turn Your Text Into Music")
24
 
25
+ # Load the model
26
  synthesizer = load_model()
27
  user_text = st.text_area("What type of beat would you like to hear: ")
28
 
29
+ # Generate and play music based on user input
30
  if user_text:
31
+ # Generate music with the synthesizer
32
  music = synthesizer(user_text, forward_params={"do_sample": True})
33
 
34
+ # Create a temporary file to store the generated audio
35
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
36
  print(f"Temporary file created at: {tmp.name}")
37
+
38
+ # Check if the audio data needs scaling and convert to int16
39
  if music["audio"].dtype != np.int16:
 
40
  audio_data = np.int16(music["audio"] / np.max(np.abs(music["audio"])) * 32767)
41
  else:
42
  audio_data = music["audio"]
43
 
44
+ # Write the scaled audio data to the temporary WAV file
45
  scipy.io.wavfile.write(tmp.name, rate=music["sampling_rate"], data=audio_data)
46
 
47
+ # Use Streamlit's audio widget to display the audio player
48
+ st.audio(tmp.name, format="audio/wav")
49
 
50
  except Exception as e:
51
+ st.error(f"An error occurred: {e}")
 
52
 
53
  # -----------------------------------------------------------
54