Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,424 +1,117 @@
|
|
| 1 |
-
|
| 2 |
-
"""
|
| 3 |
-
Flask RAG MCQ generator (subject-filtered, no-hallucination).
|
| 4 |
-
Assumptions:
|
| 5 |
-
- Your fine-tuned model weights + tokenizer are available in a HF repo (we try to load from 'Redfire-1234/pcb_tutor' by default).
|
| 6 |
-
- For best subject filtering, place these files in your HF repo (repo_type='model'):
|
| 7 |
-
- faiss_bio.bin, chunks_bio.pkl
|
| 8 |
-
- faiss_phy.bin, chunks_phy.pkl
|
| 9 |
-
- faiss_chem.bin, chunks_chem.pkl
|
| 10 |
-
OR a combined:
|
| 11 |
-
- faiss_index.bin, all_chunks.pkl
|
| 12 |
-
Optionally add subject_ranges.json describing index ranges for each subject when using combined index:
|
| 13 |
-
{ "biology": [0, 345], "physics": [346, 780], "chemistry": [781, 1200] }
|
| 14 |
-
- If subject-specific files are missing, the app falls back to combined index and (if available) subject_ranges.json.
|
| 15 |
-
- The model repo name is attempted as 'Redfire-1234/pcb_tutor'. If not available, it will fall back to 'Qwen/Qwen2.5-1.5B-Instruct' (replace as needed).
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
from flask import Flask, request, jsonify, render_template_string
|
| 19 |
-
from huggingface_hub import hf_hub_download, RepositoryNotFoundError
|
| 20 |
-
from sentence_transformers import SentenceTransformer
|
| 21 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 22 |
-
import faiss
|
| 23 |
import pickle
|
|
|
|
| 24 |
import torch
|
| 25 |
-
import
|
| 26 |
-
import
|
| 27 |
-
import
|
| 28 |
|
| 29 |
app = Flask(__name__)
|
| 30 |
|
| 31 |
-
# --------------------
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
NUM_MCQS = 5 # number of MCQs to generate per request
|
| 50 |
-
MAX_NEW_TOKENS = 480
|
| 51 |
-
MODEL_MAX_INPUT = 1024
|
| 52 |
-
|
| 53 |
-
# -------------------- Globals (populated at startup) --------------------
|
| 54 |
-
embed_model = None
|
| 55 |
-
tokenizer = None
|
| 56 |
-
gen_model = None
|
| 57 |
-
device = "cpu"
|
| 58 |
-
|
| 59 |
-
# Structures to hold indexes/chunks per subject
|
| 60 |
-
indexes = {} # subject -> faiss index
|
| 61 |
-
chunks = {} # subject -> list of chunk strings
|
| 62 |
-
combined_index = None
|
| 63 |
-
combined_chunks = None
|
| 64 |
-
subject_ranges = None # optional mapping from subject -> [start_idx, end_idx]
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# -------------------- Utility functions --------------------
|
| 68 |
-
def try_hf_download(repo_id, filename, repo_type="model"):
|
| 69 |
-
"""Download file from HF repo; return local path or None on failure."""
|
| 70 |
-
try:
|
| 71 |
-
path = hf_hub_download(repo_id=repo_id, filename=filename, repo_type=repo_type)
|
| 72 |
-
app.logger.info(f"Downloaded {filename} from {repo_id} -> {path}")
|
| 73 |
-
return path
|
| 74 |
-
except Exception as e:
|
| 75 |
-
app.logger.warning(f"Could not download {filename} from {repo_id}: {e}")
|
| 76 |
-
return None
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def safe_load_faiss(path):
|
| 80 |
-
"""Load a faiss index from path, or raise."""
|
| 81 |
-
return faiss.read_index(path)
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def safe_load_chunks(path):
|
| 85 |
-
"""Load chunk list from pickle file."""
|
| 86 |
-
with open(path, "rb") as f:
|
| 87 |
-
data = pickle.load(f)
|
| 88 |
-
return data
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
def load_subject_resources():
|
| 92 |
-
"""Try to load per-subject faiss+chunks; otherwise load combined fallback."""
|
| 93 |
-
global indexes, chunks, combined_index, combined_chunks, subject_ranges
|
| 94 |
-
|
| 95 |
-
# Try subject-specific files
|
| 96 |
-
any_subject_loaded = False
|
| 97 |
-
for subj, names in SUBJECTS.items():
|
| 98 |
-
faiss_path = try_hf_download(HF_REPO, names["faiss"])
|
| 99 |
-
chunks_path = try_hf_download(HF_REPO, names["chunks"])
|
| 100 |
-
if faiss_path and chunks_path:
|
| 101 |
-
try:
|
| 102 |
-
indexes[subj] = safe_load_faiss(faiss_path)
|
| 103 |
-
chunks[subj] = safe_load_chunks(chunks_path)
|
| 104 |
-
any_subject_loaded = True
|
| 105 |
-
app.logger.info(f"Loaded subject-specific resources for {subj}")
|
| 106 |
-
except Exception as e:
|
| 107 |
-
app.logger.warning(f"Failed to load subject resources for {subj}: {e}")
|
| 108 |
-
|
| 109 |
-
# If none per-subject present, try combined fallback
|
| 110 |
-
if not any_subject_loaded:
|
| 111 |
-
app.logger.info("No subject-specific indexes found. Trying combined index fallback.")
|
| 112 |
-
faiss_path = try_hf_download(HF_REPO, COMBINED_FAISS)
|
| 113 |
-
chunks_path = try_hf_download(HF_REPO, COMBINED_CHUNKS)
|
| 114 |
-
ranges_path = try_hf_download(HF_REPO, SUBJECT_RANGES)
|
| 115 |
-
if faiss_path and chunks_path:
|
| 116 |
-
combined_index = safe_load_faiss(faiss_path)
|
| 117 |
-
combined_chunks = safe_load_chunks(chunks_path)
|
| 118 |
-
app.logger.info("Loaded combined FAISS index and chunk list.")
|
| 119 |
-
if ranges_path:
|
| 120 |
-
try:
|
| 121 |
-
with open(ranges_path, "rb") as f:
|
| 122 |
-
subject_ranges = json.load(f)
|
| 123 |
-
app.logger.info("Loaded subject ranges mapping for combined index.")
|
| 124 |
-
except Exception as e:
|
| 125 |
-
app.logger.warning(f"Failed to load subject_ranges.json: {e}")
|
| 126 |
-
else:
|
| 127 |
-
app.logger.error("No workable FAISS index + chunks found. App cannot service queries without index.")
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
def restrict_search_results_to_subject(D, I, subject):
|
| 131 |
-
"""
|
| 132 |
-
When using combined index, restrict the returned indices to those inside the subject range
|
| 133 |
-
by filtering results and replacing by next best candidates if necessary.
|
| 134 |
-
D: distances (1 x k)
|
| 135 |
-
I: indices (1 x k)
|
| 136 |
-
returns filtered list of indices (may be shorter than k)
|
| 137 |
-
"""
|
| 138 |
-
if not subject_ranges or subject not in subject_ranges:
|
| 139 |
-
return I[0].tolist() # no restriction possible
|
| 140 |
-
s_start, s_end = subject_ranges[subject]
|
| 141 |
-
filtered = [idx for idx in I[0].tolist() if s_start <= idx <= s_end]
|
| 142 |
-
return filtered
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def retrieve_chunks_for_subject(query, subject, top_k=TOP_K):
|
| 146 |
-
"""Return the concatenated context for the subject for the given query."""
|
| 147 |
-
q_emb = embed_model.encode([query], convert_to_numpy=True).astype("float32")
|
| 148 |
-
# If subject-specific index present, use it
|
| 149 |
-
if subject in indexes:
|
| 150 |
-
D, I = indexes[subject].search(q_emb, min(top_k, indexes[subject].ntotal))
|
| 151 |
-
idxs = I[0].tolist()
|
| 152 |
-
texts = [chunks[subject][i] for i in idxs]
|
| 153 |
-
return "\n\n".join(texts)
|
| 154 |
-
# Else if combined index present, use it with optional ranges
|
| 155 |
-
elif combined_index is not None and combined_chunks is not None:
|
| 156 |
-
D, I = combined_index.search(q_emb, top_k*3) # request more to allow filtering
|
| 157 |
-
filtered = restrict_search_results_to_subject(D, I, subject)
|
| 158 |
-
if len(filtered) == 0:
|
| 159 |
-
# fallback: return top-k overall but tag that subject had no direct hits
|
| 160 |
-
idxs = I[0][:top_k].tolist()
|
| 161 |
-
else:
|
| 162 |
-
idxs = filtered[:top_k]
|
| 163 |
-
texts = [combined_chunks[i] for i in idxs]
|
| 164 |
-
return "\n\n".join(texts)
|
| 165 |
else:
|
| 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 |
Context:
|
| 193 |
{context}
|
| 194 |
|
| 195 |
-
|
| 196 |
"""
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
flags=re.DOTALL | re.IGNORECASE
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
def generate_strict_mcqs(keyword, context, num_mcq=NUM_MCQS):
|
| 205 |
-
"""
|
| 206 |
-
Build a strict prompt and generate MCQs using the LM.
|
| 207 |
-
Then parse and sanitize output to ensure we only return the required MCQs.
|
| 208 |
-
"""
|
| 209 |
-
prompt = STRICT_PROMPT_TEMPLATE.format(n=num_mcq, topic=keyword, context=context)
|
| 210 |
-
# tokenize & truncate safely if needed
|
| 211 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=MODEL_MAX_INPUT).to(device)
|
| 212 |
-
# generation: deterministic / low-temp to reduce hallucination
|
| 213 |
-
outputs = gen_model.generate(
|
| 214 |
**inputs,
|
| 215 |
-
max_new_tokens=
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
top_p=0.95,
|
| 219 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 220 |
-
pad_token_id=tokenizer.pad_token_id
|
| 221 |
)
|
| 222 |
-
|
| 223 |
-
# try to extract MCQ blocks with regex
|
| 224 |
-
matches = MCQ_CLEANUP_RE.findall(raw)
|
| 225 |
-
if matches and len(matches) >= num_mcq:
|
| 226 |
-
# take first N matches and join with double newline
|
| 227 |
-
selected = matches[:num_mcq]
|
| 228 |
-
return "\n\n".join([m.strip() for m in selected])
|
| 229 |
-
else:
|
| 230 |
-
# Fallback conservative post-processing:
|
| 231 |
-
# Attempt to clean the raw output, extract lines that look like questions/options/answers
|
| 232 |
-
lines = [ln.strip() for ln in raw.splitlines() if ln.strip()]
|
| 233 |
-
# Build MCQs by detecting markers
|
| 234 |
-
mcqs = []
|
| 235 |
-
cur = []
|
| 236 |
-
for ln in lines:
|
| 237 |
-
if ln.lower().startswith("question:"):
|
| 238 |
-
if cur:
|
| 239 |
-
mcqs.append(cur)
|
| 240 |
-
cur = [ln]
|
| 241 |
-
elif re.match(r"^[A-D]\.", ln):
|
| 242 |
-
if cur is not None:
|
| 243 |
-
cur.append(ln)
|
| 244 |
-
elif ln.lower().startswith("answer:"):
|
| 245 |
-
if cur is not None:
|
| 246 |
-
cur.append(ln)
|
| 247 |
-
mcqs.append(cur)
|
| 248 |
-
cur = []
|
| 249 |
-
else:
|
| 250 |
-
# If it's stray text, attach to current question line if present
|
| 251 |
-
if cur:
|
| 252 |
-
cur[-1] += " " + ln
|
| 253 |
-
# convert to string
|
| 254 |
-
cleaned = []
|
| 255 |
-
for block in mcqs[:num_mcq]:
|
| 256 |
-
cleaned.append("\n".join(block))
|
| 257 |
-
if len(cleaned) >= 1:
|
| 258 |
-
return "\n\n".join(cleaned[:num_mcq])
|
| 259 |
-
# As last resort, return raw trimmed
|
| 260 |
-
return raw.strip()
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
# -------------------- Startup: load models and resources --------------------
|
| 264 |
-
def startup():
|
| 265 |
-
global embed_model, tokenizer, gen_model, device
|
| 266 |
-
app.logger.info("Starting up: loading embedder, model, and RAG resources...")
|
| 267 |
-
|
| 268 |
-
# embedder
|
| 269 |
-
embed_model = SentenceTransformer(EMBED_MODEL_NAME)
|
| 270 |
-
|
| 271 |
-
# try to load user fine-tuned model from HF repo; fallback to default model
|
| 272 |
-
model_repo_to_try = HF_REPO
|
| 273 |
-
tk = None
|
| 274 |
-
gm = None
|
| 275 |
-
try:
|
| 276 |
-
tokenizer = AutoTokenizer.from_pretrained(model_repo_to_try, use_fast=True)
|
| 277 |
-
gen_model = AutoModelForCausalLM.from_pretrained(model_repo_to_try)
|
| 278 |
-
app.logger.info(f"Loaded model/tokenizer from {model_repo_to_try}")
|
| 279 |
-
except Exception as e:
|
| 280 |
-
app.logger.warning(f"Could not load model from {model_repo_to_try}: {e}")
|
| 281 |
-
app.logger.info(f"Falling back to {FALLBACK_MODEL}")
|
| 282 |
-
tokenizer = AutoTokenizer.from_pretrained(FALLBACK_MODEL, use_fast=True)
|
| 283 |
-
gen_model = AutoModelForCausalLM.from_pretrained(FALLBACK_MODEL)
|
| 284 |
-
|
| 285 |
-
# device placement
|
| 286 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 287 |
-
gen_model = gen_model.to(device)
|
| 288 |
-
app.logger.info(f"Generation model on device: {device}")
|
| 289 |
-
|
| 290 |
-
# load subject resources (faiss + chunks)
|
| 291 |
-
load_subject_resources()
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
@app.before_first_request
|
| 295 |
-
def _startup():
|
| 296 |
-
startup()
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
# -------------------- HTML (simple subject selector + topic input) --------------------
|
| 300 |
-
HTML_TEMPLATE = """
|
| 301 |
-
<!DOCTYPE html>
|
| 302 |
-
<html>
|
| 303 |
-
<head>
|
| 304 |
-
<title>PCB Tutor — MCQ Generator</title>
|
| 305 |
-
<style>
|
| 306 |
-
body { font-family: Arial, sans-serif; background:#f5f7fb; padding:30px; }
|
| 307 |
-
.card { max-width:900px; margin: 0 auto; background:white; padding:28px; border-radius:12px; box-shadow:0 10px 30px rgba(0,0,0,0.08);}
|
| 308 |
-
h1 { color:#1f2937; text-align:center; margin-bottom:6px; }
|
| 309 |
-
p { text-align:center; color:#6b7280; margin-top:0; margin-bottom:22px; }
|
| 310 |
-
.row { display:flex; gap:12px; margin-bottom:12px; }
|
| 311 |
-
select, textarea, button { font-size:16px; padding:12px; border-radius:8px; border:1px solid #e5e7eb; }
|
| 312 |
-
textarea { width:100%; min-height:120px; resize:vertical; }
|
| 313 |
-
button { background:#4f46e5; color:white; border:none; cursor:pointer; }
|
| 314 |
-
button:disabled { opacity:0.6; cursor:not-allowed; }
|
| 315 |
-
pre { white-space:pre-wrap; background:#f8fafc; padding:14px; border-radius:8px; border-left:4px solid #4f46e5; }
|
| 316 |
-
</style>
|
| 317 |
-
</head>
|
| 318 |
-
<body>
|
| 319 |
-
<div class="card">
|
| 320 |
-
<h1>📘 PCB Tutor — MCQ Generator</h1>
|
| 321 |
-
<p>Select subject, enter a keyword (e.g. "bones", "heart", "blood") and press Generate.</p>
|
| 322 |
-
<div style="margin-bottom:12px;">
|
| 323 |
-
<label for="subject">Subject:</label>
|
| 324 |
-
<select id="subject">
|
| 325 |
-
<option value="biology">Biology</option>
|
| 326 |
-
<option value="physics">Physics</option>
|
| 327 |
-
<option value="chemistry">Chemistry</option>
|
| 328 |
-
</select>
|
| 329 |
-
</div>
|
| 330 |
-
<div style="margin-bottom:12px;">
|
| 331 |
-
<textarea id="topic" placeholder="Enter topic / keyword (like: bones, heart, blood)"></textarea>
|
| 332 |
-
</div>
|
| 333 |
-
<div class="row">
|
| 334 |
-
<button id="generate">🔁 Generate MCQs</button>
|
| 335 |
-
<button id="download" disabled>⬇️ Download</button>
|
| 336 |
-
</div>
|
| 337 |
-
<div id="status" style="margin-top:8px;color:#6b7280;"></div>
|
| 338 |
-
<hr style="margin:18px 0;" />
|
| 339 |
-
<div id="output"><pre id="mcq_out">No MCQs generated yet.</pre></div>
|
| 340 |
-
</div>
|
| 341 |
-
|
| 342 |
-
<script>
|
| 343 |
-
document.getElementById('generate').addEventListener('click', async () => {
|
| 344 |
-
const subj = document.getElementById('subject').value;
|
| 345 |
-
const topic = document.getElementById('topic').value.trim();
|
| 346 |
-
if (!topic) { alert('Enter a topic keyword.'); return; }
|
| 347 |
-
document.getElementById('status').textContent = 'Generating...';
|
| 348 |
-
document.getElementById('generate').disabled = true;
|
| 349 |
-
try {
|
| 350 |
-
const resp = await fetch('/generate_mcqs', {
|
| 351 |
-
method: 'POST',
|
| 352 |
-
headers: {'Content-Type':'application/json'},
|
| 353 |
-
body: JSON.stringify({ subject: subj, topic: topic, num_mcq: 5 })
|
| 354 |
-
});
|
| 355 |
-
const j = await resp.json();
|
| 356 |
-
if (j.error) {
|
| 357 |
-
document.getElementById('mcq_out').textContent = 'Error: ' + j.error;
|
| 358 |
-
document.getElementById('download').disabled = true;
|
| 359 |
-
} else {
|
| 360 |
-
document.getElementById('mcq_out').textContent = j.mcqs;
|
| 361 |
-
document.getElementById('download').disabled = false;
|
| 362 |
-
// attach a blob to download
|
| 363 |
-
const blob = new Blob([j.mcqs], {type: 'text/plain'});
|
| 364 |
-
const url = URL.createObjectURL(blob);
|
| 365 |
-
document.getElementById('download').onclick = () => {
|
| 366 |
-
const a = document.createElement('a');
|
| 367 |
-
a.href = url;
|
| 368 |
-
a.download = `${subj}_${topic.replace(/\\s+/g,'_')}_mcqs.txt`;
|
| 369 |
-
a.click();
|
| 370 |
-
};
|
| 371 |
-
}
|
| 372 |
-
} catch (e) {
|
| 373 |
-
document.getElementById('mcq_out').textContent = 'Error: ' + e.message;
|
| 374 |
-
} finally {
|
| 375 |
-
document.getElementById('status').textContent = '';
|
| 376 |
-
document.getElementById('generate').disabled = false;
|
| 377 |
-
}
|
| 378 |
-
});
|
| 379 |
-
</script>
|
| 380 |
-
</body>
|
| 381 |
-
</html>
|
| 382 |
-
"""
|
| 383 |
-
|
| 384 |
-
# -------------------- Flask endpoints --------------------
|
| 385 |
-
@app.route("/")
|
| 386 |
-
def index():
|
| 387 |
-
return render_template_string(HTML_TEMPLATE)
|
| 388 |
|
| 389 |
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
return jsonify({"error": "Unknown subject"}), 400
|
| 399 |
-
if not topic:
|
| 400 |
-
return jsonify({"error": "Empty topic"}), 400
|
| 401 |
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
# We still call the model but prompt instructs to use only the context.
|
| 407 |
-
return jsonify({"error": f"No context found for subject '{subject}'. Ensure subject indexes are available."}), 400
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
except Exception as e:
|
| 412 |
-
app.logger.exception("Error in /generate_mcqs")
|
| 413 |
-
return jsonify({"error": str(e)}), 500
|
| 414 |
|
|
|
|
|
|
|
| 415 |
|
| 416 |
-
|
| 417 |
-
def health():
|
| 418 |
-
return jsonify({"status": "healthy"})
|
| 419 |
|
| 420 |
|
| 421 |
-
# --------------------
|
|
|
|
|
|
|
| 422 |
if __name__ == "__main__":
|
| 423 |
-
|
| 424 |
-
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pickle
|
| 3 |
+
import faiss
|
| 4 |
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from flask import Flask, request, jsonify
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
+
# ---------------------------------------------------------
|
| 12 |
+
# Load embedding model
|
| 13 |
+
# ---------------------------------------------------------
|
| 14 |
+
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 15 |
+
|
| 16 |
+
# ---------------------------------------------------------
|
| 17 |
+
# Load subject-wise chunks + FAISS indexes
|
| 18 |
+
# ---------------------------------------------------------
|
| 19 |
+
def load_subject(subject):
|
| 20 |
+
if subject == "biology":
|
| 21 |
+
chunks = pickle.load(open("bio_chunks.pkl", "rb"))
|
| 22 |
+
index = faiss.read_index("faiss_bio.bin")
|
| 23 |
+
elif subject == "chemistry":
|
| 24 |
+
chunks = pickle.load(open("chem_chunks.pkl", "rb"))
|
| 25 |
+
index = faiss.read_index("faiss_chem.bin")
|
| 26 |
+
elif subject == "physics":
|
| 27 |
+
chunks = pickle.load(open("phy_chunks.pkl", "rb"))
|
| 28 |
+
index = faiss.read_index("faiss_phy.bin")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
else:
|
| 30 |
+
return None, None
|
| 31 |
+
return chunks, index
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# ---------------------------------------------------------
|
| 35 |
+
# Search inside the selected subject
|
| 36 |
+
# ---------------------------------------------------------
|
| 37 |
+
def rag_search(query, chunks, index, k=5):
|
| 38 |
+
q_emb = embed_model.encode([query]).astype("float32")
|
| 39 |
+
D, I = index.search(q_emb, k)
|
| 40 |
+
results = [chunks[i] for i in I[0]]
|
| 41 |
+
return "\n".join(results)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# ---------------------------------------------------------
|
| 45 |
+
# Load your fine-tuned model
|
| 46 |
+
# ---------------------------------------------------------
|
| 47 |
+
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 48 |
+
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 50 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 51 |
+
model_name,
|
| 52 |
+
torch_dtype=torch.float16,
|
| 53 |
+
device_map="auto"
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
# ---------------------------------------------------------
|
| 57 |
+
# MCQ Generation (strict format, no hallucination)
|
| 58 |
+
# ---------------------------------------------------------
|
| 59 |
+
def generate_mcqs(context, topic):
|
| 60 |
+
prompt = f"""
|
| 61 |
+
You are a Class-12 PCB teacher.
|
| 62 |
+
Generate exactly 5 high-quality MCQs from the topic: "{topic}".
|
| 63 |
+
|
| 64 |
+
Rules:
|
| 65 |
+
- Use ONLY the information from the given context.
|
| 66 |
+
- Do NOT hallucinate outside the context.
|
| 67 |
+
- Each MCQ must include:
|
| 68 |
+
• 1 question
|
| 69 |
+
• 4 options (A, B, C, D)
|
| 70 |
+
• Correct answer at the end exactly like: Answer: B
|
| 71 |
+
- No explanations.
|
| 72 |
+
- No extra notes.
|
| 73 |
|
| 74 |
Context:
|
| 75 |
{context}
|
| 76 |
|
| 77 |
+
Generate now:
|
| 78 |
"""
|
| 79 |
|
| 80 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 81 |
+
outputs = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
**inputs,
|
| 83 |
+
max_new_tokens=400,
|
| 84 |
+
temperature=0.1, # LOW TEMPERATURE = NO HALLUCINATION
|
| 85 |
+
top_p=0.9
|
|
|
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
+
# ---------------------------------------------------------
|
| 91 |
+
# API Route
|
| 92 |
+
# ---------------------------------------------------------
|
| 93 |
+
@app.route("/generate", methods=["POST"])
|
| 94 |
+
def generate():
|
| 95 |
+
data = request.json
|
| 96 |
+
subject = data.get("subject", "").lower()
|
| 97 |
+
topic = data.get("topic", "")
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Load subject files
|
| 100 |
+
chunks, index = load_subject(subject)
|
| 101 |
+
if chunks is None:
|
| 102 |
+
return jsonify({"error": "Invalid subject. Choose biology, chemistry, physics."}), 400
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
# Retrieve context from RAG
|
| 105 |
+
context = rag_search(topic, chunks, index)
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
# Generate MCQs
|
| 108 |
+
mcqs = generate_mcqs(context, topic)
|
| 109 |
|
| 110 |
+
return jsonify({"mcqs": mcqs})
|
|
|
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
+
# ---------------------------------------------------------
|
| 114 |
+
# Run the App
|
| 115 |
+
# ---------------------------------------------------------
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|