Views
No views yet
1from transformers import pipeline
2
3question = "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?"
4generator = pipeline("text-generation", model="None", device="cuda")
5output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]
6print(output["generated_text"])1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3from peft import PeftModel
4
5# Define model and LoRA adapter paths
6base_model_name = "unsloth/gemma-3-1b-it"
7lora_adapter_name = "heboya8/Gemma-3-1B-it-Medical-LoRA"
8
9# Load tokenizer
10tokenizer = AutoTokenizer.from_pretrained(base_model_name)
11
12# Load base model with optimized settings
13model = AutoModelForCausalLM.from_pretrained(
14 base_model_name,
15 torch_dtype=torch.float16, # Use FP16 for efficiency
16 device_map="cpu", # Explicitly map to CUDA device
17 trust_remote_code=True
18)
19
20# Apply LoRA adapter
21model = PeftModel.from_pretrained(model, lora_adapter_name)
22
23# Set model to evaluation mode
24model.eval()
25
26# Create text generation pipeline
27generator = pipeline(
28 "text-generation",
29 model=model,
30 tokenizer=tokenizer,
31 torch_dtype=torch.float16,
32 device_map="cuda",
33 max_new_tokens=128, # Limit response length as per original script
34)
35
36# Define the question
37question = ("Khi nghi ngờ bị loét dạ dày tá tràng nên đến khoa nào "
38 "tại bệnh viện để thăm khám?")
39
40# Format input for the pipeline
41input_prompt = [{"role": "user", "content": question}]
42
43# Generate response
44output = generator(input_prompt, return_full_text=False)[0]
45
46# Print the generated text
47print(output["generated_text"])1@misc{vonwerra2022trl,
2 title = {{TRL: Transformer Reinforcement Learning}},
3 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},
4 year = 2020,
5 journal = {GitHub repository},
6 publisher = {GitHub},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}