File size: 4,895 Bytes
d06c695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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)*")