Checkout usage in Hugging Face Spaces :
Fine Tune Model : https://huggingface.co/spaces/aryan14072001/cnn-rag
Fine Tune + DPO : https://huggingface.co/spaces/aryan14072001/LORA-DPO-on-CNN-dataset
Kaggle Notebook for Complete Code with Details :https://www.kaggle.com/code/aryanparab6876868/proper-tune-model
A fine-tuned language model that generates factual news articles from rough notes while minimizing hallucinations.
Built with SFT (CNN Style) + DPO (Anti-Hallucination) training on Llama 3.2 1B.
Standard language models, even after fine-tuning, tend to hallucinate facts when generating news articles:
Input: "β’ Dr. Smith testified β’ About cancer risk"
Typical LLM Output:
"Dr. John Smith, a leading oncologist at Harvard Medical School,
testified before Congress about a 300% increase in cancer risk..."
Problems:
β Invented first name "John"
β Invented affiliation "Harvard Medical School"
β Invented specific statistic "300%"
β Assumed "Congress" from vague "testified"
This project solves this using Direct Preference Optimization (DPO).
- π― Factual Generation: Reduces hallucination rate from 70%+ to 5-10%
- π CNN Writing Style: Professional journalism tone and structure
- π¬ Optional RAG: Web search enrichment for enhanced context
- βοΈ Model Comparison: Side-by-side testing of base vs fine-tuned models
- π Production Ready: Deployable to HuggingFace Spaces
- π Measurable Results: Built-in hallucination detection and scoring
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Phase 1: Supervised Fine-Tuning (SFT) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Data: 13,629 CNN articles β
β Input: Rough notes β Full article pairs β
β Goal: Learn professional CNN writing style β
β Result: Model writes well, but still hallucinates β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Phase 2: Direct Preference Optimization (DPO) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Data: 500 preference pairs (chosen vs rejected) β
β Method: Automated generation + hallucination detection β
β Goal: Prefer factual outputs over hallucinated ones β
β Result: Conservative, accurate article generation β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Final Model: CNN Style + Anti-Hallucination β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Hallucination Rate: 5-10% (was 70%+) β
β CNN Style: Professional, structured β
β Conservative: Acknowledges missing information β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Base Model: Llama 3.2 1B Instruct
- Training: Unsloth (efficient LoRA fine-tuning)
- SFT: 13,629 CNN articles
- DPO: 500 automatically generated preference pairs
- RAG: Groq (LLM inference) + DuckDuckGo (web search)
- Deployment: HuggingFace Spaces + Gradio
Visit our HuggingFace Space: CNN Article Writer
# Clone repository
git clone https://github.com/YOUR_USERNAME/cnn-anti-hallucination.git
cd cnn-anti-hallucination
# Install dependencies
pip install -r requirements.txt
# Run Gradio app
python app.pyfrom unsloth import FastLanguageModel
# Load model
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="aryan14072001/CNN-AntiHallucination-LoRA",
max_seq_length=2048,
load_in_4bit=True,
)
FastLanguageModel.for_inference(model)
# Generate article
messages = [
{"role": "system", "content": "You are a professional journalist."},
{"role": "user", "content": "Write article from: β’ Your rough notes here"}
]
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
outputs = model.generate(inputs, max_new_tokens=300, temperature=0.3)
article = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(article)| Metric | Base Model | After SFT | After DPO |
|---|---|---|---|
| Invented Names | 85% | 45% | 8% |
| Invented Numbers | 90% | 50% | 12% |
| Invented Quotes | 75% | 40% | 5% |
| Invented Organizations | 80% | 35% | 10% |
| Overall Hallucination | 70%+ | 30% | 5-10% |
Input:
β’ Scientists testified about cell phone radiation
β’ Dr. Herberman from Pittsburgh
β’ Study found higher cancer risk
Base Model (70%+ hallucination):
Dr. John Herberman, a leading researcher at the University of Pittsburgh
Medical Center, testified before the Senate Health Committee about his
groundbreaking study showing a 400% increase in brain cancer risk among
heavy cell phone users. The $5 million NIH-funded research...
β Invented: First name, specific committee, 400%, $5M, NIH
Our Model (5-10% hallucination):
Scientists testified about cell phone radiation concerns. Dr. Herberman
from Pittsburgh presented research findings indicating elevated cancer
risk associated with cell phone use. Specific statistics and study
details were not provided in the available information.
β Factual, conservative, acknowledges gaps
# Configuration
Base Model: Llama 3.2 1B Instruct
Training Method: LoRA (r=16, alpha=16)
Dataset: 13,629 CNN articles
Format: Rough notes β Full article
Epochs: 3
Batch Size: 2 (grad accumulation: 4)
Learning Rate: 2e-4
Hardware: 1x Tesla T4 GPU
Training Time: ~6 hours# Configuration
Base Model: SFT checkpoint
Training Method: DPO on same LoRA weights
Dataset: 500 preference pairs
Format: prompt + chosen + rejected
Beta: 0.1 (DPO coefficient)
Batch Size: 1 (grad accumulation: 8)
Learning Rate: 5e-7
Training Time: ~2 hours# Automated pipeline
For each CNN article:
1. Extract rough notes
2. Generate with base model
3. Score both outputs for hallucinations
4. If base_score > truth_score + threshold:
- chosen = ground_truth
- rejected = base_output
- Save as DPO pairHallucination Detection:
- Named entities (people, orgs)
- Numbers and statistics
- Direct quotes
- Locations and dates
- Cross-reference with source material
# Required for RAG
export GROQ_API_KEY="your_groq_api_key"
# Optional: HuggingFace token for private models
export HF_TOKEN="your_huggingface_token"# Available models
MODELS = {
"lora": "aryan14072001/CNN-AntiHallucination-LoRA", # 50 MB
"merged": "aryan14072001/CNN-AntiHallucination-Merged", # 2.5 GB
"base": "unsloth/Llama-3.2-1B-Instruct" # Baseline
}Optionally enhance articles with web search:
# Enable RAG in Gradio interface
use_rag = True
# Or programmatically
from src.inference.rag_enrichment import enrich_notes
enriched = enrich_notes(rough_notes)
article = generate_article(rough_notes, enriched_context=enriched)RAG Pipeline:
- Extract entities from rough notes (Groq LLM)
- Search web for each entity (DuckDuckGo)
- Verify facts against rough notes (Groq LLM)
- Generate article with enriched context (Fine-tuned model)
Input:
β’ Mayor announced education initiative
β’ $50 million budget
β’ Starts September
β’ Focus on STEM
Output:
The mayor announced a new education initiative with a $50 million budget
set to begin in September. The program will focus on STEM education.
Additional details about implementation and target schools were not
provided in the announcement.
Input (sparse):
β’ Company announces AI chip
β’ CEO says fastest ever
β’ Available next year
Output (with RAG enrichment):
A technology company announced a new artificial intelligence chip that
its CEO characterized as the fastest ever developed. The chip is
scheduled for release next year. According to industry sources, the
announcement follows recent advances in semiconductor manufacturing
that have enabled significant performance improvements in AI processing.
Input:
β’ Meeting held
β’ Important topics discussed
Output (appropriately conservative):
A meeting was held where important topics were discussed. Specific
details about the participants, location, or topics covered were not
provided.
# 1. Create Space on HuggingFace
# 2. Upload files
git add app.py requirements.txt
git commit -m "Deploy to Spaces"
git push
# 3. Add secrets in Space settings
# GROQ_API_KEY = your_key# Build image
docker build -t cnn-article-writer .
# Run container
docker run -p 7860:7860 \
-e GROQ_API_KEY=your_key \
cnn-article-writer# Run FastAPI server
uvicorn api:app --host 0.0.0.0 --port 8000
# Request
import requests
response = requests.post("http://localhost:8000/generate", json={
"rough_notes": "Your notes here",
"use_rag": false,
"temperature": 0.3
})Contributions welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Additional news sources (BBC, Reuters, etc.)
- Multilingual support
- Better hallucination detection metrics
- Model quantization for faster inference
- More DPO training data
- UI/UX improvements
This project is licensed under the MIT License - see the LICENSE file for details.
- Unsloth - Efficient LoRA training framework
- HuggingFace - Model hosting and Transformers library
- Meta AI - Llama 3.2 base model
- Groq - Fast LLM inference for RAG
- CNN - Training data source
- DPO Paper - "Direct Preference Optimization: Your Language Model is Secretly a Reward Model"
If you find this project useful, please consider starring it on GitHub!
Built with β€οΈ by the open-source community