Views
No views yet
microsoft/Phi-3.5-mini-instruct model, fine-tuned using QLoRA on medical instruction-following datasets. This is NOT a standalone model—you must load it with the base model.microsoft/Phi-3.5-mini-instruct. Load it with peft:1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5device = "cuda" if torch.cuda.is_available() else "cpu"
6print(device)
7
8# Define base model and your fine-tuned LoRA checkpoint
9base_model_name = "microsoft/Phi-3.5-mini-instruct"
10lora_model_path = "syubraj/Phi-3.5-mini-instruct-MedicalChat-QLoRA"
11
12# Load tokenizer
13tokenizer = AutoTokenizer.from_pretrained(base_model_name)
14
15# Load model with proper 4-bit quantization settings
16bnb_config = BitsAndBytesConfig(
17 load_in_4bit=True,
18 bnb_4bit_compute_dtype=torch.float16,
19 bnb_4bit_use_double_quant=True,
20 bnb_4bit_quant_type="nf4"
21)
22
23base_model = AutoModelForCausalLM.from_pretrained(
24 "microsoft/Phi-3.5-mini-instruct",
25 quantization_config=bnb_config,
26 device_map="auto"
27)
28
29model = PeftModel.from_pretrained(base_model, lora_model_path)
30
31model = model.merge_and_unload()
32model.to(device)
33
34print("Model successfully loaded!")
35
36# Inference function
37def generate_response(user_query, system_message=None, max_length=1024):
38 if system_message is None:
39 system_message = ("You are a trusted AI-powered medical assistant. "
40 "Analyze patient queries carefully and provide accurate, professional, and empathetic responses. "
41 "Prioritize patient safety, adhere to medical best practices, and recommend consulting a healthcare provider when necessary.")
42
43 # Prepare input prompt
44 prompt = f"<|system|> {system_message} <|end|>\n<|user|> {user_query} <|end|>\n<|assistant|>"
45
46 inputs = tokenizer(prompt, return_tensors="pt").to(device)
47 outputs = model.generate(**inputs, max_length=max_length)
48
49 # Decode response
50 response = tokenizer.decode(outputs[0], skip_special_tokens=True)
51 return response.split("<|assistant|>")[-1].strip().split("<|end|>")[0].strip()
52
53if __name__ == "__main__":
54 res = generate_response("Hi, How can someone let go of fever?")
55 print(res)
56bitsandbytes)1@misc{syubraj2024phi3.5medical,
2 title={Phi-3.5 Mini Instruct Medical Chat (LoRA Adapter)},
3 author={syubraj},
4 year={2024},
5 url={https://huggingface.co/syubraj/Phi-3.5-mini-instruct-MedicalChat-adapter}
6}