Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
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:
|
@@ -59,6 +60,15 @@ html = """
|
|
59 |
<div id="output"></div>
|
60 |
|
61 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
if (!('webkitSpeechRecognition' in window)) {
|
63 |
alert('Speech recognition not supported');
|
64 |
} else {
|
@@ -71,22 +81,6 @@ html = """
|
|
71 |
let fullTranscript = '';
|
72 |
let lastUpdateTime = Date.now();
|
73 |
|
74 |
-
// Function to update Streamlit
|
75 |
-
function updateStreamlit(text, isFinal) {
|
76 |
-
// Create data object
|
77 |
-
const data = {
|
78 |
-
text: text,
|
79 |
-
isFinal: isFinal,
|
80 |
-
timestamp: new Date().toISOString()
|
81 |
-
};
|
82 |
-
|
83 |
-
// Send to Streamlit using the Streamlit component API
|
84 |
-
window.parent.postMessage({
|
85 |
-
type: 'streamlit:setComponentValue',
|
86 |
-
value: data
|
87 |
-
}, '*');
|
88 |
-
}
|
89 |
-
|
90 |
// Configure recognition
|
91 |
recognition.continuous = true;
|
92 |
recognition.interimResults = true;
|
@@ -113,7 +107,11 @@ html = """
|
|
113 |
clearButton.onclick = () => {
|
114 |
fullTranscript = '';
|
115 |
output.textContent = '';
|
116 |
-
|
|
|
|
|
|
|
|
|
117 |
};
|
118 |
|
119 |
recognition.onresult = (event) => {
|
@@ -134,10 +132,18 @@ html = """
|
|
134 |
if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
|
135 |
if (finalTranscript) {
|
136 |
fullTranscript += finalTranscript;
|
137 |
-
|
|
|
|
|
|
|
|
|
138 |
} else if (interimTranscript) {
|
139 |
fullTranscript += interimTranscript + '\\n';
|
140 |
-
|
|
|
|
|
|
|
|
|
141 |
}
|
142 |
lastUpdateTime = Date.now();
|
143 |
}
|
@@ -150,7 +156,6 @@ html = """
|
|
150 |
};
|
151 |
|
152 |
recognition.onend = () => {
|
153 |
-
// Automatically restart if not manually stopped
|
154 |
if (!stopButton.disabled) {
|
155 |
try {
|
156 |
recognition.start();
|
@@ -168,7 +173,6 @@ html = """
|
|
168 |
console.error('Recognition error:', event.error);
|
169 |
status.textContent = 'Error: ' + event.error;
|
170 |
|
171 |
-
// Only reset buttons if it's a fatal error
|
172 |
if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
|
173 |
startButton.disabled = false;
|
174 |
stopButton.disabled = true;
|
@@ -180,11 +184,6 @@ html = """
|
|
180 |
</html>
|
181 |
"""
|
182 |
|
183 |
-
# Create custom component
|
184 |
-
def speech_recognition():
|
185 |
-
component_value = st.components.v1.html(html, height=600)
|
186 |
-
return component_value
|
187 |
-
|
188 |
# Function to save transcript to file
|
189 |
def save_transcript(text):
|
190 |
if not os.path.exists('transcripts'):
|
@@ -199,24 +198,28 @@ def save_transcript(text):
|
|
199 |
# Main app
|
200 |
st.title("Speech Recognition with Transcript History")
|
201 |
|
202 |
-
#
|
203 |
-
|
204 |
|
205 |
# If we receive a new transcript
|
206 |
-
if
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
if transcript_data.get('isFinal', False):
|
211 |
-
new_text = transcript_data.get('text', '')
|
212 |
-
st.session_state.transcript_history += new_text
|
213 |
-
|
214 |
-
# Save to file
|
215 |
-
save_transcript(new_text)
|
216 |
|
217 |
-
# Update the
|
218 |
-
|
219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
|
221 |
# Add a download button for the full transcript
|
222 |
if st.session_state.transcript_history:
|
|
|
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:
|
|
|
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 {
|
|
|
81 |
let fullTranscript = '';
|
82 |
let lastUpdateTime = Date.now();
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
// Configure recognition
|
85 |
recognition.continuous = true;
|
86 |
recognition.interimResults = true;
|
|
|
107 |
clearButton.onclick = () => {
|
108 |
fullTranscript = '';
|
109 |
output.textContent = '';
|
110 |
+
sendToStreamlit({
|
111 |
+
text: '',
|
112 |
+
isFinal: true,
|
113 |
+
timestamp: new Date().toISOString()
|
114 |
+
});
|
115 |
};
|
116 |
|
117 |
recognition.onresult = (event) => {
|
|
|
132 |
if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
|
133 |
if (finalTranscript) {
|
134 |
fullTranscript += finalTranscript;
|
135 |
+
sendToStreamlit({
|
136 |
+
text: finalTranscript,
|
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 |
}
|
|
|
156 |
};
|
157 |
|
158 |
recognition.onend = () => {
|
|
|
159 |
if (!stopButton.disabled) {
|
160 |
try {
|
161 |
recognition.start();
|
|
|
173 |
console.error('Recognition error:', event.error);
|
174 |
status.textContent = 'Error: ' + event.error;
|
175 |
|
|
|
176 |
if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
|
177 |
startButton.disabled = false;
|
178 |
stopButton.disabled = true;
|
|
|
184 |
</html>
|
185 |
"""
|
186 |
|
|
|
|
|
|
|
|
|
|
|
187 |
# Function to save transcript to file
|
188 |
def save_transcript(text):
|
189 |
if not os.path.exists('transcripts'):
|
|
|
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 is not None:
|
206 |
+
try:
|
207 |
+
# Parse the JSON string
|
208 |
+
transcript_data = json.loads(component_value)
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
+
# Update the transcript history if it's a final transcript
|
211 |
+
if transcript_data['isFinal']:
|
212 |
+
new_text = transcript_data['text']
|
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:
|