Spaces:
Sleeping
Sleeping
fix: resolve transcript clickability, real-time highlighting, and summary persistence
Browse files- Fixed transcript clickability by implementing proper cross-iframe communication
- Addressed real-time highlighting with correct time synchronization
- Ensured summary persistence using dedicated placeholder and state management
- src/streamlit_app.py +189 -0
src/streamlit_app.py
CHANGED
|
@@ -80,6 +80,195 @@ with tab2:
|
|
| 80 |
with tab3:
|
| 81 |
st.subheader("Transcription & Summary")
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# Initialize audio player (unchanged)
|
| 84 |
if st.session_state.audio_path and not st.session_state.audio_base64:
|
| 85 |
with open(st.session_state.audio_path, "rb") as f:
|
|
|
|
| 80 |
with tab3:
|
| 81 |
st.subheader("Transcription & Summary")
|
| 82 |
|
| 83 |
+
# Initialize audio player
|
| 84 |
+
if st.session_state.audio_path and not st.session_state.audio_base64:
|
| 85 |
+
with open(st.session_state.audio_path, "rb") as f:
|
| 86 |
+
audio_bytes = f.read()
|
| 87 |
+
st.session_state.audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 88 |
+
|
| 89 |
+
if st.session_state.audio_base64:
|
| 90 |
+
# Audio player with time update handler
|
| 91 |
+
audio_html = f"""
|
| 92 |
+
<audio id="audioPlayer" controls ontimeupdate="updateTime(this)">
|
| 93 |
+
<source src="data:audio/mp3;base64,{st.session_state.audio_base64}" type="audio/mp3">
|
| 94 |
+
</audio>
|
| 95 |
+
<script>
|
| 96 |
+
function seekAudio(time) {{
|
| 97 |
+
const player = document.getElementById('audioPlayer');
|
| 98 |
+
player.currentTime = time;
|
| 99 |
+
player.play();
|
| 100 |
+
}}
|
| 101 |
+
|
| 102 |
+
function updateTime(audio) {{
|
| 103 |
+
const time = audio.currentTime;
|
| 104 |
+
window.parent.postMessage({{
|
| 105 |
+
type: "currentTimeUpdate",
|
| 106 |
+
time: time
|
| 107 |
+
}}, "*");
|
| 108 |
+
}}
|
| 109 |
+
|
| 110 |
+
///////////////////////////////////////////////////////////////////
|
| 111 |
+
// FIX 1: Handle transcript clicks globally
|
| 112 |
+
///////////////////////////////////////////////////////////////////
|
| 113 |
+
window.addEventListener('message', (event) => {{
|
| 114 |
+
if (event.data.type === 'seekToTime') {{
|
| 115 |
+
seekAudio(event.data.time);
|
| 116 |
+
}}
|
| 117 |
+
}});
|
| 118 |
+
</script>
|
| 119 |
+
"""
|
| 120 |
+
st.markdown(audio_html, unsafe_allow_html=True)
|
| 121 |
+
|
| 122 |
+
status_placeholder = st.empty()
|
| 123 |
+
transcript_placeholder = st.empty()
|
| 124 |
+
summary_placeholder = st.empty() # Dedicated placeholder for persistent summary
|
| 125 |
+
|
| 126 |
+
# FIX 3: Persistent summary display using dedicated placeholder
|
| 127 |
+
if st.session_state.get('summary'):
|
| 128 |
+
with summary_placeholder.container():
|
| 129 |
+
st.markdown("### Summary")
|
| 130 |
+
st.markdown(st.session_state.summary)
|
| 131 |
+
|
| 132 |
+
# Function to generate transcript HTML
|
| 133 |
+
def generate_transcript_html(utterances, current_time):
|
| 134 |
+
if not utterances:
|
| 135 |
+
return ""
|
| 136 |
+
|
| 137 |
+
# Ensure current_time is a float
|
| 138 |
+
current_time = float(current_time) if current_time is not None else 0.0
|
| 139 |
+
|
| 140 |
+
html = """
|
| 141 |
+
<div id="transcript-container">
|
| 142 |
+
"""
|
| 143 |
+
|
| 144 |
+
for start, end, text in utterances:
|
| 145 |
+
if start < 0 or end < 0:
|
| 146 |
+
html += f'<div class="utterance">{text}</div>'
|
| 147 |
+
continue
|
| 148 |
+
|
| 149 |
+
is_current = start <= current_time < end
|
| 150 |
+
html += f"""
|
| 151 |
+
<div class="utterance {'current-utterance' if is_current else ''}"
|
| 152 |
+
onclick="parent.postMessage({{type: 'seekToTime', time: {start}}}, '*')">
|
| 153 |
+
<b>[{time.strftime('%M:%S', time.gmtime(start))}-{time.strftime('%M:%S', time.gmtime(end))}]</b> {text}
|
| 154 |
+
</div>
|
| 155 |
+
"""
|
| 156 |
+
html += "</div>"
|
| 157 |
+
return html
|
| 158 |
+
|
| 159 |
+
# Add CSS globally
|
| 160 |
+
st.markdown("""
|
| 161 |
+
<style>
|
| 162 |
+
.utterance {
|
| 163 |
+
padding: 8px;
|
| 164 |
+
margin: 4px 0;
|
| 165 |
+
border-radius: 4px;
|
| 166 |
+
cursor: pointer;
|
| 167 |
+
transition: background 0.2s;
|
| 168 |
+
}
|
| 169 |
+
.utterance:hover { background-color: #f0f0f0; }
|
| 170 |
+
.current-utterance {
|
| 171 |
+
background-color: #ffebee;
|
| 172 |
+
border-left: 3px solid #f44336;
|
| 173 |
+
font-weight: 500;
|
| 174 |
+
}
|
| 175 |
+
</style>
|
| 176 |
+
""", unsafe_allow_html=True)
|
| 177 |
+
|
| 178 |
+
if st.button("🎙️ Transcribe", key="transcribe_button_tab3"):
|
| 179 |
+
if st.session_state.audio_path:
|
| 180 |
+
status_placeholder.text("Transcribing...")
|
| 181 |
+
st.session_state.utterances = []
|
| 182 |
+
st.session_state.transcript = ""
|
| 183 |
+
|
| 184 |
+
# Initialize transcript display
|
| 185 |
+
transcript_placeholder.empty()
|
| 186 |
+
transcript_display = st.empty()
|
| 187 |
+
|
| 188 |
+
# Process ASR output
|
| 189 |
+
for current_utt, all_utts in transcribe_file(
|
| 190 |
+
st.session_state.audio_path, vad_threshold, model_names[model_name]
|
| 191 |
+
):
|
| 192 |
+
st.session_state.utterances = all_utts
|
| 193 |
+
|
| 194 |
+
# Build plain text transcript
|
| 195 |
+
st.session_state.transcript = "\n".join(
|
| 196 |
+
f"[{start:.1f}-{end:.1f}] {text}"
|
| 197 |
+
for start, end, text in all_utts
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
# Update transcript display
|
| 201 |
+
html = generate_transcript_html(
|
| 202 |
+
all_utts,
|
| 203 |
+
st.session_state.get('current_time', 0.0)
|
| 204 |
+
)
|
| 205 |
+
transcript_display.empty()
|
| 206 |
+
with transcript_display:
|
| 207 |
+
st.components.v1.html(html, height=500, scrolling=True)
|
| 208 |
+
|
| 209 |
+
# Final update after transcription
|
| 210 |
+
html = generate_transcript_html(
|
| 211 |
+
st.session_state.utterances,
|
| 212 |
+
st.session_state.get('current_time', 0.0)
|
| 213 |
+
)
|
| 214 |
+
transcript_placeholder.empty()
|
| 215 |
+
with transcript_placeholder:
|
| 216 |
+
st.components.v1.html(html, height=500, scrolling=True)
|
| 217 |
+
transcript_display.empty()
|
| 218 |
+
|
| 219 |
+
status_placeholder.empty()
|
| 220 |
+
|
| 221 |
+
# Time update handling
|
| 222 |
+
current_time_js = """
|
| 223 |
+
<script>
|
| 224 |
+
// Listen for time updates from audio player
|
| 225 |
+
window.addEventListener('message', (event) => {
|
| 226 |
+
if (event.data.type === 'currentTimeUpdate') {
|
| 227 |
+
Streamlit.setComponentValue(event.data.time);
|
| 228 |
+
}
|
| 229 |
+
});
|
| 230 |
+
</script>
|
| 231 |
+
"""
|
| 232 |
+
|
| 233 |
+
# Create component and get its value
|
| 234 |
+
current_time_component = st.components.v1.html(
|
| 235 |
+
current_time_js,
|
| 236 |
+
height=0,
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
# FIX 2: Update highlighting when time changes
|
| 240 |
+
if isinstance(current_time_component, (int, float)):
|
| 241 |
+
st.session_state.current_time = current_time_component
|
| 242 |
+
if "utterances" in st.session_state and st.session_state.utterances:
|
| 243 |
+
html = generate_transcript_html(
|
| 244 |
+
st.session_state.utterances,
|
| 245 |
+
st.session_state.current_time
|
| 246 |
+
)
|
| 247 |
+
transcript_placeholder.empty()
|
| 248 |
+
with transcript_placeholder:
|
| 249 |
+
st.components.v1.html(html, height=500, scrolling=True)
|
| 250 |
+
|
| 251 |
+
if st.button("📝 Summarize", key="summarize_button_tab3"):
|
| 252 |
+
if st.session_state.transcript:
|
| 253 |
+
status_placeholder.text("Summarizing...")
|
| 254 |
+
live_summary_placeholder = st.empty()
|
| 255 |
+
st.session_state.summary = ""
|
| 256 |
+
|
| 257 |
+
for accumulated_summary in summarize_transcript(
|
| 258 |
+
st.session_state.transcript, llm_model, prompt_input
|
| 259 |
+
):
|
| 260 |
+
st.session_state.summary = accumulated_summary
|
| 261 |
+
live_summary_placeholder.markdown(accumulated_summary)
|
| 262 |
+
|
| 263 |
+
# FIX 3: Update persistent summary instead of removing
|
| 264 |
+
with summary_placeholder.container():
|
| 265 |
+
st.markdown("### Summary")
|
| 266 |
+
st.markdown(st.session_state.summary)
|
| 267 |
+
|
| 268 |
+
live_summary_placeholder.empty()
|
| 269 |
+
status_placeholder.empty()
|
| 270 |
+
st.subheader("Transcription & Summary")
|
| 271 |
+
|
| 272 |
# Initialize audio player (unchanged)
|
| 273 |
if st.session_state.audio_path and not st.session_state.audio_base64:
|
| 274 |
with open(st.session_state.audio_path, "rb") as f:
|