File size: 1,882 Bytes
0455eb3
8ad7a67
 
 
2691203
8ad7a67
2691203
8ad7a67
 
0455eb3
8ad7a67
 
 
 
 
 
0455eb3
 
8ad7a67
0455eb3
8ad7a67
 
0455eb3
8ad7a67
 
 
 
0455eb3
2691203
 
 
8ad7a67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2691203
 
0455eb3
8ad7a67
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
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.")