create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
# Load model and tokenizer
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("aaditya/Llama3-OpenBioLLM-8B")
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
7 |
+
"aaditya/Llama3-OpenBioLLM-8B",
|
8 |
+
torch_dtype=torch.bfloat16, # Use bfloat16 for better memory efficiency
|
9 |
+
device_map="auto" # Automatically handle device placement
|
10 |
+
)
|
11 |
+
|
12 |
+
# Define the system prompt
|
13 |
+
SYSTEM_PROMPT = """You are an advanced medical documentation assistant analyzing clinical documentation. For each case, provide:
|
14 |
+
Documentation Quality Score (1-10):
|
15 |
+
- Assess completeness, clarity, and adherence to clinical standards
|
16 |
+
Diagnostic Confidence (0-100%):
|
17 |
+
- Evaluate certainty of current diagnosis
|
18 |
+
- Consider supporting evidence
|
19 |
+
Key Findings:
|
20 |
+
- List significant clinical observations
|
21 |
+
- Note critical vital signs
|
22 |
+
- Highlight abnormal results
|
23 |
+
Missing Information:
|
24 |
+
- Identify crucial missing data
|
25 |
+
- Note incomplete documentation areas
|
26 |
+
Recommended Actions:
|
27 |
+
- Suggest immediate clinical steps
|
28 |
+
- Propose management changes
|
29 |
+
Additional Tests:
|
30 |
+
- Recommend relevant diagnostics
|
31 |
+
- Suggest appropriate imaging
|
32 |
+
- Propose lab work
|
33 |
+
Safety Concerns:
|
34 |
+
- Flag potential drug interactions
|
35 |
+
- Highlight clinical red flags
|
36 |
+
- Note urgent safety issues"""
|
37 |
+
|
38 |
+
def analyze_medical_doc(medical_doc):
|
39 |
+
# Prepare the prompt
|
40 |
+
prompt = f"{SYSTEM_PROMPT}\n\nPlease analyze this medical documentation:\n\n{medical_doc}\n\nAnalysis:"
|
41 |
+
|
42 |
+
# Tokenize input
|
43 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048)
|
44 |
+
inputs = inputs.to(model.device) # Move to same device as model
|
45 |
+
|
46 |
+
# Generate response
|
47 |
+
outputs = model.generate(
|
48 |
+
**inputs,
|
49 |
+
max_new_tokens=512,
|
50 |
+
temperature=0.7,
|
51 |
+
top_p=0.9,
|
52 |
+
do_sample=True,
|
53 |
+
pad_token_id=tokenizer.eos_token_id
|
54 |
+
)
|
55 |
+
|
56 |
+
# Decode and clean up the response
|
57 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
58 |
+
|
59 |
+
# Extract only the generated part (remove the input prompt)
|
60 |
+
response = response[len(prompt):]
|
61 |
+
|
62 |
+
return response.strip()
|
63 |
+
|
64 |
+
# Example usage
|
65 |
+
sample_doc = """
|
66 |
+
Patient: 45-year-old male
|
67 |
+
Chief Complaint: Chest pain and shortness of breath
|
68 |
+
Vitals:
|
69 |
+
- BP: 145/90
|
70 |
+
- HR: 98
|
71 |
+
- RR: 20
|
72 |
+
- O2 Sat: 95% on RA
|
73 |
+
|
74 |
+
History:
|
75 |
+
- Onset: 2 hours ago
|
76 |
+
- Character: Sharp, radiating to left arm
|
77 |
+
- Previous MI: No
|
78 |
+
- HTN: Yes, on lisinopril
|
79 |
+
|
80 |
+
Current Medications:
|
81 |
+
- Lisinopril 10mg daily
|
82 |
+
"""
|
83 |
+
|
84 |
+
# Get the analysis
|
85 |
+
analysis = analyze_medical_doc(sample_doc)
|
86 |
+
print(analysis)
|