Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Add speaker identification enhancements: split button, apply suggested, name sanitization, JSON schema option

Murtaza Nasir0d3f0e05a8a8
diff --git a/config/env.whisperx.example b/config/env.whisperx.example
index a16d1b86..229c3c47 100644
--- a/config/env.whisperx.example
+++ b/config/env.whisperx.example
@@ -50,6 +50,12 @@ GPT5_REASONING_EFFORT=medium
# - high: Detailed explanations
GPT5_VERBOSITY=medium
+# --- Auto-Identify Speaker Response Format ---
+# When enabled, auto-identify uses JSON Schema response format (structured outputs)
+# to constrain LLM output to valid SPEAKER_XX keys. Falls back to json_object mode
+# if the model doesn't support it. Leave disabled for widest model compatibility.
+# AUTO_IDENTIFY_RESPONSE_SCHEMA=1
+
# --- Chat Model Configuration (Optional) ---
# Configure a separate model for real-time chat interactions.
# If not set, chat will use the TEXT_MODEL_* settings above.
diff --git a/src/api/recordings.py b/src/api/recordings.py
index aeb1b4c3..9d0d53fa 100644
--- a/src/api/recordings.py
+++ b/src/api/recordings.py
@@ -983,27 +983,112 @@ JSON Response:
from src.services.llm import call_llm_completion
current_app.logger.info(f"[Auto-Identify] Calling LLM directly with prompt")
- completion = call_llm_completion(
- messages=[
- {"role": "system", "content": "You are an expert in analyzing conversation transcripts to identify speakers based on contextual clues in the dialogue. Analyze the conversation carefully to find names mentioned when speakers address each other or introduce themselves. Your response must be a single, valid JSON object containing only the requested speaker identifications."},
- {"role": "user", "content": prompt}
- ],
- temperature=0.2,
- response_format={"type": "json_object"},
- user_id=current_user.id,
- operation_type='speaker_identification'
- )
- response_content = completion.choices[0].message.content
- current_app.logger.info(f"[Auto-Identify] LLM Raw Response: {response_content}")
+ use_schema = os.environ.get('AUTO_IDENTIFY_RESPONSE_SCHEMA', '').strip() in ('1', 'true', 'yes')
+ system_msg = "You are an expert in analyzing conversation transcripts to identify speakers based on contextual clues in the dialogue. Analyze the conversation carefully to find names mentioned when speakers address each other or introduce themselves. Your response must be a single, valid JSON object containing only the requested speaker identifications."
+
+ response_content = None
+ if use_schema:
+ # Build JSON schema response format with constrained keys
+ schema_properties = {label: {"type": "string"} for label in speaker_labels}
+ schema_response_format = {
+ "type": "json_schema",
+ "json_schema": {
+ "name": "speaker_identification",
+ "strict": True,
+ "schema": {
+ "type": "object",
+ "properties": schema_properties,
+ "required": speaker_labels,
+ "additionalProperties": False
+ }
+ }
+ }
+ schema_prompt = prompt + f"\n\nIMPORTANT: Your JSON response must contain exactly these keys: {', '.join(speaker_labels)}"
+ try:
+ current_app.logger.info("[Auto-Identify] Trying json_schema response format")
+ completion = call_llm_completion(
+ messages=[
+ {"role": "system", "content": system_msg},
+ {"role": "user", "content": schema_prompt}
+ ],
+ temperature=0.2,
+ response_format=schema_response_format,
+ user_id=current_user.id,
+ operation_type='speaker_identification'
+ )
+ response_content = completion.choices[0].message.content
+ current_app.logger.info(f"[Auto-Identify] LLM Raw Response (schema mode): {response_content}")
+ except Exception as schema_err:
+ current_app.logger.warning(f"[Auto-Identify] json_schema mode failed, falling back to json_object: {schema_err}")
+ response_content = None
+
+ if response_content is None:
+ completion = call_llm_completion(
+ messages=[
+ {"role": "system", "content": system_msg},
+ {"role": "user", "content": prompt}
+ ],
+ temperature=0.2,
+ response_format={"type": "json_object"},
+ user_id=current_user.id,
+ operation_type='speaker_identification'
+ )
+ response_content = completion.choices[0].message.content
+ current_app.logger.info(f"[Auto-Identify] LLM Raw Response: {response_content}")
from src.utils import safe_json_loads
identified_map = safe_json_loads(response_content, {})
current_app.logger.info(f"[Auto-Identify] Parsed identified_map: {identified_map}")
- # Post-process the map to replace "Unknown" with an empty string
+ # --- Sanitize identified_map ---
+ speaker_label_re = re.compile(r'^SPEAKER_\d{2}$')
+
+ # Detect inverted map ({name: "SPEAKER_XX"}) and flip it
+ if identified_map and all(
+ speaker_label_re.match(str(v)) for v in identified_map.values() if v
+ ) and not any(speaker_label_re.match(str(k)) for k in identified_map.keys()):
+ current_app.logger.warning("[Auto-Identify] Detected inverted map, flipping keys/values")
+ identified_map = {v: k for k, v in identified_map.items() if v}
+
+ sanitized = {}
for speaker_label, identified_name in identified_map.items():
- if identified_name and identified_name.strip().lower() in ["unknown", "n/a", "not available", "unclear"]:
- identified_map[speaker_label] = ""
+ # Skip entries whose key isn't a valid SPEAKER_XX label
+ if not speaker_label_re.match(str(speaker_label)):
+ continue
+ if not identified_name or not isinstance(identified_name, str):
+ sanitized[speaker_label] = ""
+ continue
+
+ name = identified_name.strip()
+
+ # Clear generic placeholders
+ if name.lower() in ["unknown", "n/a", "not available", "unclear", "unidentified", ""]:
+ sanitized[speaker_label] = ""
+ continue
+
+ # Clear label-to-label entries (e.g. "SPEAKER_01": "SPEAKER_02")
+ if speaker_label_re.match(name):
+ sanitized[speaker_label] = ""
+ continue
+
+ # Strip parenthetical content: "John (the host)" → "John"
+ name = re.sub(r'\s*\([^)]*\)', '', name).strip()
+
+ # Take first name segment before comma, semicolon, or slash
+ name = re.split(r'[,;/]', name)[0].strip()
+
+ # Collapse whitespace
+ name = re.sub(r'\s+', ' ', name)
+
+ # Final check: if result still matches SPEAKER_XX, clear it
+ if speaker_label_re.match(name) or not name:
+ sanitized[speaker_label] = ""
+ continue
+
+ sanitized[speaker_label] = name
+
+ identified_map = sanitized
+ current_app.logger.info(f"[Auto-Identify] Sanitized identified_map: {identified_map}")
except Exception as e:
current_app.logger.error(f"[Auto-Identify] Error calling LLM: {e}", exc_info=True)
return jsonify({'error': f'Failed to identify speakers: {str(e)}'}), 500
diff --git a/static/js/modules/composables/speakers.js b/static/js/modules/composables/speakers.js
index 9febadb5..d8525702 100644
--- a/static/js/modules/composables/speakers.js
+++ b/static/js/modules/composables/speakers.js
@@ -174,6 +174,7 @@ export function useSpeakers(state, utils, processedTranscription) {
}
showSpeakerModal.value = false;
+ showAutoIdDropdown.value = false;
highlightedSpeaker.value = null;
// Clear the speaker map to prevent stale data from persisting
speakerMap.value = {};
@@ -735,7 +736,31 @@ export function useSpeakers(state, utils, processedTranscription) {
// Auto-Identify Speakers
// =========================================
- const autoIdentifySpeakers = async () => {
+ // Split button dropdown visibility + click-outside handling
+ const showAutoIdDropdown = Vue.ref(false);
+ const autoIdSplitBtn = Vue.ref(null);
+
+ const onAutoIdClickOutside = (e) => {
+ if (autoIdSplitBtn.value && !autoIdSplitBtn.value.contains(e.target)) {
+ showAutoIdDropdown.value = false;
+ }
+ };
+ Vue.watch(showAutoIdDropdown, (open) => {
+ if (open) {
+ document.addEventListener('click', onAutoIdClickOutside, true);
+ } else {
+ document.removeEventListener('click', onAutoIdClickOutside, true);
+ }
+ });
+
+ /**
+ * Auto-identify speakers via LLM.
+ * @param {boolean} identifyAll - When false (default), only fill speakers with empty names.
+ * When true, overwrite all speaker names.
+ */
+ const autoIdentifySpeakers = async (identifyAll = false) => {
+ showAutoIdDropdown.value = false;
+
if (!selectedRecording.value) {
showToast('No recording selected.', 'fa-exclamation-circle');
return;
@@ -773,13 +798,17 @@ export function useSpeakers(state, utils, processedTranscription) {
for (const speakerId in data.speaker_map) {
const identifiedName = data.speaker_map[speakerId];
if (speakerMap.value[speakerId] && identifiedName && identifiedName.trim() !== '') {
+ // Skip speakers that already have a name unless identifyAll is true
+ if (!identifyAll && speakerMap.value[speakerId].name && speakerMap.value[speakerId].name.trim() !== '') {
+ continue;
+ }
speakerMap.value[speakerId].name = identifiedName;
identifiedCount++;
}
}
if (identifiedCount > 0) {
- showToast(`${identifiedCount} speaker(s) identified successfully!`, 'fa-check-circle');
+ showToast(`${identifiedCount} speaker${identifiedCount === 1 ? '' : 's'} identified successfully!`, 'fa-check-circle');
} else {
showToast('No speakers could be identified from the context.', 'fa-info-circle');
}
@@ -795,6 +824,59 @@ export function useSpeakers(state, utils, processedTranscription) {
}
};
+ // =========================================
+ // Apply Suggested Names
+ // =========================================
+
+ /** True when any unnamed, non-isMe speaker has voice or autocomplete suggestions */
+ const hasAnySuggestions = Vue.computed(() => {
+ for (const speakerId of modalSpeakers.value) {
+ const data = speakerMap.value[speakerId];
+ if (!data || data.isMe) continue;
+ if (data.name && data.name.trim() !== '') continue;
+ // Check voice suggestions
+ if (voiceSuggestions.value[speakerId] && voiceSuggestions.value[speakerId].length > 0) {
+ return true;
+ }
+ // Check autocomplete suggestions
+ if (speakerSuggestions.value[speakerId] && speakerSuggestions.value[speakerId].length > 0) {
+ return true;
+ }
+ }
+ return false;
+ });
+
+ /** Bulk-apply voice suggestions (priority) then autocomplete suggestions to empty names only */
+ const applySuggestedNames = () => {
+ let appliedCount = 0;
+ for (const speakerId of modalSpeakers.value) {
+ const data = speakerMap.value[speakerId];
+ if (!data || data.isMe) continue;
+ if (data.name && data.name.trim() !== '') continue;
+
+ // Priority 1: voice suggestions
+ const voice = voiceSuggestions.value[speakerId];
+ if (voice && voice.length > 0) {
+ data.name = voice[0].name;
+ appliedCount++;
+ continue;
+ }
+
+ // Priority 2: autocomplete suggestions
+ const auto = speakerSuggestions.value[speakerId];
+ if (auto && auto.length > 0) {
+ data.name = auto[0].name;
+ appliedCount++;
+ }
+ }
+
+ if (appliedCount > 0) {
+ showToast(`Applied ${appliedCount} suggested name${appliedCount === 1 ? '' : 's'}`, 'fa-check-circle');
+ } else {
+ showToast('No suggestions to apply', 'fa-info-circle');
+ }
+ };
+
// =========================================
// Add Speaker Modal
// =========================================
@@ -1128,6 +1210,10 @@ export function useSpeakers(state, utils, processedTranscription) {
// Auto-identify
autoIdentifySpeakers,
+ showAutoIdDropdown,
+ autoIdSplitBtn,
+ hasAnySuggestions,
+ applySuggestedNames,
// Add speaker
openAddSpeakerModal,
diff --git a/templates/modals/speaker-modal.html b/templates/modals/speaker-modal.html
index 58259103..aea9db49 100644
--- a/templates/modals/speaker-modal.html
+++ b/templates/modals/speaker-modal.html
@@ -281,16 +281,38 @@
<!-- Footer Buttons -->
<div class="bg-[var(--bg-tertiary)] px-3 sm:px-6 py-2 sm:py-4 flex flex-row justify-between items-center gap-2 border-t border-[var(--border-primary)] flex-shrink-0 rounded-b-xl">
- <!-- Auto Identify Button -->
- <button @click="autoIdentifySpeakers" class="px-2 sm:px-4 py-1.5 sm:py-2 bg-purple-600 text-white rounded-md hover:bg-purple-700 active:scale-[0.98] transition-all flex items-center justify-center text-xs sm:text-sm font-medium" :disabled="isAutoIdentifying">
- <i class="fas fa-magic mr-1 sm:mr-2"></i>
- <span v-if="!isAutoIdentifying" class="hidden sm:inline" v-text="t('help.autoIdentify')"></span>
- <span v-if="!isAutoIdentifying" class="sm:hidden">Auto</span>
- <span v-else>
- <i class="fas fa-spinner fa-spin"></i>
- </span>
- </button>
- <!-- Cancel & Save Buttons -->
+ <!-- Left: Auto Identify Split Button + Apply Suggested -->
+ <div class="flex items-center gap-2">
+ <!-- Split Button: Auto Identify -->
+ <div class="relative inline-flex" ref="autoIdSplitBtn">
+ <!-- Main button: identify missing only -->
+ <button @click="autoIdentifySpeakers(false)" class="px-2 sm:px-3 py-1.5 sm:py-2 bg-purple-600 text-white rounded-l-md hover:bg-purple-700 active:scale-[0.98] transition-all flex items-center justify-center text-xs sm:text-sm font-medium" :disabled="isAutoIdentifying">
+ <i class="fas fa-magic mr-1 sm:mr-1.5"></i>
+ <span v-if="!isAutoIdentifying" class="hidden sm:inline" v-text="t('help.autoIdentify')"></span>
+ <span v-if="!isAutoIdentifying" class="sm:hidden">Auto</span>
+ <span v-else>
+ <i class="fas fa-spinner fa-spin"></i>
+ </span>
+ </button>
+ <!-- Dropdown arrow -->
+ <button @click="showAutoIdDropdown = !showAutoIdDropdown" class="px-1.5 py-1.5 sm:py-2 bg-purple-600 text-white rounded-r-md border-l border-purple-500 hover:bg-purple-700 active:scale-[0.98] transition-all text-xs sm:text-sm" :disabled="isAutoIdentifying">
+ <i class="fas fa-caret-down"></i>
+ </button>
+ <!-- Dropdown menu -->
+ <div v-if="showAutoIdDropdown" class="absolute bottom-full left-0 mb-1 bg-[var(--bg-secondary)] border border-[var(--border-primary)] rounded-md shadow-lg z-50 min-w-[180px]">
+ <button @click="autoIdentifySpeakers(true)" class="w-full px-3 py-2 text-left text-xs sm:text-sm text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors rounded-md">
+ <i class="fas fa-users mr-2"></i>Identify all speakers
+ </button>
+ </div>
+ </div>
+ <!-- Apply Suggested Button -->
+ <button v-if="hasAnySuggestions" @click="applySuggestedNames" class="px-2 sm:px-3 py-1.5 sm:py-2 bg-gradient-to-r from-blue-600 to-purple-600 text-white rounded-md hover:from-blue-700 hover:to-purple-700 active:scale-[0.98] transition-all flex items-center text-xs sm:text-sm font-medium">
+ <i class="fas fa-lightbulb mr-1 sm:mr-1.5"></i>
+ <span class="hidden sm:inline">Apply Suggested</span>
+ <span class="sm:hidden">Suggest</span>
+ </button>
+ </div>
+ <!-- Right: Cancel & Save Buttons -->
<div class="flex gap-2">
<button @click="closeSpeakerModal" class="px-3 sm:px-4 py-1.5 sm:py-2 bg-[var(--bg-secondary)] text-[var(--text-secondary)] rounded-md border border-[var(--border-secondary)] hover:bg-[var(--bg-tertiary)] active:scale-[0.98] transition-all text-xs sm:text-sm" v-text="t('common.cancel')"></button>
<button