Views
No views yet
1from transformers import pipeline
2from transformers import (
3 AutoTokenizer,
4 AutoModelForCausalLM)
5from peft import PeftModel
6
7
8model_id = "microsoft/Phi-4-mini-instruct"
9
10tokeniser = AutoTokenizer.from_pretrained(model_id)
11
12# load base model
13model = AutoModelForCausalLM.from_pretrained(
14 model_id,
15 device_map = "auto",
16 torch_dtype = torch.bfloat16,
17 trust_remote_code = False
18)
19
20# Load the fine-tuned LoRA model
21lora_id = "DannyAI/phi4_african_history_lora_ds2"
22lora_model = PeftModel.from_pretrained(
23 model,lora_id
24)
25
26generator = pipeline(
27 "text-generation",
28 model=lora_model,
29 tokenizer=tokeniser,
30)
31question = "What is the significance of African feminist scholarly activism in contemporary resistance movements?"
32def generate_answer(question)->str:
33 """Generates an answer for the given question using the fine-tuned LoRA model.
34 """
35 messages = [
36 {"role": "system", "content": "You are a helpful AI assistant specialised in African history which gives concise answers to questions asked."},
37 {"role": "user", "content": question}
38 ]
39
40 output = generator(
41 messages,
42 max_new_tokens=2048,
43 temperature=0.1,
44 do_sample=False,
45 return_full_text=False
46 )
47 return output[0]['generated_text'].strip()# Example output
African feminist scholarly activism is significant in contemporary resistance movements as it provides a critical framework for understanding and addressing the specific challenges faced by African women in the context of global capitalism, neocolonialism, and patriarchal structures.| Training Loss | Epoch | Step | Validation Loss |
|---|---|---|---|
| 1.6515 | 0.3784 | 100 | 1.6736 |
| 1.5844 | 0.7569 | 200 | 1.6175 |
| 1.6068 | 1.1325 | 300 | 1.5855 |
| 1.6075 | 1.5109 | 400 | 1.5679 |
| 1.5188 | 1.8893 | 500 | 1.5525 |
| 1.4248 | 2.2649 | 600 | 1.5423 |
| 1.5465 | 2.6433 | 700 | 1.5363 |
| 1.454 | 3.0189 | 800 | 1.5331 |
| 1.5759 | 3.3974 | 900 | 1.5275 |
| 1.4626 | 3.7758 | 1000 | 1.5268 |
| 1.4861 | 4.1514 | 1100 | 1.5230 |
| 1.4863 | 4.5298 | 1200 | 1.5232 |
| 1.4312 | 4.9082 | 1300 | 1.5185 |
| 1.5311 | 5.2838 | 1400 | 1.5193 |
| 1.5135 | 5.6623 | 1500 | 1.5179 |
| 1.4092 | 6.0378 | 1600 | 1.5144 |
| 1.5621 | 6.4163 | 1700 | 1.5145 |
| 1.485 | 6.7947 | 1800 | 1.5147 |
| 1.4301 | 7.1703 | 1900 | 1.5109 |
| 1.5346 | 7.5487 | 2000 | 1.5156 |
| 1.4597 | 7.9272 | 2100 | 1.5124 |
| 1.4548 | 8.3027 | 2200 | 1.5118 |
| 1.4485 | 8.6812 | 2300 | 1.5108 |
| 1.4466 | 9.0568 | 2400 | 1.5116 |
| 1.4672 | 9.4352 | 2500 | 1.5132 |
| 1.4881 | 9.8136 | 2600 | 1.5099 |
1{
2 "fp16": { "enabled": false },
3 "bf16": { "enabled": true },
4 "zero_optimization": {
5 "stage": 2,
6 "offload_optimizer": {
7 "device": "cpu",
8 "pin_memory": true
9 },
10 "overlap_comm": true,
11 "contiguous_gradients": true,
12 "reduce_bucket_size": "auto"
13 },
14 "gradient_accumulation_steps": "auto",
15 "gradient_clipping": "auto",
16 "train_batch_size": "auto",
17 "train_micro_batch_size_per_gpu": "auto"
18}| Models | Bert Score | TinyMMLU | TinyTrufulQA |
|---|---|---|---|
| Base model | 0.88868 | 0.6837 | 0.49745 |
| Fine tuned Model | 0.90726 | 0.67788 | 0.43822 |
@Model{
Ihenacho2026phi4_african_history_lora_ds2,
author = {Daniel Ihenacho},
title = {phi4_african_history_lora_ds2},
year = {2026},
publisher = {Hugging Face Models},
url = {https://huggingface.co/DannyAI/phi4_african_history_lora_ds2},
urldate = {2026-01-27},
}