Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3import torch
4
5# Load base model
6base_model = AutoModelForCausalLM.from_pretrained(
7 "unsloth/granite-4.0-h-micro",
8 torch_dtype=torch.float16,
9 device_map="auto"
10)
11
12# Load LoRA adapters
13model = PeftModel.from_pretrained(base_model, "cernis-intelligence/precis")
14tokenizer = AutoTokenizer.from_pretrained("cernis-intelligence/precis")
15
16# Generate summary
17document = """Your long document here..."""
18
19messages = [
20 {"role": "user", "content": f"Summarize the following document in around 300 words:\n\n{document}"}
21]
22
23inputs = tokenizer.apply_chat_template(
24 messages,
25 tokenize=True,
26 add_generation_prompt=True,
27 return_tensors="pt"
28).to(model.device)
29
30outputs = model.generate(
31 inputs,
32 max_new_tokens=512,
33 temperature=0.3,
34 top_p=0.9,
35 do_sample=True
36)
37
38summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
39print(summary)1from unsloth import FastLanguageModel
2
3model, tokenizer = FastLanguageModel.from_pretrained(
4 model_name="cernis-intelligence/precis",
5 max_seq_length=2048,
6 load_in_4bit=True, # For lower memory usage
7)
8
9FastLanguageModel.for_inference(model)
10
11messages = [
12 {"role": "user", "content": f"Summarize the following document in around 300 words:\n\n{document}"}
13]
14
15inputs = tokenizer.apply_chat_template(
16 messages,
17 tokenize=True,
18 add_generation_prompt=True,
19 return_tensors="pt"
20).to("cuda")
21
22outputs = model.generate(inputs, max_new_tokens=512, temperature=0.3)
23summary = tokenizer.decode(outputs[0], skip_special_tokens=True)1from vllm import LLM, SamplingParams
2from vllm.lora.request import LoRARequest
3
4# Initialize vLLM with base model
5llm = LLM(
6 model="unsloth/granite-4.0-h-micro",
7 enable_lora=True,
8 max_lora_rank=32,
9 gpu_memory_utilization=0.9
10)
11
12# Create LoRA request
13lora_request = LoRARequest(
14 "precis-granite",
15 1,
16 "cernis-intelligence/precis"
17)
18
19# Sampling parameters
20sampling_params = SamplingParams(
21 temperature=0.3,
22 top_p=0.9,
23 max_tokens=512
24)
25
26# Generate
27prompts = ["Summarize the following document in around 300 words:\n\n" + document]
28outputs = llm.generate(prompts, sampling_params, lora_request=lora_request)
29
30print(outputs[0].outputs[0].text)Copyright 2025
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0