Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Track embedding API token usage and cost; surface in admin Vector Store tab; add re-embed-all action
Murtaza Nasirfeaf86287789
diff --git a/src/api/admin.py b/src/api/admin.pyindex 1dac4f2f..e7750435 100644--- a/src/api/admin.py+++ b/src/api/admin.py@@ -1067,69 +1067,111 @@ def admin_trigger_auto_process():@admin_bp.route('/admin/inquire/process-recordings', methods=['POST'])@login_requireddef admin_process_recordings_for_inquire():- """Process all remaining recordings for inquire mode (chunk and embed them)."""+ """Process recordings for inquire mode (chunk and embed them).++ Default behaviour processes only recordings that do not yet have chunks.+ Pass ``force=true`` in the JSON body to re-embed every completed+ recording regardless of existing chunks. This is the migration path after+ swapping ``EMBEDDING_MODEL`` or ``EMBEDDING_BASE_URL``: the old vectors+ are deleted and replaced with vectors from the new configuration.++ Body fields:+ force (bool, optional, default false): re-embed all recordings+ batch_size (int, optional, default 10): commit cadence+ max_recordings (int, optional): cap the number processed in one call+ """if not current_user.is_admin:return jsonify({'error': 'Unauthorized'}), 403-+try:- # Get optional parameters from requestdata = request.json or {}- batch_size = data.get('batch_size', 10)+ batch_size = int(data.get('batch_size', 10) or 10)max_recordings = data.get('max_recordings', None)-- # Find recordings that need processing- completed_recordings = Recording.query.filter_by(status='COMPLETED').all()- recordings_needing_processing = []-- for recording in completed_recordings:- if recording.transcription: # Has transcription- chunk_count = TranscriptChunk.query.filter_by(recording_id=recording.id).count()- if chunk_count == 0: # No chunks yet- recordings_needing_processing.append(recording)- if max_recordings and len(recordings_needing_processing) >= max_recordings:- break-+ force = bool(data.get('force', False))++ if force:+ # Re-embed every completed recording with a transcription.+ recordings_query = Recording.query.filter(+ Recording.status == 'COMPLETED',+ Recording.transcription.isnot(None),+ Recording.transcription != ''+ )+ recordings_needing_processing = recordings_query.all()+ else:+ # Only process recordings that have no chunks yet.+ recordings_with_chunks = db.session.query(TranscriptChunk.recording_id).distinct()+ recordings_needing_processing = Recording.query.filter(+ Recording.status == 'COMPLETED',+ Recording.transcription.isnot(None),+ Recording.transcription != '',+ ~Recording.id.in_(recordings_with_chunks)+ ).all()++ if max_recordings:+ recordings_needing_processing = recordings_needing_processing[:int(max_recordings)]+total_to_process = len(recordings_needing_processing)-+if total_to_process == 0:return jsonify({'success': True,- 'message': 'All recordings are already processed for inquire mode.',+ 'message': (+ 'No recordings to re-embed.' if force+ else 'All recordings are already processed for inquire mode.'+ ),'processed': 0,- 'total': 0+ 'total': 0,+ 'force': force,})-- # Process recordings in batches+processed = 0failed = []-+for recording in recordings_needing_processing:try:success = process_recording_chunks(recording.id)if success:processed += 1- current_app.logger.info(f"Admin API: Processed chunks for recording: {recording.title} ({recording.id})")+ current_app.logger.info(+ f"Admin API: {'Re-embedded' if force else 'Processed chunks for'} "+ f"recording: {recording.title} ({recording.id})"+ )else:failed.append({'id': recording.id, 'title': recording.title, 'reason': 'Processing returned false'})except Exception as e:current_app.logger.error(f"Admin API: Failed to process recording {recording.id}: {e}")failed.append({'id': recording.id, 'title': recording.title, 'reason': str(e)})-- # Commit after each batch+if processed % batch_size == 0:db.session.commit()-- # Final commit+db.session.commit()-++ # On a successful force re-embed, refresh the stored identifier so the+ # mismatch warning clears.+ if force and processed > 0 and not failed:+ try:+ from src.models import SystemSetting+ from src.services.embeddings import EMBEDDING_IDENTIFIER+ SystemSetting.set_setting(+ key='embedding_identifier',+ value=EMBEDDING_IDENTIFIER,+ description='Identifier of the embedding provider and model that produced the stored chunk vectors. Used to detect dimensionality and semantic-space mismatches at startup.',+ setting_type='string',+ )+ except Exception as e:+ current_app.logger.warning(f"Failed to refresh embedding_identifier after re-embed: {e}")++ verb = 'Re-embedded' if force else 'Processed'return jsonify({'success': True,- 'message': f'Processed {processed} out of {total_to_process} recordings.',+ 'message': f'{verb} {processed} out of {total_to_process} recordings.','processed': processed,'total': total_to_process,- 'failed': failed+ 'failed': failed,+ 'force': force,})-+except Exception as e:current_app.logger.error(f"Error in admin process recordings endpoint: {e}")db.session.rollback()@@ -1138,48 +1180,84 @@ def admin_process_recordings_for_inquire():@admin_bp.route('/admin/inquire/status', methods=['GET'])-@login_required+@login_requireddef admin_inquire_status():"""Get the status of recordings for inquire mode."""if not current_user.is_admin:return jsonify({'error': 'Unauthorized'}), 403-+try:+ from src.services.embeddings import (+ EMBEDDING_MODEL, EMBEDDING_BASE_URL, EMBEDDING_IDENTIFIER,+ USE_API_EMBEDDINGS, EMBEDDING_DIMENSIONS,+ )+ from src.models import SystemSetting, TokenUsage+ from sqlalchemy import func+# Count total completed recordingstotal_completed = Recording.query.filter_by(status='COMPLETED').count()-+# Count recordings with transcriptionsrecordings_with_transcriptions = Recording.query.filter(Recording.status == 'COMPLETED',Recording.transcription.isnot(None),Recording.transcription != '').count()-+# Count recordings that have been processed for inquire modeprocessed_recordings = db.session.query(Recording.id).join(TranscriptChunk, Recording.id == TranscriptChunk.recording_id).distinct().count()-- # Count recordings that still need processing- completed_recordings = Recording.query.filter_by(status='COMPLETED').all()- need_processing = 0-- for recording in completed_recordings:- if recording.transcription: # Has transcription- chunk_count = TranscriptChunk.query.filter_by(recording_id=recording.id).count()- if chunk_count == 0: # No chunks yet- need_processing += 1-- # Get total chunks and embeddings count++ # Count recordings that still need processing (have transcription but no chunks)+ need_processing = db.session.query(func.count(Recording.id)).filter(+ Recording.status == 'COMPLETED',+ Recording.transcription.isnot(None),+ Recording.transcription != '',+ ~Recording.id.in_(db.session.query(TranscriptChunk.recording_id).distinct())+ ).scalar() or 0++ # Total chunks across the systemtotal_chunks = TranscriptChunk.query.count()-++ # Detect embedded chunks that pre-date the current configuration. The+ # stored identifier is what produced the existing vectors; if it does+ # not match the current EMBEDDING_IDENTIFIER, those chunks are stale+ # and Inquire mode will return wrong results until they are re-embedded.+ stored_identifier = SystemSetting.get_setting('embedding_identifier', None)+ if stored_identifier is None:+ legacy = SystemSetting.get_setting('embedding_model_name', None)+ stored_identifier = f"local::{legacy}" if legacy else None++ identifier_mismatch = bool(+ stored_identifier and stored_identifier != EMBEDDING_IDENTIFIER and total_chunks > 0+ )++ # Embedding-API usage aggregates (lifetime totals across all users).+ usage_rows = TokenUsage.query.filter_by(operation_type='embedding').all()+ embedding_usage = {+ 'total_tokens': sum(u.total_tokens for u in usage_rows),+ 'total_cost': sum((u.cost or 0.0) for u in usage_rows),+ 'request_count': sum(u.request_count for u in usage_rows),+ }+return jsonify({'total_completed_recordings': total_completed,'recordings_with_transcriptions': recordings_with_transcriptions,'processed_for_inquire': processed_recordings,'need_processing': need_processing,'total_chunks': total_chunks,- 'embeddings_available': EMBEDDINGS_AVAILABLE+ 'embeddings_available': EMBEDDINGS_AVAILABLE,+ # Embedding configuration surfaced to the UI+ 'embedding_model': EMBEDDING_MODEL,+ 'embedding_provider': 'api' if USE_API_EMBEDDINGS else 'local',+ 'embedding_base_url': EMBEDDING_BASE_URL or None,+ 'embedding_dimensions_override': EMBEDDING_DIMENSIONS,+ 'embedding_identifier': EMBEDDING_IDENTIFIER,+ 'embedding_identifier_stored': stored_identifier,+ 'embedding_identifier_mismatch': identifier_mismatch,+ # Embedding-API usage (only meaningful in API mode; local mode = 0)+ 'embedding_usage': embedding_usage,})except Exception as e:diff --git a/src/services/embeddings.py b/src/services/embeddings.pyindex dd427a0b..421c9c35 100644--- a/src/services/embeddings.py+++ b/src/services/embeddings.py@@ -115,8 +115,15 @@ def get_embedding_api_client():return _embedding_api_client-def _api_embed(texts):- """Call the OpenAI-compatible embeddings endpoint and return numpy vectors."""+def _api_embed(texts, user_id=None):+ """Call the OpenAI-compatible embeddings endpoint and return numpy vectors.++ When ``user_id`` is provided and the response includes a ``usage`` block,+ record the call against the daily token-usage aggregate so admins can see+ embedding cost alongside chat/summarization. Cost is captured when the+ provider returns it (OpenRouter populates ``usage.cost``); for providers+ that omit it, only token counts are recorded.+ """client = get_embedding_api_client()if client is None or not texts:return []@@ -125,6 +132,29 @@ def _api_embed(texts):if EMBEDDING_DIMENSIONS is not None:kwargs['dimensions'] = EMBEDDING_DIMENSIONSresponse = client.embeddings.create(**kwargs)++ if user_id is not None and getattr(response, 'usage', None) is not None:+ try:+ usage = response.usage+ # Pydantic v2 stores non-schema fields under model_extra; fall+ # back to attribute access for SDKs that surface them directly.+ extras = getattr(usage, 'model_extra', None) or {}+ prompt_tokens = getattr(usage, 'prompt_tokens', 0) or 0+ total_tokens = getattr(usage, 'total_tokens', prompt_tokens) or prompt_tokens+ cost = extras.get('cost') if 'cost' in extras else getattr(usage, 'cost', None)+ from src.services.token_tracking import token_tracker+ token_tracker.record_usage(+ user_id=user_id,+ operation_type='embedding',+ prompt_tokens=int(prompt_tokens),+ completion_tokens=0,+ total_tokens=int(total_tokens),+ model_name=EMBEDDING_MODEL,+ cost=float(cost) if cost is not None else None,+ )+ except Exception as track_err:+ current_app.logger.warning(f"Failed to record embedding usage: {track_err}")+return [np.array(d.embedding, dtype=np.float32) for d in response.data]except Exception as e:current_app.logger.error(f"Embedding API call failed: {e}")@@ -181,7 +211,7 @@ def chunk_transcription(transcription, max_chunk_length=500, overlap=50):-def generate_embeddings(texts):+def generate_embeddings(texts, user_id=None):"""Generate embeddings for a list of texts.@@ -190,6 +220,9 @@ def generate_embeddings(texts):Args:texts (list): List of text strings+ user_id (int, optional): User to attribute the API call to for token+ and cost tracking. Local-mode calls do not consume billable usage+ and ignore this argument.Returns:list: List of embedding vectors as numpy arrays, or empty list if no@@ -199,7 +232,7 @@ def generate_embeddings(texts):return []if USE_API_EMBEDDINGS:- return _api_embed(texts)+ return _api_embed(texts, user_id=user_id)if not LOCAL_EMBEDDINGS_AVAILABLE:current_app.logger.warning("Embeddings not available - skipping embedding generation")@@ -275,18 +308,18 @@ def process_recording_chunks(recording_id):recording = db.session.get(Recording, recording_id)if not recording or not recording.transcription:return False-+# Delete existing chunks for this recordingTranscriptChunk.query.filter_by(recording_id=recording_id).delete()-+# Create chunkschunks = chunk_transcription(recording.transcription)-+if not chunks:return True-- # Generate embeddings- embeddings = generate_embeddings(chunks)++ # Generate embeddings (recording owner gets billed for API-mode usage)+ embeddings = generate_embeddings(chunks, user_id=recording.user_id)# Store chunks in databasefor i, (chunk_text, embedding) in enumerate(zip(chunks, embeddings)):@@ -437,9 +470,11 @@ def semantic_search_chunks(user_id, query, filters=None, top_k=5):current_app.logger.info("scikit-learn not installed - using basic text search as fallback")return basic_text_search_chunks(user_id, query, filters, top_k)- # Generate embedding for the query (via API or local model)+ # Generate embedding for the query (via API or local model). Attribute+ # the API call to the searching user so embedding cost tracking shows+ # who issued the query.if USE_API_EMBEDDINGS:- api_vectors = _api_embed([query])+ api_vectors = _api_embed([query], user_id=user_id)if not api_vectors:return basic_text_search_chunks(user_id, query, filters, top_k)query_embedding = api_vectors[0]diff --git a/templates/admin.html b/templates/admin.htmlindex 03a4849f..c514cdac 100644--- a/templates/admin.html+++ b/templates/admin.html@@ -857,11 +857,45 @@ Summarization Instructions:</div><div><span class="text-[var(--text-muted)]">${t('adminDashboard.embeddingModel')}:</span>- <span class="ml-2 text-[var(--text-primary)]">all-MiniLM-L6-v2</span>+ <span class="ml-2 text-[var(--text-primary)] break-all">${ inquireStatus.embedding_model || 'all-MiniLM-L6-v2' }</span></div><div>+ <span class="text-[var(--text-muted)]">Provider:</span>+ <span class="ml-2 text-[var(--text-primary)]">+ <template v-if="inquireStatus.embedding_provider === 'api'">+ API (${ inquireStatus.embedding_base_url })+ </template>+ <template v-else>+ Local (sentence-transformers)+ </template>+ </span>+ </div>+ <div v-if="inquireStatus.embedding_dimensions_override"><span class="text-[var(--text-muted)]">${t('adminDashboard.vectorDimensions')}:</span>- <span class="ml-2 text-[var(--text-primary)]">384</span>+ <span class="ml-2 text-[var(--text-primary)]">${ inquireStatus.embedding_dimensions_override }</span>+ </div>+ </div>+ </div>++ <!-- Identifier mismatch alert: chunks were embedded with a previous configuration -->+ <div v-if="inquireStatus.embedding_identifier_mismatch" class="mb-6 p-4 rounded-lg bg-[var(--bg-warning-light)] border border-[var(--border-warning)]">+ <div class="flex items-start">+ <i class="fas fa-exclamation-triangle text-[var(--text-warning-strong)] mt-0.5 mr-3"></i>+ <div class="flex-1">+ <p class="font-semibold text-[var(--text-warning-strong)]">Embedding configuration changed</p>+ <p class="text-sm text-[var(--text-secondary)] mt-1">+ ${ inquireStatus.total_chunks } stored chunks were embedded with+ <code class="px-1 rounded bg-[var(--bg-tertiary)]">${ inquireStatus.embedding_identifier_stored }</code>,+ but the active configuration is+ <code class="px-1 rounded bg-[var(--bg-tertiary)]">${ inquireStatus.embedding_identifier }</code>.+ Inquire mode will return wrong results until you re-embed all recordings.+ </p>+ <button @click="reembedAllRecordings"+ :disabled="isProcessingRecordings"+ class="mt-3 px-4 py-2 bg-[var(--bg-warning-strong)] text-white rounded-lg shadow hover:opacity-90 disabled:opacity-50 disabled:cursor-not-allowed">+ <i :class="isProcessingRecordings ? 'fas fa-spinner fa-spin' : 'fas fa-sync-alt'" class="mr-2"></i>+ ${ isProcessingRecordings ? 'Re-embedding...' : 'Re-embed all recordings now' }+ </button></div></div></div>@@ -937,6 +971,29 @@ Summarization Instructions:<i class="fas fa-tasks text-3xl text-[var(--text-accent)] opacity-50"></i></div></div>++ <!-- Embedding API usage (only meaningful in API mode) -->+ <div v-if="inquireStatus.embedding_provider === 'api'" class="bg-[var(--bg-secondary)] p-6 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <div class="flex items-center justify-between">+ <div>+ <p class="text-sm text-[var(--text-muted)]">Embedding API tokens</p>+ <p class="text-2xl font-semibold text-[var(--text-primary)]">${ (inquireStatus.embedding_usage && inquireStatus.embedding_usage.total_tokens || 0).toLocaleString() }</p>+ <p class="text-xs text-[var(--text-muted)] mt-1">${ inquireStatus.embedding_usage && inquireStatus.embedding_usage.request_count || 0 } API requests</p>+ </div>+ <i class="fas fa-coins text-3xl text-[var(--text-accent)] opacity-50"></i>+ </div>+ </div>++ <div v-if="inquireStatus.embedding_provider === 'api'" class="bg-[var(--bg-secondary)] p-6 rounded-lg border border-[var(--border-primary)] shadow-sm">+ <div class="flex items-center justify-between">+ <div>+ <p class="text-sm text-[var(--text-muted)]">Embedding API cost</p>+ <p class="text-2xl font-semibold text-[var(--text-primary)]">$${ (inquireStatus.embedding_usage && inquireStatus.embedding_usage.total_cost || 0).toFixed(4) }</p>+ <p class="text-xs text-[var(--text-muted)] mt-1" v-if="inquireStatus.embedding_usage && inquireStatus.embedding_usage.total_cost === 0">Provider did not return cost</p>+ </div>+ <i class="fas fa-dollar-sign text-3xl text-[var(--text-accent)] opacity-50"></i>+ </div>+ </div></div><!-- Processing Actions -->@@ -985,6 +1042,19 @@ Summarization Instructions:<p class="text-[var(--text-success-strong)] font-medium">${t('adminDashboard.allRecordingsProcessed')}</p><p class="text-sm text-[var(--text-muted)] mt-1">${t('adminDashboard.vectorStoreUpToDate')}</p></div>++ <!-- Re-embed all (always available) -->+ <div class="mt-6 pt-4 border-t border-[var(--border-primary)]">+ <p class="text-sm text-[var(--text-secondary)] mb-3">+ Re-embed every completed recording with the current configuration. Use this after switching <code class="px-1 rounded bg-[var(--bg-tertiary)]">EMBEDDING_MODEL</code> or <code class="px-1 rounded bg-[var(--bg-tertiary)]">EMBEDDING_BASE_URL</code>. Existing chunks are deleted and replaced.+ </p>+ <button @click="reembedAllRecordings"+ :disabled="isProcessingRecordings || (inquireStatus.processed_for_inquire || 0) === 0"+ class="px-4 py-2 bg-[var(--bg-secondary)] text-[var(--text-secondary)] border border-[var(--border-secondary)] rounded-lg shadow hover:bg-[var(--bg-tertiary)] focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] focus:ring-offset-2 transition duration-150 ease-in-out disabled:opacity-50 disabled:cursor-not-allowed">+ <i :class="isProcessingRecordings ? 'fas fa-spinner fa-spin' : 'fas fa-sync-alt'" class="mr-2"></i>+ ${ isProcessingRecordings ? 'Re-embedding...' : 'Re-embed all recordings' }+ </button>+ </div></div></div>@@ -3039,6 +3109,34 @@ Summarization Instructions:}};+ const reembedAllRecordings = async () => {+ if (isProcessingRecordings.value) return;+ if (!confirm('Re-embed every completed recording with the current configuration? Existing chunks will be deleted and rebuilt. This may take a while and (in API mode) will incur cost.')) {+ return;+ }+ isProcessingRecordings.value = true;+ processingResult.value = null;+ try {+ const response = await fetch('/admin/inquire/process-recordings', {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ 'X-CSRFToken': document.querySelector('meta[name="csrf-token"]').getAttribute('content')+ },+ body: JSON.stringify({ force: true })+ });+ if (!response.ok) throw new Error('Failed to re-embed recordings');+ const data = await response.json();+ processingResult.value = data;+ await loadInquireStatus();+ } catch (error) {+ console.error('Error re-embedding:', error);+ processingResult.value = { success: false, message: 'Failed to re-embed recordings.' };+ } finally {+ isProcessingRecordings.value = false;+ }+ };+const processBatchRecordings = async (batchSize) => {if (isProcessingRecordings.value) return;@@ -3308,6 +3406,7 @@ Summarization Instructions:loadInquireStatus,processAllRecordings,processBatchRecordings,+ reembedAllRecordings,// Default Prompts methodsloadDefaultPrompt,