👨💻 Author
Ritik Rana
NLP Engineer | AI Developer | RAG Engineer
contact : ritikrana056@gmail.com
📘 Question Answering System using BERT & SQuAD
An end-to-end Question Answering (QA) system built using Hugging Face Transformers, trained on the SQuAD dataset, and deployed with a Streamlit web app.
This project demonstrates:
- Fine-tuning BERT for Extractive Question Answering
- Custom preprocessing with offset mapping
- Hugging Face Trainer API (v5 compatible)
- Model saving & loading
- Streamlit deployment
🚀 Project Overview
This project trains a BERT-based model to answer questions from a given paragraph (context).
Example:
Context:
TensorFlow is an open-source machine learning framework developed by Google.
Question:
Who developed TensorFlow?
Answer:
Google
The model predicts:
- Start token index
- End token index
And extracts the correct span from the context.
🧠 Model Architecture
- Base Model:
bert-base-uncased - Task: Extractive Question Answering
- Dataset: SQuAD v1
- Framework: Hugging Face Transformers
- Training API: Trainer
- Deployment: Streamlit
📂 Project Structure
qa_squad_project/
│
├── config.py # Configuration file
├── data_loader.py # Loads SQuAD dataset
├── preprocess.py # Tokenization + offset mapping
├── model.py # Loads QA model
├── train.py # Training script
├── inference.py # Inference script
├── streamlit_app.py # Web application
├── qa_model/ # Saved trained model
└── README.md
Training dataset
Squad dataset
Model initial version train on only
2000 training dataset and 500 validation dataset on 3 epochs
🏋️ Training the Model
Run:
python train.py
Training will:
- Load SQuAD dataset
- Tokenize using offset mapping
- Fine-tune BERT
- Save model in
qa_model/
🧪 Run Inference (CLI)
After training:
python inference.py
This will test the model with a sample question.
🌐 Run Streamlit Web App
streamlit run streamlit_app.py
If using remote/cloud environment:
streamlit run streamlit_app.py --server.address 0.0.0.0 --server.port 8501
Open browser at:
http://localhost:8501
🔍 How It Works
1️⃣ Tokenization
- Question and context are tokenized together.
- Offset mapping maps tokens back to original character positions.
2️⃣ Label Creation
- Convert answer character positions → token indices.
- Save as:
start_positionsend_positions
3️⃣ Training
Model learns to predict:
- Start logits
- End logits
Loss is computed using CrossEntropyLoss.
4️⃣ Inference
Model predicts best start & end token span. Pipeline extracts text from context.
🧪 Example Output
Question: Who developed TensorFlow?
Answer: Google
Confidence: 0.98
💾 Saved Model Files
Inside qa_model/:
| File | Purpose |
|---|---|
| pytorch_model.bin | Trained weights |
| config.json | Model architecture |
| tokenizer.json | Tokenizer rules |
| training_args.bin | Training config |
Load later using:
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
model = AutoModelForQuestionAnswering.from_pretrained("qa_model")
tokenizer = AutoTokenizer.from_pretrained("qa_model")
📈 Future Improvements
- Train on full SQuAD dataset
- Add SQuAD v2 (no-answer support)
- Add answer highlighting in Streamlit
- Add PDF upload support
- Convert to RAG system
- Deploy on Hugging Face Spaces
🖥 Hardware Used
Recommended GPU:
- NVIDIA T4 (16GB)
Works on CPU for small datasets (slower).
license apache-2.0
- Downloads last month
- 9