ferferefer commited on
Commit
3082c34
·
verified ·
1 Parent(s): 987927e

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +72 -0
  2. app.py +109 -0
  3. requirements.txt +7 -0
README.md ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: AI Clinical Notes
3
+ emoji: 🦀
4
+ colorFrom: yellow
5
+ colorTo: yellow
6
+ sdk: streamlit
7
+ sdk_version: 1.42.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
13
+
14
+ # AI Clinical Notes
15
+
16
+ Created by Dr. Fernando Ly
17
+
18
+ ## Description
19
+ This application helps medical professionals automatically generate clinical notes from doctor-patient conversations. It uses:
20
+ - Whisper for speech-to-text transcription
21
+ - Mixtral-8x7B for clinical note generation
22
+ - Streamlit for the user interface
23
+
24
+ ## Features
25
+ - Real-time audio recording
26
+ - Automatic speech transcription
27
+ - AI-powered clinical note generation
28
+ - Clean and intuitive interface
29
+
30
+ ## Setup
31
+
32
+ ### System Dependencies
33
+ First, install the required system dependencies:
34
+
35
+ For Ubuntu/Debian:
36
+ ```bash
37
+ sudo apt-get update
38
+ sudo apt-get install portaudio19-dev python3-pyaudio libasound2-dev
39
+ ```
40
+
41
+ For other systems, check the PortAudio documentation for installation instructions.
42
+
43
+ ### Python Dependencies
44
+ 1. Install the required Python dependencies:
45
+ ```bash
46
+ pip install -r requirements.txt
47
+ ```
48
+
49
+ 2. Make sure you have a `.env` file with your Huggingface API token:
50
+ ```
51
+ HUGGINGFACE_TOKEN=your_token_here
52
+ ```
53
+
54
+ 3. Run the application:
55
+ ```bash
56
+ streamlit run app.py
57
+ ```
58
+
59
+ ## Usage
60
+ 1. Open the application in your web browser
61
+ 2. Set the desired recording duration using the slider
62
+ 3. Click "Start Recording" to begin capturing the conversation
63
+ 4. Wait for the transcription and clinical notes to be generated
64
+ 5. Review and verify the generated notes
65
+
66
+ ## Important Notes
67
+ - This is an AI-assisted tool. Always review and verify the generated notes
68
+ - Ensure you have proper consent before recording conversations
69
+ - Keep patient privacy and HIPAA compliance in mind when using this tool
70
+
71
+ ## License
72
+ This project is licensed under the MIT License.
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import whisper
3
+ import requests
4
+ import json
5
+ from datetime import datetime
6
+ import os
7
+ from dotenv import load_dotenv
8
+ import numpy as np
9
+
10
+ # Load environment variables - try both .env and system environment
11
+ load_dotenv()
12
+ HUGGINGFACE_TOKEN = os.getenv('HUGGINGFACE_TOKEN')
13
+
14
+ if not HUGGINGFACE_TOKEN:
15
+ st.error("Please set the HUGGINGFACE_TOKEN in your Space's secrets.")
16
+ st.stop()
17
+
18
+ # Initialize Whisper model
19
+ @st.cache_resource
20
+ def load_whisper_model():
21
+ return whisper.load_model("base")
22
+
23
+ # Mixtral API call function
24
+ def get_clinical_notes(transcription):
25
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
26
+ headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
27
+
28
+ messages = [
29
+ {"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."},
30
+ {"role": "user", "content": f"Generate clinical notes from this doctor-patient conversation: {transcription}"}
31
+ ]
32
+
33
+ payload = {
34
+ "model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
35
+ "messages": messages,
36
+ "max_tokens": 500,
37
+ "stream": False
38
+ }
39
+
40
+ try:
41
+ response = requests.post(API_URL, headers=headers, json=payload)
42
+ response.raise_for_status()
43
+ return response.json()['choices'][0]['message']['content']
44
+ except Exception as e:
45
+ st.error(f"Error generating clinical notes: {str(e)}")
46
+ return None
47
+
48
+ # Main app
49
+ st.set_page_config(
50
+ page_title="AI Clinical Notes",
51
+ page_icon="🦀",
52
+ layout="wide"
53
+ )
54
+
55
+ st.title("🦀 AI Clinical Notes")
56
+ st.markdown("### Created by Dr. Fernando Ly")
57
+ st.markdown("This application helps medical professionals automatically generate clinical notes from patient conversations.")
58
+
59
+ # Create columns for better layout
60
+ col1, col2 = st.columns(2)
61
+
62
+ with col1:
63
+ st.subheader("Recording Controls")
64
+ # Audio recorder
65
+ audio_bytes = st.audio_recorder(
66
+ text="Click to record",
67
+ recording_color="#e87676",
68
+ neutral_color="#6aa36f",
69
+ icon_name="microphone"
70
+ )
71
+
72
+ if audio_bytes:
73
+ try:
74
+ # Save audio temporarily
75
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
76
+ filename = f"recording_{timestamp}.wav"
77
+ with open(filename, "wb") as f:
78
+ f.write(audio_bytes)
79
+
80
+ with st.spinner("Transcribing audio..."):
81
+ # Transcribe audio
82
+ model = load_whisper_model()
83
+ result = model.transcribe(filename)
84
+ st.session_state.transcription = result["text"]
85
+
86
+ with st.spinner("Generating clinical notes..."):
87
+ # Generate clinical notes
88
+ st.session_state.clinical_notes = get_clinical_notes(st.session_state.transcription)
89
+
90
+ # Clean up audio file
91
+ os.remove(filename)
92
+
93
+ except Exception as e:
94
+ st.error(f"Error processing audio: {str(e)}")
95
+
96
+ with col2:
97
+ # Display results
98
+ if "transcription" in st.session_state and st.session_state.transcription:
99
+ st.subheader("📝 Transcription")
100
+ st.write(st.session_state.transcription)
101
+
102
+ if "clinical_notes" in st.session_state and st.session_state.clinical_notes:
103
+ st.subheader("🏥 Clinical Notes")
104
+ st.write(st.session_state.clinical_notes)
105
+
106
+ # Footer
107
+ st.markdown("---")
108
+ st.markdown("*Note: This is an AI-assisted tool. Please review and verify all generated notes.*")
109
+ st.markdown("*For issues or feedback, please visit the [GitHub repository](https://huggingface.co/spaces/fernandoly/AI-Clinical-Notes/discussions)*")
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit==1.42.0
2
+ openai-whisper==20231117
3
+ transformers==4.38.2
4
+ torch==2.2.0
5
+ requests==2.31.0
6
+ numpy==1.26.4
7
+ python-dotenv==1.0.1