Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,165 +1,424 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template_string
|
|
|
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
-
from transformers import
|
| 4 |
-
from huggingface_hub import hf_hub_download
|
| 5 |
import faiss
|
| 6 |
import pickle
|
| 7 |
import torch
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 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 |
-
q_emb = embed_model.encode([query]).astype("float32")
|
| 44 |
-
D, I = index.search(q_emb, k)
|
| 45 |
-
return "\n".join([all_chunks[i] for i in I[0]])
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# Generate MULTIPLE MCQs from context
|
| 49 |
-
def generate_mcqs(keyword, context, num_mcq=5):
|
| 50 |
-
prompt = f"""
|
| 51 |
-
Generate {num_mcq} high-quality MCQs from the following topic and context.
|
| 52 |
-
Each MCQ must include:
|
| 53 |
-
- 1 question
|
| 54 |
-
- 4 options (A, B, C, D)
|
| 55 |
-
- Mark the correct answer clearly at the end like: "Answer: B"
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
|
| 66 |
-
outputs = model.generate(**inputs, max_new_tokens=500)
|
| 67 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# ------------------ HTML UI ------------------
|
| 71 |
|
|
|
|
| 72 |
HTML_TEMPLATE = """
|
| 73 |
<!DOCTYPE html>
|
| 74 |
<html>
|
| 75 |
<head>
|
| 76 |
-
<title>PCB MCQ Generator</title>
|
| 77 |
<style>
|
| 78 |
-
body { font-family: Arial
|
| 79 |
-
.
|
| 80 |
-
h1 { color:
|
| 81 |
-
p { text-align:
|
| 82 |
-
|
| 83 |
-
button {
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
.loading.show { display: block; }
|
| 89 |
</style>
|
| 90 |
</head>
|
| 91 |
<body>
|
| 92 |
-
<div class="
|
| 93 |
-
<h1>📘 PCB MCQ Generator</h1>
|
| 94 |
-
<p>
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
</div>
|
| 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 |
</body>
|
| 130 |
</html>
|
| 131 |
"""
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
@app.route('/')
|
| 137 |
-
def home():
|
| 138 |
return render_template_string(HTML_TEMPLATE)
|
| 139 |
|
| 140 |
|
| 141 |
-
@app.route(
|
| 142 |
-
def
|
| 143 |
try:
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
except Exception as e:
|
|
|
|
| 156 |
return jsonify({"error": str(e)}), 500
|
| 157 |
|
| 158 |
|
| 159 |
-
@app.route(
|
| 160 |
def health():
|
| 161 |
return jsonify({"status": "healthy"})
|
| 162 |
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 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 json
|
| 26 |
+
import os
|
| 27 |
+
import re
|
| 28 |
|
| 29 |
app = Flask(__name__)
|
| 30 |
|
| 31 |
+
# -------------------- Configuration --------------------
|
| 32 |
+
HF_REPO = "Redfire-1234/pcb_tutor" # change only if your model+files are hosted in another repo
|
| 33 |
+
FALLBACK_MODEL = "Qwen/Qwen2.5-1.5B-Instruct" # fallback if your repo doesn't contain model weights
|
| 34 |
+
|
| 35 |
+
SUBJECTS = {
|
| 36 |
+
"biology": {"faiss": "faiss_bio.bin", "chunks": "chunks_bio.pkl"},
|
| 37 |
+
"physics": {"faiss": "faiss_phy.bin", "chunks": "chunks_phy.pkl"},
|
| 38 |
+
"chemistry": {"faiss": "faiss_chem.bin", "chunks": "chunks_chem.pkl"},
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# Combined fallback names (if subject-specific files are not present)
|
| 42 |
+
COMBINED_FAISS = "faiss_index.bin"
|
| 43 |
+
COMBINED_CHUNKS = "all_chunks.pkl"
|
| 44 |
+
SUBJECT_RANGES = "subject_ranges.json" # optional mapping of index ranges for each subject
|
| 45 |
+
|
| 46 |
+
# RAG & generation hyperparams
|
| 47 |
+
EMBED_MODEL_NAME = "all-MiniLM-L6-v2"
|
| 48 |
+
TOP_K = 6 # retrieval candidates
|
| 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 |
+
# Last resort: no index loaded
|
| 167 |
+
return ""
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
# -------------------- Strict MCQ prompt and post-process --------------------
|
| 171 |
+
STRICT_PROMPT_TEMPLATE = """You are an exam-quality MCQ generator. Generate exactly {n} distinct multiple-choice questions (MCQs)
|
| 172 |
+
from the given Topic and Context.
|
| 173 |
+
|
| 174 |
+
Requirements (STRICT):
|
| 175 |
+
- Output exactly {n} MCQs and nothing else.
|
| 176 |
+
- Each MCQ must be in this exact format (no extra whitespace lines, no numbering aside from the format below):
|
| 177 |
+
Question: <question text>
|
| 178 |
+
A. <option A>
|
| 179 |
+
B. <option B>
|
| 180 |
+
C. <option C>
|
| 181 |
+
D. <option D>
|
| 182 |
+
Answer: <A|B|C|D>
|
| 183 |
+
|
| 184 |
+
- Do NOT include explanations, commentary, or anything other than the MCQs in the format above.
|
| 185 |
+
- Use only facts that can be directly inferred from the Context. If the Context does not support a fact, do not assert it.
|
| 186 |
+
- Do NOT hallucinate. If you cannot form {n} MCQs from the Context, still output {n} MCQs but make them conservative and anchored to text in the Context (use literal phrases from Context as needed).
|
| 187 |
+
- Ensure that for each MCQ, exactly one option is clearly correct and indicated in the Answer line.
|
| 188 |
+
- Do not repeat Answer lines or produce dangling "Answer: B" sequences.
|
| 189 |
+
|
| 190 |
+
Topic: {topic}
|
| 191 |
|
| 192 |
+
Context:
|
| 193 |
+
{context}
|
| 194 |
|
| 195 |
+
Now output the MCQs exactly as specified.
|
| 196 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
MCQ_CLEANUP_RE = re.compile(
|
| 199 |
+
r"(Question:.*?A\..*?B\..*?C\..*?D\..*?Answer:\s*[ABCD])",
|
| 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=MAX_NEW_TOKENS,
|
| 216 |
+
do_sample=False,
|
| 217 |
+
temperature=0.0,
|
| 218 |
+
top_p=0.95,
|
| 219 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 220 |
+
pad_token_id=tokenizer.pad_token_id
|
| 221 |
+
)
|
| 222 |
+
raw = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 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 |
+
@app.route("/generate_mcqs", methods=["POST"])
|
| 391 |
+
def generate_mcqs_endpoint():
|
| 392 |
try:
|
| 393 |
+
payload = request.get_json()
|
| 394 |
+
subject = payload.get("subject", "biology").lower()
|
| 395 |
+
topic = payload.get("topic", "").strip()
|
| 396 |
+
num_mcq = int(payload.get("num_mcq", NUM_MCQS))
|
| 397 |
+
if subject not in SUBJECTS:
|
| 398 |
+
return jsonify({"error": "Unknown subject"}), 400
|
| 399 |
+
if not topic:
|
| 400 |
+
return jsonify({"error": "Empty topic"}), 400
|
| 401 |
+
|
| 402 |
+
# retrieve context from subject-specific store
|
| 403 |
+
context = retrieve_chunks_for_subject(topic, subject, top_k=TOP_K)
|
| 404 |
+
if not context:
|
| 405 |
+
# If no context found, return a clear message (this avoids hallucination)
|
| 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 |
+
mcqs = generate_strict_mcqs(topic, context, num_mcq=num_mcq)
|
| 410 |
+
return jsonify({"subject": subject, "topic": topic, "mcqs": mcqs})
|
| 411 |
except Exception as e:
|
| 412 |
+
app.logger.exception("Error in /generate_mcqs")
|
| 413 |
return jsonify({"error": str(e)}), 500
|
| 414 |
|
| 415 |
|
| 416 |
+
@app.route("/health")
|
| 417 |
def health():
|
| 418 |
return jsonify({"status": "healthy"})
|
| 419 |
|
| 420 |
|
| 421 |
+
# -------------------- Run --------------------
|
| 422 |
+
if __name__ == "__main__":
|
| 423 |
+
# HF Spaces prefer port 7860; local testing works too.
|
| 424 |
+
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|