Views
No views yet

1from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2import torch
3
4# Configure 4-bit quantization
5bnb_config = BitsAndBytesConfig(
6 load_in_4bit=True,
7 bnb_4bit_quant_type="nf4",
8 bnb_4bit_compute_dtype=torch.bfloat16,
9 bnb_4bit_use_double_quant=True
10)
11
12# Load tokenizer and model
13tokenizer = AutoTokenizer.from_pretrained("alfaxadeyembe/gemma2-27b-swahili-it")
14model = AutoModelForCausalLM.from_pretrained(
15 "alfaxadeyembe/gemma2-27b-swahili-it",
16 quantization_config=bnb_config,
17 device_map="auto",
18 torch_dtype=torch.bfloat16
19)
20
21# Always set to eval mode for inference
22model.eval()
23
24# Example usage
25prompt = "Eleza dhana ya uchumi wa kidijitali na umuhimu wake katika ulimwengu wa leo."
26inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
27
28with torch.no_grad():
29 outputs = model.generate(
30 **inputs,
31 max_new_tokens=500,
32 do_sample=True,
33 temperature=0.7,
34 top_p=0.95
35 )
36
37response = tokenizer.decode(outputs[0], skip_special_tokens=True)
38print(response)1@misc{gemma2-27b-swahili-it,
2 author = {Alfaxad Eyembe},
3 title = {Gemma2-27B-Swahili-IT: Swahili Variation of Gemma2-27b-it Model},
4 year = {2025},
5 publisher = {Hugging Face},
6 journal = {Hugging Face Model Hub},
7}