ferferefer commited on
Commit
a2e27db
Β·
verified Β·
1 Parent(s): e7fde0b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +160 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from io import BytesIO
10
+ import tempfile
11
+
12
+ # Load environment variables - try both .env and system environment
13
+ load_dotenv()
14
+ HUGGINGFACE_TOKEN = os.getenv('HUGGINGFACE_TOKEN')
15
+
16
+ if not HUGGINGFACE_TOKEN:
17
+ st.error("Please set the HUGGINGFACE_TOKEN in your Space's secrets.")
18
+ st.stop()
19
+
20
+ # Initialize Whisper model
21
+ @st.cache_resource
22
+ def load_whisper_model():
23
+ return whisper.load_model("base")
24
+
25
+ def process_audio(audio_file):
26
+ """Process audio file and generate transcription and clinical notes"""
27
+ try:
28
+ with st.spinner("Transcribing audio..."):
29
+ # Transcribe audio
30
+ model = load_whisper_model()
31
+ result = model.transcribe(audio_file)
32
+ st.session_state.transcription = result["text"]
33
+
34
+ with st.spinner("Generating clinical notes..."):
35
+ # Generate clinical notes
36
+ st.session_state.clinical_notes = get_clinical_notes(st.session_state.transcription)
37
+
38
+ except Exception as e:
39
+ st.error(f"Error processing audio: {str(e)}")
40
+
41
+ # Mixtral API call function
42
+ def get_clinical_notes(transcription):
43
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
44
+ headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
45
+
46
+ messages = [
47
+ {"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."},
48
+ {"role": "user", "content": f"Generate clinical notes from this doctor-patient conversation: {transcription}"}
49
+ ]
50
+
51
+ payload = {
52
+ "model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
53
+ "messages": messages,
54
+ "max_tokens": 500,
55
+ "stream": False
56
+ }
57
+
58
+ try:
59
+ response = requests.post(API_URL, headers=headers, json=payload)
60
+ response.raise_for_status()
61
+ return response.json()['choices'][0]['message']['content']
62
+ except Exception as e:
63
+ st.error(f"Error generating clinical notes: {str(e)}")
64
+ return None
65
+
66
+ # Main app
67
+ st.set_page_config(
68
+ page_title="AI Clinical Notes",
69
+ page_icon="πŸ¦€",
70
+ layout="wide"
71
+ )
72
+
73
+ st.title("πŸ¦€ AI Clinical Notes")
74
+ st.markdown("### Created by Dr. Fernando Ly")
75
+ st.markdown("This application helps medical professionals automatically generate clinical notes from patient conversations.")
76
+
77
+ # Create columns for better layout
78
+ col1, col2 = st.columns(2)
79
+
80
+ with col1:
81
+ st.subheader("Audio Input")
82
+
83
+ # Create tabs for different input methods
84
+ tab1, tab2 = st.tabs(["🎀 Record", "πŸ“ Upload"])
85
+
86
+ with tab1:
87
+ st.write("Record your conversation directly:")
88
+ st.write("⚠️ Please allow microphone access when prompted")
89
+
90
+ if "audio_recorder_key" not in st.session_state:
91
+ st.session_state["audio_recorder_key"] = 0
92
+
93
+ audio_bytes = st.audio_recorder(
94
+ text="Click to record",
95
+ recording_color="#e87676",
96
+ neutral_color="#6aa36f",
97
+ key=f"audio_recorder_{st.session_state.audio_recorder_key}"
98
+ )
99
+
100
+ if st.button("Clear Recording"):
101
+ st.session_state.audio_recorder_key += 1
102
+ st.experimental_rerun()
103
+
104
+ if audio_bytes:
105
+ # Display the recorded audio
106
+ st.audio(audio_bytes, format="audio/wav")
107
+
108
+ # Save audio temporarily and process
109
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
110
+ tmp_file.write(audio_bytes)
111
+ process_audio(tmp_file.name)
112
+ # Clean up
113
+ os.unlink(tmp_file.name)
114
+
115
+ with tab2:
116
+ st.write("Or upload a pre-recorded file:")
117
+ uploaded_file = st.file_uploader("Upload an audio file (WAV format)", type=['wav'])
118
+
119
+ if uploaded_file:
120
+ # Display the uploaded audio
121
+ st.audio(uploaded_file, format="audio/wav")
122
+
123
+ # Save audio temporarily
124
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
125
+ tmp_file.write(uploaded_file.getvalue())
126
+ process_audio(tmp_file.name)
127
+ # Clean up
128
+ os.unlink(tmp_file.name)
129
+
130
+ with col2:
131
+ # Display results
132
+ if "transcription" in st.session_state and st.session_state.transcription:
133
+ st.subheader("πŸ“ Transcription")
134
+ st.write(st.session_state.transcription)
135
+
136
+ if "clinical_notes" in st.session_state and st.session_state.clinical_notes:
137
+ st.subheader("πŸ₯ Clinical Notes")
138
+ st.write(st.session_state.clinical_notes)
139
+
140
+ # Instructions
141
+ st.markdown("---")
142
+ st.markdown("""
143
+ ### How to use:
144
+ 1. Choose your preferred input method:
145
+ - **Record**: Click the microphone button to start/stop recording directly in your browser
146
+ - **Upload**: Upload a pre-recorded WAV file
147
+ 2. Wait for the transcription and clinical notes to be generated
148
+ 3. Review the generated notes
149
+
150
+ **Note**: For best results, ensure:
151
+ - Clear audio quality
152
+ - Minimal background noise
153
+ - Proper microphone placement
154
+ - Allow microphone access in your browser when prompted
155
+ """)
156
+
157
+ # Footer
158
+ st.markdown("---")
159
+ st.markdown("*Note: This is an AI-assisted tool. Please review and verify all generated notes.*")
160
+ st.markdown("*For issues or feedback, please visit the [GitHub repository](https://huggingface.co/spaces/fernandoly/AI-Clinical-Notes/discussions)*")