Views
No views yet
pip install transformers torch peft accelerate1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3
4# Load model
5model_id = "TesterColab/mistral-finetuned-v3"
6base_model_id = "mistralai/Mistral-7B-Instruct-v0.3"
7
8tokenizer = AutoTokenizer.from_pretrained(model_id)
9base_model = AutoModelForCausalLM.from_pretrained(base_model_id, device_map='auto', torch_dtype=torch.float16)
10model = PeftModel.from_pretrained(base_model, model_id)
11
12# Use the model
13prompt = """<s>[INST] You are a medical doctor. A patient shares their blood pressure reading. Analyze it and respond appropriately - if abnormal, ask relevant questions; if normal, provide encouragement. Never assume information the patient hasn't told you.
14
15Patient: My blood pressure is 145 over 95 [/INST]
16
17"""
18
19inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
20outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.7, top_p=0.9)
21response = tokenizer.decode(outputs[0], skip_special_tokens=True)
22print(response.split('[/INST]')[-1].strip())User: My blood pressure is 115 over 75
Assistant: Your blood pressure reading of 115/75 mmHg is within the normal range - that's great! To maintain this healthy blood pressure, continue with regular exercise, a balanced diet low in sodium, maintain a healthy weight, limit alcohol, and manage stress. Keep monitoring it regularly.User: 145 over 95
Assistant: Your blood pressure reading of 145/95 indicates Stage 2 hypertension. To help you properly, I need to understand your health better. Can you tell me: Do you smoke? How often do you exercise? Do you have any family history of heart disease or stroke? Are you currently taking any medications?User: My BP is 130 over 85 and I smoke
Assistant: Your blood pressure reading of 130/85 shows Stage 1 hypertension, and smoking is a significant risk factor that can worsen this. I need to understand more: How much do you smoke per day? How long have you been smoking? Have you tried to quit before? Also, tell me about your exercise routine and diet.1@model{mistral-bp-doctor,
2 author = {TesterColab},
3 title = {Mistral-7B Blood Pressure Medical Assistant},
4 year = {2024},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/TesterColab/mistral-finetuned-v3}
7}