Spaces:
Sleeping
Sleeping
import streamlit as st | |
from gtts import gTTS | |
import os | |
import base64 | |
# Helper function to preprocess text (if needed) | |
def preprocess_text(text): | |
# Implement any desired text cleaning or formatting here | |
return text.strip() | |
# Helper function to convert text to speech using gTTS | |
def text_to_speech(text, language): | |
tts = gTTS(text=text, lang=language, slow=False) | |
file_path = "output.mp3" | |
tts.save(file_path) | |
return file_path | |
# Streamlit App | |
st.title("Text-to-Speech Announcer (Powered by gTTS)") | |
# Text input for user to type keystrokes | |
key_input = st.text_input("Enter Keystroke:", key="") | |
# User Interface for customization options | |
voice_selected = st.selectbox("Language", ["en", "fr", "es"]) # Language codes for gTTS | |
speed = st.slider("Speaking Speed (placeholder)", min_value=0.5, max_value=2.0, value=1.0) | |
pitch = st.slider("Speaking Pitch (placeholder)", min_value=0.5, max_value=2.0, value=1.0) | |
# Button to trigger processing of entered text | |
if st.button("Announce Keystroke"): | |
if key_input: | |
processed_text = preprocess_text(key_input) | |
audio_file = text_to_speech(processed_text, voice_selected) | |
# Play the audio file in Streamlit | |
with open(audio_file, "rb") as file: | |
audio_bytes = file.read() | |
b64_audio = base64.b64encode(audio_bytes).decode() | |
audio_player = f""" | |
<audio controls> | |
<source src="data:audio/mp3;base64,{b64_audio}" type="audio/mp3"> | |
Your browser does not support the audio element. | |
</audio> | |
""" | |
st.markdown(audio_player, unsafe_allow_html=True) | |
# Clean up the temporary file | |
os.remove(audio_file) | |
else: | |
st.error("Please enter a keystroke to announce.") | |
st.write("Enter a keystroke in the text box to synthesize speech.") | |