File size: 9,938 Bytes
4828c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import gradio as gr
import torch
import numpy as np
from PIL import Image
import time
import io
import subprocess
import sys

# Install required packages
def install_packages():
    packages = [
        "transformers",
        "accelerate", 
        "timm",
        "easyocr"
    ]
    for package in packages:
        try:
            subprocess.check_call([sys.executable, "-m", "pip", "install", package])
        except:
            print(f"Warning: Could not install {package}")

# Install packages at startup
install_packages()

from transformers import AutoProcessor, AutoModelForImageTextToText, AutoConfig

# Global variables for model
processor = None
model = None
config = None
ocr_reader = None

def load_model():
    """Load the Gemma 3n model"""
    global processor, model, config, ocr_reader
    
    try:
        print("πŸš€ Loading Gemma 3n model...")
        GEMMA_PATH = "google/gemma-3n-e2b-it"
        
        # Load configuration
        config = AutoConfig.from_pretrained(GEMMA_PATH, trust_remote_code=True)
        print("βœ… Config loaded")
        
        # Load processor
        processor = AutoProcessor.from_pretrained(GEMMA_PATH, trust_remote_code=True)
        print("βœ… Processor loaded")
        
        # Load model
        model = AutoModelForImageTextToText.from_pretrained(
            GEMMA_PATH,
            config=config,
            torch_dtype="auto",
            device_map="auto",
            trust_remote_code=True
        )
        print("βœ… Model loaded successfully!")
        
        # Set up compilation fix
        import torch._dynamo
        torch._dynamo.config.suppress_errors = True
        
        # Initialize OCR
        try:
            import easyocr
            ocr_reader = easyocr.Reader(['en'], gpu=False, verbose=False)
            print("βœ… EasyOCR initialized")
        except Exception as e:
            print(f"⚠️ EasyOCR not available: {e}")
            ocr_reader = None
            
        return True
        
    except Exception as e:
        print(f"❌ Model loading failed: {e}")
        return False

def generate_soap_note(text):
    """Generate SOAP note using Gemma 3n"""
    if model is None or processor is None:
        return "❌ Model not loaded. Please wait for initialization."
    
    soap_prompt = f"""You are a medical AI assistant. Convert the following medical notes into a properly formatted SOAP note.



Medical notes:

{text}



Please format as:

S - SUBJECTIVE: (chief complaint, history of present illness, past medical history, medications, allergies)

O - OBJECTIVE: (vital signs, physical examination findings)  

A - ASSESSMENT: (diagnosis/clinical impression)

P - PLAN: (treatment plan, follow-up instructions)



Generate a complete, professional SOAP note:"""
    
    messages = [{
        "role": "system",
        "content": [{"type": "text", "text": "You are an expert medical AI assistant specialized in creating SOAP notes from medical documentation."}]
    }, {
        "role": "user", 
        "content": [{"type": "text", "text": soap_prompt}]
    }]
    
    try:
        inputs = processor.apply_chat_template(
            messages,
            add_generation_prompt=True,
            tokenize=True,
            return_dict=True,
            return_tensors="pt"
        ).to(model.device)
        
        input_len = inputs["input_ids"].shape[-1]
        
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_new_tokens=400,
                do_sample=True,
                temperature=0.1,
                top_p=0.95,
                pad_token_id=processor.tokenizer.eos_token_id,
                disable_compile=True
            )
        
        response = processor.batch_decode(
            outputs[:, input_len:],
            skip_special_tokens=True
        )[0].strip()
        
        return response
        
    except Exception as e:
        return f"❌ SOAP generation failed: {str(e)}"

def extract_text_from_image(image):
    """Extract text using EasyOCR"""
    if ocr_reader is None:
        return "❌ OCR not available"
    
    try:
        if hasattr(image, 'convert'):
            image = image.convert('RGB')
        img_array = np.array(image)
        
        results = ocr_reader.readtext(img_array, detail=0, paragraph=True)
        if results:
            return ' '.join(results).strip()
        else:
            return "❌ No text detected in image"
            
    except Exception as e:
        return f"❌ OCR failed: {str(e)}"

def process_medical_input(image, text):
    """Main processing function for the Gradio interface"""
    
    if image is not None and text.strip():
        return "⚠️ Please provide either an image OR text, not both.", ""
    
    if image is not None:
        # Process image
        print("πŸ” Extracting text from image...")
        extracted_text = extract_text_from_image(image)
        
        if extracted_text.startswith('❌'):
            return extracted_text, ""
        
        print("πŸ€– Generating SOAP note...")
        soap_note = generate_soap_note(extracted_text)
        
        return extracted_text, soap_note
        
    elif text.strip():
        # Process text directly
        print("πŸ€– Generating SOAP note from text...")
        soap_note = generate_soap_note(text.strip())
        return text.strip(), soap_note
        
    else:
        return "❌ Please provide either an image or text input.", ""

def create_demo():
    """Create the Gradio demo interface"""
    
    # Sample text for demonstration
    sample_text = """Patient: John Smith, 45yo male

CC: Chest pain

Vitals: BP 140/90, HR 88, RR 16, O2 98%, Temp 98.6F

HPI: Patient reports crushing chest pain x 2 hours, radiating to left arm

PMH: HTN, DM Type 2

Current Meds: Lisinopril 10mg daily, Metformin 500mg BID

PE: Diaphoretic, anxious appearance

EKG: ST elevation in leads II, III, aVF"""
    
    with gr.Blocks(title="Medical OCR SOAP Generator", theme=gr.themes.Soft()) as demo:
        
        gr.Markdown("""

        # πŸ₯ Medical OCR SOAP Generator

        ### Powered by Gemma 3n - Convert handwritten medical notes to professional SOAP format

        

        **Instructions:**

        - **Option 1:** Upload an image of handwritten medical notes

        - **Option 2:** Enter medical text directly

        - The system will generate a properly formatted SOAP note

        

        ⚠️ **Note:** First generation may take ~60-90 seconds as the model loads

        """)
        
        with gr.Row():
            with gr.Column():
                image_input = gr.Image(
                    type="pil", 
                    label="πŸ“· Upload Medical Image",
                    height=300
                )
                
                text_input = gr.Textbox(
                    label="πŸ“ Or Enter Medical Text",
                    placeholder=sample_text,
                    lines=8,
                    max_lines=15
                )
                
                submit_btn = gr.Button(
                    "Generate SOAP Note",
                    variant="primary",
                    size="lg"
                )
                
            with gr.Column():
                extracted_output = gr.Textbox(
                    label="πŸ“‹ Extracted/Input Text",
                    lines=6,
                    max_lines=10
                )
                
                soap_output = gr.Textbox(
                    label="πŸ₯ Generated SOAP Note",
                    lines=12,
                    max_lines=20
                )
        
        # Example section
        gr.Markdown("### πŸ“‹ Quick Test Example")
        example_btn = gr.Button("Try Sample Medical Text", variant="secondary")
        
        def load_example():
            return sample_text, None
            
        example_btn.click(
            load_example,
            outputs=[text_input, image_input]
        )
        
        # Process function
        submit_btn.click(
            process_medical_input,
            inputs=[image_input, text_input],
            outputs=[extracted_output, soap_output]
        )
        
        gr.Markdown("""

        ---

        **About:** This application uses Google's Gemma 3n model for medical text understanding and EasyOCR for handwriting recognition. 

        All processing is done locally for HIPAA compliance.

        

        **Competition Entry:** Medical AI Innovation Challenge 2024

        """)
    
    return demo

# Initialize the application
if __name__ == "__main__":
    print("πŸš€ Starting Medical OCR SOAP Generator...")
    
    # Load model
    model_loaded = load_model()
    
    if model_loaded:
        print("βœ… All systems ready!")
        demo = create_demo()
        demo.launch(
            share=True,
            server_name="0.0.0.0",
            server_port=7860
        )
    else:
        print("❌ Failed to load model. Creating fallback demo...")
        
        def fallback_demo():
            return "❌ Model loading failed. Please check the logs.", "❌ Model not available."
        
        demo = gr.Interface(
            fn=fallback_demo,
            inputs=[
                gr.Image(type="pil", label="Upload Medical Image"),
                gr.Textbox(label="Enter Medical Text", lines=5)
            ],
            outputs=[
                gr.Textbox(label="Status"),
                gr.Textbox(label="Error Message")
            ],
            title="❌ Medical OCR - Model Loading Failed"
        )
        
        demo.launch(share=True)