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"
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.| Step | Training Loss | Validation Loss |
|---|---|---|
| 100 | 1.643300 | 1.649192 |
| 200 | 1.546300 | 1.576022 |
| 300 | 1.580200 | 1.552545 |
| 400 | 1.575900 | 1.538777 |
| 500 | 1.499500 | 1.529112 |
| 600 | 1.400600 | 1.516559 |
| 700 | 1.524000 | 1.513925 |
| 800 | 1.437100 | 1.507401 |
| 900 | 1.547300 | 1.504273 |
| 1000 | 1.441300 | 1.502129 |
| 1100 | 1.452500 | 1.499649 |
| 1200 | 1.466400 | 1.495797 |
| 1300 | 1.407500 | 1.494715 |
| 1400 | 1.511400 | 1.493275 |
| 1500 | 1.489600 | 1.495470 |
| 1600 | 1.384400 | 1.492817 |
| 1700 | 1.534900 | 1.490099 |
| 1800 | 1.469300 | 1.490490 |
| 1900 | 1.407500 | 1.488490 |
| 2000 | 1.512300 | 1.487388 |
| 2100 | 1.438900 | 1.490232 |
| 2200 | 1.434700 | 1.490498 |
| 2300 | 1.421200 | 1.489342 |
| 2400 | 1.418200 | 1.487220 |
| 2500 | 1.441200 | 1.487831 |
| 2600 | 1.453900 | 1.488960 |
| Models | Bert Score | TinyMMLU | TinyTrufulQA |
|---|---|---|---|
| Base model | 0.88868 | 0.6837 | 0.49745 |
| Fine tuned Model | 0.90726 | 0.67751 | 0.43555 |
@Model{
Ihenacho2026phi4_african_history_lora,
author = {Daniel Ihenacho},
title = {phi4_african_history_lora},
year = {2026},
publisher = {Hugging Face Models},
url = {https://huggingface.co/DannyAI/phi4_african_history_lora},
urldate = {2026-01-27},
}