PatienceIzere commited on
Commit
5c1dd7e
·
verified ·
1 Parent(s): 71f837d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -23
app.py CHANGED
@@ -2,11 +2,8 @@ import os
2
  import streamlit as st
3
  import tempfile
4
  import base64
5
- import numpy as np
6
  import time
7
  from datetime import datetime
8
- import soundfile as sf
9
- import io
10
  from hf_transcriber import HFTranscriber
11
  from huggingface_hub import login
12
  from dotenv import load_dotenv, find_dotenv
@@ -53,8 +50,7 @@ app_config = {
53
  def init_recording():
54
  """Initialize recording capability and return status."""
55
  try:
56
- # Import required modules
57
- from hf_transcriber import HFTranscriber
58
  from recorder import AudioRecorder, list_audio_devices
59
 
60
  # Update config with recording components
@@ -67,24 +63,24 @@ def init_recording():
67
  app_config['AUDIO_DEVICES'] = devices
68
 
69
  if not devices or not any(d.get('max_input_channels', 0) > 0 for d in devices):
70
- st.warning("⚠️ No input devices with recording capability found. Using fallback mode.")
71
  app_config['RECORDING_ENABLED'] = False
72
  else:
73
  app_config['RECORDING_ENABLED'] = True
74
 
75
  except Exception as e:
76
- st.warning(f"⚠️ Could not detect audio devices: {str(e)}. Using fallback mode.")
77
  app_config['RECORDING_ENABLED'] = False
78
  app_config['AUDIO_DEVICES'] = []
79
 
80
  return True
81
 
82
  except ImportError as e:
83
- st.warning(f"⚠️ Some features may be limited: {str(e)}")
84
  app_config['RECORDING_ENABLED'] = False
85
  return False
86
  except Exception as e:
87
- st.warning(f"⚠️ Audio initialization failed: {str(e)}. Using fallback mode.")
88
  app_config['RECORDING_ENABLED'] = False
89
  return False
90
 
@@ -120,9 +116,50 @@ def transcribe_audio(file_path, model_name):
120
  st.exception(e) # Show full error in debug mode
121
  return None
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  def main():
124
  st.title("🎵 Audio to Sheet Music Transcriber")
125
- st.markdown("### Convert monophonic audio to sheet music")
126
 
127
  # Model selection in sidebar
128
  with st.sidebar:
@@ -143,22 +180,32 @@ def main():
143
  )
144
  model_name = model_options[selected_model]
145
 
146
- # Main content area
147
- st.header("🎤 Upload Audio File")
148
- st.info("ℹ️ Please upload an audio file for transcription (WAV, MP3, or OGG format)")
149
 
150
- uploaded_file = st.file_uploader(
151
- "Choose an audio file",
152
- type=["wav", "mp3", "ogg"],
153
- accept_multiple_files=False,
154
- help="Select an audio file to transcribe (max 30MB)"
155
- )
 
 
 
 
 
 
 
 
156
 
157
  if uploaded_file is not None:
158
  with st.spinner("Processing audio..."):
159
  try:
160
- # Save the uploaded file temporarily
161
- temp_file_path = save_uploaded_file(uploaded_file)
 
 
 
162
 
163
  # Display the audio player
164
  st.audio(temp_file_path, format=f'audio/{os.path.splitext(uploaded_file.name)[1][1:]}')
@@ -301,6 +348,5 @@ if __name__ == "__main__":
301
  This app uses Hugging Face's Transformers library for speech-to-text transcription.
302
  Models are loaded on-demand and require an internet connection.
303
 
304
- **Note:** This is a web-based version that only supports file uploads.
305
- For local use with microphone support, run the main app.py instead.
306
  """)
 
2
  import streamlit as st
3
  import tempfile
4
  import base64
 
5
  import time
6
  from datetime import datetime
 
 
7
  from hf_transcriber import HFTranscriber
8
  from huggingface_hub import login
9
  from dotenv import load_dotenv, find_dotenv
 
50
  def init_recording():
51
  """Initialize recording capability and return status."""
52
  try:
53
+ # Try to import recording-related modules
 
54
  from recorder import AudioRecorder, list_audio_devices
55
 
56
  # Update config with recording components
 
63
  app_config['AUDIO_DEVICES'] = devices
64
 
65
  if not devices or not any(d.get('max_input_channels', 0) > 0 for d in devices):
66
+ st.warning("⚠️ No input devices with recording capability found. Using file upload only.")
67
  app_config['RECORDING_ENABLED'] = False
68
  else:
69
  app_config['RECORDING_ENABLED'] = True
70
 
71
  except Exception as e:
72
+ st.warning(f"⚠️ Could not detect audio devices: {str(e)}. Using file upload only.")
73
  app_config['RECORDING_ENABLED'] = False
74
  app_config['AUDIO_DEVICES'] = []
75
 
76
  return True
77
 
78
  except ImportError as e:
79
+ st.warning(f"⚠️ Some features may be limited: {str(e)}. Using file upload only.")
80
  app_config['RECORDING_ENABLED'] = False
81
  return False
82
  except Exception as e:
83
+ st.warning(f"⚠️ Audio initialization failed: {str(e)}. Using file upload only.")
84
  app_config['RECORDING_ENABLED'] = False
85
  return False
86
 
 
116
  st.exception(e) # Show full error in debug mode
117
  return None
118
 
119
+ def record_audio():
120
+ """Handle audio recording functionality."""
121
+ st.header("🎤 Record Audio")
122
+
123
+ if not app_config['RECORDING_ENABLED']:
124
+ st.warning("Audio recording is not available on this device.")
125
+ return
126
+
127
+ AudioRecorder = app_config['AudioRecorder']
128
+
129
+ if 'recorder' not in st.session_state:
130
+ st.session_state.recorder = AudioRecorder()
131
+
132
+ col1, col2 = st.columns(2)
133
+
134
+ with col1:
135
+ if st.button("🎤 Start Recording"):
136
+ st.session_state.recorder.start()
137
+ st.session_state.recording = True
138
+ st.experimental_rerun()
139
+
140
+ with col2:
141
+ if st.button("⏹️ Stop Recording") and st.session_state.get('recording', False):
142
+ audio_data = st.session_state.recorder.stop()
143
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
144
+ output_file = os.path.join("outputs", f"recording_{timestamp}.wav")
145
+ os.makedirs("outputs", exist_ok=True)
146
+ audio_data.export(output_file, format="wav")
147
+ st.session_state.recorded_file = output_file
148
+ st.session_state.recording = False
149
+ st.experimental_rerun()
150
+
151
+ if st.session_state.get('recording', False):
152
+ st.warning("Recording in progress... Click 'Stop Recording' when finished.")
153
+
154
+ if 'recorded_file' in st.session_state and os.path.exists(st.session_state.recorded_file):
155
+ st.audio(st.session_state.recorded_file)
156
+ return st.session_state.recorded_file
157
+
158
+ return None
159
+
160
  def main():
161
  st.title("🎵 Audio to Sheet Music Transcriber")
162
+ st.markdown("### Record or upload audio for transcription")
163
 
164
  # Model selection in sidebar
165
  with st.sidebar:
 
180
  )
181
  model_name = model_options[selected_model]
182
 
183
+ # Main content area - Tabs for different input methods
184
+ tab1, tab2 = st.tabs(["🎤 Record Audio", "📁 Upload File"])
 
185
 
186
+ recorded_file = None
187
+ uploaded_file = None
188
+
189
+ with tab1:
190
+ recorded_file = record_audio()
191
+
192
+ with tab2:
193
+ st.info("ℹ️ Please upload an audio file for transcription (WAV, MP3, or OGG format)")
194
+ uploaded_file = st.file_uploader(
195
+ "Choose an audio file",
196
+ type=["wav", "mp3", "ogg"],
197
+ help="Select an audio file to transcribe (max 30MB)",
198
+ key="file_uploader"
199
+ )
200
 
201
  if uploaded_file is not None:
202
  with st.spinner("Processing audio..."):
203
  try:
204
+ # Get the file path (either recorded or uploaded)
205
+ if recorded_file:
206
+ temp_file_path = recorded_file
207
+ else:
208
+ temp_file_path = save_uploaded_file(uploaded_file)
209
 
210
  # Display the audio player
211
  st.audio(temp_file_path, format=f'audio/{os.path.splitext(uploaded_file.name)[1][1:]}')
 
348
  This app uses Hugging Face's Transformers library for speech-to-text transcription.
349
  Models are loaded on-demand and require an internet connection.
350
 
351
+ **Note:** This version supports both file uploads and live recording (if your device supports it).
 
352
  """)