File size: 2,457 Bytes
2ce9600 60ab783 263d442 d04894f 2ce9600 263d442 2ce9600 263d442 2ce9600 569b020 47a10c9 569b020 47a10c9 de1cd95 47a10c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import streamlit as st
from gtts import gTTS
import os
# Streamlit app UI
st.title("Text-to-Audio App")
st.text("This app converts your text input into audio using TTS.")
# User input
text_input = st.text_area("Enter some text:")
if st.button("Generate Audio"):
if not text_input.strip():
st.error("Please enter some text!")
else:
try:
# Generate speech using gTTS
tts = gTTS(text=text_input, lang="en")
audio_file = "output.wav"
tts.save(audio_file)
# Check if file exists
if os.path.exists(audio_file):
# JavaScript-based audio player with speed adjustment
audio_data = open(audio_file, "rb").read()
audio_base64 = f"data:audio/wav;base64,{st.base64.b64encode(audio_data).decode()}"
st.markdown(
f"""
<audio id="audio" controls>
<source src="{audio_base64}" type="audio/wav">
Your browser does not support the audio element.
</audio>
<br>
<label for="speed">Playback Speed: </label>
<input type="range" id="speed" min="0.5" max="2.0" value="1.0" step="0.1" onchange="document.getElementById('audio').playbackRate = this.value;">
<span id="speed-value">1.0x</span>
<script>
const speedSlider = document.getElementById("speed");
const speedValue = document.getElementById("speed-value");
speedSlider.addEventListener("input", () => {{
speedValue.textContent = speedSlider.value + "x";
}});
</script>
""",
unsafe_allow_html=True,
)
st.success("Audio generated successfully!")
# Provide download option
with open(audio_file, "rb") as f:
st.download_button(
label="Download Audio",
data=f.read(),
file_name="output.wav",
mime="audio/wav",
)
else:
st.error("Audio file could not be generated.")
except Exception as e:
st.error(f"An error occurred: {e}")
|