def transcribeaudio(audiopath, showprogress=True): """Transcribe audio file using the…
- User
def transcribe_audio(audio_path, show_progress=True): """Transcribe audio file using the model with detailed progress reporting.""" start_time = time.time() converted_path = None
try: model = load_model() if model is None: return None processed_path, duration_sec = process_audio(audio_path) if processed_path is None: return None # Store converted path for cleanup if processed_path != audio_path: converted_path = processed_path # Handle long audio long_audio_settings_applied = False if duration_sec > LONG_AUDIO_THRESHOLD: try: custom_info(f"Audio longer than {LONG_AUDIO_THRESHOLD/60:.1f} minutes. Applying optimized settings...") model.change_attention_model("rel_pos_local_attn", [256, 256]) model.change_subsampling_conv_chunking_factor(1) long_audio_settings_applied = True except Exception as e: custom_warning(f"Could not apply long audio settings: {str(e)}") try: if show_progress: with st.spinner("Transcribing audio..."): progress_bar = st.progress(0) estimated_total_time = duration_sec * 0.5 def update_progress(): for i in range(1, 101): elapsed = time.time() - start_time if elapsed >= estimated_total_time: break progress = min(elapsed / estimated_total_time, 0.95) progress_bar.progress(progress) time.sleep(estimated_total_time / 100) import threading progress_thread = threading.Thread(target=update_progress) progress_thread.daemon = True progress_thread.start() output = model.transcribe([processed_path], timestamps=True) progress_bar.progress(1.0) else: output = model.transcribe([processed_path], timestamps=True) if not output or not isinstance(output, list) or not output[0] or not hasattr(output[0], 'timestamp'): custom_error("Transcription failed or produced unexpected output.") return None segment_timestamps = output[0].timestamp['segment'] csv_data = [["From (s)", "To (s)", "From (time)", "To (time)", "Duration", "Transcription"]] for ts in segment_timestamps: start_s = ts['start'] end_s = ts['end'] start_formatted = format_time(start_s) end_formatted = format_time(end_s) duration = end_s - start_s csv_data.append([ f"{start_s:.2f}", f"{end_s:.2f}", start_formatted, end_formatted, f"{duration:.2f}", ts['segment'] ]) processing_time = time.time() - start_time custom_success(f"Transcription completed in {processing_time:.1f} seconds!") filename = os.path.basename(audio_path) st.session_state.transcription_history.append({ 'filename': filename, 'duration': duration_sec, 'timestamp': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'csv_data': csv_data }) return csv_data finally: if long_audio_settings_applied: try: model.change_attention_model("rel_pos") model.change_subsampling_conv_chunking_factor(-1) except Exception as e: custom_warning(f"Issue reverting model settings: {str(e)}") # Cleanup all processed files if converted_path and os.path.exists(converted_path): os.remove(converted_path) torch.cuda.empty_cache() gc.collect() except Exception as e: custom_error(f"Error during transcription: {str(e)}") if "CUDA out of memory" in str(e): custom_info("GPU ran out of memory. Try a shorter audio file.") return Nonedef export_to_formats(csv_data): """Create exportable data in multiple formats""" # CSV csv_string = "\n".join([",".join([f'"{cell}"' if ',' in cell else cell for cell in row]) for row in csv_data])
# Plain text text_string = "\n\n".join([row[5] for row in csv_data[1:]]) # SRT format srt_string = "" for i, row in enumerate(csv_data[1:], 1): start_s = float(row[0]) end_s = float(row[1]) start_srt = f"{int(start_s//3600):02d}:{int((start_s%3600)//60):02d}:{int(start_s%60):02d},{int((start_s%1)*1000):03d}" end_srt = f"{int(end_s//3600):02d}:{int((end_s%3600)//60):02d}:{int(end_s%60):02d},{int((end_s%1)*1000):03d}" srt_string += f"{i}\n{start_srt} --> {end_srt}\n{row[5]}\n\n" return csv_string, text_string, srt_string - Assistant
The provided code for audio transcription and export has several areas that could be improved for robustness, accuracy, and compatibility. Below is a detailed analysis and recommendations for enhancement:
1. Progress Reporting in Streamlit
Issue:
The code uses a separate thread to update the progress bar, which can lead to inconsistent updates or race conditions in Streamlit. Streamlit is not thread-safe, and background threads may not interact correctly with the UI.Recommendation:
Avoid using threads for progress updates. Instead, use a loop in the main thread withst.progressandtime.sleep, or leverage the model's built-in progress tracking if available. If the model does not provide progress, use a simplified progress estimation without threads.Example Fix:
def transcribe_audio(...): # ... if show_progress: progress_bar = st.progress(0) estimated_total_time = duration_sec * 0.5 for i in range(1, 101): time.sleep(estimated_total_time / 100) progress_bar.progress(min(i / 100, 0.95)) progress_bar.progress(1.0) # ...
2. CSV Export Reliability
Issue:
The CSV export uses manual string formatting, which can fail for edge cases (e.g., commas in text, special characters).Recommendation:
Use Python'scsvmodule for proper CSV formatting. This ensures compatibility and avoids issues with escaping characters.Example Fix:
import csv from io import StringIO def export_to_formats(csv_data): # CSV csv_output = StringIO() writer = csv.writer(csv_output) writer.writerows(csv_data) csv_string = csv_output.getvalue() # ... rest of the code
3. SRT Time Formatting
Issue:
The SRT time formatting usesint((start_s%1)*1000)for milliseconds, which may lose precision. For example,0.999seconds becomes999, but0.9995becomes999.Recommendation:
Useround()to ensure proper rounding for milliseconds.Example Fix:
milliseconds = round((start_s % 1) * 1000)
4. Model Settings Reversion
Issue:
The code reverts model settings in afinallyblock, but ifmodel.change_attention_modelormodel.change_subsampling_conv_chunking_factorfails, it may leave the model in an inconsistent state.Recommendation:
Log the reversion attempt and handle exceptions gracefully to avoid silent failures.Example Fix:
finally: if long_audio_settings_applied: try: model.change_attention_model("rel_pos") model.change_subsampling_conv_chunking_factor(-1) except Exception as e: custom_warning(f"Failed to revert model settings: {str(e)}")
5. Error Handling for Transcription Output
Issue:
The code assumesoutput[0].timestamp['segment']exists, but this may not be the case for all models or edge cases.Recommendation:
Add explicit checks for the structure of the output to avoid runtime errors.Example Fix:
if not output or not isinstance(output, list) or not output[0]: custom_error("Invalid transcription output structure.") return None segments = getattr(output[0].timestamp, 'segment', []) if not segments: custom_error("No segments found in transcription output.") return None
6. Cleanup of Temporary Files
Issue:
The code deletesconverted_pathonly ifprocessed_path != audio_path. However, ifprocess_audiofails,converted_pathmay not be set correctly.Recommendation:
Ensureconverted_pathis always initialized and cleaned up, even ifprocess_audiofails.Example Fix:
converted_path = None try: processed_path, duration_sec = process_audio(audio_path) if processed_path != audio_path: converted_path = processed_path except Exception as e: custom_error(f"Audio processing failed: {str(e)}") return None # ... later in finally block if converted_path and os.path.exists(converted_path): os.remove(converted_path)
7. GPU Memory Management
Issue:
The code callstorch.cuda.empty_cache()andgc.collect(), but this may not be sufficient for all GPU memory leaks.Recommendation:
Usetorch.cuda.reset_peak_memory_stats()to track memory usage and ensure proper cleanup.
8. Long Audio Settings
Issue:
The code applies long audio settings (e.g.,change_attention_model) without checking if the model supports these methods.Recommendation:
Add checks for method availability before calling them.Example Fix:
if hasattr(model, 'change_attention_model'): model.change_attention_model("rel_pos_local_attn", [256, 256])
9. Thread Safety in Streamlit
Issue:
Usingthreading.Threadfor progress updates may cause unexpected behavior in Streamlit.Recommendation:
Avoid threads for UI updates. Usest.progressdirectly in the main thread with a loop.
10. Code Readability and Modularity
Issue:
The code is tightly coupled with Streamlit components (st.spinner,st.progress,st.session_state), making it less reusable.Recommendation:
Decouple the transcription logic from UI-specific code. For example, separate the model processing from the UI updates.
Final Recommendations Summary
Issue Fix Progress Bar Replace thread-based updates with a loop in the main thread. CSV Export Use csv.writerfor reliable formatting.SRT Time Formatting Use round()for milliseconds.Model Settings Reversion Add error handling for reversion. Transcription Output Validation Add explicit checks for output structure. Temporary File Cleanup Ensure converted_pathis always initialized.GPU Memory Management Use torch.cuda.reset_peak_memory_stats().Long Audio Settings Check method availability before calling. Thread Safety Avoid threads for UI updates in Streamlit. Code Modularity Decouple model logic from UI components. By addressing these issues, the code will be more robust, maintainable, and compatible with different environments.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.