Changing Media view logic
Browse files
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 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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}")
|