Spaces:
Runtime error
Runtime error
Create model_utils.py
Browse files- model_utils.py +31 -0
model_utils.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# Replace this with your fine-tuned MCQ extractor if needed
|
| 4 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 5 |
+
|
| 6 |
+
labels = ["question", "option", "answer", "other"]
|
| 7 |
+
|
| 8 |
+
def classify_chunk(text):
|
| 9 |
+
result = classifier(text, labels)
|
| 10 |
+
return result['labels'][0] # Most likely label
|
| 11 |
+
|
| 12 |
+
def extract_mcqs_with_model(text):
|
| 13 |
+
chunks = [chunk.strip() for chunk in text.split("\n\n") if chunk.strip()]
|
| 14 |
+
mcqs = []
|
| 15 |
+
current = {"question": "", "options": [], "answer": ""}
|
| 16 |
+
|
| 17 |
+
for chunk in chunks:
|
| 18 |
+
label = classify_chunk(chunk)
|
| 19 |
+
if label == "question":
|
| 20 |
+
if current["question"]:
|
| 21 |
+
mcqs.append(current)
|
| 22 |
+
current = {"question": "", "options": [], "answer": ""}
|
| 23 |
+
current["question"] = chunk
|
| 24 |
+
elif label == "option":
|
| 25 |
+
current["options"].append(chunk)
|
| 26 |
+
elif label == "answer":
|
| 27 |
+
current["answer"] = chunk
|
| 28 |
+
|
| 29 |
+
if current["question"]:
|
| 30 |
+
mcqs.append(current)
|
| 31 |
+
return mcqs
|