Khaled21 commited on
Commit
88dae99
1 Parent(s): d5d708e

Changing Media view logic

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
  import scipy.io.wavfile
4
- import tempfile
5
  import numpy as np
 
6
 
7
  # -----------------------------------------------------------
8
  def load_css(file_path):
@@ -31,21 +31,19 @@ def main():
31
  # Generate music with the synthesizer
32
  music = synthesizer(user_text, forward_params={"do_sample": True, "max_new_tokens": 500})
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}")
 
1
  import streamlit as st
2
  from transformers import pipeline
3
  import scipy.io.wavfile
 
4
  import numpy as np
5
+ import io # Import the io module for BytesIO
6
 
7
  # -----------------------------------------------------------
8
  def load_css(file_path):
 
31
  # Generate music with the synthesizer
32
  music = synthesizer(user_text, forward_params={"do_sample": True, "max_new_tokens": 500})
33
 
34
+ # Check if the audio data needs scaling and convert to int16
35
+ if music["audio"].dtype != np.int16:
36
+ audio_data = np.int16(music["audio"] / np.max(np.abs(music["audio"])) * 32767)
37
+ else:
38
+ audio_data = music["audio"]
39
+
40
+ # Instead of writing to a temp file, write the audio data to a BytesIO object
41
+ audio_bytes_io = io.BytesIO()
42
+ scipy.io.wavfile.write(audio_bytes_io, rate=music["sampling_rate"], data=audio_data)
43
+ audio_bytes_io.seek(0) # Go to the beginning of the BytesIO object
44
+
45
+ # Use Streamlit's audio widget to display the audio player with the BytesIO object
46
+ st.audio(audio_bytes_io, format="audio/wav")
 
 
47
 
48
  except Exception as e:
49
  st.error(f"An error occurred: {e}")