Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4# Load model and tokenizer
5model_id = "MedLlama-3-Healthcare" # Replace with your Hugging Face model ID
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(
8 model_id,
9 torch_dtype=torch.float16,
10 device_map="auto"
11)
12
13# Set up role context - choose one: "gp", "nurse", "midwife", "obstetrician"
14role = "gp"
15
16# Format prompt with role context
17prompt = f"[INST] I want you to act as a {role}. What can you tell me about diabetes management? [/INST]"
18
19# Tokenize and generate
20inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
21outputs = model.generate(
22 **inputs,
23 max_length=1024,
24 temperature=0.7,
25 top_p=0.9,
26 pad_token_id=tokenizer.eos_token_id
27)
28
29# Decode and print response
30response = tokenizer.decode(outputs[0], skip_special_tokens=True)
31print(response)[INST] I want you to act as a {role}. {your medical question or scenario} [/INST]