Skip to content

Commit bdfa35e

Browse files
authored
Download results through browser (#87)
* Use download button * Add function to convert numpy array to .wav binary * Update from main
1 parent f933916 commit bdfa35e

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

demo/app.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import re
44
from pathlib import Path
5+
import io
56

7+
import numpy as np
68
import soundfile as sf
79
import streamlit as st
810

@@ -29,6 +31,16 @@ def load_text_to_speech_model():
2931
return load_tts_model("OuteAI/OuteTTS-0.2-500M-GGUF/OuteTTS-0.2-500M-FP16.gguf")
3032

3133

34+
def numpy_to_wav(audio_array: np.ndarray, sample_rate: int) -> io.BytesIO:
35+
"""
36+
Convert a numpy array to audio bytes in .wav format, ready to save into a file.
37+
"""
38+
wav_io = io.BytesIO()
39+
sf.write(wav_io, audio_array, sample_rate, format="WAV")
40+
wav_io.seek(0)
41+
return wav_io
42+
43+
3244
script = "script"
3345
audio = "audio"
3446
gen_button = "generate podcast button"
@@ -171,21 +183,23 @@ def gen_button_clicked():
171183

172184
st.session_state.audio.append(speech)
173185
text = ""
186+
st.session_state.script += "}"
174187

175188
if st.session_state[gen_button]:
176-
if st.button("Save Podcast to audio file"):
177-
st.session_state.audio = stack_audio_segments(
178-
st.session_state.audio, speech_model.sample_rate
179-
)
180-
sf.write(
181-
"podcast.wav",
182-
st.session_state.audio,
183-
samplerate=speech_model.sample_rate,
184-
)
189+
audio_np = stack_audio_segments(
190+
st.session_state.audio, speech_model.sample_rate
191+
)
192+
audio_wav = numpy_to_wav(audio_np, speech_model.sample_rate)
193+
if st.download_button(
194+
label="Save Podcast to audio file",
195+
data=audio_wav,
196+
file_name="podcast.wav",
197+
):
185198
st.markdown("Podcast saved to disk!")
186199

187-
if st.button("Save Podcast script to text file"):
188-
with open("script.txt", "w") as f:
189-
st.session_state.script += "}"
190-
f.write(st.session_state.script)
200+
if st.download_button(
201+
label="Save Podcast script to text file",
202+
data=st.session_state.script,
203+
file_name="script.txt",
204+
):
191205
st.markdown("Script saved to disk!")

0 commit comments

Comments
 (0)