amkj84 commited on
Commit
23c16ef
·
verified ·
1 Parent(s): e8d0f61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -35
app.py CHANGED
@@ -1,49 +1,31 @@
1
  import streamlit as st
2
- from pynput import keyboard
3
- import transformers
4
- from transformers import WhisperFeatureExtractor, WhisperForConditionalGeneration
5
- import soundfile as sf
6
 
7
  # Define voice, speed, and pitch variables (initial values)
8
  voice = "en" # English (change for other voices)
9
  speed = 1.0
10
  pitch = 1.0
11
 
12
- # Initialize feature extractor and model from Hugging Face Transformers
13
- feature_extractor = WhisperFeatureExtractor.from_pretrained("openai/whisper")
14
- model = WhisperForConditionalGeneration.from_pretrained("openai/whisper")
15
-
16
-
17
- def on_press(key):
18
- global voice, speed, pitch
19
-
20
- # Handle special keys (consider modifications for your needs)
21
- if key == keyboard.Key.esc:
22
- return False # Exit keystroke listener
23
-
24
  pressed_key = str(key).replace("'", "")
 
25
 
26
- # Text pre-processing (optional, customize for specific needs)
27
- text = f"{pressed_key}"
28
-
29
- # Encode text
30
- inputs = feature_extractor(text, return_tensors="pt")
31
-
32
- # Generate speech tokens with adjusted voice, speed, and pitch
33
- generation = model.generate(
34
- **inputs, voice=voice, speed=speed, pitch=pitch
35
- )
36
 
37
- # Convert generated tokens to audio waveform
38
- audio_output = model.to_audio(generation)
39
 
40
- # Play the audio (replace with your preferred audio playback library)
41
- sf.write("output.wav", audio_output, samplerate=16000)
42
- st.audio("output.wav", format="audio/wav")
43
 
 
44
 
45
  # Streamlit App
46
- st.title("Text-to-Speech Keystroke Announcer")
47
 
48
  # User Interface for customization options
49
  voice_selected = st.selectbox("Voice", ["en", "fr", "es"]) # Add more options
@@ -57,7 +39,7 @@ pitch = pitch_slider
57
 
58
  # Start keystroke listener on button press
59
  if st.button("Start Keystroke Announcer"):
60
- with keyboard.Listener(on_press=on_press) as listener:
61
- listener.join()
62
 
63
- st.write("Press 'Esc' to stop keystroke detection.")
 
1
  import streamlit as st
2
+ from keyboard import on_press, wait
 
 
 
3
 
4
  # Define voice, speed, and pitch variables (initial values)
5
  voice = "en" # English (change for other voices)
6
  speed = 1.0
7
  pitch = 1.0
8
 
9
+ # (Optional) Text pre-processing function (customize for specific needs)
10
+ def preprocess_text(key):
 
 
 
 
 
 
 
 
 
 
11
  pressed_key = str(key).replace("'", "")
12
+ return pressed_key
13
 
14
+ # Function to handle key presses and trigger text-to-speech
15
+ def on_press_wrapper(key):
16
+ global voice, speed, pitch
 
 
 
 
 
 
 
17
 
18
+ # Call pre-processing function (if implemented)
19
+ text = preprocess_text(key)
20
 
21
+ # Simulate text input to a text-to-speech API (replace with your chosen API)
22
+ # This example demonstrates a basic placeholder
23
+ print(f"Simulating text input to TTS API: {text} (Voice: {voice}, Speed: {speed}, Pitch: {pitch})")
24
 
25
+ # You'll need to integrate your chosen text-to-speech API here
26
 
27
  # Streamlit App
28
+ st.title("Text-to-Speech Keystroke Announcer (Headless)")
29
 
30
  # User Interface for customization options
31
  voice_selected = st.selectbox("Voice", ["en", "fr", "es"]) # Add more options
 
39
 
40
  # Start keystroke listener on button press
41
  if st.button("Start Keystroke Announcer"):
42
+ on_press(on_press_wrapper)
43
+ wait() # Block execution until the listener is stopped
44
 
45
+ st.write("Press 'Esc' to stop keystroke detection (simulated).")