Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,14 +8,14 @@ from flask import Flask, request, jsonify
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
-
# ------------------------------
|
| 12 |
-
#
|
| 13 |
-
# ------------------------------
|
| 14 |
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 15 |
|
| 16 |
-
# ------------------------------
|
| 17 |
-
#
|
| 18 |
-
# ------------------------------
|
| 19 |
def load_subject(subject):
|
| 20 |
if subject == "biology":
|
| 21 |
chunks = pickle.load(open("bio_chunks.pkl", "rb"))
|
|
@@ -30,32 +30,31 @@ def load_subject(subject):
|
|
| 30 |
return None, None
|
| 31 |
return chunks, index
|
| 32 |
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
#
|
| 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 |
-
#
|
| 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 |
-
|
| 53 |
-
device_map="auto"
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# ------------------------------
|
| 57 |
-
#
|
| 58 |
-
# ------------------------------
|
| 59 |
def generate_mcqs(context, topic):
|
| 60 |
prompt = f"""
|
| 61 |
You are a Class-12 PCB teacher.
|
|
@@ -69,27 +68,24 @@ Rules:
|
|
| 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, #
|
| 85 |
top_p=0.9
|
| 86 |
)
|
| 87 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
#
|
| 91 |
-
#
|
| 92 |
-
# ---------------------------------------------------------
|
| 93 |
@app.route("/generate", methods=["POST"])
|
| 94 |
def generate():
|
| 95 |
data = request.json
|
|
@@ -109,9 +105,8 @@ def generate():
|
|
| 109 |
|
| 110 |
return jsonify({"mcqs": mcqs})
|
| 111 |
|
| 112 |
-
|
| 113 |
-
#
|
| 114 |
-
#
|
| 115 |
-
# ---------------------------------------------------------
|
| 116 |
if __name__ == "__main__":
|
| 117 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 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"))
|
|
|
|
| 30 |
return None, None
|
| 31 |
return chunks, index
|
| 32 |
|
| 33 |
+
# ------------------------------
|
| 34 |
+
# Search inside the selected subject
|
| 35 |
+
# ------------------------------
|
|
|
|
| 36 |
def rag_search(query, chunks, index, k=5):
|
| 37 |
q_emb = embed_model.encode([query]).astype("float32")
|
| 38 |
D, I = index.search(q_emb, k)
|
| 39 |
results = [chunks[i] for i in I[0]]
|
| 40 |
return "\n".join(results)
|
| 41 |
|
| 42 |
+
# ------------------------------
|
| 43 |
+
# Load your fine-tuned model
|
| 44 |
+
# ------------------------------
|
|
|
|
| 45 |
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
|
|
|
| 46 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 47 |
+
|
| 48 |
+
# Load model safely
|
| 49 |
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
model_name,
|
| 51 |
+
dtype=torch.float16, # correct dtype parameter
|
| 52 |
+
device_map="auto" # requires `pip install accelerate`
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# ------------------------------
|
| 56 |
+
# MCQ Generation (strict, no hallucination)
|
| 57 |
+
# ------------------------------
|
| 58 |
def generate_mcqs(context, topic):
|
| 59 |
prompt = f"""
|
| 60 |
You are a Class-12 PCB teacher.
|
|
|
|
| 68 |
• 4 options (A, B, C, D)
|
| 69 |
• Correct answer at the end exactly like: Answer: B
|
| 70 |
- No explanations.
|
|
|
|
| 71 |
|
| 72 |
Context:
|
| 73 |
{context}
|
| 74 |
|
| 75 |
Generate now:
|
| 76 |
"""
|
|
|
|
| 77 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 78 |
outputs = model.generate(
|
| 79 |
**inputs,
|
| 80 |
max_new_tokens=400,
|
| 81 |
+
temperature=0.1, # low temperature = no hallucination
|
| 82 |
top_p=0.9
|
| 83 |
)
|
| 84 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 85 |
|
| 86 |
+
# ------------------------------
|
| 87 |
+
# API Route
|
| 88 |
+
# ------------------------------
|
|
|
|
| 89 |
@app.route("/generate", methods=["POST"])
|
| 90 |
def generate():
|
| 91 |
data = request.json
|
|
|
|
| 105 |
|
| 106 |
return jsonify({"mcqs": mcqs})
|
| 107 |
|
| 108 |
+
# ------------------------------
|
| 109 |
+
# Run the App
|
| 110 |
+
# ------------------------------
|
|
|
|
| 111 |
if __name__ == "__main__":
|
| 112 |
app.run(host="0.0.0.0", port=7860)
|