Views
No views yet
1@misc{owiti2026caracalafrollama,
2 title = {Caracal AfroLlama int4},
3 author = {Owiti, Theophilus Lincoln},
4 year = {2026},
5 publisher = {Hugging Face},
6 journal = {Hugging Face Repository},
7 howpublished = {\url{[https://huggingface.co/theophilusowiti/Caracal_AfroLlama_int4](https://huggingface.co/theophilusowiti/Caracal_AfroLlama_int4)}},
8 institution = {Carnegie Mellon University Africa},
9 note = {GPU compute provided by OpenToken and Leafcloud}
10}AfroLlama_V1, this variant optimizes performance boundaries across diverse structural contexts while drastically reducing deployment memory constraints and hardware requirements.1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftConfig, PeftModel
3#Configuration
4model_name = "theophilusowiti/Caracal_AfroLlama_int4"
5
6# Get the adapter config to find the base model
7peft_config = PeftConfig.from_pretrained(model_name)
8
9# Load the tokenizer
10tokenizer = AutoTokenizer.from_pretrained(model_name)
11
12# Load the base model with quantization configuration
13base_model = AutoModelForCausalLM.from_pretrained(
14 peft_config.base_model_name_or_path,
15 device_map="auto",
16 trust_remote_code=True,
17 torch_dtype=torch.float16,
18 force_download=True,
19 quantization_config = bnb_config_4bit
20 # quantization_config=bnb_config_8bit, # Uncomment if bnb_config_8bit is defined
21)
22
23#Load and attach the PEFT adapter on top of the base model
24model = PeftModel.from_pretrained(base_model, model_name)
25
26if tokenizer.pad_token is None:
27 tokenizer.pad_token = tokenizer.eos_token
28
29
30
31SHOW_TAGS = True # True: Shows structural SFT tags | False: Clean, raw text only
32
33# Fixed Swahili prompt
34#USER_INPUT = 'Context: Rais wa Jamuhuri wa Kenya kwa sasa hivi ni William Samoei Ruto. Wanzeka katika Afrika Mashariki ambao pia ni marais ni Rais wa nchi ya kigeni Rwanda, ambaye ni Mweshimiwa Paul Kagame, upande wa Uganda ni Yoweri Kaguta Museveni na Tanzania, rais wake ni Mama Samia Suluhu.\nQuestion: Rais wa Kenya ni nani?'
35
36#USER_INPUT = "In this task you are given a premise and two alternatives in Swahili. You must choose the alternative that is more plausibly the cause or effect of the situation described by the premise. The input format is \"premise (1)alternative_1(2)alternative_2\", the output should either be \"1\" or \"2\" based on your judgment. Naam, sikukuwa nafikiri juu ya hilo, lakini nilichanganyikiwa sana, na, hatimaye nikaendelea kuzungumza naye tena. (1)Sijaongea na yeye tena. (2)Ulitokea mambo tukawa tunawasiliana."
37
38
39
40USER_INPUT = f"Context: Kenya na wananchi wa Kenya wamesaidika kwa ukuzaji wa Mpesa. Mpesa imefanya biashara ya wa Kenya kuwa rahisi.
41\nQuestion: Niambie jinsi ambavyo Mpesa imewasadia wakenya?""
42
43prompt = f"<Input>\n{USER_INPUT}\n</Input>\n<Answer>\n"
44
45# Tokenize and generate
46inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
47
48print("\nGenerating response, please wait...")
49
50with torch.no_grad():
51 outputs = model.generate(
52 **inputs,
53 max_new_tokens=120,
54 do_sample=True,
55 temperature=0.1,
56 top_p=0.9,
57 repetition_penalty=1.15,
58 no_repeat_ngram_size=3,
59 pad_token_id=tokenizer.eos_token_id,
60 eos_token_id=tokenizer.eos_token_id,
61 )
62
63
64print("\nCaracal:")
65
66if SHOW_TAGS:
67 # Option A: Show the entire true SFT structure exactly as generated
68 true_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
69 print(true_output.strip())
70else:
71 # Option B: Extract only the text between <Answer> and </Answer>
72 # Slicing out the prompt first
73 raw_generation = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
74
75 # Programmatically clean up any structural tags or artifacts from the final view
76 clean_output = raw_generation.replace("<s>", "").replace("</s>", "").strip()
77 print(clean_output)
78
79<Input>...</Input> / <Answer>...</Answer> instruction format used during SFT, e.g.:Generating response, please wait ...
Caracal:
<Input>
Context: Kenya na wananchi wa Kenya wamesaidika kwa ukuzaji wa Mpesa. Mpesa imefanya biashara ya wa Kenya kuwa rahisi.
Question: Niambie jinsi ambavyo Mpesa imewasadia wakenya?
</Input>
<Answer>
Mpesa inawapa wakazi wa Kenya njia salama, yenye ufanisi na rahisishaji ya kusafirisha pesa .< /Answer>