import streamlit as st
import datetime
import os
import json
# Initialize session state for transcript history if not exists
if 'transcript_history' not in st.session_state:
st.session_state.transcript_history = ""
# Create a container for the transcript history
history_container = st.empty()
text_area = st.empty()
html = """
Continuous Speech Demo
Continuous Speech Recognition
Ready
"""
# Function to save transcript to file
def save_transcript(text):
if not os.path.exists('transcripts'):
os.makedirs('transcripts')
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"transcripts/transcript_{timestamp}.md"
with open(filename, 'a', encoding='utf-8') as f:
f.write(text + '\n')
# Main app
st.title("Speech Recognition with Transcript History")
# Create custom component
component_value = st.components.v1.html(html, height=600)
# If we receive a new transcript
if component_value is not None:
try:
# Parse the JSON string
transcript_data = json.loads(component_value)
# Update the transcript history if it's a final transcript
if transcript_data['isFinal']:
new_text = transcript_data['text']
st.session_state.transcript_history += new_text
# Save to file
save_transcript(new_text)
# Update the display
history_container.markdown(st.session_state.transcript_history)
text_area.text_area("Full Transcript", st.session_state.transcript_history, height=200)
except json.JSONDecodeError:
st.error("Error processing transcript data")
# Add a download button for the full transcript
if st.session_state.transcript_history:
st.download_button(
label="Download Full Transcript",
data=st.session_state.transcript_history,
file_name=f"transcript_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.md",
mime="text/markdown"
)