Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Remove legacy ASR transcription paths, migrate to connector architecture
Murtaza Nasiraace86a79daf
diff --git a/src/api/recordings.py b/src/api/recordings.pyindex d58b586e..d01bf0bc 100644--- a/src/api/recordings.py+++ b/src/api/recordings.py@@ -23,7 +23,7 @@ from src.database import dbfrom src.models import *from src.utils import *from src.config.app_config import ASR_MIN_SPEAKERS, ASR_MAX_SPEAKERS, ASR_DIARIZE, USE_NEW_TRANSCRIPTION_ARCHITECTURE-from src.tasks.processing import format_transcription_for_llm, transcribe_with_chunking+from src.tasks.processing import format_transcription_for_llmfrom src.utils.ffmpeg_utils import FFmpegError, FFmpegNotFoundErrorfrom src.services.speaker import update_speaker_usage, identify_unidentified_speakers_from_textfrom src.services.speaker_embedding_matcher import update_speaker_embeddingdiff --git a/src/app.py b/src/app.pyindex 2ecc2147..fca14105 100644--- a/src/app.py+++ b/src/app.py@@ -405,8 +405,8 @@ from src.services.speaker import (# Import background task functionsfrom src.tasks.processing import (generate_title_task, generate_summary_only_task, extract_events_from_transcript,- extract_audio_from_video, transcribe_audio_asr, transcribe_audio_task,- transcribe_single_file, transcribe_with_chunking+ extract_audio_from_video, transcribe_audio_task, transcribe_with_connector,+ transcribe_chunks_with_connector, transcribe_incognito)# Import configuration helpersdiff --git a/src/tasks/__init__.py b/src/tasks/__init__.pyindex 000e5686..408c7735 100644--- a/src/tasks/__init__.py+++ b/src/tasks/__init__.py@@ -1,5 +1,8 @@"""Background task functions for asynchronous processing.++Note: Legacy functions (transcribe_audio_asr, transcribe_single_file, transcribe_with_chunking)+were removed. All transcription now uses the connector architecture via transcribe_audio_task."""from .processing import (@@ -8,10 +11,10 @@ from .processing import (extract_events_from_transcript,extract_audio_from_video,compress_lossless_audio,- transcribe_audio_asr,transcribe_audio_task,- transcribe_single_file,- transcribe_with_chunking+ transcribe_with_connector,+ transcribe_chunks_with_connector,+ transcribe_incognito,)__all__ = [@@ -20,8 +23,8 @@ __all__ = ['extract_events_from_transcript','extract_audio_from_video','compress_lossless_audio',- 'transcribe_audio_asr','transcribe_audio_task',- 'transcribe_single_file',- 'transcribe_with_chunking',+ 'transcribe_with_connector',+ 'transcribe_chunks_with_connector',+ 'transcribe_incognito',]diff --git a/src/tasks/processing.py b/src/tasks/processing.pyindex 64268492..931612d6 100644--- a/src/tasks/processing.py+++ b/src/tasks/processing.py@@ -33,8 +33,7 @@ from src.config.app_config import AUDIO_COMPRESS_UPLOADS, AUDIO_CODEC, AUDIO_BITfrom src.audio_chunking import AudioChunkingService, ChunkProcessingError, ChunkingNotSupportedErrorfrom src.config.app_config import (ASR_DIARIZE, ASR_BASE_URL, ASR_RETURN_SPEAKER_EMBEDDINGS,- transcription_api_key, transcription_base_url, chunking_service, ENABLE_CHUNKING,- USE_NEW_TRANSCRIPTION_ARCHITECTURE+ transcription_api_key, transcription_base_url, chunking_service, ENABLE_CHUNKING)from src.file_exporter import export_recording, ENABLE_AUTO_EXPORTfrom src.services.transcription_tracking import transcription_tracker@@ -182,9 +181,8 @@ def clean_llm_response(text):return cleaned.strip()# Configuration from environment-USE_ASR_ENDPOINT = os.environ.get('USE_ASR_ENDPOINT', 'false').lower() == 'true'-# Note: ASR_ENDPOINT and ASR_API_KEY were removed - they were dead code-# ASR configuration is now handled via the connector architecture+# Note: Legacy ASR paths (USE_ASR_ENDPOINT, transcribe_audio_asr, etc.) were removed.+# All transcription now uses the connector architecture via transcribe_with_connector().ENABLE_INQUIRE_MODE = os.environ.get('ENABLE_INQUIRE_MODE', 'false').lower() == 'true'# chunking_service, ENABLE_CHUNKING, transcription_api_key, and transcription_base_url@@ -1728,386 +1726,15 @@ def transcribe_with_connector(app_context, recording_id, filepath, original_fileraise-def transcribe_audio_asr(app_context, recording_id, filepath, original_filename, start_time, mime_type=None, language=None, diarize=False, min_speakers=None, max_speakers=None, tag_id=None):- """Transcribes audio using the ASR webservice."""- with app_context:- recording = db.session.get(Recording, recording_id)- if not recording:- current_app.logger.error(f"Error: Recording {recording_id} not found for ASR transcription.")- return-- try:- current_app.logger.info(f"Starting ASR transcription for recording {recording_id}...")- recording.status = 'PROCESSING'- transcription_start_time = time.time()- db.session.commit()-- # Check if we need to extract audio from video container- actual_filepath = filepath- actual_content_type = mime_type or mimetypes.guess_type(original_filename)[0] or 'application/octet-stream'- actual_filename = original_filename-- # Use codec detection to check if file is a video- try:- is_video = is_video_file(filepath, timeout=10)- if is_video:- current_app.logger.info(f"Video detected via codec analysis for {original_filename}")- except FFProbeError as e:- current_app.logger.warning(f"Failed to probe {original_filename}: {e}. Falling back to MIME type detection.")- # Fallback to MIME type detection- video_mime_types = [- 'video/mp4', 'video/quicktime', 'video/x-msvideo', 'video/webm',- 'video/avi', 'video/x-ms-wmv', 'video/3gpp'- ]- is_video = (- actual_content_type.startswith('video/') or- actual_content_type in video_mime_types- )-- if is_video:- current_app.logger.info(f"Video container detected ({actual_content_type}), extracting audio...")- try:- # Extract audio from video (uses compression settings)- audio_filepath, audio_mime_type = extract_audio_from_video(filepath)-- # Update paths and MIME type for ASR processing- actual_filepath = audio_filepath- actual_content_type = audio_mime_type- actual_filename = os.path.basename(audio_filepath)-- # Update recording with extracted audio path and new MIME type- recording.audio_path = audio_filepath- recording.mime_type = audio_mime_type- db.session.commit()-- current_app.logger.info(f"Audio extracted successfully: {audio_filepath}")- except Exception as e:- current_app.logger.error(f"Failed to extract audio from video: {str(e)}")- recording.status = 'FAILED'- recording.error_msg = f"Audio extraction failed: {str(e)}"- db.session.commit()- raise # Re-raise so job queue marks the job as failed-- # Keep track of converted filepath for retry logic- converted_filepath = None-- # Retry loop for handling 500 errors with WAV conversion- max_attempts = 2- for attempt in range(max_attempts):- try:- # Use converted MP3 if available from previous attempt- current_filepath = converted_filepath if converted_filepath else actual_filepath- current_content_type = 'audio/mpeg' if converted_filepath else actual_content_type- current_filename = os.path.basename(current_filepath)-- with open(current_filepath, 'rb') as audio_file:- url = f"{ASR_BASE_URL}/asr"- params = {- 'encode': True,- 'task': 'transcribe',- 'output': 'json'- }- if language:- params['language'] = language- if diarize:- # Send both parameter names for compatibility:- # - 'diarize' is used by whisper-asr-webservice- # - 'enable_diarization' is used by WhisperX- params['diarize'] = True- params['enable_diarization'] = True- # Only request speaker embeddings if explicitly enabled (WhisperX only)- if ASR_RETURN_SPEAKER_EMBEDDINGS:- params['return_speaker_embeddings'] = True- if min_speakers:- params['min_speakers'] = min_speakers- if max_speakers:- params['max_speakers'] = max_speakers-- content_type = current_content_type- current_app.logger.info(f"Using MIME type {content_type} for ASR upload.")- files = {'audio_file': (current_filename, audio_file, content_type)}-- with httpx.Client() as client:- # Get configurable ASR timeout from database (default 30 minutes)- asr_timeout_seconds = SystemSetting.get_setting('asr_timeout_seconds', 1800)- # Use generous timeouts: write=300s for large file uploads, pool=None to wait indefinitely- timeout = httpx.Timeout(None, connect=60.0, read=float(asr_timeout_seconds), write=300.0, pool=None)- current_app.logger.info(f"Sending ASR request to {url} with params: {params} (timeout: {asr_timeout_seconds}s)")- response = client.post(url, params=params, files=files, timeout=timeout)- current_app.logger.info(f"ASR request completed with status: {response.status_code}")- response.raise_for_status()-- # Parse the JSON response from ASR- # Try to parse as JSON first, fall back to content-type check for error handling- response_text = response.text- try:- asr_response_data = response.json()- except Exception as json_err:- # If JSON parsing fails, check if it looks like HTML (error page)- if response_text.strip().startswith('<'):- current_app.logger.error(f"ASR returned HTML error page (status {response.status_code}): {response_text[:500]}")- raise Exception(f"ASR service returned HTML error page (status {response.status_code})")- else:- current_app.logger.error(f"ASR returned non-JSON response (status {response.status_code}): {response_text[:500]}")- raise Exception(f"ASR service returned invalid response: {json_err}")-- # Extract speaker embeddings if present- if 'speaker_embeddings' in asr_response_data:- current_app.logger.info(f"Received speaker embeddings for speakers: {list(asr_response_data['speaker_embeddings'].keys())}")- # Store speaker embeddings in the recording- recording.speaker_embeddings = asr_response_data['speaker_embeddings']- db.session.commit()-- # If we reach here, the request was successful- break-- except httpx.HTTPStatusError as e:- # Check if it's a 500 error and we haven't tried MP3 conversion yet- if e.response.status_code == 500 and attempt == 0 and not converted_filepath:- current_app.logger.warning(f"ASR returned 500 error for recording {recording_id}, attempting high-quality MP3 conversion and retry...")-- # Check if file is already MP3 using codec detection- try:- codec_info = get_codec_info(actual_filepath, timeout=10)- audio_codec = codec_info.get('audio_codec')- needs_conversion = audio_codec != 'mp3'- except FFProbeError:- # Fallback to extension check if probe fails- needs_conversion = not actual_filename.lower().endswith('.mp3')-- if needs_conversion:- try:- current_app.logger.info(f"Converting {actual_filename} to high-quality MP3 format for retry...")- temp_mp3_filepath = convert_to_mp3(actual_filepath)- current_app.logger.info(f"Successfully converted to MP3: {temp_mp3_filepath}")-- converted_filepath = temp_mp3_filepath- # Continue to next iteration to retry with MP3- continue- except (FFmpegError, FFmpegNotFoundError) as conv_error:- current_app.logger.error(f"Failed to convert to MP3: {conv_error}")- # Re-raise the original HTTP error if conversion fails- raise e- else:- # Already an MP3 file, can't convert further- current_app.logger.error(f"File is already MP3 but still getting 500 error")- raise e- else:- # Not a 500 error or already tried conversion, propagate the error- raise e-- # Optional: Preserve converted file for debugging- if os.getenv('PRESERVE_DEBUG_CONVERSIONS', 'false').lower() == 'true':- if converted_filepath and os.path.exists(converted_filepath):- try:- # Get file size and basic info for debugging- converted_size = os.path.getsize(converted_filepath)- converted_size_mb = converted_size / (1024 * 1024)-- # Create a debug copy in a known location- debug_dir = os.path.join(current_app.config['UPLOAD_FOLDER'], 'debug_converted')- os.makedirs(debug_dir, exist_ok=True)-- # Copy the converted file with a timestamp- from shutil import copy2- file_ext = os.path.splitext(converted_filepath)[1] or '.mp3'- debug_filename = f"debug_{recording_id}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}{file_ext}"- debug_filepath = os.path.join(debug_dir, debug_filename)- copy2(converted_filepath, debug_filepath)-- current_app.logger.info(f"DEBUG: Converted file preserved at: {debug_filepath}")- current_app.logger.info(f"DEBUG: Converted file size: {converted_size_mb:.2f} MB ({converted_size} bytes)")- current_app.logger.info(f"DEBUG: Original file: {actual_filename}")- current_app.logger.info(f"DEBUG: Recording ID: {recording_id}")-- except Exception as debug_error:- current_app.logger.warning(f"DEBUG: Failed to preserve converted file: {debug_error}")-- # Clean up the temporary converted file- try:- if converted_filepath and os.path.exists(converted_filepath):- os.remove(converted_filepath)- current_app.logger.info(f"Cleaned up temporary converted file: {converted_filepath}")- except Exception as cleanup_error:- current_app.logger.warning(f"Failed to clean up temporary converted file: {cleanup_error}")-- # Debug logging for ASR response- current_app.logger.info(f"ASR response keys: {list(asr_response_data.keys())}")-- # Log the complete raw JSON response (truncated for readability)- import json as json_module- raw_json_str = json_module.dumps(asr_response_data, indent=2)- if len(raw_json_str) > 5000:- current_app.logger.info(f"Raw ASR response (first 5000 chars): {raw_json_str[:5000]}...")- else:- current_app.logger.info(f"Raw ASR response: {raw_json_str}")-- if 'segments' in asr_response_data:- current_app.logger.info(f"Number of segments: {len(asr_response_data['segments'])}")-- # Collect all unique speakers from the response- all_speakers = set()- segments_with_speakers = 0- segments_without_speakers = 0-- for segment in asr_response_data['segments']:- if 'speaker' in segment and segment['speaker'] is not None:- all_speakers.add(segment['speaker'])- segments_with_speakers += 1- else:- segments_without_speakers += 1-- current_app.logger.info(f"Unique speakers found in raw response: {sorted(list(all_speakers))}")- current_app.logger.info(f"Segments with speakers: {segments_with_speakers}, without speakers: {segments_without_speakers}")-- # Log first few segments for debugging- for i, segment in enumerate(asr_response_data['segments'][:5]):- segment_keys = list(segment.keys())- current_app.logger.info(f"Segment {i} keys: {segment_keys}")- current_app.logger.info(f"Segment {i}: speaker='{segment.get('speaker')}', text='{segment.get('text', '')[:50]}...'")-- # Simplify the JSON data- simplified_segments = []- if 'segments' in asr_response_data and isinstance(asr_response_data['segments'], list):- last_known_speaker = None-- for i, segment in enumerate(asr_response_data['segments']):- speaker = segment.get('speaker')- text = segment.get('text', '').strip()-- # If segment doesn't have a speaker, use the previous segment's speaker- if speaker is None:- if last_known_speaker is not None:- speaker = last_known_speaker- current_app.logger.info(f"Assigned speaker '{speaker}' to segment {i} from previous segment")- else:- speaker = 'UNKNOWN_SPEAKER'- current_app.logger.warning(f"No previous speaker available for segment {i}, using UNKNOWN_SPEAKER")- else:- # Update the last known speaker when we have a valid one- last_known_speaker = speaker-- simplified_segments.append({- 'speaker': speaker,- 'sentence': text,- 'start_time': segment.get('start'),- 'end_time': segment.get('end')- })-- # Log final simplified segments count- current_app.logger.info(f"Created {len(simplified_segments)} simplified segments")- null_speaker_count = sum(1 for seg in simplified_segments if seg['speaker'] is None)- if null_speaker_count > 0:- current_app.logger.warning(f"Found {null_speaker_count} segments with null speakers in final output")-- # Store the simplified JSON as a string- recording.transcription = json.dumps(simplified_segments)-- # Commit the transcription data- db.session.commit()-- # Apply auto speaker labelling if enabled and embeddings available- if recording.speaker_embeddings:- try:- from src.services.speaker_embedding_matcher import (- apply_auto_speaker_labels,- apply_speaker_names_to_transcription,- update_speaker_profiles_from_recording- )- user = User.query.get(recording.user_id)- if user and user.auto_speaker_labelling:- current_app.logger.info(f"Applying auto speaker labelling for recording {recording.id}")- speaker_map = apply_auto_speaker_labels(recording, user)- if speaker_map:- current_app.logger.info(f"Auto-matched speakers: {speaker_map}")- if apply_speaker_names_to_transcription(recording, speaker_map):- current_app.logger.info(f"Applied speaker names to transcription")- updated_count = update_speaker_profiles_from_recording(recording, speaker_map, user)- if updated_count > 0:- current_app.logger.info(f"Updated {updated_count} speaker profiles with new embeddings")- else:- current_app.logger.warning(f"Failed to apply speaker names to transcription for recording {recording.id}")- else:- current_app.logger.info(f"No speakers matched for auto-labelling")- except Exception as auto_label_err:- current_app.logger.warning(f"Failed to apply auto speaker labelling: {auto_label_err}")-- # Calculate and save transcription duration- transcription_end_time = time.time()- recording.transcription_duration_seconds = int(transcription_end_time - transcription_start_time)- db.session.commit()- current_app.logger.info(f"ASR transcription completed for recording {recording_id} in {recording.transcription_duration_seconds}s.")-- # Record transcription usage for billing/budgeting (legacy ASR)- try:- audio_duration = None- if chunking_service:- audio_duration = chunking_service.get_audio_duration(filepath)-- if audio_duration and audio_duration > 0:- transcription_tracker.record_usage(- user_id=recording.user_id,- connector_type='asr_endpoint',- audio_duration_seconds=int(audio_duration),- model_name='asr-endpoint'- )- current_app.logger.info(f"Recorded ASR transcription usage: {int(audio_duration)}s for user {recording.user_id}")- except Exception as usage_err:- current_app.logger.warning(f"Failed to record ASR transcription usage: {usage_err}")-- # Check if auto-summarization is disabled (admin setting or user preference)- admin_setting = SystemSetting.get_setting('disable_auto_summarization', False)- admin_disabled = admin_setting if isinstance(admin_setting, bool) else str(admin_setting).lower() == 'true'- user = User.query.get(recording.user_id)- user_disabled = user and user.auto_summarization is False- will_auto_summarize = not admin_disabled and not user_disabled-- # Generate title immediately (pass flag so it knows whether to set COMPLETED)- generate_title_task(app_context, recording_id, will_auto_summarize=will_auto_summarize)-- if not will_auto_summarize:- reason = "admin setting" if admin_disabled else "user preference"- current_app.logger.info(f"Auto-summarization disabled ({reason}), skipping summary for recording {recording_id}")- recording = db.session.get(Recording, recording_id)- if recording:- recording.status = 'COMPLETED'- recording.completed_at = datetime.utcnow()- db.session.commit()-- # Apply auto-shares for group tags after processing completes- apply_team_tag_auto_shares(recording_id)-- # Export transcription-only if auto-export is enabled- if ENABLE_AUTO_EXPORT:- export_recording(recording_id)- else:- # Auto-generate summary for all recordings- current_app.logger.info(f"Auto-generating summary for recording {recording_id}")- generate_summary_only_task(app_context, recording_id)-- except Exception as e:- db.session.rollback()-- # Handle timeout errors specifically- error_msg = str(e)- error_type = type(e).__name__- current_app.logger.error(f"ASR processing FAILED for recording {recording_id}: [{error_type}] {error_msg}")-- if "timed out" in error_msg.lower() or "timeout" in error_msg.lower() or "Timeout" in error_type:- asr_timeout = SystemSetting.get_setting('asr_timeout_seconds', 1800)- current_app.logger.error(f"Timeout details - configured ASR timeout: {asr_timeout}s. Error: {error_msg}")-- # Don't set recording.status = 'FAILED' here - let the job queue handle it- # The job queue will decide whether to retry or permanently fail,- # and only set FAILED status when all retries are exhausted-- # Re-raise so job queue marks the job as failed (and potentially retries)- raise--def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr, start_time, language=None, min_speakers=None, max_speakers=None, tag_id=None):"""Runs the transcription and summarization in a background thread.+ Uses the connector-based architecture which supports:+ - OpenAI Whisper (whisper-1)+ - OpenAI GPT-4o Transcribe (gpt-4o-transcribe, gpt-4o-mini-transcribe)+ - OpenAI GPT-4o Transcribe Diarize (gpt-4o-transcribe-diarize)+ - Custom ASR endpoints (whisper-asr-webservice, WhisperX, etc.)+Args:app_context: Flask app contextrecording_id: ID of the recording to process@@ -2119,524 +1746,40 @@ def transcribe_audio_task(app_context, recording_id, filepath, filename_for_asr,max_speakers: Optional maximum speakers override (from upload form)tag_id: Optional tag ID to apply custom prompt from"""- # Use new connector-based architecture if enabled- if USE_NEW_TRANSCRIPTION_ARCHITECTURE:- with app_context:- recording = db.session.get(Recording, recording_id)- # Determine diarization setting based on connector capabilities- # The connector will handle this, but we pass the user's preference- diarize_setting = None # Let connector decide based on its capabilities-- # Use language from upload form if provided, otherwise use user's default- # language='' (empty string) means auto-detect, language=None means use default- if language is not None:- # Explicit language selection (including empty string for auto-detect)- user_transcription_language = language if language else None # '' becomes None for connector- else:- # No language specified - use user's default- user_transcription_language = recording.owner.transcription_language if recording and recording.owner else None-- mime_type = recording.mime_type if recording else None-- transcribe_with_connector(- app_context, recording_id, filepath, filename_for_asr, start_time,- mime_type=mime_type,- language=user_transcription_language,- diarize=diarize_setting,- min_speakers=min_speakers,- max_speakers=max_speakers,- tag_id=tag_id- )-- # After transcription completes, calculate processing time- with app_context:- recording = db.session.get(Recording, recording_id)- if recording and recording.status in ['COMPLETED', 'FAILED']:- end_time = datetime.utcnow()- recording.processing_time_seconds = (end_time - start_time).total_seconds()- db.session.commit()- return-- # Legacy path: use old implementation for backwards compatibility- if USE_ASR_ENDPOINT:- with app_context:- recording = db.session.get(Recording, recording_id)- # Environment variable ASR_DIARIZE overrides user setting- if 'ASR_DIARIZE' in os.environ:- diarize_setting = ASR_DIARIZE- elif USE_ASR_ENDPOINT:- # When using ASR endpoint, use the configured ASR_DIARIZE value- diarize_setting = ASR_DIARIZE- else:- diarize_setting = recording.owner.diarize if recording.owner else False-- # Use language from upload form if provided, otherwise use user's default- if language:- user_transcription_language = language- else:- user_transcription_language = recording.owner.transcription_language if recording.owner else None- # Use min/max speakers from upload form (already processed with precedence hierarchy)- # If None, ASR will auto-detect the number of speakers- final_min_speakers = min_speakers- final_max_speakers = max_speakers-- transcribe_audio_asr(app_context, recording_id, filepath, filename_for_asr, start_time,- mime_type=recording.mime_type,- language=user_transcription_language,- diarize=diarize_setting,- min_speakers=final_min_speakers,- max_speakers=final_max_speakers,- tag_id=tag_id)-- # After ASR task completes, calculate processing time- with app_context:- recording = db.session.get(Recording, recording_id)- if recording.status in ['COMPLETED', 'FAILED']:- end_time = datetime.utcnow()- recording.processing_time_seconds = (end_time - start_time).total_seconds()- db.session.commit()- return-- with app_context: # Need app context for db operations in thread+ with app_context:recording = db.session.get(Recording, recording_id)- if not recording:- current_app.logger.error(f"Error: Recording {recording_id} not found for transcription.")- return-- try:- current_app.logger.info(f"Starting transcription for recording {recording_id} ({filename_for_asr})...")- recording.status = 'PROCESSING'- transcription_start_time = time.time()- db.session.commit()-- # Check if chunking is needed for large files- # Get connector specifications for smart chunking decisions- connector_specs = None- if USE_NEW_TRANSCRIPTION_ARCHITECTURE:- try:- from src.services.transcription import get_registry- registry = get_registry()- connector = registry.get_active_connector()- if connector:- connector_specs = connector.specifications- except Exception as e:- current_app.logger.warning(f"Could not get connector specs for chunking: {e}")-- # Use connector-aware chunking (respects connector.handles_chunking_internally,- # user ENV settings, and connector defaults in that priority order)- should_chunk = (chunking_service and- chunking_service.needs_chunking(filepath, USE_ASR_ENDPOINT, connector_specs))-- if should_chunk:- current_app.logger.info(f"File {filepath} is large ({os.path.getsize(filepath)/1024/1024:.1f}MB), using chunking for transcription")- transcription_text = transcribe_with_chunking(app_context, recording_id, filepath, filename_for_asr)- else:- # --- Standard transcription for smaller files ---- transcription_text = transcribe_single_file(filepath, recording)-- recording.transcription = transcription_text-- # Calculate and save transcription duration- transcription_end_time = time.time()- recording.transcription_duration_seconds = int(transcription_end_time - transcription_start_time)- db.session.commit()- current_app.logger.info(f"Transcription completed for recording {recording_id} in {recording.transcription_duration_seconds}s. Text length: {len(recording.transcription)}")-- # Record transcription usage for billing/budgeting (legacy Whisper API)- try:- audio_duration = None- if chunking_service:- audio_duration = chunking_service.get_audio_duration(filepath)-- if audio_duration and audio_duration > 0:- transcription_tracker.record_usage(- user_id=recording.user_id,- connector_type='openai_whisper',- audio_duration_seconds=int(audio_duration),- model_name='whisper-1'- )- current_app.logger.info(f"Recorded Whisper transcription usage: {int(audio_duration)}s for user {recording.user_id}")- except Exception as usage_err:- current_app.logger.warning(f"Failed to record Whisper transcription usage: {usage_err}")-- # Check if auto-summarization is disabled (admin setting or user preference)- admin_setting = SystemSetting.get_setting('disable_auto_summarization', False)- admin_disabled = admin_setting if isinstance(admin_setting, bool) else str(admin_setting).lower() == 'true'- user = User.query.get(recording.user_id)- user_disabled = user and user.auto_summarization is False- will_auto_summarize = not admin_disabled and not user_disabled-- # Generate title immediately (pass flag so it knows whether to set COMPLETED)- generate_title_task(app_context, recording_id, will_auto_summarize=will_auto_summarize)-- if not will_auto_summarize:- reason = "admin setting" if admin_disabled else "user preference"- current_app.logger.info(f"Auto-summarization disabled ({reason}), skipping summary for recording {recording_id}")- recording.status = 'COMPLETED'- recording.completed_at = datetime.utcnow()- db.session.commit()-- # Apply auto-shares for group tags after processing completes- apply_team_tag_auto_shares(recording_id)-- # Export transcription-only if auto-export is enabled- if ENABLE_AUTO_EXPORT:- export_recording(recording_id)- else:- # Auto-generate summary for all recordings- current_app.logger.info(f"Auto-generating summary for recording {recording_id}")- generate_summary_only_task(app_context, recording_id)-- except Exception as e:- db.session.rollback() # Rollback if any step failed critically- current_app.logger.error(f"Processing FAILED for recording {recording_id}: {str(e)}", exc_info=True)- # Retrieve recording again in case session was rolled back- recording = db.session.get(Recording, recording_id)- if recording:- # Ensure status reflects failure even after rollback/retrieve attempt- if recording.status not in ['COMPLETED', 'FAILED']: # Avoid overwriting final state- recording.status = 'FAILED'- if not recording.transcription: # If transcription itself failed- recording.transcription = format_error_for_storage(str(e))- # Add error note to summary if appropriate stage was reached- if recording.status == 'SUMMARIZING' and not recording.summary:- recording.summary = f"[Processing failed during summarization: {str(e)}]"-- end_time = datetime.utcnow()- recording.processing_time_seconds = (end_time - start_time).total_seconds()- db.session.commit()-- # Re-raise so job queue marks the job as failed- raise---def transcribe_single_file(filepath, recording):- """Transcribe a single audio file using OpenAI Whisper API."""-- # Check if we need to extract audio from video container- actual_filepath = filepath- mime_type = recording.mime_type if recording else None-- # Use codec detection to check if file is a video- try:- is_video = is_video_file(filepath, timeout=10)- if is_video:- current_app.logger.info(f"Video detected via codec analysis for {filepath}")- except FFProbeError as e:- current_app.logger.warning(f"Failed to probe {filepath}: {e}. Falling back to MIME type detection.")- # Fallback to MIME type detection- if mime_type:- is_video = mime_type.startswith('video/')+ # Determine diarization setting based on connector capabilities+ # The connector will handle this, but we pass the user's preference+ diarize_setting = None # Let connector decide based on its capabilities++ # Use language from upload form if provided, otherwise use user's default+ # language='' (empty string) means auto-detect, language=None means use default+ if language is not None:+ # Explicit language selection (including empty string for auto-detect)+ user_transcription_language = language if language else None # '' becomes None for connectorelse:- is_video = False-- if is_video:- current_app.logger.info(f"Video container detected for Whisper transcription, extracting audio...")- try:- # Extract audio from video (uses compression settings)- audio_filepath, audio_mime_type = extract_audio_from_video(filepath)- actual_filepath = audio_filepath-- # Update recording with extracted audio path and new MIME type if recording exists- if recording:- recording.audio_path = audio_filepath- recording.mime_type = audio_mime_type- db.session.commit()-- current_app.logger.info(f"Audio extracted successfully for Whisper: {audio_filepath}")- except Exception as e:- current_app.logger.error(f"Failed to extract audio from video for Whisper: {str(e)}")- if recording:- recording.status = 'FAILED'- recording.error_msg = f"Audio extraction failed: {str(e)}"- db.session.commit()- raise Exception(f"Audio extraction failed: {str(e)}")-- # List of formats supported by Whisper API- WHISPER_SUPPORTED_FORMATS = ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm']-- # Get user transcription language preference- user_transcription_language = None- if recording and recording.owner:- user_transcription_language = recording.owner.transcription_language-- transcription_language = user_transcription_language-- try:- with open(actual_filepath, 'rb') as audio_file:- transcription_client = OpenAI(- api_key=transcription_api_key,- base_url=transcription_base_url,- http_client=http_client_no_proxy- )- whisper_model = os.environ.get("WHISPER_MODEL", "Systran/faster-distil-whisper-large-v3")-- transcription_params = {- "model": whisper_model,- "file": audio_file- }-- if transcription_language:- transcription_params["language"] = transcription_language- current_app.logger.info(f"Using transcription language: {transcription_language}")- else:- current_app.logger.info("Transcription language not set, using auto-detection or service default.")-- transcript = transcription_client.audio.transcriptions.create(**transcription_params)- return transcript.text-- except Exception as e:- # Check if it's a format error- error_message = str(e)- if "Invalid file format" in error_message or "Supported formats" in error_message:- file_ext = os.path.splitext(actual_filepath)[1].lower().lstrip('.')- current_app.logger.warning(f"Unsupported audio format '{file_ext}' detected, converting to MP3...")-- # Convert to MP3 using centralized utility- temp_mp3_filepath = None- try:- temp_mp3_filepath = convert_to_mp3(actual_filepath)- current_app.logger.info(f"Successfully converted {actual_filepath} to MP3 format")-- # Retry transcription with converted file- with open(temp_mp3_filepath, 'rb') as audio_file:- transcription_client = OpenAI(- api_key=transcription_api_key,- base_url=transcription_base_url,- http_client=http_client_no_proxy- )-- whisper_model = os.environ.get("WHISPER_MODEL", "Systran/faster-distil-whisper-large-v3")- transcription_params = {- "model": whisper_model,- "file": audio_file- }-- if transcription_language:- transcription_params["language"] = transcription_language-- transcript = transcription_client.audio.transcriptions.create(**transcription_params)- return transcript.text-- except (FFmpegError, FFmpegNotFoundError) as conv_error:- current_app.logger.error(f"Failed to convert audio format: {conv_error}")- raise Exception(f"Audio format conversion failed: {str(conv_error)}")- finally:- # Clean up temporary converted file- if temp_mp3_filepath and os.path.exists(temp_mp3_filepath):- try:- os.unlink(temp_mp3_filepath)- current_app.logger.info(f"Cleaned up temporary converted file: {temp_mp3_filepath}")- except Exception as cleanup_error:- current_app.logger.warning(f"Failed to clean up temporary file {temp_mp3_filepath}: {cleanup_error}")- else:- # Re-raise if it's not a format error- raise----def transcribe_with_chunking(app_context, recording_id, filepath, filename_for_asr):- """Transcribe a large audio file using chunking."""- import tempfile+ # No language specified - use user's default+ user_transcription_language = recording.owner.transcription_language if recording and recording.owner else None++ mime_type = recording.mime_type if recording else None++ transcribe_with_connector(+ app_context, recording_id, filepath, filename_for_asr, start_time,+ mime_type=mime_type,+ language=user_transcription_language,+ diarize=diarize_setting,+ min_speakers=min_speakers,+ max_speakers=max_speakers,+ tag_id=tag_id+ )+ # After transcription completes, calculate processing timewith app_context:recording = db.session.get(Recording, recording_id)- if not recording:- raise ValueError(f"Recording {recording_id} not found")-- # Create temporary directory for chunks- with tempfile.TemporaryDirectory() as temp_dir:- try:- # Create chunks- current_app.logger.info(f"Creating chunks for large file: {filepath}")- chunks = chunking_service.create_chunks(filepath, temp_dir)-- if not chunks:- raise ChunkProcessingError("No chunks were created from the audio file")-- current_app.logger.info(f"Created {len(chunks)} chunks, processing each with Whisper API...")-- # Process each chunk with proper timeout and retry handling- chunk_results = []-- # Create HTTP client with proper timeouts- timeout_config = httpx.Timeout(- connect=30.0, # 30 seconds to establish connection- read=300.0, # 5 minutes to read response (for large audio files)- write=60.0, # 1 minute to write request- pool=10.0 # 10 seconds to get connection from pool- )-- http_client_with_timeout = httpx.Client(- verify=True,- timeout=timeout_config,- limits=httpx.Limits(max_connections=5, max_keepalive_connections=2)- )-- transcription_client = OpenAI(- api_key=transcription_api_key,- base_url=transcription_base_url,- http_client=http_client_with_timeout,- max_retries=3, # Increased retries for better reliability- timeout=300.0 # 5 minute timeout for API calls- )- whisper_model = os.environ.get("WHISPER_MODEL", "Systran/faster-distil-whisper-large-v3")-- # Get user language preference- user_transcription_language = None- with app_context:- recording = db.session.get(Recording, recording_id)- if recording and recording.owner:- user_transcription_language = recording.owner.transcription_language-- for i, chunk in enumerate(chunks):- max_chunk_retries = 3- chunk_retry_count = 0- chunk_success = False-- while chunk_retry_count < max_chunk_retries and not chunk_success:- try:- retry_suffix = f" (retry {chunk_retry_count + 1}/{max_chunk_retries})" if chunk_retry_count > 0 else ""- current_app.logger.info(f"Processing chunk {i+1}/{len(chunks)}: {chunk['filename']} ({chunk['size_mb']:.1f}MB){retry_suffix}")-- # Log detailed timing for each step- step_start_time = time.time()-- # Step 1: File opening- file_open_start = time.time()- with open(chunk['path'], 'rb') as chunk_file:- file_open_time = time.time() - file_open_start- current_app.logger.info(f"Chunk {i+1}: File opened in {file_open_time:.2f}s")-- # Step 2: Prepare transcription parameters- param_start = time.time()- transcription_params = {- "model": whisper_model,- "file": chunk_file- }-- if user_transcription_language:- transcription_params["language"] = user_transcription_language-- param_time = time.time() - param_start- current_app.logger.info(f"Chunk {i+1}: Parameters prepared in {param_time:.2f}s")-- # Step 3: API call with detailed timing- api_start = time.time()- current_app.logger.info(f"Chunk {i+1}: Starting API call to {transcription_base_url}")-- # Log connection details- current_app.logger.info(f"Chunk {i+1}: Using timeout config - connect: 30s, read: 300s, write: 60s")- current_app.logger.info(f"Chunk {i+1}: Max retries: 2, API timeout: 300s")-- try:- transcript = transcription_client.audio.transcriptions.create(**transcription_params)- except Exception as chunk_error:- # Check if it's a format error (unlikely for chunks since they're MP3, but handle it)- error_msg = str(chunk_error)- if "Invalid file format" in error_msg or "Supported formats" in error_msg:- current_app.logger.warning(f"Chunk {i+1} format issue, attempting conversion...")- # Convert chunk to MP3 if needed- temp_mp3_path = None- try:- temp_mp3_path = convert_to_mp3(chunk['path'])- with open(temp_mp3_path, 'rb') as converted_chunk:- transcription_params['file'] = converted_chunk- transcript = transcription_client.audio.transcriptions.create(**transcription_params)- except (FFmpegError, FFmpegNotFoundError) as conv_error:- current_app.logger.error(f"Failed to convert chunk {i+1}: {conv_error}")- raise chunk_error- finally:- if temp_mp3_path and os.path.exists(temp_mp3_path):- os.unlink(temp_mp3_path)- else:- raise-- api_time = time.time() - api_start- current_app.logger.info(f"Chunk {i+1}: API call completed in {api_time:.2f}s")-- # Step 4: Process response- response_start = time.time()- chunk_result = {- 'index': chunk['index'],- 'start_time': chunk['start_time'],- 'end_time': chunk['end_time'],- 'duration': chunk['duration'],- 'size_mb': chunk['size_mb'],- 'transcription': transcript.text,- 'filename': chunk['filename'],- 'processing_time': api_time # Store the actual API processing time- }- chunk_results.append(chunk_result)- response_time = time.time() - response_start-- total_time = time.time() - step_start_time- current_app.logger.info(f"Chunk {i+1}: Response processed in {response_time:.2f}s")- current_app.logger.info(f"Chunk {i+1}: Total processing time: {total_time:.2f}s")- current_app.logger.info(f"Chunk {i+1} transcribed successfully: {len(transcript.text)} characters")- chunk_success = True-- except Exception as chunk_error:- chunk_retry_count += 1- error_msg = str(chunk_error)-- if chunk_retry_count < max_chunk_retries:- # Determine wait time based on error type- if "timeout" in error_msg.lower() or "timed out" in error_msg.lower():- wait_time = 30 # 30 seconds for timeout errors- elif "rate limit" in error_msg.lower():- wait_time = 60 # 1 minute for rate limit errors- else:- wait_time = 15 # 15 seconds for other errors-- current_app.logger.warning(f"Chunk {i+1} failed (attempt {chunk_retry_count}/{max_chunk_retries}): {chunk_error}. Retrying in {wait_time} seconds...")- time.sleep(wait_time)- else:- current_app.logger.error(f"Chunk {i+1} failed after {max_chunk_retries} attempts: {chunk_error}")- # Add failed chunk to results- chunk_result = {- 'index': chunk['index'],- 'start_time': chunk['start_time'],- 'end_time': chunk['end_time'],- 'transcription': f"[Chunk {i+1} transcription failed after {max_chunk_retries} attempts: {str(chunk_error)}]",- 'filename': chunk['filename']- }- chunk_results.append(chunk_result)-- # Add small delay between chunks to avoid overwhelming the API- if i < len(chunks) - 1: # Don't delay after the last chunk- time.sleep(2)-- # Merge transcriptions- current_app.logger.info(f"Merging {len(chunk_results)} chunk transcriptions...")- merged_transcription = chunking_service.merge_transcriptions(chunk_results)-- if not merged_transcription.strip():- raise ChunkProcessingError("Merged transcription is empty")-- # Log detailed performance statistics and analysis- chunking_service.log_processing_statistics(chunk_results)-- # Get performance recommendations- recommendations = chunking_service.get_performance_recommendations(chunk_results)- if recommendations:- current_app.logger.info("=== PERFORMANCE RECOMMENDATIONS ===")- for i, rec in enumerate(recommendations, 1):- current_app.logger.info(f"{i}. {rec}")- current_app.logger.info("=== END RECOMMENDATIONS ===")-- current_app.logger.info(f"Chunked transcription completed. Final length: {len(merged_transcription)} characters")- return merged_transcription-- except Exception as e:- current_app.logger.error(f"Chunking transcription failed for {filepath}: {e}")- # Clean up chunks if they exist- if 'chunks' in locals():- chunking_service.cleanup_chunks(chunks)- raise ChunkProcessingError(f"Chunked transcription failed: {str(e)}")- finally:- # Cleanup is handled by tempfile.TemporaryDirectory context manager- pass+ if recording and recording.status in ['COMPLETED', 'FAILED']:+ end_time = datetime.utcnow()+ recording.processing_time_seconds = (end_time - start_time).total_seconds()+ db.session.commit()def transcribe_incognito(filepath, original_filename, language=None, min_speakers=None, max_speakers=None, user=None):