PatienceIzere commited on
Commit
014a2de
Β·
verified Β·
1 Parent(s): d099202

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -4
app.py CHANGED
@@ -115,12 +115,48 @@ def save_uploaded_file(uploaded_file):
115
  def transcribe_audio(file_path, model_name):
116
  """Transcribe audio using the specified model."""
117
  try:
 
 
 
 
 
118
  transcriber = HFTranscriber(model_name=model_name)
119
- result = transcriber.transcribe_audio(file_path, 16000) # 16kHz sample rate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  return result
 
121
  except Exception as e:
122
  st.error(f"❌ Transcription failed: {str(e)}")
123
- st.exception(e) # Show full error in debug mode
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  return None
125
 
126
  def record_audio():
@@ -204,17 +240,23 @@ def main():
204
  uploaded_file = render_file_uploader()
205
  recorded_file = None
206
 
207
- if uploaded_file is not None:
208
  with st.spinner("Processing audio..."):
209
  try:
210
  # Get the file path (either recorded or uploaded)
211
  if recorded_file:
212
  temp_file_path = recorded_file
 
213
  else:
214
  temp_file_path = save_uploaded_file(uploaded_file)
 
215
 
216
  # Display the audio player
217
- st.audio(temp_file_path, format=f'audio/{os.path.splitext(uploaded_file.name)[1][1:]}')
 
 
 
 
218
 
219
  except Exception as e:
220
  st.error(f"Error processing uploaded file: {str(e)}")
 
115
  def transcribe_audio(file_path, model_name):
116
  """Transcribe audio using the specified model."""
117
  try:
118
+ # Debug: Show authentication status
119
+ hf_token = os.getenv('HUGGINGFACE_TOKEN') or os.getenv('HF_TOKEN')
120
+ st.sidebar.info(f"πŸ”‘ Using model: {model_name}")
121
+ st.sidebar.info(f"πŸ”‘ Token present: {'Yes' if hf_token else 'No'}")
122
+
123
  transcriber = HFTranscriber(model_name=model_name)
124
+
125
+ # Read the audio file
126
+ try:
127
+ audio_data, sample_rate = librosa.load(file_path, sr=16000)
128
+ st.sidebar.info(f"πŸ”Š Loaded audio: {len(audio_data)} samples @ {sample_rate}Hz")
129
+ except Exception as e:
130
+ st.error(f"❌ Error loading audio file: {str(e)}")
131
+ return None
132
+
133
+ # Transcribe
134
+ with st.spinner("πŸ”„ Transcribing audio..."):
135
+ result = transcriber.transcribe_audio(audio_data, sample_rate)
136
+
137
+ if not result or 'text' not in result:
138
+ st.error("❌ No transcription results returned. The model might not be accessible.")
139
+ return None
140
+
141
  return result
142
+
143
  except Exception as e:
144
  st.error(f"❌ Transcription failed: {str(e)}")
145
+ st.error("This might be due to:")
146
+ st.error("1. Invalid or missing Hugging Face token")
147
+ st.error("2. Insufficient permissions for the model")
148
+ st.error("3. Network connectivity issues")
149
+ st.error("4. Model not found or not accessible")
150
+
151
+ # Add debug info
152
+ st.sidebar.error("πŸ” Debug Info:")
153
+ st.sidebar.json({
154
+ "model": model_name,
155
+ "token_present": bool(hf_token),
156
+ "token_prefix": hf_token[:8] + '...' if hf_token else None,
157
+ "error": str(e)
158
+ })
159
+
160
  return None
161
 
162
  def record_audio():
 
240
  uploaded_file = render_file_uploader()
241
  recorded_file = None
242
 
243
+ if uploaded_file is not None or recorded_file:
244
  with st.spinner("Processing audio..."):
245
  try:
246
  # Get the file path (either recorded or uploaded)
247
  if recorded_file:
248
  temp_file_path = recorded_file
249
+ file_ext = os.path.splitext(temp_file_path)[1][1:]
250
  else:
251
  temp_file_path = save_uploaded_file(uploaded_file)
252
+ file_ext = os.path.splitext(uploaded_file.name)[1][1:]
253
 
254
  # Display the audio player
255
+ st.audio(temp_file_path, format=f'audio/{file_ext}')
256
+
257
+ # Show file info
258
+ file_size = os.path.getsize(temp_file_path) / (1024 * 1024) # in MB
259
+ st.info(f"πŸ“‚ Processing: {os.path.basename(temp_file_path)} ({file_size:.2f} MB)")
260
 
261
  except Exception as e:
262
  st.error(f"Error processing uploaded file: {str(e)}")