Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
fixed json transcripts
Murtaza Nasir7abdf7fbb6e0
diff --git a/app.py b/app.pyindex 805a06ff..114877c2 100644--- a/app.py+++ b/app.py@@ -44,6 +44,26 @@ def md_to_html(text):])return html+def format_transcription_for_llm(transcription_text):+ """+ Formats transcription for LLM. If it's our simplified JSON, convert it to plain text.+ Otherwise, return as is.+ """+ try:+ transcription_data = json.loads(transcription_text)+ if isinstance(transcription_data, list):+ # It's our simplified JSON format+ formatted_lines = []+ for segment in transcription_data:+ speaker = segment.get('speaker', 'Unknown Speaker')+ sentence = segment.get('sentence', '')+ formatted_lines.append(f"[{speaker}]: {sentence}")+ return "\n".join(formatted_lines)+ except (json.JSONDecodeError, TypeError):+ # Not a JSON, or not the format we expect, so return as is.+ pass+ return transcription_text+app = Flask(__name__)# Use environment variables or default paths for Docker compatibilityapp.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:////data/instance/transcriptions.db')@@ -435,9 +455,23 @@ def transcribe_audio_asr(app_context, recording_id, filepath, original_filename,with httpx.Client() as client:response = client.post(url, params=params, files=files, timeout=None)response.raise_for_status()- # The ASR service now returns JSON as a string.- # We will store it directly. The frontend will parse it.- recording.transcription = response.text++ # Parse the JSON response from ASR+ asr_response_data = response.json()++ # Simplify the JSON data+ simplified_segments = []+ if 'segments' in asr_response_data and isinstance(asr_response_data['segments'], list):+ for segment in asr_response_data['segments']:+ simplified_segments.append({+ 'speaker': segment.get('speaker'),+ 'sentence': segment.get('text', '').strip(),+ 'start_time': segment.get('start'),+ 'end_time': segment.get('end')+ })++ # Store the simplified JSON as a string+ recording.transcription = json.dumps(simplified_segments)app.logger.info(f"ASR transcription completed for recording {recording_id}.")generate_summary_task(app_context, recording_id)@@ -683,15 +717,16 @@ def update_speakers(recording_id):is_json = Falsetry:transcription_data = json.loads(transcription_text)- is_json = isinstance(transcription_data, dict) and 'segments' in transcription_data+ # Updated check for our new simplified JSON format (a list of segment objects)+ is_json = isinstance(transcription_data, list)except (json.JSONDecodeError, TypeError):is_json = Falsespeaker_names_used = []if is_json:- # Handle JSON transcript- for segment in transcription_data.get('segments', []):+ # Handle new simplified JSON transcript (list of segments)+ for segment in transcription_data:original_speaker_label = segment.get('speaker')if original_speaker_label in speaker_map:new_name_info = speaker_map[original_speaker_label]@@ -707,9 +742,8 @@ def update_speakers(recording_id):recording.transcription = json.dumps(transcription_data)# Update participants from the final list of speakers in the JSON- if transcription_data.get('segments'):- final_speakers = set(seg.get('speaker') for seg in transcription_data['segments'] if seg.get('speaker'))- recording.participants = ', '.join(sorted(list(final_speakers)))+ final_speakers = set(seg.get('speaker') for seg in transcription_data if seg.get('speaker'))+ recording.participants = ', '.join(sorted(list(final_speakers)))else:# Handle plain text transcript@@ -761,8 +795,11 @@ def identify_speakers_from_text(transcription):if not TEXT_MODEL_API_KEY:raise ValueError("TEXT_MODEL_API_KEY not configured.")+ # The transcription passed here could be JSON, so we format it.+ formatted_transcription = format_transcription_for_llm(transcription)+# Extract existing speaker labels (e.g., SPEAKER_00, SPEAKER_01) in order of appearance- all_labels = re.findall(r'\[(SPEAKER_\d+)\]', transcription)+ all_labels = re.findall(r'\[(SPEAKER_\d+)\]', formatted_transcription)seen = set()speaker_labels = [x for x in all_labels if not (x in seen or seen.add(x))]@@ -773,7 +810,7 @@ def identify_speakers_from_text(transcription):Transcription:----{transcription[:28000]}+{formatted_transcription[:28000]}---Respond with a single JSON object where keys are the speaker labels (e.g., "SPEAKER_00") and values are the identified full names. If a name cannot be determined, use the value "Unknown".@@ -818,6 +855,9 @@ def identify_unidentified_speakers_from_text(transcription, unidentified_speakerif not TEXT_MODEL_API_KEY:raise ValueError("TEXT_MODEL_API_KEY not configured.")+ # The transcription passed here could be JSON, so we format it.+ formatted_transcription = format_transcription_for_llm(transcription)+if not unidentified_speakers:return {}@@ -827,7 +867,7 @@ Note: Only identify the speakers listed above. Other speakers in the transcriptiTranscription:----{transcription[:28000]}+{formatted_transcription[:28000]}---Respond with a single JSON object where keys are the unidentified speaker labels (e.g., "SPEAKER_01") and values are the identified full names. If a name cannot be determined, use the value "Unknown".@@ -887,7 +927,8 @@ def auto_identify_speakers(recording_id):current_speaker_map = data.get('current_speaker_map', {})# Extract all speaker labels from transcription- all_labels = re.findall(r'\[(SPEAKER_\d+)\]', recording.transcription)+ formatted_transcription = format_transcription_for_llm(recording.transcription)+ all_labels = re.findall(r'\[(SPEAKER_\d+)\]', formatted_transcription)seen = set()speaker_labels = [x for x in all_labels if not (x in seen or seen.add(x))]@@ -905,7 +946,7 @@ def auto_identify_speakers(recording_id):return jsonify({'success': True, 'speaker_map': {}, 'message': 'All speakers are already identified'})# Call the helper function with only unidentified speakers- speaker_map = identify_unidentified_speakers_from_text(recording.transcription, unidentified_speakers)+ speaker_map = identify_unidentified_speakers_from_text(formatted_transcription, unidentified_speakers)return jsonify({'success': True, 'speaker_map': speaker_map})@@ -959,6 +1000,8 @@ def chat_with_transcription():user_title = current_user.job_title if current_user.is_authenticated and current_user.job_title else "a professional"user_company = current_user.company if current_user.is_authenticated and current_user.company else "their organization"+ formatted_transcription = format_transcription_for_llm(recording.transcription)+system_prompt = f"""You are a professional meeting and audio transcription analyst assisting {user_name}, who is a(n) {user_title} at {user_company}. {language_instruction} Analyze the following meeting information and respond to the specific request.Following are the meeting participants and their roles:@@ -966,7 +1009,7 @@ Following are the meeting participants and their roles:Following is the meeting transcript:<<start transcript>>-{recording.transcription or "No transcript available."}+{formatted_transcription or "No transcript available."}<<end transcript>>Additional context and notes about the meeting:diff --git a/static/js/app.js b/static/js/app.jsindex 40197819..2ae75c36 100644--- a/static/js/app.js+++ b/static/js/app.js@@ -184,16 +184,17 @@ document.addEventListener('DOMContentLoaded', () => {transcriptionData = null;}- if (transcriptionData && transcriptionData.segments) {+ // Updated to handle new simplified JSON format (array of segments)+ if (transcriptionData && Array.isArray(transcriptionData)) {// JSON format - extract speakers from segmentsconst speakers = new Set();- transcriptionData.segments.forEach(segment => {+ transcriptionData.forEach(segment => {if (segment.speaker) {speakers.add(segment.speaker);}});- return Array.from(speakers);- } else {+ return Array.from(speakers).sort(); // Sort for consistent color mapping+ } else if (typeof transcription === 'string') {// Plain text format - use regex to find speaker patternsconst speakerRegex = /\[([^\]]+)\]:/g;const speakers = new Set();@@ -201,8 +202,9 @@ document.addEventListener('DOMContentLoaded', () => {while ((match = speakerRegex.exec(transcription)) !== null) {speakers.add(match[1]);}- return Array.from(speakers);+ return Array.from(speakers).sort();}+ return [];});const processedTranscription = computed(() => {@@ -219,28 +221,23 @@ document.addEventListener('DOMContentLoaded', () => {transcriptionData = null;}- if (transcriptionData && transcriptionData.segments) {- // JSON processing logic+ // Updated to handle new simplified JSON format (array of segments)+ if (transcriptionData && Array.isArray(transcriptionData)) {const speakerColors = {};- const speakerList = Array.from(new Set(transcriptionData.segments.map(seg => seg.speaker)));+ const speakerList = identifiedSpeakers.value; // Use computed property which is already sortedspeakerList.forEach((speaker, index) => {speakerColors[speaker] = `speaker-color-${(index % 8) + 1}`;});- const simpleSegments = transcriptionData.segments.map(segment => ({+ const simpleSegments = transcriptionData.map(segment => ({+ speakerId: segment.speaker,speaker: (speakerMap.value[segment.speaker]?.name || segment.speaker),- text: segment.text,- words: segment.words.map((word, index) => ({- ...word,- // Add a click handler or data attribute for seeking- 'data-start-time': word.start,- // Add space after word unless it's the last word in the segment- displayWord: word.word + (index < segment.words.length - 1 ? ' ' : '')- })),+ sentence: segment.sentence,+ startTime: segment.start_time,+ endTime: segment.end_time,color: speakerColors[segment.speaker] || 'speaker-color-1'}));- // Process simple segments to add showSpeaker flag for proper displayconst processedSimpleSegments = [];let lastSpeaker = null;simpleSegments.forEach(segment => {@@ -258,14 +255,14 @@ document.addEventListener('DOMContentLoaded', () => {bubbleRows.push({speaker: segment.speaker,color: segment.color,- isMe: segment.speaker && segment.speaker.toLowerCase().includes('me'),+ isMe: segment.speaker && (typeof segment.speaker === 'string') && segment.speaker.toLowerCase().includes('me'),bubbles: []});lastBubbleSpeaker = segment.speaker;}bubbleRows[bubbleRows.length - 1].bubbles.push({- text: segment.text,- words: segment.words,+ sentence: segment.sentence,+ startTime: segment.startTime,color: segment.color});});@@ -322,7 +319,7 @@ document.addEventListener('DOMContentLoaded', () => {if (currentSpeaker && currentText.trim()) {segments.push({speaker: currentSpeaker,- text: currentText.trim(),+ sentence: currentText.trim(),color: speakerColors[currentSpeaker] || 'speaker-color-1'});}@@ -333,7 +330,7 @@ document.addEventListener('DOMContentLoaded', () => {} else if (!currentSpeaker && line.trim()) {segments.push({speaker: null,- text: line.trim(),+ sentence: line.trim(),color: 'speaker-color-1'});}@@ -342,7 +339,7 @@ document.addEventListener('DOMContentLoaded', () => {if (currentSpeaker && currentText.trim()) {segments.push({speaker: currentSpeaker,- text: currentText.trim(),+ sentence: currentText.trim(),color: speakerColors[currentSpeaker] || 'speaker-color-1'});}@@ -352,7 +349,9 @@ document.addEventListener('DOMContentLoaded', () => {segments.forEach(segment => {simpleSegments.push({...segment,- showSpeaker: segment.speaker !== lastSpeaker+ showSpeaker: segment.speaker !== lastSpeaker,+ // Ensure sentence property exists for plain text+ sentence: segment.sentence || segment.text});lastSpeaker = segment.speaker;});@@ -370,7 +369,7 @@ document.addEventListener('DOMContentLoaded', () => {};}currentRow.bubbles.push({- text: segment.text,+ sentence: segment.sentence,color: segment.color});});@@ -2092,26 +2091,40 @@ document.addEventListener('DOMContentLoaded', () => {return;}- // Get the button that was clickedconst button = event.currentTarget;+ let textToCopy = '';- // Show visual feedback on button+ try {+ const transcriptionData = JSON.parse(selectedRecording.value.transcription);+ if (Array.isArray(transcriptionData)) {+ // It's our simplified JSON, format it for copying+ textToCopy = transcriptionData.map(segment => {+ const speakerName = speakerMap.value[segment.speaker]?.name || segment.speaker;+ return `[${speakerName}]: ${segment.sentence}`;+ }).join('\n');+ } else {+ // It's some other JSON or plain text, copy as is+ textToCopy = selectedRecording.value.transcription;+ }+ } catch (e) {+ // Not JSON, so it's plain text+ textToCopy = selectedRecording.value.transcription;+ }+animateCopyButton(button);if (navigator.clipboard && window.isSecureContext) {- // Use clipboard API if available (secure context)- navigator.clipboard.writeText(selectedRecording.value.transcription)+ navigator.clipboard.writeText(textToCopy).then(() => {showToast('Transcription copied to clipboard!');}).catch(err => {console.error('Copy failed:', err);showToast('Failed to copy: ' + err.message, 'fa-exclamation-circle');- fallbackCopyTextToClipboard(selectedRecording.value.transcription);+ fallbackCopyTextToClipboard(textToCopy);});} else {- // Fallback for non-secure contexts- fallbackCopyTextToClipboard(selectedRecording.value.transcription);+ fallbackCopyTextToClipboard(textToCopy);}};@@ -2181,14 +2194,13 @@ document.addEventListener('DOMContentLoaded', () => {};const seekAudioFromEvent = (event) => {- const wordSpan = event.target.closest('[data-start-time]');- if (!wordSpan) return;+ const segmentElement = event.target.closest('[data-start-time]');+ if (!segmentElement) return;- const time = parseFloat(wordSpan.dataset.startTime);+ const time = parseFloat(segmentElement.dataset.startTime);if (isNaN(time)) return;// Determine context by checking if we're inside the speaker modal- // The speaker modal has a specific structure with showSpeakerModal conditionconst isInSpeakerModal = event.target.closest('.speaker-modal-transcript') !== null;const context = isInSpeakerModal ? 'modal' : 'main';diff --git a/templates/index.html b/templates/index.htmlindex 318d91c5..0013b8d8 100644--- a/templates/index.html+++ b/templates/index.html@@ -323,20 +323,16 @@</div><!-- Transcript Section -->- <div class="flex-grow overflow-y-auto custom-scrollbar speaker-modal-transcript">+ <div class="flex-grow overflow-y-auto custom-scrollbar speaker-modal-transcript" @click="seekAudioFromEvent"><div v-if="processedTranscription.isJson">- <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="index" class="speaker-segment mb-2">- <div v-if="segment.showSpeaker"- :class="[segment.color, 'speaker-tablet', 'font-semibold', 'mb-1', { 'speaker-highlight': highlightedSpeaker === segment.speaker }]"- :data-speaker-id="segment.speaker">- ${ segment.speaker }- </div>- <div class="speaker-text" @click="seekAudioFromEvent">- <span v-for="word in segment.words" :key="word.start" class="word cursor-pointer hover:bg-[var(--bg-accent)] hover:text-[var(--text-accent)] rounded px-0.5" :data-start-time="word.start">${ word.displayWord }</span>- </div>+ <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="index" class="speaker-segment mb-2" :data-start-time="segment.startTime">+ <p>+ <span :class="[segment.color, 'speaker-tag', { 'speaker-highlight': highlightedSpeaker === segment.speakerId }]" :data-speaker-id="segment.speakerId">[${ segment.speaker }]</span>:+ <span class="word cursor-pointer hover:bg-[var(--bg-accent)] hover:text-[var(--text-accent)] rounded px-0.5">${ segment.sentence }</span>+ </p></div></div>- <div v-else v-html="highlightedTranscript" @click="seekAudioFromEvent"></div>+ <div v-else v-html="highlightedTranscript"></div></div></div></div>@@ -535,26 +531,24 @@</div><!-- Transcription Content -->- <div v-if="processedTranscription.hasDialogue">+ <div v-if="processedTranscription.hasDialogue" @click="seekAudioFromEvent"><!-- Simple View -->- <div v-if="transcriptionViewMode === 'simple'" class="transcription-simple-view" @click="seekAudioFromEvent">- <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="index" class="speaker-segment">+ <div v-if="transcriptionViewMode === 'simple'" class="transcription-simple-view">+ <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="index" class="speaker-segment" :data-start-time="segment.startTime"><div v-if="segment.showSpeaker" :class="[segment.color, 'speaker-tablet']">${ segment.speaker }</div><div class="speaker-text">- <span v-if="processedTranscription.isJson" v-for="word in segment.words" :key="word.start" class="word" :data-start-time="word.start">${ word.displayWord }</span>- <span v-else>${ segment.text }</span>+ <span class="word">${ segment.sentence }</span></div></div></div><!-- Bubble View -->- <div v-else class="transcription-with-speakers" @click="seekAudioFromEvent">+ <div v-else class="transcription-with-speakers"><div v-for="(row, index) in processedTranscription.bubbleRows" :key="index":class="['bubble-row', { 'speaker-me': row.isMe }]"><div v-for="(bubble, bubbleIndex) in row.bubbles" :key="bubbleIndex"- :class="[row.color, 'speaker-bubble', { 'speaker-me': row.isMe }]">+ :class="[row.color, 'speaker-bubble', { 'speaker-me': row.isMe }]" :data-start-time="bubble.startTime"><div class="speaker-bubble-content">- <span v-if="processedTranscription.isJson" v-for="word in bubble.words" :key="word.start" class="word" :data-start-time="word.start">${ word.displayWord }</span>- <span v-else>${ bubble.text }</span>+ <span class="word">${ bubble.sentence }</span></div></div></div>@@ -987,11 +981,10 @@<div v-if="processedTranscription.hasDialogue" @click="seekAudioFromEvent"><!-- Simple View --><div v-if="transcriptionViewMode === 'simple'" class="transcription-simple-view">- <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="index" class="speaker-segment">+ <div v-for="(segment, index) in processedTranscription.simpleSegments" :key="index" class="speaker-segment" :data-start-time="segment.startTime"><div v-if="segment.showSpeaker" :class="[segment.color, 'speaker-tablet']">${ segment.speaker }</div><div class="speaker-text">- <span v-if="processedTranscription.isJson" v-for="word in segment.words" :key="word.start" class="word" :data-start-time="word.start">${ word.displayWord }</span>- <span v-else>${ segment.text }</span>+ <span class="word">${ segment.sentence }</span></div></div></div>@@ -1000,10 +993,9 @@<div v-for="(row, index) in processedTranscription.bubbleRows" :key="index":class="['bubble-row', { 'speaker-me': row.isMe }]"><div v-for="(bubble, bubbleIndex) in row.bubbles" :key="bubbleIndex"- :class="[row.color, 'speaker-bubble', { 'speaker-me': row.isMe }]">+ :class="[row.color, 'speaker-bubble', { 'speaker-me': row.isMe }]" :data-start-time="bubble.startTime"><div class="speaker-bubble-content">- <span v-if="processedTranscription.isJson" v-for="word in bubble.words" :key="word.start" class="word" :data-start-time="word.start">${ word.displayWord }</span>- <span v-else>${ bubble.text }</span>+ <span class="word">${ bubble.sentence }</span></div></div></div>