Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,23 @@
|
|
1 |
import streamlit as st
|
2 |
import datetime
|
3 |
import os
|
|
|
4 |
|
5 |
# Initialize session state variables
|
6 |
if 'transcript_history' not in st.session_state:
|
7 |
st.session_state.transcript_history = []
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
11 |
|
12 |
# Create the main layout
|
|
|
|
|
13 |
col1, col2 = st.columns([2, 1])
|
14 |
|
15 |
with col1:
|
16 |
-
# Speech recognition component
|
17 |
html = """
|
18 |
<!DOCTYPE html>
|
19 |
<html>
|
@@ -78,7 +82,8 @@ with col1:
|
|
78 |
recognition.continuous = true;
|
79 |
recognition.interimResults = true;
|
80 |
|
81 |
-
|
|
|
82 |
try {
|
83 |
recognition.start();
|
84 |
status.textContent = 'Listening...';
|
@@ -90,6 +95,13 @@ with col1:
|
|
90 |
}
|
91 |
};
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
stopButton.onclick = () => {
|
94 |
recognition.stop();
|
95 |
status.textContent = 'Stopped';
|
@@ -100,6 +112,8 @@ with col1:
|
|
100 |
clearButton.onclick = () => {
|
101 |
fullTranscript = '';
|
102 |
output.textContent = '';
|
|
|
|
|
103 |
};
|
104 |
|
105 |
recognition.onresult = (event) => {
|
@@ -118,15 +132,11 @@ with col1:
|
|
118 |
if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
|
119 |
if (finalTranscript) {
|
120 |
fullTranscript += finalTranscript;
|
121 |
-
//
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
}, '*');
|
127 |
-
} catch (e) {
|
128 |
-
console.error('Failed to send transcript:', e);
|
129 |
-
}
|
130 |
}
|
131 |
lastUpdateTime = Date.now();
|
132 |
}
|
@@ -158,52 +168,63 @@ with col1:
|
|
158 |
stopButton.disabled = true;
|
159 |
}
|
160 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
}
|
162 |
</script>
|
163 |
</body>
|
164 |
</html>
|
165 |
"""
|
166 |
-
|
|
|
|
|
167 |
|
168 |
with col2:
|
169 |
# Display transcript history
|
170 |
st.subheader("Transcript History")
|
171 |
-
transcript_text = st.empty()
|
172 |
|
173 |
-
#
|
174 |
def save_transcript(text):
|
175 |
if not os.path.exists('transcripts'):
|
176 |
os.makedirs('transcripts')
|
177 |
-
|
178 |
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
179 |
filename = f"transcripts/transcript_{timestamp}.md"
|
180 |
-
|
181 |
with open(filename, 'w', encoding='utf-8') as f:
|
182 |
f.write(text)
|
|
|
183 |
|
184 |
-
# Display
|
185 |
if st.session_state.transcript_history:
|
186 |
full_transcript = "\n".join(st.session_state.transcript_history)
|
187 |
-
|
|
|
|
|
|
|
|
|
188 |
|
189 |
-
#
|
190 |
-
|
191 |
-
with col1:
|
192 |
-
if st.button("Save Transcript"):
|
193 |
-
save_transcript(full_transcript)
|
194 |
-
st.success("Transcript saved!")
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
if
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import datetime
|
3 |
import os
|
4 |
+
import base64
|
5 |
|
6 |
# Initialize session state variables
|
7 |
if 'transcript_history' not in st.session_state:
|
8 |
st.session_state.transcript_history = []
|
9 |
|
10 |
+
# Function to create a download link for a string
|
11 |
+
def get_download_link(text, filename):
|
12 |
+
b64 = base64.b64encode(text.encode()).decode()
|
13 |
+
return f'<a href="data:text/plain;base64,{b64}" download="{filename}">Download Transcript</a>'
|
14 |
|
15 |
# Create the main layout
|
16 |
+
st.title("Speech Recognition with Transcript History")
|
17 |
+
|
18 |
col1, col2 = st.columns([2, 1])
|
19 |
|
20 |
with col1:
|
|
|
21 |
html = """
|
22 |
<!DOCTYPE html>
|
23 |
<html>
|
|
|
82 |
recognition.continuous = true;
|
83 |
recognition.interimResults = true;
|
84 |
|
85 |
+
// Function to start recognition
|
86 |
+
const startRecognition = () => {
|
87 |
try {
|
88 |
recognition.start();
|
89 |
status.textContent = 'Listening...';
|
|
|
95 |
}
|
96 |
};
|
97 |
|
98 |
+
// Auto-start on load
|
99 |
+
window.addEventListener('load', () => {
|
100 |
+
setTimeout(startRecognition, 1000); // Delay start by 1 second to ensure everything is loaded
|
101 |
+
});
|
102 |
+
|
103 |
+
startButton.onclick = startRecognition;
|
104 |
+
|
105 |
stopButton.onclick = () => {
|
106 |
recognition.stop();
|
107 |
status.textContent = 'Stopped';
|
|
|
112 |
clearButton.onclick = () => {
|
113 |
fullTranscript = '';
|
114 |
output.textContent = '';
|
115 |
+
// Send clear signal to Streamlit
|
116 |
+
window.parent.postMessage({type: 'clear'}, '*');
|
117 |
};
|
118 |
|
119 |
recognition.onresult = (event) => {
|
|
|
132 |
if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
|
133 |
if (finalTranscript) {
|
134 |
fullTranscript += finalTranscript;
|
135 |
+
// Send to Streamlit
|
136 |
+
window.parent.postMessage({
|
137 |
+
type: 'transcript',
|
138 |
+
text: finalTranscript
|
139 |
+
}, '*');
|
|
|
|
|
|
|
|
|
140 |
}
|
141 |
lastUpdateTime = Date.now();
|
142 |
}
|
|
|
168 |
stopButton.disabled = true;
|
169 |
}
|
170 |
};
|
171 |
+
|
172 |
+
// Listen for messages from Streamlit
|
173 |
+
window.addEventListener('message', (event) => {
|
174 |
+
if (event.data.type === 'clear') {
|
175 |
+
fullTranscript = '';
|
176 |
+
output.textContent = '';
|
177 |
+
}
|
178 |
+
});
|
179 |
}
|
180 |
</script>
|
181 |
</body>
|
182 |
</html>
|
183 |
"""
|
184 |
+
|
185 |
+
# Display the HTML component
|
186 |
+
component = st.components.v1.html(html, height=400)
|
187 |
|
188 |
with col2:
|
189 |
# Display transcript history
|
190 |
st.subheader("Transcript History")
|
|
|
191 |
|
192 |
+
# Function to save transcript
|
193 |
def save_transcript(text):
|
194 |
if not os.path.exists('transcripts'):
|
195 |
os.makedirs('transcripts')
|
|
|
196 |
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
197 |
filename = f"transcripts/transcript_{timestamp}.md"
|
|
|
198 |
with open(filename, 'w', encoding='utf-8') as f:
|
199 |
f.write(text)
|
200 |
+
return filename
|
201 |
|
202 |
+
# Display transcript
|
203 |
if st.session_state.transcript_history:
|
204 |
full_transcript = "\n".join(st.session_state.transcript_history)
|
205 |
+
st.text_area("Full Transcript", value=full_transcript, height=300)
|
206 |
+
|
207 |
+
# Save transcript to file
|
208 |
+
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
209 |
+
filename = f"transcript_{timestamp}.md"
|
210 |
|
211 |
+
# Create download link
|
212 |
+
st.markdown(get_download_link(full_transcript, filename), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
213 |
|
214 |
+
# Save to file system
|
215 |
+
if st.button("Save to File"):
|
216 |
+
saved_file = save_transcript(full_transcript)
|
217 |
+
st.success(f"Saved to {saved_file}")
|
218 |
+
|
219 |
+
# Handle transcript updates from JavaScript
|
220 |
+
if component:
|
221 |
+
try:
|
222 |
+
data = component
|
223 |
+
if isinstance(data, dict) and data.get('type') == 'transcript':
|
224 |
+
st.session_state.transcript_history.append(data['text'])
|
225 |
+
st.experimental_rerun()
|
226 |
+
elif isinstance(data, dict) and data.get('type') == 'clear':
|
227 |
+
st.session_state.transcript_history = []
|
228 |
+
st.experimental_rerun()
|
229 |
+
except Exception as e:
|
230 |
+
st.error(f"Error processing transcript: {e}")
|