PatienceIzere commited on
Commit
f7dd6d5
·
verified ·
1 Parent(s): 26f06e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -24
app.py CHANGED
@@ -171,46 +171,121 @@ def main():
171
  col1, col2 = st.columns(2)
172
 
173
  with col1:
174
- if st.button("🎤 Start Recording", disabled=st.session_state.get('recording', False)):
 
 
 
 
 
 
 
 
 
175
  try:
 
176
  if 'recorder' not in st.session_state or st.session_state.recorder is None:
 
177
  st.session_state.recorder = AudioRecorder(device_index=selected_device)
178
- st.session_state.recorder.start_recording()
179
- st.session_state.recording = True
180
- st.success("Recording started...")
181
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  except Exception as e:
183
- st.error(f"Error starting recording: {str(e)}")
 
 
 
 
 
184
 
185
  with col2:
186
- if st.button("⏹️ Stop Recording", disabled=not st.session_state.get('recording', False)):
 
 
187
  try:
188
  if 'recorder' in st.session_state and st.session_state.recorder is not None:
189
- st.session_state.recorder.stop_recording()
 
 
 
 
 
 
 
 
 
190
 
191
- # Save recording
192
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
193
  os.makedirs("recordings", exist_ok=True)
194
  audio_file = os.path.join("recordings", f"recording_{timestamp}.wav")
195
- st.session_state.recorder.save_recording(audio_file)
196
 
197
- st.success("Recording saved!")
198
  try:
199
- st.audio(audio_file)
200
- except Exception as e:
201
- st.warning(f"Could not display audio preview: {str(e)}")
202
-
203
- # Keep the audio_file in session state for transcription
204
- st.session_state.last_recorded_audio = audio_file
205
- # Clean up old recordings (keep last 5)
206
- clean_up_recordings(keep_last=5)
207
- # Update UI without rerun to prevent state issues
208
- st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  except Exception as e:
210
- st.error(f"Error stopping recording: {str(e)}")
211
- finally:
 
 
 
 
212
  st.session_state.recording = False
213
- # Don't use rerun() here as it can cause state issues
 
 
 
 
 
 
 
 
 
214
  # The UI will update automatically due to Streamlit's reactivity
215
 
216
  # Transcription Section
 
171
  col1, col2 = st.columns(2)
172
 
173
  with col1:
174
+ # Check if we have any audio devices
175
+ devices_available = any(device.get('max_input_channels', 0) > 0
176
+ for device in app_config.get('AUDIO_DEVICES', []))
177
+
178
+ if not devices_available:
179
+ st.warning("⚠️ No audio input devices detected. Recording may not work correctly.")
180
+
181
+ if st.button("🎤 Start Recording",
182
+ disabled=st.session_state.get('recording', False),
183
+ key='start_recording_btn'):
184
  try:
185
+ # Create a new recorder instance if needed
186
  if 'recorder' not in st.session_state or st.session_state.recorder is None:
187
+ print("Creating new AudioRecorder instance...")
188
  st.session_state.recorder = AudioRecorder(device_index=selected_device)
189
+
190
+ print("Starting recording...")
191
+ if not devices_available:
192
+ st.warning("Recording started without an audio device. This will generate silent audio.")
193
+
194
+ if st.session_state.recorder.start_recording():
195
+ st.session_state.recording = True
196
+ st.session_state.recording_started = True
197
+ st.session_state.recording_error = None
198
+ print("Recording started successfully")
199
+ st.rerun()
200
+ else:
201
+ error_msg = "Failed to start recording. Please try again."
202
+ print(error_msg)
203
+ st.error(error_msg)
204
+ st.session_state.recording_error = error_msg
205
+ st.session_state.recording = False
206
+ st.session_state.recording_started = False
207
+
208
  except Exception as e:
209
+ error_msg = f"Error starting recording: {str(e)}"
210
+ print(error_msg)
211
+ st.error(error_msg)
212
+ st.session_state.recording_error = error_msg
213
+ st.session_state.recording = False
214
+ st.session_state.recording_started = False
215
 
216
  with col2:
217
+ if st.button("⏹️ Stop Recording",
218
+ disabled=not st.session_state.get('recording', False),
219
+ key='stop_recording_btn'):
220
  try:
221
  if 'recorder' in st.session_state and st.session_state.recorder is not None:
222
+ print("Stopping recording...")
223
+
224
+ # Stop the recording
225
+ audio_data = st.session_state.recorder.stop_recording()
226
+
227
+ if audio_data is None:
228
+ st.warning("No audio data was recorded")
229
+ st.session_state.recording = False
230
+ st.session_state.recording_started = False
231
+ return
232
 
233
+ # Save the recording
234
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
235
  os.makedirs("recordings", exist_ok=True)
236
  audio_file = os.path.join("recordings", f"recording_{timestamp}.wav")
 
237
 
238
+ print(f"Saving recording to {audio_file}...")
239
  try:
240
+ saved_file = st.session_state.recorder.save_recording(audio_file)
241
+ if saved_file and os.path.exists(saved_file):
242
+ print(f"Successfully saved recording to {saved_file}")
243
+ st.session_state.last_recording = saved_file
244
+ st.session_state.last_recorded_audio = saved_file
245
+
246
+ # Clean up old recordings
247
+ clean_up_recordings(keep_last=5)
248
+
249
+ # Display success and audio player
250
+ st.success(f"Recording saved successfully!")
251
+ st.audio(saved_file)
252
+
253
+ # Rerun to update the UI
254
+ st.rerun()
255
+ else:
256
+ error_msg = "Failed to save recording. No audio data was captured."
257
+ print(error_msg)
258
+ st.error(error_msg)
259
+ st.session_state.recording_error = error_msg
260
+
261
+ except Exception as save_error:
262
+ error_msg = f"Error saving recording: {str(save_error)}"
263
+ print(f"Save error details: {error_msg}")
264
+ st.error(error_msg)
265
+ st.session_state.recording_error = error_msg
266
+
267
+ # Reset recording state
268
+ st.session_state.recording = False
269
+ st.session_state.recording_started = False
270
+
271
  except Exception as e:
272
+ error_msg = f"Error stopping recording: {str(e)}"
273
+ print(error_msg)
274
+ st.error(error_msg)
275
+ st.session_state.recording_error = error_msg
276
+
277
+ # Ensure we reset the recording state
278
  st.session_state.recording = False
279
+ st.session_state.recording_started = False
280
+ finally:
281
+ # Always clean up the recorder
282
+ if 'recorder' in st.session_state:
283
+ try:
284
+ st.session_state.recorder = None
285
+ except:
286
+ pass
287
+
288
+ # Don't use rerun() in finally as it can cause infinite loops
289
  # The UI will update automatically due to Streamlit's reactivity
290
 
291
  # Transcription Section