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
5device = "cuda" if torch.cuda.is_available() else "cpu"
6
7
8# Define model and LoRA adapter paths
9base_model_name = "google/gemma-3-1b-it"
10lora_adapter_name = "danhtran2mind/Gemma-3-1B-Instruct-Vi-Medical-LoRA"
11
12# Load tokenizer
13tokenizer = AutoTokenizer.from_pretrained(base_model_name)
14
15# Load base model with optimized settings
16model = AutoModelForCausalLM.from_pretrained(
17 base_model_name,
18 torch_dtype=torch.float16, # Use FP16 for efficiency
19 device_map=device,
20 trust_remote_code=True
21)
22
23# Apply LoRA adapter
24model = PeftModel.from_pretrained(model, lora_adapter_name)
25
26# Set model to evaluation mode
27model.eval()
28
29# Define the question
30question = ("Khi nghi ngờ bị loét dạ dày tá tràng nên đến khoa nào "
31 "tại bệnh viện để thăm khám?")
32
33seed = 42
34torch.manual_seed(seed)
35if torch.cuda.is_available():
36 torch.cuda.manual_seed(seed)
37 torch.cuda.manual_seed_all(seed)
38
39# Create text generation pipeline
40generator = pipeline(
41 "text-generation",
42 model=model,
43 tokenizer=tokenizer,
44 torch_dtype=torch.float16,
45 device_map=device,
46 max_new_tokens=2048,
47 num_return_sequences=1,
48 do_sample=True,
49 temperature=0.7,
50 top_p=0.9,
51 top_k=64,
52)
53
54# Format input for the pipeline
55input_prompt = [{"role": "user", "content": question}]
56
57# Generate response
58output = generator(input_prompt, return_full_text=False)[0]
59
60# Print the generated text
61print(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}