Views
No views yet
1
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_name = "GetSoloTech/Llama3.2-Medical-Notes-1B"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
7
8
9SYSTEM_PROMPT = """Convert the following medical transcript to a structured medical note.
10
11Use these sections in this order:
12
131. Demographics
14 - Name, Age, Sex, DOB
15
162. Presenting Illness
17 - Bullet point statements of the main problem and duration.
18
193. History of Presenting Illness
20 - Chronological narrative: symptom onset, progression, modifiers, associated factors.
21
224. Past Medical History
23 - List chronic illnesses and past medical diagnoses mentioned in the transcript. Do not include surgeries.
24
255. Surgical History
26 - List prior surgeries with year if known, as mentioned in the transcript.
27
286. Family History
29 - Relevant family history mentioned in the transcript.
30
317. Social History
32 - Occupation, tobacco/alcohol/drug use, exercise, living situation if mentioned in the transcript.
33
348. Allergy History
35 - Drug, food, or environmental allergies and reactions, if mentioned in the transcript.
36
379. Medication History
38 - List medications the patient is already taking. Do not include any new or proposed drugs in this section.
39
4010. Dietary History
41 - If unrelated, write “Not applicable”; otherwise, summarize the diet pattern.
42
4311. Review of Systems
44 - Head-to-toe, alphabetically ordered bullet points; include both positives and pertinent negatives as mentioned in the transcript.
45
4612. Physical Exam Findings
47 - Vital Signs (BP, HR, RR, Temp, SpO₂, HT, WT, BMI) if mentioned in the transcript.
48 - Structured by system: General, HEENT, Cardiovascular, Respiratory, Abdomen, Neurological, Musculoskeletal, Skin, Psychiatric—as mentioned in the transcript.
49
5013. Labs and Imaging
51 - Summarize labs and imaging results.
52
5314. ASSESSMENT
54 - Provide a brief summary of the clinical assessment or diagnosis based on the information in the transcript.
55
5615. PLAN
57 - Outline the proposed management plan, including treatments, medications, follow-up, and patient instructions as discussed.
58
59Please use only the information present in the transcript. If an information is not mentioned or not applicable, state “Not applicable.” Format each section clearly with its heading.
60"""
61
62def generate_structured_note(transcript):
63 message = [
64 {"role": "system", "content": SYSTEM_PROMPT},
65 {"role": "user", "content": f"<START_TRANSCRIPT>\n{transcript}\n<END_TRANSCRIPT>\n"},
66 ]
67
68 inputs = tokenizer.apply_chat_template(
69 message,
70 tokenize=True,
71 add_generation_prompt=True,
72 return_tensors="pt",
73 ).to(model.device)
74
75 outputs = model.generate(
76 input_ids=inputs,
77 max_new_tokens=2048,
78 temperature=0.2,
79 top_p=0.85,
80 min_p=0.1,
81 top_k=20,
82 do_sample=True,
83 eos_token_id=tokenizer.eos_token_id,
84 use_cache=True,
85 )
86
87 input_token_len = len(inputs[0])
88 generated_tokens = outputs[:, input_token_len:]
89 note = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
90 if "<START_NOTES>" in note:
91 note = note.split("<START_NOTES>")[-1].strip()
92 if "<END_NOTES>" in note:
93 note = note.split("<END_NOTES>")[0].strip()
94 return note
95
96# Example usage
97transcript = "Patient is a 45-year-old male presenting with..."
98note = generate_structured_note(transcript)
99print("\n--- Generated Response ---")
100print(note)
101print("---------------------------")