ferferefer's picture
Upload app.py
d06c695 verified
import streamlit as st
import whisper
import requests
import json
from datetime import datetime
import os
from dotenv import load_dotenv
import numpy as np
from io import BytesIO
import tempfile
# Load environment variables - try both .env and system environment
load_dotenv()
HUGGINGFACE_TOKEN = os.getenv('HUGGINGFACE_TOKEN')
if not HUGGINGFACE_TOKEN:
st.error("Please set the HUGGINGFACE_TOKEN in your Space's secrets.")
st.stop()
# Initialize Whisper model
@st.cache_resource
def load_whisper_model():
return whisper.load_model("base")
def process_audio(audio_file):
"""Process audio file and generate transcription and clinical notes"""
try:
with st.spinner("Transcribing audio..."):
# Transcribe audio
model = load_whisper_model()
result = model.transcribe(audio_file)
st.session_state.transcription = result["text"]
with st.spinner("Generating clinical notes..."):
# Generate clinical notes
st.session_state.clinical_notes = get_clinical_notes(st.session_state.transcription)
except Exception as e:
st.error(f"Error processing audio: {str(e)}")
# Mixtral API call function
def get_clinical_notes(transcription):
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
messages = [
{"role": "system", "content": "You are a medical assistant helping to generate clinical notes from doctor-patient conversations. Format the notes in a clear, professional structure with the following sections: Chief Complaint, History of Present Illness, Review of Systems, Physical Examination, Assessment, and Plan."},
{"role": "user", "content": f"Generate clinical notes from this doctor-patient conversation: {transcription}"}
]
payload = {
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"messages": messages,
"max_tokens": 500,
"stream": False
}
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()['choices'][0]['message']['content']
except Exception as e:
st.error(f"Error generating clinical notes: {str(e)}")
return None
# Main app
st.set_page_config(
page_title="AI Clinical Notes",
page_icon="πŸ¦€",
layout="wide"
)
st.title("πŸ¦€ AI Clinical Notes")
st.markdown("### Created by Dr. Fernando Ly")
st.markdown("This application helps medical professionals automatically generate clinical notes from patient conversations.")
# Create columns for better layout
col1, col2 = st.columns(2)
with col1:
st.subheader("Audio Input")
# File upload section
st.write("Upload your recorded conversation:")
uploaded_file = st.file_uploader("Upload an audio file (WAV format)", type=['wav'])
if uploaded_file:
# Display the uploaded audio
st.audio(uploaded_file, format="audio/wav")
# Save audio temporarily
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
tmp_file.write(uploaded_file.getvalue())
process_audio(tmp_file.name)
# Clean up
os.unlink(tmp_file.name)
# Add recording instructions
st.markdown("---")
st.markdown("""
### Recording Instructions
To record your conversation:
1. Use your phone's voice recorder app
2. Save the recording as a WAV file
3. Upload it here
Alternatively, you can use these free tools:
- Windows: Voice Recorder app
- Mac: QuickTime Player
- Online: [Vocaroo](https://vocaroo.com) (saves directly as WAV)
""")
with col2:
# Display results
if "transcription" in st.session_state and st.session_state.transcription:
st.subheader("πŸ“ Transcription")
st.write(st.session_state.transcription)
if "clinical_notes" in st.session_state and st.session_state.clinical_notes:
st.subheader("πŸ₯ Clinical Notes")
st.write(st.session_state.clinical_notes)
# Instructions
st.markdown("---")
st.markdown("""
### How to use:
1. Record your conversation using any recording app
2. Save the recording as a WAV file
3. Upload the file using the uploader above
4. Wait for the transcription and clinical notes to be generated
5. Review the generated notes
**Note**: For best results, ensure:
- Clear audio quality
- Minimal background noise
- Proper microphone placement during recording
""")
# Footer
st.markdown("---")
st.markdown("*Note: This is an AI-assisted tool. Please review and verify all generated notes.*")
st.markdown("*For issues or feedback, please visit the [GitHub repository](https://huggingface.co/spaces/fernandoly/AI-Clinical-Notes/discussions)*")