Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,571 +1,309 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
import
|
6 |
-
import
|
7 |
-
import
|
8 |
-
import
|
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 |
-
return
|
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 |
-
print("
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
try:
|
311 |
-
results = ocr_reader.readtext(processed_img, detail=0, paragraph=True)
|
312 |
-
if results:
|
313 |
-
extracted_text = ' '.join(results)
|
314 |
-
if len(extracted_text.strip()) > 10:
|
315 |
-
return clean_medical_text(extracted_text)
|
316 |
-
except Exception as e:
|
317 |
-
print(f"EasyOCR failed: {e}")
|
318 |
-
|
319 |
-
# Fallback to Tesseract with medical optimization
|
320 |
-
try:
|
321 |
-
import pytesseract
|
322 |
-
|
323 |
-
# Medical-optimized Tesseract config
|
324 |
-
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,;:()[]{}/-+= '
|
325 |
-
|
326 |
-
tesseract_text = pytesseract.image_to_string(processed_img, config=custom_config)
|
327 |
-
|
328 |
-
if len(tesseract_text.strip()) > 5:
|
329 |
-
return clean_medical_text(tesseract_text)
|
330 |
-
|
331 |
-
except Exception as e:
|
332 |
-
print(f"Tesseract failed: {e}")
|
333 |
-
|
334 |
-
return "β Could not extract text from image. Please ensure the image is clear and try again."
|
335 |
-
|
336 |
-
except Exception as e:
|
337 |
-
return f"β Error processing image: {str(e)}"
|
338 |
-
|
339 |
-
def preprocess_medical_image(image):
|
340 |
-
"""Optimized preprocessing for medical handwriting"""
|
341 |
-
try:
|
342 |
-
img_array = np.array(image)
|
343 |
-
|
344 |
-
# Convert to grayscale
|
345 |
-
if len(img_array.shape) == 3:
|
346 |
-
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
347 |
-
else:
|
348 |
-
gray = img_array
|
349 |
-
|
350 |
-
# Resize for optimal OCR (medical notes are often small)
|
351 |
-
height, width = gray.shape
|
352 |
-
if height < 400 or width < 400:
|
353 |
-
scale_factor = max(400/height, 400/width)
|
354 |
-
new_width = int(width * scale_factor)
|
355 |
-
new_height = int(height * scale_factor)
|
356 |
-
gray = cv2.resize(gray, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
|
357 |
-
|
358 |
-
# Advanced preprocessing for handwritten medical text
|
359 |
-
# 1. Noise reduction
|
360 |
-
denoised = cv2.fastNlMeansDenoising(gray)
|
361 |
-
|
362 |
-
# 2. Contrast enhancement specifically for handwriting
|
363 |
-
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
364 |
-
enhanced = clahe.apply(denoised)
|
365 |
-
|
366 |
-
# 3. Morphological operations to clean up text
|
367 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,1))
|
368 |
-
cleaned = cv2.morphologyEx(enhanced, cv2.MORPH_CLOSE, kernel)
|
369 |
-
|
370 |
-
# 4. Adaptive thresholding (better for varying lighting)
|
371 |
-
thresh = cv2.adaptiveThreshold(
|
372 |
-
cleaned, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2
|
373 |
-
)
|
374 |
-
|
375 |
-
return thresh
|
376 |
-
|
377 |
-
except Exception as e:
|
378 |
-
print(f"β Image preprocessing error: {e}")
|
379 |
-
return np.array(image)
|
380 |
-
|
381 |
-
def clean_medical_text(text):
|
382 |
-
"""Clean extracted text with medical context awareness"""
|
383 |
-
# Remove excessive whitespace and empty lines
|
384 |
-
lines = [line.strip() for line in text.split('\n') if line.strip()]
|
385 |
-
|
386 |
-
# Medical text cleaning
|
387 |
-
cleaned_lines = []
|
388 |
-
for line in lines:
|
389 |
-
# Remove obvious OCR artifacts
|
390 |
-
line = line.replace('|', 'l').replace('_', ' ').replace('~', '-')
|
391 |
-
|
392 |
-
# Fix common medical abbreviations that OCR might misread
|
393 |
-
medical_corrections = {
|
394 |
-
'BP': 'BP', 'HR': 'HR', 'RR': 'RR', 'O2': 'O2',
|
395 |
-
'mg': 'mg', 'ml': 'ml', 'cc': 'cc', 'cm': 'cm'
|
396 |
-
}
|
397 |
-
|
398 |
-
for wrong, correct in medical_corrections.items():
|
399 |
-
line = line.replace(wrong.lower(), correct)
|
400 |
-
|
401 |
-
if len(line) > 1: # Filter out single characters
|
402 |
-
cleaned_lines.append(line)
|
403 |
-
|
404 |
-
return '\n'.join(cleaned_lines)
|
405 |
-
|
406 |
-
# Enhanced Gradio Interface
|
407 |
-
def gradio_generate_soap(medical_notes, uploaded_image, model_data):
|
408 |
-
"""Main Gradio interface function"""
|
409 |
-
model, tokenizer = model_data if model_data else (None, None)
|
410 |
-
ocr_reader = getattr(gradio_generate_soap, 'ocr_reader', None)
|
411 |
-
|
412 |
-
text_to_process = medical_notes.strip() if medical_notes else ""
|
413 |
-
|
414 |
-
# Process uploaded image with enhanced OCR
|
415 |
-
if uploaded_image is not None:
|
416 |
-
try:
|
417 |
-
print("π Extracting text from medical image...")
|
418 |
-
extracted_text = extract_text_from_image(uploaded_image, ocr_reader)
|
419 |
-
|
420 |
-
if not extracted_text.startswith("β"):
|
421 |
-
if not text_to_process:
|
422 |
-
text_to_process = f"--- Extracted from uploaded image ---\n{extracted_text}"
|
423 |
-
else:
|
424 |
-
text_to_process = f"{text_to_process}\n\n--- Additional text from image ---\n{extracted_text}"
|
425 |
-
else:
|
426 |
-
return extracted_text
|
427 |
-
|
428 |
-
except Exception as e:
|
429 |
-
return f"β Error processing image: {str(e)}"
|
430 |
-
|
431 |
-
if not text_to_process:
|
432 |
-
return "β Please enter medical notes manually or upload an image with medical text"
|
433 |
-
|
434 |
-
# Generate SOAP note using Gemma 3n
|
435 |
-
try:
|
436 |
-
return generate_soap_note_gemma(text_to_process, model, tokenizer)
|
437 |
-
except Exception as e:
|
438 |
-
return f"β Error generating SOAP note: {str(e)}"
|
439 |
-
|
440 |
-
# Example medical notes for testing
|
441 |
-
medical_examples = {
|
442 |
-
'chest_pain': """Patient: John Smith, 45yo M
|
443 |
-
CC: Chest pain x 2 hours
|
444 |
-
HPI: Sudden onset sharp substernal chest pain 7/10, radiating to L arm. Associated SOB, diaphoresis. No N/V.
|
445 |
-
PMH: HTN, no CAD
|
446 |
-
VS: BP 150/90, HR 110, RR 22, O2 96% RA
|
447 |
-
PE: Anxious, diaphoretic. RRR, no murmur. CTAB. No edema.
|
448 |
-
A: Acute chest pain, r/o MI
|
449 |
-
P: EKG, troponins, CXR, ASA 325mg, monitor""",
|
450 |
-
|
451 |
-
'diabetes': """Patient: Maria Garcia, 52yo F
|
452 |
-
CC: Increased thirst, urination x 3 weeks
|
453 |
-
HPI: Polyuria, polydipsia, 10lb weight loss. FH DM. No fever, abd pain.
|
454 |
-
VS: BP 140/85, HR 88, BMI 28
|
455 |
-
PE: Mild dehydration, dry MM. RRR. No diabetic foot changes.
|
456 |
-
Labs: Random glucose 280, HbA1c pending
|
457 |
-
A: New onset DM Type 2
|
458 |
-
P: HbA1c, CMP, diabetic education, metformin, f/u 2 weeks""",
|
459 |
-
|
460 |
-
'pediatric': """Patient: Emma Thompson, 8yo F
|
461 |
-
CC: Fever, sore throat x 2 days
|
462 |
-
HPI: Fever 102F, sore throat, odynophagia, decreased appetite. No cough/rhinorrhea.
|
463 |
-
VS: T 101.8F, HR 110, RR 20, O2 99%
|
464 |
-
PE: Alert, mildly ill. Throat erythematous w/ tonsillar exudate. Anterior cervical LAD.
|
465 |
-
A: Strep pharyngitis (probable)
|
466 |
-
P: Rapid strep, throat culture, amoxicillin if +, supportive care, RTC PRN"""
|
467 |
-
}
|
468 |
-
|
469 |
-
# Initialize everything
|
470 |
-
def initialize_app():
|
471 |
-
"""Initialize the complete application"""
|
472 |
-
print("π Initializing Scribbled Docs SOAP Generator...")
|
473 |
-
|
474 |
-
# Setup device
|
475 |
-
device = setup_device()
|
476 |
-
|
477 |
-
# Load model
|
478 |
-
model, tokenizer = load_unsloth_gemma_model(device)
|
479 |
-
|
480 |
-
# Initialize OCR
|
481 |
-
ocr_reader = initialize_ocr()
|
482 |
-
gradio_generate_soap.ocr_reader = ocr_reader
|
483 |
-
|
484 |
-
return model, tokenizer
|
485 |
-
|
486 |
-
# Create the main Gradio interface
|
487 |
-
def create_interface(model, tokenizer):
|
488 |
-
"""Create the main Gradio interface"""
|
489 |
-
|
490 |
-
interface = gr.Interface(
|
491 |
-
fn=lambda notes, image: gradio_generate_soap(notes, image, (model, tokenizer)),
|
492 |
-
inputs=[
|
493 |
-
gr.Textbox(
|
494 |
-
lines=8,
|
495 |
-
placeholder="Enter medical notes here...\n\nExample:\nPatient: John Doe, 45yo M\nCC: Chest pain\nVS: BP 140/90, HR 88\n...",
|
496 |
-
label="π Medical Notes (Manual Entry)",
|
497 |
-
info="Enter unstructured medical notes or upload an image below"
|
498 |
-
),
|
499 |
-
gr.Image(
|
500 |
-
type="pil",
|
501 |
-
label="π· Upload Medical Image (Handwritten/Typed Notes)",
|
502 |
-
sources=["upload", "webcam"],
|
503 |
-
info="Upload PNG/JPG images of medical notes - handwritten or typed"
|
504 |
-
)
|
505 |
-
],
|
506 |
-
outputs=[
|
507 |
-
gr.Textbox(
|
508 |
-
lines=20,
|
509 |
-
label="π Generated SOAP Note",
|
510 |
-
show_copy_button=True,
|
511 |
-
info="Professional SOAP note generated from your input"
|
512 |
-
)
|
513 |
-
],
|
514 |
-
title="π₯ Scribbled Docs - Medical SOAP Note Generator",
|
515 |
-
description="""
|
516 |
-
**Transform medical notes into professional SOAP documentation using Gemma 3n AI**
|
517 |
-
|
518 |
-
π **100% Offline & HIPAA Compliant** - All processing happens locally on your device
|
519 |
-
π€ **Powered by Unsloth-optimized Gemma 3n** - 4-bit quantized for efficiency
|
520 |
-
π **Supports handwritten & typed notes** - Advanced OCR for medical handwriting
|
521 |
-
|
522 |
-
**Instructions:**
|
523 |
-
1. Enter medical notes manually OR upload an image
|
524 |
-
2. Click Submit to generate a structured SOAP note
|
525 |
-
3. Copy the result for use in your medical records
|
526 |
-
|
527 |
-
**Perfect for:** Emergency medicine, family practice, internal medicine, pediatrics
|
528 |
-
""",
|
529 |
-
examples=[
|
530 |
-
[medical_examples['chest_pain'], None],
|
531 |
-
[medical_examples['diabetes'], None],
|
532 |
-
[medical_examples['pediatric'], None]
|
533 |
-
],
|
534 |
-
theme=gr.themes.Soft(
|
535 |
-
primary_hue="blue",
|
536 |
-
secondary_hue="green"
|
537 |
-
),
|
538 |
-
allow_flagging="never",
|
539 |
-
analytics_enabled=False
|
540 |
-
)
|
541 |
-
|
542 |
-
return interface
|
543 |
-
|
544 |
-
# Main execution
|
545 |
-
if __name__ == "__main__":
|
546 |
-
try:
|
547 |
-
# Initialize app
|
548 |
-
model, tokenizer = initialize_app()
|
549 |
-
|
550 |
-
# Create and launch interface
|
551 |
-
interface = create_interface(model, tokenizer)
|
552 |
-
|
553 |
-
print("\nπ― Scribbled Docs SOAP Generator Ready!")
|
554 |
-
print("π± Features:")
|
555 |
-
print(" β
Offline processing (HIPAA compliant)")
|
556 |
-
print(" β
Unsloth-optimized Gemma 3n model")
|
557 |
-
print(" β
Handwritten note OCR")
|
558 |
-
print(" β
Professional SOAP formatting")
|
559 |
-
print(" β
Medical terminology aware")
|
560 |
-
|
561 |
-
# Launch interface
|
562 |
-
interface.launch(
|
563 |
-
share=True, # Creates public link
|
564 |
-
server_port=7860,
|
565 |
-
show_error=True,
|
566 |
-
quiet=False
|
567 |
-
)
|
568 |
-
|
569 |
-
except Exception as e:
|
570 |
-
print(f"β Error launching application: {e}")
|
571 |
-
print("π‘ Make sure you have installed: pip install unsloth gradio easyocr opencv-python")
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import time
|
6 |
+
import io
|
7 |
+
import subprocess
|
8 |
+
import sys
|
9 |
+
|
10 |
+
# Install required packages
|
11 |
+
def install_packages():
|
12 |
+
packages = [
|
13 |
+
"transformers",
|
14 |
+
"accelerate",
|
15 |
+
"timm",
|
16 |
+
"easyocr"
|
17 |
+
]
|
18 |
+
for package in packages:
|
19 |
+
try:
|
20 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
21 |
+
except:
|
22 |
+
print(f"Warning: Could not install {package}")
|
23 |
+
|
24 |
+
# Install packages at startup
|
25 |
+
install_packages()
|
26 |
+
|
27 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText, AutoConfig
|
28 |
+
|
29 |
+
# Global variables for model
|
30 |
+
processor = None
|
31 |
+
model = None
|
32 |
+
config = None
|
33 |
+
ocr_reader = None
|
34 |
+
|
35 |
+
def load_model():
|
36 |
+
"""Load the Gemma 3n model"""
|
37 |
+
global processor, model, config, ocr_reader
|
38 |
+
|
39 |
+
try:
|
40 |
+
print("π Loading Gemma 3n model...")
|
41 |
+
GEMMA_PATH = "google/gemma-3n-e2b-it"
|
42 |
+
|
43 |
+
# Load configuration
|
44 |
+
config = AutoConfig.from_pretrained(GEMMA_PATH, trust_remote_code=True)
|
45 |
+
print("β
Config loaded")
|
46 |
+
|
47 |
+
# Load processor
|
48 |
+
processor = AutoProcessor.from_pretrained(GEMMA_PATH, trust_remote_code=True)
|
49 |
+
print("β
Processor loaded")
|
50 |
+
|
51 |
+
# Load model
|
52 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
53 |
+
GEMMA_PATH,
|
54 |
+
config=config,
|
55 |
+
torch_dtype="auto",
|
56 |
+
device_map="auto",
|
57 |
+
trust_remote_code=True
|
58 |
+
)
|
59 |
+
print("β
Model loaded successfully!")
|
60 |
+
|
61 |
+
# Set up compilation fix
|
62 |
+
import torch._dynamo
|
63 |
+
torch._dynamo.config.suppress_errors = True
|
64 |
+
|
65 |
+
# Initialize OCR
|
66 |
+
try:
|
67 |
+
import easyocr
|
68 |
+
ocr_reader = easyocr.Reader(['en'], gpu=False, verbose=False)
|
69 |
+
print("β
EasyOCR initialized")
|
70 |
+
except Exception as e:
|
71 |
+
print(f"β οΈ EasyOCR not available: {e}")
|
72 |
+
ocr_reader = None
|
73 |
+
|
74 |
+
return True
|
75 |
+
|
76 |
+
except Exception as e:
|
77 |
+
print(f"β Model loading failed: {e}")
|
78 |
+
return False
|
79 |
+
|
80 |
+
def generate_soap_note(text):
|
81 |
+
"""Generate SOAP note using Gemma 3n"""
|
82 |
+
if model is None or processor is None:
|
83 |
+
return "β Model not loaded. Please wait for initialization."
|
84 |
+
|
85 |
+
soap_prompt = f"""You are a medical AI assistant. Convert the following medical notes into a properly formatted SOAP note.
|
86 |
+
|
87 |
+
Medical notes:
|
88 |
+
{text}
|
89 |
+
|
90 |
+
Please format as:
|
91 |
+
S - SUBJECTIVE: (chief complaint, history of present illness, past medical history, medications, allergies)
|
92 |
+
O - OBJECTIVE: (vital signs, physical examination findings)
|
93 |
+
A - ASSESSMENT: (diagnosis/clinical impression)
|
94 |
+
P - PLAN: (treatment plan, follow-up instructions)
|
95 |
+
|
96 |
+
Generate a complete, professional SOAP note:"""
|
97 |
+
|
98 |
+
messages = [{
|
99 |
+
"role": "system",
|
100 |
+
"content": [{"type": "text", "text": "You are an expert medical AI assistant specialized in creating SOAP notes from medical documentation."}]
|
101 |
+
}, {
|
102 |
+
"role": "user",
|
103 |
+
"content": [{"type": "text", "text": soap_prompt}]
|
104 |
+
}]
|
105 |
+
|
106 |
+
try:
|
107 |
+
inputs = processor.apply_chat_template(
|
108 |
+
messages,
|
109 |
+
add_generation_prompt=True,
|
110 |
+
tokenize=True,
|
111 |
+
return_dict=True,
|
112 |
+
return_tensors="pt"
|
113 |
+
).to(model.device)
|
114 |
+
|
115 |
+
input_len = inputs["input_ids"].shape[-1]
|
116 |
+
|
117 |
+
with torch.no_grad():
|
118 |
+
outputs = model.generate(
|
119 |
+
**inputs,
|
120 |
+
max_new_tokens=400,
|
121 |
+
do_sample=True,
|
122 |
+
temperature=0.1,
|
123 |
+
top_p=0.95,
|
124 |
+
pad_token_id=processor.tokenizer.eos_token_id,
|
125 |
+
disable_compile=True
|
126 |
+
)
|
127 |
+
|
128 |
+
response = processor.batch_decode(
|
129 |
+
outputs[:, input_len:],
|
130 |
+
skip_special_tokens=True
|
131 |
+
)[0].strip()
|
132 |
+
|
133 |
+
return response
|
134 |
+
|
135 |
+
except Exception as e:
|
136 |
+
return f"β SOAP generation failed: {str(e)}"
|
137 |
+
|
138 |
+
def extract_text_from_image(image):
|
139 |
+
"""Extract text using EasyOCR"""
|
140 |
+
if ocr_reader is None:
|
141 |
+
return "β OCR not available"
|
142 |
+
|
143 |
+
try:
|
144 |
+
if hasattr(image, 'convert'):
|
145 |
+
image = image.convert('RGB')
|
146 |
+
img_array = np.array(image)
|
147 |
+
|
148 |
+
results = ocr_reader.readtext(img_array, detail=0, paragraph=True)
|
149 |
+
if results:
|
150 |
+
return ' '.join(results).strip()
|
151 |
+
else:
|
152 |
+
return "β No text detected in image"
|
153 |
+
|
154 |
+
except Exception as e:
|
155 |
+
return f"β OCR failed: {str(e)}"
|
156 |
+
|
157 |
+
def process_medical_input(image, text):
|
158 |
+
"""Main processing function for the Gradio interface"""
|
159 |
+
|
160 |
+
if image is not None and text.strip():
|
161 |
+
return "β οΈ Please provide either an image OR text, not both.", ""
|
162 |
+
|
163 |
+
if image is not None:
|
164 |
+
# Process image
|
165 |
+
print("π Extracting text from image...")
|
166 |
+
extracted_text = extract_text_from_image(image)
|
167 |
+
|
168 |
+
if extracted_text.startswith('β'):
|
169 |
+
return extracted_text, ""
|
170 |
+
|
171 |
+
print("π€ Generating SOAP note...")
|
172 |
+
soap_note = generate_soap_note(extracted_text)
|
173 |
+
|
174 |
+
return extracted_text, soap_note
|
175 |
+
|
176 |
+
elif text.strip():
|
177 |
+
# Process text directly
|
178 |
+
print("π€ Generating SOAP note from text...")
|
179 |
+
soap_note = generate_soap_note(text.strip())
|
180 |
+
return text.strip(), soap_note
|
181 |
+
|
182 |
+
else:
|
183 |
+
return "β Please provide either an image or text input.", ""
|
184 |
+
|
185 |
+
def create_demo():
|
186 |
+
"""Create the Gradio demo interface"""
|
187 |
+
|
188 |
+
# Sample text for demonstration
|
189 |
+
sample_text = """Patient: John Smith, 45yo male
|
190 |
+
CC: Chest pain
|
191 |
+
Vitals: BP 140/90, HR 88, RR 16, O2 98%, Temp 98.6F
|
192 |
+
HPI: Patient reports crushing chest pain x 2 hours, radiating to left arm
|
193 |
+
PMH: HTN, DM Type 2
|
194 |
+
Current Meds: Lisinopril 10mg daily, Metformin 500mg BID
|
195 |
+
PE: Diaphoretic, anxious appearance
|
196 |
+
EKG: ST elevation in leads II, III, aVF"""
|
197 |
+
|
198 |
+
with gr.Blocks(title="Medical OCR SOAP Generator", theme=gr.themes.Soft()) as demo:
|
199 |
+
|
200 |
+
gr.Markdown("""
|
201 |
+
# π₯ Medical OCR SOAP Generator
|
202 |
+
### Powered by Gemma 3n - Convert handwritten medical notes to professional SOAP format
|
203 |
+
|
204 |
+
**Instructions:**
|
205 |
+
- **Option 1:** Upload an image of handwritten medical notes
|
206 |
+
- **Option 2:** Enter medical text directly
|
207 |
+
- The system will generate a properly formatted SOAP note
|
208 |
+
|
209 |
+
β οΈ **Note:** First generation may take ~60-90 seconds as the model loads
|
210 |
+
""")
|
211 |
+
|
212 |
+
with gr.Row():
|
213 |
+
with gr.Column():
|
214 |
+
image_input = gr.Image(
|
215 |
+
type="pil",
|
216 |
+
label="π· Upload Medical Image",
|
217 |
+
height=300
|
218 |
+
)
|
219 |
+
|
220 |
+
text_input = gr.Textbox(
|
221 |
+
label="π Or Enter Medical Text",
|
222 |
+
placeholder=sample_text,
|
223 |
+
lines=8,
|
224 |
+
max_lines=15
|
225 |
+
)
|
226 |
+
|
227 |
+
submit_btn = gr.Button(
|
228 |
+
"Generate SOAP Note",
|
229 |
+
variant="primary",
|
230 |
+
size="lg"
|
231 |
+
)
|
232 |
+
|
233 |
+
with gr.Column():
|
234 |
+
extracted_output = gr.Textbox(
|
235 |
+
label="π Extracted/Input Text",
|
236 |
+
lines=6,
|
237 |
+
max_lines=10
|
238 |
+
)
|
239 |
+
|
240 |
+
soap_output = gr.Textbox(
|
241 |
+
label="π₯ Generated SOAP Note",
|
242 |
+
lines=12,
|
243 |
+
max_lines=20
|
244 |
+
)
|
245 |
+
|
246 |
+
# Example section
|
247 |
+
gr.Markdown("### π Quick Test Example")
|
248 |
+
example_btn = gr.Button("Try Sample Medical Text", variant="secondary")
|
249 |
+
|
250 |
+
def load_example():
|
251 |
+
return sample_text, None
|
252 |
+
|
253 |
+
example_btn.click(
|
254 |
+
load_example,
|
255 |
+
outputs=[text_input, image_input]
|
256 |
+
)
|
257 |
+
|
258 |
+
# Process function
|
259 |
+
submit_btn.click(
|
260 |
+
process_medical_input,
|
261 |
+
inputs=[image_input, text_input],
|
262 |
+
outputs=[extracted_output, soap_output]
|
263 |
+
)
|
264 |
+
|
265 |
+
gr.Markdown("""
|
266 |
+
---
|
267 |
+
**About:** This application uses Google's Gemma 3n model for medical text understanding and EasyOCR for handwriting recognition.
|
268 |
+
All processing is done locally for HIPAA compliance.
|
269 |
+
|
270 |
+
**Competition Entry:** Medical AI Innovation Challenge 2024
|
271 |
+
""")
|
272 |
+
|
273 |
+
return demo
|
274 |
+
|
275 |
+
# Initialize the application
|
276 |
+
if __name__ == "__main__":
|
277 |
+
print("π Starting Medical OCR SOAP Generator...")
|
278 |
+
|
279 |
+
# Load model
|
280 |
+
model_loaded = load_model()
|
281 |
+
|
282 |
+
if model_loaded:
|
283 |
+
print("β
All systems ready!")
|
284 |
+
demo = create_demo()
|
285 |
+
demo.launch(
|
286 |
+
share=True,
|
287 |
+
server_name="0.0.0.0",
|
288 |
+
server_port=7860
|
289 |
+
)
|
290 |
+
else:
|
291 |
+
print("β Failed to load model. Creating fallback demo...")
|
292 |
+
|
293 |
+
def fallback_demo():
|
294 |
+
return "β Model loading failed. Please check the logs.", "β Model not available."
|
295 |
+
|
296 |
+
demo = gr.Interface(
|
297 |
+
fn=fallback_demo,
|
298 |
+
inputs=[
|
299 |
+
gr.Image(type="pil", label="Upload Medical Image"),
|
300 |
+
gr.Textbox(label="Enter Medical Text", lines=5)
|
301 |
+
],
|
302 |
+
outputs=[
|
303 |
+
gr.Textbox(label="Status"),
|
304 |
+
gr.Textbox(label="Error Message")
|
305 |
+
],
|
306 |
+
title="β Medical OCR - Model Loading Failed"
|
307 |
+
)
|
308 |
+
|
309 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|