Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Load model and tokenizer
5model_name = "Manal0809/Mistral_instructive_full"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
8
9# Example input
10patient_note = """HPI: 35 yo F with heavy uterine bleeding. Last normal period was 6 month ago.
11LMP was 2 months ago. No clots.
12Changes tampon every few hours, previously 4/day. Menarche at 12.
13Attempted using OCPs for menstrual regulation previously but unsuccessful.
14Two adolescent children (ages unknown) at home.
15Last PAP 6 months ago was normal, never abnormal.
16Gained 10-15 lbs over the past few months, eating out more though.
17Hyperpigmented spots on hands and LT neck that she noticed 1-2 years ago.
18SH: state social worker; no smoking or drug use; beer or two on weekends;
19sexually active with boyfriend of 14 months, uses condoms at first but no longer uses them."""
20
21features_to_extract = ["35-year", "Female", "heavy-periods", "symptoms-for-6-months",
22 "Weight-Gain", "Last-menstrual-period-2-months-ago",
23 "Fatigue", "Unprotected-Sex", "Infertility"]
24
25# Format input as shown in the paper
26input_text = f"""###instruction: Extract medical features from the patient note.
27###patient_history: {patient_note}
28###features: {features_to_extract}
29### Annotation:"""
30
31# Generate output
32inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
33outputs = model.generate(
34 inputs["input_ids"],
35 max_new_tokens=512,
36 temperature=0.2,
37 num_return_sequences=1
38)
39result = tokenizer.decode(outputs[0], skip_special_tokens=True)
40print(result)