bravewiki commited on
Commit
8ad7a67
·
verified ·
1 Parent(s): c615de0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -1,49 +1,52 @@
1
  import streamlit as st
 
 
 
2
 
3
- # Text input for user to type keystrokes
4
- key_input = st.text_input("Enter Keystroke:", key="")
5
-
6
- # Define voice, speed, and pitch variables (initial values)
7
- voice = "en" # English (change for other voices)
8
- speed = 1.0
9
- pitch = 1.0
10
-
11
- # (Optional) Text pre-processing function (customize for specific needs)
12
  def preprocess_text(text):
13
- # Implement your desired text cleaning or modification logic here
14
- return text
15
-
16
- # Function to simulate text input to a text-to-speech API
17
- def process_text(text):
18
- global voice, speed, pitch
19
-
20
- # Call pre-processing function (if implemented)
21
- preprocessed_text = preprocess_text(text)
22
 
23
- # Simulate text input to a text-to-speech API (replace with your chosen API)
24
- # This example demonstrates a basic placeholder
25
- print(f"Simulating text input to TTS API: {preprocessed_text} (Voice: {voice}, Speed: {speed}, Pitch: {pitch})")
26
-
27
- # You'll need to integrate your chosen text-to-speech API here
 
28
 
29
  # Streamlit App
30
- st.title("Text-to-Speech Announcer (Headless)")
31
 
32
- # User Interface for customization options
33
- voice_selected = st.selectbox("Voice", ["en", "fr", "es"]) # Add more options
34
- speed_slider = st.slider("Speaking Speed", min_value=0.5, max_value=2.0, value=1.0)
35
- pitch_slider = st.slider("Speaking Pitch", min_value=0.5, max_value=2.0, value=1.0)
36
 
37
- # Update variables based on user selections
38
- voice = voice_selected
39
- speed = speed_slider
40
- pitch = pitch_slider
41
 
42
  # Button to trigger processing of entered text
43
  if st.button("Announce Keystroke"):
44
  if key_input:
45
- process_text(key_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  else:
47
  st.error("Please enter a keystroke to announce.")
48
 
49
- st.write("Enter a keystroke in the text box to synthesize speech.")
 
1
  import streamlit as st
2
+ from gtts import gTTS
3
+ import os
4
+ import base64
5
 
6
+ # Helper function to preprocess text (if needed)
 
 
 
 
 
 
 
 
7
  def preprocess_text(text):
8
+ # Implement any desired text cleaning or formatting here
9
+ return text.strip()
 
 
 
 
 
 
 
10
 
11
+ # Helper function to convert text to speech using gTTS
12
+ def text_to_speech(text, language):
13
+ tts = gTTS(text=text, lang=language, slow=False)
14
+ file_path = "output.mp3"
15
+ tts.save(file_path)
16
+ return file_path
17
 
18
  # Streamlit App
19
+ st.title("Text-to-Speech Announcer (Powered by gTTS)")
20
 
21
+ # Text input for user to type keystrokes
22
+ key_input = st.text_input("Enter Keystroke:", key="")
 
 
23
 
24
+ # User Interface for customization options
25
+ voice_selected = st.selectbox("Language", ["en", "fr", "es"]) # Language codes for gTTS
26
+ speed = st.slider("Speaking Speed (placeholder)", min_value=0.5, max_value=2.0, value=1.0)
27
+ pitch = st.slider("Speaking Pitch (placeholder)", min_value=0.5, max_value=2.0, value=1.0)
28
 
29
  # Button to trigger processing of entered text
30
  if st.button("Announce Keystroke"):
31
  if key_input:
32
+ processed_text = preprocess_text(key_input)
33
+ audio_file = text_to_speech(processed_text, voice_selected)
34
+
35
+ # Play the audio file in Streamlit
36
+ with open(audio_file, "rb") as file:
37
+ audio_bytes = file.read()
38
+ b64_audio = base64.b64encode(audio_bytes).decode()
39
+ audio_player = f"""
40
+ <audio controls>
41
+ <source src="data:audio/mp3;base64,{b64_audio}" type="audio/mp3">
42
+ Your browser does not support the audio element.
43
+ </audio>
44
+ """
45
+ st.markdown(audio_player, unsafe_allow_html=True)
46
+
47
+ # Clean up the temporary file
48
+ os.remove(audio_file)
49
  else:
50
  st.error("Please enter a keystroke to announce.")
51
 
52
+ st.write("Enter a keystroke in the text box to synthesize speech.")