MedDictate / app.py
JulsdL's picture
Enhance audio processing in MedDictate: Add audio file saving, timestamp naming, and cleanup
37e8d87
import streamlit as st
from st_audiorec import st_audiorec
import os
from datetime import datetime
# Placeholder function to simulate processing and generating a medical note
def process_audio_and_generate_note(audio_data_path):
# In a real scenario, this function would likely perform operations such as:
# 1. Converting the audio data into a suitable format for analysis.
# 2. Performing speech recognition on the audio to extract text.
# 3. Using the extracted text to generate a medical note.
# Here, we'll simulate this with a placeholder return value.
# IMPORTANT: Assume processing is done here, and we no longer need the audio file
return "Path to generated medical consultation note in Word format"
# Ensure the directory exists
audio_dir_path = "meddictate/data/audio"
os.makedirs(audio_dir_path, exist_ok=True)
st.title('MedDictate: Medical Consultation Note Generator')
# Display instructions and initiate the audio recorder component
st.write("Click the button below to start and stop recording:")
wav_audio_data = st_audiorec() # This initiates the audio recorder
# Store the audio data in session state if received from the recorder
if wav_audio_data is not None:
st.session_state.audio_bytes = wav_audio_data
# Button to submit the recording for processing
if 'audio_bytes' in st.session_state and st.session_state.audio_bytes:
if st.button('Process Recording', key='process_recording'):
# Construct a filename with a timestamp to avoid overwriting existing files
timestamp = datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
filename = f"recorded_audio_{timestamp}.wav"
audio_file_path = os.path.join(audio_dir_path, filename)
# Save the audio data to a WAV file
with open(audio_file_path, "wb") as f:
f.write(st.session_state.audio_bytes)
# Process the audio data
generated_note_path = process_audio_and_generate_note(audio_file_path)
# After processing, delete the audio file as it's no longer needed
os.remove(audio_file_path)
st.write('Processing complete. Your medical consultation note is ready for download.')
st.download_button('Download Medical Note', generated_note_path, 'medical_note.docx')
else:
# Prompt to record if no audio data is found
st.warning('Please click the record button above to start recording your audio.')