Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -60,15 +60,6 @@ html = """
|
|
60 |
<div id="output"></div>
|
61 |
|
62 |
<script>
|
63 |
-
// Function to serialize data for Streamlit
|
64 |
-
function sendToStreamlit(data) {
|
65 |
-
const serializedData = JSON.stringify(data);
|
66 |
-
window.parent.postMessage({
|
67 |
-
type: 'streamlit:setComponentValue',
|
68 |
-
value: serializedData
|
69 |
-
}, '*');
|
70 |
-
}
|
71 |
-
|
72 |
if (!('webkitSpeechRecognition' in window)) {
|
73 |
alert('Speech recognition not supported');
|
74 |
} else {
|
@@ -107,11 +98,8 @@ html = """
|
|
107 |
clearButton.onclick = () => {
|
108 |
fullTranscript = '';
|
109 |
output.textContent = '';
|
110 |
-
|
111 |
-
|
112 |
-
isFinal: true,
|
113 |
-
timestamp: new Date().toISOString()
|
114 |
-
});
|
115 |
};
|
116 |
|
117 |
recognition.onresult = (event) => {
|
@@ -132,18 +120,8 @@ html = """
|
|
132 |
if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
|
133 |
if (finalTranscript) {
|
134 |
fullTranscript += finalTranscript;
|
135 |
-
|
136 |
-
|
137 |
-
isFinal: true,
|
138 |
-
timestamp: new Date().toISOString()
|
139 |
-
});
|
140 |
-
} else if (interimTranscript) {
|
141 |
-
fullTranscript += interimTranscript + '\\n';
|
142 |
-
sendToStreamlit({
|
143 |
-
text: interimTranscript,
|
144 |
-
isFinal: false,
|
145 |
-
timestamp: new Date().toISOString()
|
146 |
-
});
|
147 |
}
|
148 |
lastUpdateTime = Date.now();
|
149 |
}
|
@@ -198,28 +176,25 @@ def save_transcript(text):
|
|
198 |
# Main app
|
199 |
st.title("Speech Recognition with Transcript History")
|
200 |
|
201 |
-
# Create custom component
|
202 |
component_value = st.components.v1.html(html, height=600)
|
203 |
|
204 |
# If we receive a new transcript
|
205 |
-
if component_value
|
206 |
-
|
207 |
-
|
208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
-
# Update the
|
211 |
-
|
212 |
-
|
213 |
-
st.session_state.transcript_history += new_text
|
214 |
-
|
215 |
-
# Save to file
|
216 |
-
save_transcript(new_text)
|
217 |
-
|
218 |
-
# Update the display
|
219 |
-
history_container.markdown(st.session_state.transcript_history)
|
220 |
-
text_area.text_area("Full Transcript", st.session_state.transcript_history, height=200)
|
221 |
-
except json.JSONDecodeError:
|
222 |
-
st.error("Error processing transcript data")
|
223 |
|
224 |
# Add a download button for the full transcript
|
225 |
if st.session_state.transcript_history:
|
|
|
60 |
<div id="output"></div>
|
61 |
|
62 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
if (!('webkitSpeechRecognition' in window)) {
|
64 |
alert('Speech recognition not supported');
|
65 |
} else {
|
|
|
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 |
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 |
}
|
|
|
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:
|