File size: 5,772 Bytes
bd8ae0e
 
 
 
 
 
 
7eac884
bd8ae0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa1c2c4
bd8ae0e
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import streamlit as st
import openai
from pydub import AudioSegment
import io
import time

# Set your OpenAI API key
openai_api_key = "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"

# Function to summarize the transcript into medical sections
def medical_summary(transcript):
    prompt = f"""
    Organize the following medical transcript into the predefined sections:

    Sections:
    1. Medical Specialty
    2. CHIEF COMPLAINT
    3. Purpose of visit
    4. HISTORY and Physical
       - PAST MEDICAL HISTORY
       - PAST SURGICAL HISTORY
       - ALLERGIES History
       - Social History
       - REVIEW OF SYSTEMS
    5. PHYSICAL EXAMINATION
       - GENERAL
       - Vitals
       - ENT
       - Head
       - Neck
       - Chest
       - Heart
       - Abdomen
       - Pelvic
       - Extremities

    Transcript:
    {transcript}

    Provide a structured summary in the above format.
    """
    try:
        response = openai.ChatCompletion.create(
            model="gpt-4",  # Use gpt-3.5-turbo if GPT-4 is unavailable
            messages=[
                {"role": "system", "content": "You are a helpful medical assistant."},
                {"role": "user", "content": prompt}
            ],
            temperature=0.5,
            max_tokens=3000  # Adjust max_tokens based on the expected size of the summary
        )
        structured_summary = response['choices'][0]['message']['content'].strip()
        return structured_summary
    except Exception as e:
        return f"An error occurred: {e}"

# Function to convert audio to text using OpenAI's Whisper or any suitable service
def transcribe_audio(audio_file):
    audio = AudioSegment.from_file(audio_file)
    audio = audio.set_channels(1).set_frame_rate(16000)  # Convert to mono and set appropriate frame rate

    buffer = io.BytesIO()
    audio.export(buffer, format="wav")
    buffer.seek(0)

    # Here, you would normally call the transcription API or process the file
    # Example (assuming transcription API integration):
    # response = openai.Audio.transcribe(model="whisper-1", file=buffer)

    return "Patient complains of chest pain, shortness of breath, and dizziness. He has a history of hypertension."

# Streamlit UI
def main():
    # Custom CSS to improve UI aesthetics
    st.markdown("""
        <style>
            body {
                font-family: 'Arial', sans-serif;
                background-color: #f4f4f9;
            }
            .title {
                font-size: 36px;
                color: #1E2A47;
                font-weight: 600;
                text-align: center;
                padding-top: 30px;
                padding-bottom: 10px;
            }
            .subheader {
                font-size: 24px;
                color: #2F4F9A;
                text-align: center;
                margin-bottom: 20px;
            }
            .card {
                background-color: #ffffff;
                border-radius: 8px;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                padding: 20px;
                margin-bottom: 30px;
            }
            .button {
                background-color: #4CAF50;
                color: white;
                padding: 12px 20px;
                font-size: 16px;
                border-radius: 8px;
                border: none;
                cursor: pointer;
                transition: background-color 0.3s ease;
            }
            .button:hover {
                background-color: #45a049;
            }
            .error {
                color: red;
                font-weight: bold;
                text-align: center;
            }
            .footer {
                text-align: center;
                margin-top: 40px;
                color: #555;
            }
        </style>
    """, unsafe_allow_html=True)

    # Title and subtitle
    st.markdown('<div class="title">Medical Transcription Dashboard</div>', unsafe_allow_html=True)
    st.markdown('<div class="subheader">Upload an audio file or enter the transcript to generate a medical summary</div>', unsafe_allow_html=True)

    # Layout: Input Area in Card
    with st.container():
        st.markdown('<div class="card">', unsafe_allow_html=True)

        # Audio upload and text input options
        audio_file = st.file_uploader("Upload an audio file (wav, mp3, etc.)", type=["wav", "mp3"], label_visibility="collapsed")
        transcript_text = st.text_area("Or, enter the transcript manually:", height=200)

        # Show "Generate Medical Summary" button
        if st.button("Generate Medical Summary", key="generate", use_container_width=True):
            if audio_file:
                # Show loading spinner while processing
                with st.spinner('Transcribing audio...'):
                    transcript = transcribe_audio(audio_file)
                    time.sleep(2)  # Simulate delay
                    st.write(f"Transcript: {transcript}")
            elif transcript_text:
                transcript = transcript_text
            else:
                st.markdown('<div class="error">Please upload an audio file or enter a transcript.</div>', unsafe_allow_html=True)

            if transcript:
                # Show loading spinner while generating summary
                with st.spinner('Generating Medical Summary...'):
                    time.sleep(2)  # Simulate processing time
                    summary = medical_summary(transcript)
                    st.markdown("### Medical Summary:")
                    st.write(summary)

        st.markdown('</div>', unsafe_allow_html=True)



if __name__ == "__main__":
    main()