Spaces:
Sleeping
Sleeping
File size: 11,935 Bytes
1048023 ebfd52f 1048023 ebfd52f 1048023 ebfd52f 1048023 ebfd52f 1048023 ebfd52f 1048023 ebfd52f 1048023 ebfd52f 1048023 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
import gradio as gr
import torch
import numpy as np
from PIL import Image
import time
import io
import subprocess
import sys
import cv2
# Install required packages
def install_packages():
packages = [
"transformers",
"accelerate",
"timm",
"easyocr",
"opencv-python"
]
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 preprocess_image_for_ocr(image):
"""Preprocess image for better OCR results using CLAHE"""
try:
if hasattr(image, 'convert'):
image = image.convert('RGB')
img_array = np.array(image)
# Convert to grayscale
if len(img_array.shape) == 3:
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
else:
gray = img_array
# Resize if too small
height, width = gray.shape
if height < 300 or width < 300:
scale = max(300/height, 300/width)
new_height = int(height * scale)
new_width = int(width * scale)
gray = cv2.resize(gray, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
# Enhance image with CLAHE
gray = cv2.medianBlur(gray, 3)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
gray = clahe.apply(gray)
_, gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return gray
except Exception as e:
print(f"β οΈ Image preprocessing failed: {e}")
# Fallback to original image if preprocessing fails
return np.array(image)
def extract_text_from_image(image):
"""Extract text using EasyOCR with CLAHE preprocessing"""
if ocr_reader is None:
return "β OCR not available"
try:
# Apply CLAHE preprocessing for better OCR
processed_img = preprocess_image_for_ocr(image)
results = ocr_reader.readtext(processed_img, 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.HTML("""
<h1>π₯ Medical OCR SOAP Generator - LIVE DEMO</h1>
<h2>π― For Competition Judges - Quick 2-Minute Demo:</h2>
<div style="background-color: #e6f3ff; padding: 15px; border-radius: 10px; margin: 10px 0;">
<h3>π SAMPLE IMAGE PROVIDED:</h3>
<p><strong>π Download "docs-note-to-upload.jpg" from the Files tab above, then upload it below</strong></p>
<p><strong>OR</strong> click "Try Sample Medical Text" button for instant text demo</p>
</div>
<h3>Demo Steps:</h3>
<ol>
<li><strong>Upload the sample image</strong> (docs-note-to-upload.jpg from Files tab) <strong>OR</strong> click sample text button</li>
<li><strong>Click "Generate SOAP Note"</strong></li>
<li><strong>Wait ~60-90 seconds</strong> for AI processing (first time only)</li>
<li><strong>See professional SOAP note</strong> generated by Gemma 3n</li>
</ol>
<h3>β
What This Demo Shows:</h3>
<ul>
<li><strong>Real OCR</strong> extraction from handwritten medical notes</li>
<li><strong>AI-powered medical reasoning</strong> with Gemma 3n</li>
<li><strong>Professional SOAP formatting</strong> (Subjective, Objective, Assessment, Plan)</li>
<li><strong>HIPAA-compliant</strong> local processing</li>
</ul>
<p><strong>β οΈ Note:</strong> First generation takes ~60-90 seconds as model loads. Subsequent ones are faster.</p>
<hr>
""")
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) |