iisadia commited on
Commit
dacb1ad
·
verified ·
1 Parent(s): 105da9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -16
app.py CHANGED
@@ -5,6 +5,7 @@ import torchaudio
5
  from audio_recorder_streamlit import audio_recorder
6
  import torch
7
  from io import BytesIO
 
8
 
9
  # Load Whisper model (cached)
10
  @st.cache_resource
@@ -25,11 +26,13 @@ def process_audio(audio_bytes):
25
  st.title("Real-Time Voice Typing")
26
  st.write("Type or speak - text will appear instantly!")
27
 
28
- # Initialize text in session state
29
  if 'text_input' not in st.session_state:
30
  st.session_state.text_input = ""
 
 
31
 
32
- # Main text area (auto-updates from session state)
33
  text_input = st.text_area(
34
  "Your text will appear here:",
35
  value=st.session_state.text_input,
@@ -39,32 +42,38 @@ text_input = st.text_area(
39
 
40
  # Audio recorder component
41
  audio_bytes = audio_recorder(
42
- pause_threshold=2.0, # Stop after 2 seconds of silence
43
  text="Speak to type",
44
  recording_color="#e8b62c",
45
  neutral_color="#6aa36f",
46
  )
47
 
48
- # Process audio in real-time
49
  if audio_bytes:
50
- try:
51
- audio_input = process_audio(audio_bytes)
52
- whisper = load_model()
53
- transcribed_text = whisper(audio_input)["text"]
54
-
55
- # Append new transcription to existing text
56
- st.session_state.text_input = st.session_state.text_input + " " + transcribed_text
57
- st.rerun() # Updated to use st.rerun() instead of experimental_rerun()
58
-
59
- except Exception as e:
60
- st.error(f"Error: {str(e)}")
 
 
 
 
 
61
 
62
  # Control buttons
63
  col1, col2 = st.columns(2)
64
  with col1:
65
  if st.button("Clear Text"):
66
  st.session_state.text_input = ""
67
- st.rerun() # Updated here as well
 
68
  with col2:
69
  st.download_button(
70
  "Download Text",
 
5
  from audio_recorder_streamlit import audio_recorder
6
  import torch
7
  from io import BytesIO
8
+ import hashlib
9
 
10
  # Load Whisper model (cached)
11
  @st.cache_resource
 
26
  st.title("Real-Time Voice Typing")
27
  st.write("Type or speak - text will appear instantly!")
28
 
29
+ # Initialize session state
30
  if 'text_input' not in st.session_state:
31
  st.session_state.text_input = ""
32
+ if 'last_audio_hash' not in st.session_state:
33
+ st.session_state.last_audio_hash = ""
34
 
35
+ # Main text area
36
  text_input = st.text_area(
37
  "Your text will appear here:",
38
  value=st.session_state.text_input,
 
42
 
43
  # Audio recorder component
44
  audio_bytes = audio_recorder(
45
+ pause_threshold=1.5, # Shorter pause threshold
46
  text="Speak to type",
47
  recording_color="#e8b62c",
48
  neutral_color="#6aa36f",
49
  )
50
 
51
+ # Process new audio only if it's different from last time
52
  if audio_bytes:
53
+ current_hash = hashlib.md5(audio_bytes).hexdigest()
54
+ if current_hash != st.session_state.last_audio_hash:
55
+ st.session_state.last_audio_hash = current_hash
56
+ try:
57
+ audio_input = process_audio(audio_bytes)
58
+ whisper = load_model()
59
+ transcribed_text = whisper(audio_input)["text"]
60
+
61
+ # Append new transcription only if different
62
+ if (not st.session_state.text_input.endswith(transcribed_text.strip()) and
63
+ len(transcribed_text.strip()) > 0):
64
+ st.session_state.text_input += " " + transcribed_text
65
+ st.rerun()
66
+
67
+ except Exception as e:
68
+ st.error(f"Error: {str(e)}")
69
 
70
  # Control buttons
71
  col1, col2 = st.columns(2)
72
  with col1:
73
  if st.button("Clear Text"):
74
  st.session_state.text_input = ""
75
+ st.session_state.last_audio_hash = ""
76
+ st.rerun()
77
  with col2:
78
  st.download_button(
79
  "Download Text",