ferferefer commited on
Commit
d06c695
·
verified ·
1 Parent(s): 04e1ff3

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # File upload section
84
+ st.write("Upload your recorded conversation:")
85
+ uploaded_file = st.file_uploader("Upload an audio file (WAV format)", type=['wav'])
86
+
87
+ if uploaded_file:
88
+ # Display the uploaded audio
89
+ st.audio(uploaded_file, format="audio/wav")
90
+
91
+ # Save audio temporarily
92
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
93
+ tmp_file.write(uploaded_file.getvalue())
94
+ process_audio(tmp_file.name)
95
+ # Clean up
96
+ os.unlink(tmp_file.name)
97
+
98
+ # Add recording instructions
99
+ st.markdown("---")
100
+ st.markdown("""
101
+ ### Recording Instructions
102
+ To record your conversation:
103
+ 1. Use your phone's voice recorder app
104
+ 2. Save the recording as a WAV file
105
+ 3. Upload it here
106
+
107
+ Alternatively, you can use these free tools:
108
+ - Windows: Voice Recorder app
109
+ - Mac: QuickTime Player
110
+ - Online: [Vocaroo](https://vocaroo.com) (saves directly as WAV)
111
+ """)
112
+
113
+ with col2:
114
+ # Display results
115
+ if "transcription" in st.session_state and st.session_state.transcription:
116
+ st.subheader("📝 Transcription")
117
+ st.write(st.session_state.transcription)
118
+
119
+ if "clinical_notes" in st.session_state and st.session_state.clinical_notes:
120
+ st.subheader("🏥 Clinical Notes")
121
+ st.write(st.session_state.clinical_notes)
122
+
123
+ # Instructions
124
+ st.markdown("---")
125
+ st.markdown("""
126
+ ### How to use:
127
+ 1. Record your conversation using any recording app
128
+ 2. Save the recording as a WAV file
129
+ 3. Upload the file using the uploader above
130
+ 4. Wait for the transcription and clinical notes to be generated
131
+ 5. Review the generated notes
132
+
133
+ **Note**: For best results, ensure:
134
+ - Clear audio quality
135
+ - Minimal background noise
136
+ - Proper microphone placement during recording
137
+ """)
138
+
139
+ # Footer
140
+ st.markdown("---")
141
+ st.markdown("*Note: This is an AI-assisted tool. Please review and verify all generated notes.*")
142
+ st.markdown("*For issues or feedback, please visit the [GitHub repository](https://huggingface.co/spaces/fernandoly/AI-Clinical-Notes/discussions)*")