File size: 6,270 Bytes
0606723
 
 
 
 
 
 
 
4d67ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31969d0
4d67ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31969d0
 
4d67ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
title: Question Answering Classification
emoji: πŸ¦€
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: false
---
# 🧬 NEET Biology Q&A β€” AI Study Assistant

An AI-powered Question Answering system for NEET Biology built with **BERT classification**, **RAG pipeline**, and **NCERT textbook context**.

---

## πŸ—οΈ Architecture

```
User Question
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  BERT Classifier       β”‚ β†’ Predicts NCERT chapter (31 classes, 91.7% F1)
β”‚  (bert-base-uncased)   β”‚ β†’ Extracts 1-3 relevant topics/subtopics
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Supabase pgvector     β”‚ β†’ Retrieves top-k text chunks filtered by chapter
β”‚  (BGE-small-en-v1.5)   β”‚ β†’ Cosine similarity search on NCERT embeddings
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Groq LLM (Llama 3)   β”‚ β†’ Generates answer from retrieved context
β”‚  RAG Pipeline          β”‚ β†’ Justifies with step-by-step reasoning
β”‚                        β”‚ β†’ Verifies answer accuracy
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
    JSON Response β†’ Frontend
```

---

## πŸ“ Project Structure

```
Question_AndAnswerGeneration/
β”‚
β”œβ”€β”€ Backend/                    # FastAPI REST API
β”‚   β”œβ”€β”€ main.py                 # App entrypoint, CORS, model preloading
β”‚   β”œβ”€β”€ schemas.py              # Pydantic request/response models
β”‚   └── routers/
β”‚       └── qa.py               # POST /api/qa, POST /api/classify
β”‚
β”œβ”€β”€ Frontend/                   # Vanilla HTML/CSS/JS UI
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ styles.css
β”‚   └── app.js
β”‚
β”œβ”€β”€ Classification/             # BERT Chapter Classifier
β”‚   β”œβ”€β”€ config.json             # Training hyperparameters
β”‚   β”œβ”€β”€ dataset.py              # PyTorch Dataset class
β”‚   β”œβ”€β”€ train.py                # Fine-tuning script
β”‚   β”œβ”€β”€ predict.py              # Inference wrapper
β”‚   β”œβ”€β”€ classify_and_extract.py # BERT + topic extraction orchestrator
β”‚   β”œβ”€β”€ checkpoints/
β”‚   β”‚   └── best_model/         # Trained BERT weights
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”œβ”€β”€ generated_questions.json  # 5,673 labeled questions
β”‚   β”‚   └── label_map.json            # chapter_id β†’ chapter_name
β”‚   └── Evaluation_metrics/
β”‚       β”œβ”€β”€ eval_metrics.json
β”‚       └── training_log.json
β”‚
β”œβ”€β”€ rag/                        # RAG Pipeline
β”‚   β”œβ”€β”€ retriever.py            # Supabase vector search
β”‚   β”œβ”€β”€ prompt_builder.py       # LLM prompt templates
β”‚   β”œβ”€β”€ generator.py            # Groq API calls
β”‚   β”œβ”€β”€ confidence.py           # Confidence scoring
β”‚   └── pipeline.py             # Full orchestrator
β”‚
└── pdf_to_embedding/           # Data Ingestion
    β”œβ”€β”€ clean_pdf_to_json.py    # PDF β†’ cleaned JSON chunks
    β”œβ”€β”€ embed_and_upload.py     # JSON β†’ embeddings β†’ Supabase
    └── supabase_schema.sql     # Database schema + RPC function
```

---

## πŸš€ Quick Start

### Prerequisites

- Python 3.10+
- Supabase account (with pgvector enabled)
- Groq API key

### 1. Clone & Setup

```bash
git clone https://github.com/PranilBankar/Question_AndAnswerGeneration.git
cd Question_AndAnswerGeneration
```

### 2. Create Virtual Environment

```bash
python -m venv .venv

# Windows
.venv\Scripts\Activate.ps1

# macOS/Linux
source .venv/bin/activate
```

### 3. Install Dependencies

```bash
pip install torch transformers sentence-transformers scikit-learn
pip install fastapi uvicorn
pip install supabase python-dotenv
pip install groq numpy
```

### 4. Environment Variables

Create `pdf_to_embedding/.env`:

```env
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_KEY=your-anon-key
GROQ_API_KEY=your-groq-api-key
EMBEDDING_MODEL=BAAI/bge-small-en-v1.5
```

---

## ▢️ Running the Project

### Start the Backend (Terminal 1)

```bash
cd Question_AndAnswerGeneration
.venv\Scripts\Activate.ps1
uvicorn Backend.main:app --reload --port 8000
```

Wait for: `βœ… All models loaded β€” server ready!`

### Start the Frontend (Terminal 2)

```bash
cd Question_AndAnswerGeneration
python -m http.server 5500 --directory Frontend
```

### Open in Browser

```
Frontend:  http://localhost:5500
Swagger:   http://localhost:8000/docs
Health:    http://localhost:8000/health
```

---

## πŸ”Œ API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| `POST` | `/api/qa` | Full pipeline: classify β†’ retrieve β†’ answer β†’ verify |
| `POST` | `/api/classify` | BERT classification + topic extraction only |
| `GET`  | `/health` | Health check (server + model status) |
| `GET`  | `/docs` | Swagger UI (auto-generated) |

### Example: POST /api/qa

```bash
curl -X POST http://localhost:8000/api/qa \
  -H "Content-Type: application/json" \
  -d '{"question": "What is the role of cofactors in enzyme activity?"}'
```

### Example: POST /api/classify

```bash
curl -X POST http://localhost:8000/api/classify \
  -H "Content-Type: application/json" \
  -d '{"question": "What is the role of cofactors in enzyme activity?"}'
```

---

## 🧠 Tech Stack

| Component | Technology |
|-----------|------------|
| Classifier | BERT (bert-base-uncased), PyTorch, HuggingFace Transformers |
| Embeddings | BGE-small-en-v1.5 (384-dim), Sentence Transformers |
| Vector DB | Supabase PostgreSQL + pgvector (HNSW index) |
| LLM | Groq API (Llama 3) |
| Backend | FastAPI + Uvicorn |
| Frontend | Vanilla HTML/CSS/JS |
| Data Source | NCERT Biology Class 11 & 12 (31 chapters) |

---

## πŸ“Š Model Performance

| Metric | Score |
|--------|-------|
| Accuracy | 91.19% |
| F1 (macro) | 91.68% |
| Classes | 31 chapters |
| Training Data | 5,673 questions |
| Training Time | 8 min 30 sec (CPU) |

---

## πŸ“ License

This project is for educational purposes.