awacke1 commited on
Commit
8e7963e
·
verified ·
1 Parent(s): 172a0f1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +209 -0
app.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Create containers at the top level
10
+ st.title("Speech Recognition with Transcript History")
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>
20
+ <head>
21
+ <title>Continuous Speech Demo</title>
22
+ <style>
23
+ body {
24
+ font-family: sans-serif;
25
+ padding: 20px;
26
+ max-width: 800px;
27
+ margin: 0 auto;
28
+ }
29
+ button {
30
+ padding: 10px 20px;
31
+ margin: 10px 5px;
32
+ font-size: 16px;
33
+ }
34
+ #status {
35
+ margin: 10px 0;
36
+ padding: 10px;
37
+ background: #e8f5e9;
38
+ border-radius: 4px;
39
+ }
40
+ #output {
41
+ white-space: pre-wrap;
42
+ padding: 15px;
43
+ background: #f5f5f5;
44
+ border-radius: 4px;
45
+ margin: 10px 0;
46
+ min-height: 100px;
47
+ max-height: 400px;
48
+ overflow-y: auto;
49
+ }
50
+ .controls {
51
+ margin: 10px 0;
52
+ }
53
+ </style>
54
+ </head>
55
+ <body>
56
+ <div class="controls">
57
+ <button id="start">Start Listening</button>
58
+ <button id="stop" disabled>Stop Listening</button>
59
+ <button id="clear">Clear Text</button>
60
+ </div>
61
+ <div id="status">Ready</div>
62
+ <div id="output"></div>
63
+
64
+ <script>
65
+ if (!('webkitSpeechRecognition' in window)) {
66
+ alert('Speech recognition not supported');
67
+ } else {
68
+ const recognition = new webkitSpeechRecognition();
69
+ const startButton = document.getElementById('start');
70
+ const stopButton = document.getElementById('stop');
71
+ const clearButton = document.getElementById('clear');
72
+ const status = document.getElementById('status');
73
+ const output = document.getElementById('output');
74
+ let fullTranscript = '';
75
+ let lastUpdateTime = Date.now();
76
+
77
+ // Configure recognition
78
+ recognition.continuous = true;
79
+ recognition.interimResults = true;
80
+
81
+ startButton.onclick = () => {
82
+ try {
83
+ recognition.start();
84
+ status.textContent = 'Listening...';
85
+ startButton.disabled = true;
86
+ stopButton.disabled = false;
87
+ } catch (e) {
88
+ console.error(e);
89
+ status.textContent = 'Error: ' + e.message;
90
+ }
91
+ };
92
+
93
+ stopButton.onclick = () => {
94
+ recognition.stop();
95
+ status.textContent = 'Stopped';
96
+ startButton.disabled = false;
97
+ stopButton.disabled = true;
98
+ };
99
+
100
+ clearButton.onclick = () => {
101
+ fullTranscript = '';
102
+ output.textContent = '';
103
+ };
104
+
105
+ recognition.onresult = (event) => {
106
+ let interimTranscript = '';
107
+ let finalTranscript = '';
108
+
109
+ for (let i = event.resultIndex; i < event.results.length; i++) {
110
+ const transcript = event.results[i][0].transcript;
111
+ if (event.results[i].isFinal) {
112
+ finalTranscript += transcript + '\\n';
113
+ } else {
114
+ interimTranscript += transcript;
115
+ }
116
+ }
117
+
118
+ if (finalTranscript || (Date.now() - lastUpdateTime > 5000)) {
119
+ if (finalTranscript) {
120
+ fullTranscript += finalTranscript;
121
+ // Try to communicate with Streamlit
122
+ try {
123
+ parent.postMessage({
124
+ type: 'transcriptUpdate',
125
+ text: finalTranscript
126
+ }, '*');
127
+ } catch (e) {
128
+ console.error('Failed to send transcript:', e);
129
+ }
130
+ }
131
+ lastUpdateTime = Date.now();
132
+ }
133
+
134
+ output.textContent = fullTranscript + (interimTranscript ? '... ' + interimTranscript : '');
135
+ output.scrollTop = output.scrollHeight;
136
+ };
137
+
138
+ recognition.onend = () => {
139
+ if (!stopButton.disabled) {
140
+ try {
141
+ recognition.start();
142
+ console.log('Restarted recognition');
143
+ } catch (e) {
144
+ console.error('Failed to restart recognition:', e);
145
+ status.textContent = 'Error restarting: ' + e.message;
146
+ startButton.disabled = false;
147
+ stopButton.disabled = true;
148
+ }
149
+ }
150
+ };
151
+
152
+ recognition.onerror = (event) => {
153
+ console.error('Recognition error:', event.error);
154
+ status.textContent = 'Error: ' + event.error;
155
+
156
+ if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
157
+ startButton.disabled = false;
158
+ stopButton.disabled = true;
159
+ }
160
+ };
161
+ }
162
+ </script>
163
+ </body>
164
+ </html>
165
+ """
166
+ st.components.v1.html(html, height=400)
167
+
168
+ with col2:
169
+ # Display transcript history
170
+ st.subheader("Transcript History")
171
+ transcript_text = st.empty()
172
+
173
+ # Save transcript function
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 full transcript
185
+ if st.session_state.transcript_history:
186
+ full_transcript = "\n".join(st.session_state.transcript_history)
187
+ transcript_text.text_area("Full Transcript", value=full_transcript, height=300)
188
+
189
+ # Save and download buttons
190
+ col1, col2 = st.columns(2)
191
+ with col1:
192
+ if st.button("Save Transcript"):
193
+ save_transcript(full_transcript)
194
+ st.success("Transcript saved!")
195
+
196
+ with col2:
197
+ st.download_button(
198
+ label="Download Transcript",
199
+ data=full_transcript,
200
+ file_name=f"transcript_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.md",
201
+ mime="text/markdown"
202
+ )
203
+
204
+ # Listen for transcript updates
205
+ if st.session_state.get('transcriptUpdate'):
206
+ new_text = st.session_state.transcriptUpdate
207
+ st.session_state.transcript_history.append(new_text)
208
+ st.session_state.transcriptUpdate = None # Clear the update
209
+ st.experimental_rerun()