awacke1 commited on
Commit
916759b
·
verified ·
1 Parent(s): 972ecb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import streamlit as st
2
  import datetime
3
  import os
4
- import json
5
 
6
  # Initialize session state for transcript history if not exists
7
  if 'transcript_history' not in st.session_state:
@@ -16,6 +15,7 @@ html = """
16
  <html>
17
  <head>
18
  <title>Continuous Speech Demo</title>
 
19
  <style>
20
  body {
21
  font-family: sans-serif;
@@ -60,6 +60,9 @@ html = """
60
  <div id="output"></div>
61
 
62
  <script>
 
 
 
63
  if (!('webkitSpeechRecognition' in window)) {
64
  alert('Speech recognition not supported');
65
  } else {
@@ -98,8 +101,7 @@ html = """
98
  clearButton.onclick = () => {
99
  fullTranscript = '';
100
  output.textContent = '';
101
- // Send empty string to Streamlit
102
- window.Streamlit.setComponentValue('');
103
  };
104
 
105
  recognition.onresult = (event) => {
@@ -120,8 +122,8 @@ html = """
120
  if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
121
  if (finalTranscript) {
122
  fullTranscript += finalTranscript;
123
- // Send final transcript to Streamlit
124
- window.Streamlit.setComponentValue(finalTranscript);
125
  }
126
  lastUpdateTime = Date.now();
127
  }
@@ -157,6 +159,9 @@ html = """
157
  }
158
  };
159
  }
 
 
 
160
  </script>
161
  </body>
162
  </html>
@@ -176,25 +181,25 @@ def save_transcript(text):
176
  # Main app
177
  st.title("Speech Recognition with Transcript History")
178
 
179
- # Create custom component and get its value
180
- component_value = st.components.v1.html(html, height=600)
 
 
 
 
181
 
182
- # If we receive a new transcript
183
- if component_value:
184
- # The component_value will be the direct transcript text
185
  new_text = str(component_value)
 
 
 
 
186
 
187
- # Only process non-empty transcripts
188
- if new_text.strip():
189
- # Update the transcript history
190
- st.session_state.transcript_history += new_text
191
-
192
- # Save to file
193
- save_transcript(new_text)
194
-
195
- # Update the display
196
- history_container.markdown(st.session_state.transcript_history)
197
- text_area.text_area("Full Transcript", st.session_state.transcript_history, height=200)
198
 
199
  # Add a download button for the full transcript
200
  if st.session_state.transcript_history:
 
1
  import streamlit as st
2
  import datetime
3
  import os
 
4
 
5
  # Initialize session state for transcript history if not exists
6
  if 'transcript_history' not in st.session_state:
 
15
  <html>
16
  <head>
17
  <title>Continuous Speech Demo</title>
18
+ <script src="https://streamlit.io/releases/latest/streamlit.js"></script>
19
  <style>
20
  body {
21
  font-family: sans-serif;
 
60
  <div id="output"></div>
61
 
62
  <script>
63
+ // Initialize Streamlit
64
+ const streamlit = new Streamlit.SharedObject();
65
+
66
  if (!('webkitSpeechRecognition' in window)) {
67
  alert('Speech recognition not supported');
68
  } else {
 
101
  clearButton.onclick = () => {
102
  fullTranscript = '';
103
  output.textContent = '';
104
+ Streamlit.setComponentValue("");
 
105
  };
106
 
107
  recognition.onresult = (event) => {
 
122
  if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
123
  if (finalTranscript) {
124
  fullTranscript += finalTranscript;
125
+ // Send to Streamlit
126
+ Streamlit.setComponentValue(finalTranscript);
127
  }
128
  lastUpdateTime = Date.now();
129
  }
 
159
  }
160
  };
161
  }
162
+
163
+ // Initialize the component
164
+ Streamlit.setComponentReady();
165
  </script>
166
  </body>
167
  </html>
 
181
  # Main app
182
  st.title("Speech Recognition with Transcript History")
183
 
184
+ # Create custom component with key
185
+ component_value = st.components.v1.html(
186
+ html,
187
+ height=600,
188
+ key="speech_recognition"
189
+ )
190
 
191
+ # Handle component value
192
+ if component_value != "":
193
+ # Update the transcript history
194
  new_text = str(component_value)
195
+ st.session_state.transcript_history += new_text
196
+
197
+ # Save to file
198
+ save_transcript(new_text)
199
 
200
+ # Update the display
201
+ history_container.markdown(st.session_state.transcript_history)
202
+ text_area.text_area("Full Transcript", st.session_state.transcript_history, height=200)
 
 
 
 
 
 
 
 
203
 
204
  # Add a download button for the full transcript
205
  if st.session_state.transcript_history: