Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3
4# Load base model
5base_model = AutoModelForCausalLM.from_pretrained(
6 "meta-llama/Llama-3.1-8B-Instruct",
7 torch_dtype=torch.bfloat16,
8 device_map="auto"
9)
10
11# Load tokenizer
12tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
13
14# Load LoRA adapter
15model = PeftModel.from_pretrained(base_model, "YOUR_HF_USERNAME/REPO_NAME")1import json
2
3# Input format
4input_data = {
5 "question_id": 12345,
6 "question": "Quel est le traitement de première intention de l'hypertension artérielle?",
7 "option_a": "Inhibiteurs de l'ECA",
8 "option_b": "Bêta-bloquants",
9 "option_c": "Diurétiques thiazidiques",
10 "option_d": "Antagonistes calciques",
11 "option_e": "Sartans"
12}
13
14# Format prompt
15system_prompt = "You are a helpful medical assistant. Given a multiple-choice question in JSON format, provide the correct answer options and a detailed explanation in JSON format."
16
17messages = [
18 {"role": "system", "content": system_prompt},
19 {"role": "user", "content": json.dumps(input_data, ensure_ascii=False)}
20]
21
22# Generate response
23formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
24inputs = tokenizer(formatted_prompt, return_tensors="pt")
25
26with torch.no_grad():
27 outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.0)
28
29response = tokenizer.decode(outputs[0], skip_special_tokens=True)1{
2 "correct_options": "A, C",
3 "explanation": "Les inhibiteurs de l'ECA et les diurétiques thiazidiques sont recommandés en première intention..."
4}1@misc{llama31-8b-mcq-lora,
2 title={Llama-3.1-8B-Instruct LoRA Adapter for Medical MCQ JSON Generation},
3 author={Your Name},
4 year={2025},
5 publisher={Hugging Face},
6 url={https://huggingface.co/YOUR_USERNAME/REPO_NAME}
7}