Views
No views yet
!pip install transformers peft accelerate torch bitsandbytes1from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
2import torch
3
4# Clear CUDA memory before using the model
5torch.cuda.empty_cache()
6
7device = "cuda" if torch.cuda.is_available() else "cpu"
8
9model_name = "syubraj/MedicalChat-Phi-3.5-mini-instruct"
10
11try:
12 model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code = True)
13 tokenizer = AutoTokenizer.from_pretrained(model_name)
14 print("Model and Tokenizer loaded successfully.")
15except Exception as e:
16 raise ValueError(f"Error loading Model and Tokenizer: {e}")
17
18
19def generate_response(user_query: str, system_message: str = None, max_length: int = 1024) -> str:
20 """
21 Generates a response based on the given user query.
22
23 :param user_query: The user's input message.
24 :param system_message: Custom system instruction (optional, defaults to medical assistant).
25 :param max_length: Max tokens to generate.
26 :return: Generated assistant response.
27 """
28 if not user_query.strip():
29 return "Error: User query cannot be empty."
30
31 if system_message is None:
32 system_message = ("You are a trusted AI-powered medical assistant. "
33 "Analyze patient queries carefully and provide accurate, professional, and empathetic responses. "
34 "Prioritize patient safety, adhere to medical best practices, and recommend consulting a healthcare provider when necessary.")
35
36 messages = [
37 {"role": "system", "content": system_message},
38 {'role': "user", "content": user_query}
39 ]
40
41 pipe = pipeline("text-generation",
42 model=model,
43 tokenizer=tokenizer)
44
45 generation_args = {
46 "max_new_tokens": max_length,
47 "return_full_text": False,
48 "temperature": 0.0,
49 "do_sample": False,
50}
51 output = pipe(messages, **generation_args)
52 return(output[0]['generated_text'])
53
54if __name__ == "__main__":
55 user_input = "Hi Doctor, I have headache."
56 response = generate_response(user_input)
57 print("Assistant Response:", response)| Step | Training Loss | Validation Loss |
|---|---|---|
| 50 | 15.751700 | 1.201298 |
| 100 | 4.741700 | 1.170077 |
| 150 | 4.734700 | 1.158106 |
| 200 | 4.521200 | 1.146289 |
| 250 | 4.638000 | 1.135715 |
| 300 | 3.985500 | 1.138858 |
| 350 | 4.079600 | 1.135848 |
| 400 | 4.299300 | 1.136855 |
| 450 | 4.298400 | 1.127821 |
| 500 | 3.974000 | 1.125197 |
| 550 | 3.747800 | 1.158925 |
| 600 | 3.638500 | 1.157699 |
| 650 | 3.661100 | 1.162100 |
| 700 | 3.460500 | 1.160023 |
| 750 | 3.640400 | 1.159106 |
| Parameter | Value |
|---|---|
output_dir | ./results |
per_device_train_batch_size | 1 |
per_device_eval_batch_size | 1 |
gradient_accumulation_steps | 4 |
logging_steps | 50 |
num_train_epochs | 3 |
save_steps | 50 |
save_total_limit | 2 |
eval_strategy | "steps" |
eval_steps | 50 |
learning_rate | 2e-4 |
weight_decay | 0.01 |
lr_scheduler_type | "cosine" |
warmup_steps | 25 |
fp16 | True (Mixed Precision) |
push_to_hub | False |
report_to | "wandb" (WandB Logging) |
run_name | "phi3.5-finetune-nf4" |