Skip to content
The Chat Archive
qwen3-30b-a3b· Alibaba (Qwen) LMArena Human Preference 140K

def transcribeaudio(audiopath, showprogress=True): """Transcribe audio file using the…

  1. 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 None
    

    def 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
    
  2. 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 with st.progress and time.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's csv module 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 uses int((start_s%1)*1000) for milliseconds, which may lose precision. For example, 0.999 seconds becomes 999, but 0.9995 becomes 999.

    Recommendation:
    Use round() 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 a finally block, but if model.change_attention_model or model.change_subsampling_conv_chunking_factor fails, 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 assumes output[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 deletes converted_path only if processed_path != audio_path. However, if process_audio fails, converted_path may not be set correctly.

    Recommendation:
    Ensure converted_path is always initialized and cleaned up, even if process_audio fails.

    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 calls torch.cuda.empty_cache() and gc.collect(), but this may not be sufficient for all GPU memory leaks.

    Recommendation:
    Use torch.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:
    Using threading.Thread for progress updates may cause unexpected behavior in Streamlit.

    Recommendation:
    Avoid threads for UI updates. Use st.progress directly 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

    IssueFix
    Progress BarReplace thread-based updates with a loop in the main thread.
    CSV ExportUse csv.writer for reliable formatting.
    SRT Time FormattingUse round() for milliseconds.
    Model Settings ReversionAdd error handling for reversion.
    Transcription Output ValidationAdd explicit checks for output structure.
    Temporary File CleanupEnsure converted_path is always initialized.
    GPU Memory ManagementUse torch.cuda.reset_peak_memory_stats().
    Long Audio SettingsCheck method availability before calling.
    Thread SafetyAvoid threads for UI updates in Streamlit.
    Code ModularityDecouple model logic from UI components.

    By addressing these issues, the code will be more robust, maintainable, and compatible with different environments.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.