tmnam20/ViMedAQA
Viewer • Updated • 88.6k • 124 • 12
How to use danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("unsloth/llama-3.2-3b-instruct-unsloth-bnb-4bit")
model = PeftModel.from_pretrained(base_model, "danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA")How to use danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA with Unsloth Studio:
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA to start chatting
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA to start chatting
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA to start chatting
pip install unsloth
from unsloth import FastModel
model, tokenizer = FastModel.from_pretrained(
model_name="danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA",
max_seq_length=2048,
)This model is a fine-tuned version of unsloth/llama-3.2-3b-instruct-unsloth-bnb-4bit. It has been trained using TRL.
This model was trained with SFT.
import os
from huggingface_hub import login
# Set the Hugging Face API token
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "<your_huggingface_token>"
# # Initialize API
login(os.environ.get("HUGGINGFACEHUB_API_TOKEN"))
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from peft import PeftModel
device = "cuda" if torch.cuda.is_available() else "cpu"
# Define model and LoRA adapter paths
base_model_name = "meta-llama/Llama-3.2-3B-Instruct"
lora_adapter_name = "danhtran2mind/Llama-3.2-3B-Instruct-Vi-Medical-LoRA"
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
# Load base model with optimized settings
model = AutoModelForCausalLM.from_pretrained(
base_model_name,
torch_dtype=torch.float16, # Use FP16 for efficiency
device_map=device,
trust_remote_code=True
)
# Apply LoRA adapter
model = PeftModel.from_pretrained(model, lora_adapter_name)
# Set model to evaluation mode
model.eval()
instruction = '''Bạn là một trợ lý hữu ích được giao nhiệm vụ trích xuất các đoạn văn trả lời câu hỏi của người dùng từ một ngữ cảnh cho trước. Xuất ra các đoạn văn chính xác từng từ một trả lời câu hỏi của người dùng. Không xuất ra bất kỳ văn bản nào khác ngoài các đoạn văn trong ngữ cảnh. Xuất ra lượng tối thiểu để trả lời câu hỏi, ví dụ chỉ 2-3 từ từ đoạn văn. Nếu không thể tìm thấy câu trả lời trong ngữ cảnh, xuất ra 'Ngữ cảnh không cung cấp câu trả lời...'
'''
question = "Tôi bị viêm loét dạ dày, tôi nên đến chuyên khoa nào để thăm khám?"
# Set random seed for reproducibility
seed = 42
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# Create conversation messages
messages = [
{"role": "system", "content": instruction},
{"role": "user", "content": question},
]
text = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=False # Ensure the output is a string
)
# Tokenize the input and move to device
inputs = tokenizer(text, return_tensors="pt").to(device)
# Generate response with TextStreamer
from transformers import TextStreamer
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
_ = model.generate(
**inputs,
max_new_tokens=2048,
temperature=0.7,
top_p=0.95,
top_k=64,
streamer=streamer
)
Bạn nên đến chuyên khoa tiêu hóa để thăm khám.
Cite TRL as:
@misc{vonwerra2022trl,
title = {{TRL: Transformer Reinforcement Learning}},
author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
year = 2020,
journal = {GitHub repository},
publisher = {GitHub},
howpublished = {\url{https://github.com/huggingface/trl}}
}
Base model
meta-llama/Llama-3.2-3B-Instruct