Add audio convert button
Browse files
app.py
CHANGED
@@ -103,29 +103,31 @@ def launch_bot():
|
|
103 |
|
104 |
# If assistant has most recently reaponded create audio of response
|
105 |
if st.session_state.messages[-1]["role"] == "assistant":
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
|
|
|
|
|
130 |
if __name__ == "__main__":
|
131 |
launch_bot()
|
|
|
103 |
|
104 |
# If assistant has most recently reaponded create audio of response
|
105 |
if st.session_state.messages[-1]["role"] == "assistant":
|
106 |
+
audio_result = st.button("Convert to Audio 🔊")
|
107 |
+
if audio_result:
|
108 |
+
#text-to-speech
|
109 |
+
print("Calling in Text-to-speech via suno/bark-small")
|
110 |
+
synthesiser = pipeline("text-to-speech", "suno/bark-small")
|
111 |
+
|
112 |
+
speech = synthesiser(response, forward_params={"do_sample": True})
|
113 |
+
|
114 |
+
scipy.io.wavfile.write("bark_out.wav", rate=speech["sampling_rate"], data=speech["audio"])
|
115 |
+
# ST interface for audio
|
116 |
+
print("Now we try to display the audio file in the app")
|
117 |
+
audio_file = open('bark_out.wav', 'rb')
|
118 |
+
audio_bytes = audio_file.read()
|
119 |
+
|
120 |
+
st.audio(audio_bytes, format='audio/wav')
|
121 |
+
|
122 |
+
sample_rate = 44100 # 44100 samples per second
|
123 |
+
seconds = 2 # Note duration of 2 seconds
|
124 |
+
frequency_la = 440 # Our played note will be 440 Hz
|
125 |
+
# Generate array with seconds*sample_rate steps, ranging between 0 and seconds
|
126 |
+
t = np.linspace(0, seconds, seconds * sample_rate, False)
|
127 |
+
# Generate a 440 Hz sine wave
|
128 |
+
note_la = np.sin(frequency_la * t * 2 * np.pi)
|
129 |
|
130 |
+
st.audio(note_la, sample_rate=sample_rate)
|
131 |
+
|
132 |
if __name__ == "__main__":
|
133 |
launch_bot()
|