Spaces:
Sleeping
Sleeping
Merge pull request #10 from srishtimaggo/patch-2
Browse files- truefalse_quiz.py +15 -46
truefalse_quiz.py
CHANGED
|
@@ -4,7 +4,7 @@ from transformers import pipeline
|
|
| 4 |
from nltk.tokenize import sent_tokenize
|
| 5 |
|
| 6 |
# Download required tokenizer
|
| 7 |
-
nltk.download('
|
| 8 |
|
| 9 |
# Load NLI model
|
| 10 |
nli = pipeline("text-classification", model="facebook/bart-large-mnli")
|
|
@@ -27,7 +27,7 @@ def apply_noise(sentence: str, level: str) -> str:
|
|
| 27 |
elif level == "medium":
|
| 28 |
if "Sun" in sentence:
|
| 29 |
return sentence.replace("Sun", "Moon")
|
| 30 |
-
return sentence.replace("is", "is not") if "is" in sentence else sentence
|
| 31 |
elif level == "hard":
|
| 32 |
if "eight" in sentence:
|
| 33 |
return sentence.replace("eight", "ten")
|
|
@@ -48,51 +48,20 @@ def generate_statements(context, n, difficulty, sentences):
|
|
| 48 |
break
|
| 49 |
return final
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
print("Please enter 'true' or 'false'.")
|
| 58 |
-
|
| 59 |
-
# Main logic
|
| 60 |
-
try:
|
| 61 |
-
context = input(">> Enter context text: ")
|
| 62 |
-
num_questions = int(input("\n>> How many questions do you want to generate? "))
|
| 63 |
-
difficulty = input("\n>> Enter difficulty level (easy/medium/hard): ").strip().lower()
|
| 64 |
-
|
| 65 |
sentences = validate_inputs(context, num_questions, difficulty)
|
| 66 |
questions = generate_statements(context, num_questions, difficulty, sentences)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
print("\n--- QUIZ STARTS ---\n")
|
| 72 |
-
score = 0
|
| 73 |
-
|
| 74 |
-
for idx, (statement, actual_label) in enumerate(questions, 1):
|
| 75 |
-
print(f"Q{idx}: {statement}")
|
| 76 |
-
user = get_user_answer()
|
| 77 |
-
|
| 78 |
-
# Format input for facebook/bart-large-mnli
|
| 79 |
input_text = f"{context} [SEP] {statement}"
|
| 80 |
-
|
| 81 |
-
if
|
| 82 |
-
|
| 83 |
-
continue
|
| 84 |
-
model_label = "ENTAILMENT" if result["label"] == "entailment" else "CONTRADICTION"
|
| 85 |
-
|
| 86 |
-
if model_label == "ENTAILMENT" and user == "true":
|
| 87 |
-
print("Correct!\n")
|
| 88 |
-
score += 1
|
| 89 |
-
elif model_label == "CONTRADICTION" and user == "false":
|
| 90 |
-
print("Correct!\n")
|
| 91 |
-
score += 1
|
| 92 |
-
else:
|
| 93 |
-
print(f"Incorrect! (Correct answer: {'True' if model_label == 'ENTAILMENT' else 'False'})\n")
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
except ValueError as e:
|
| 98 |
-
print(f"Error: {e}")
|
|
|
|
| 4 |
from nltk.tokenize import sent_tokenize
|
| 5 |
|
| 6 |
# Download required tokenizer
|
| 7 |
+
nltk.download('punkt', quiet=True)
|
| 8 |
|
| 9 |
# Load NLI model
|
| 10 |
nli = pipeline("text-classification", model="facebook/bart-large-mnli")
|
|
|
|
| 27 |
elif level == "medium":
|
| 28 |
if "Sun" in sentence:
|
| 29 |
return sentence.replace("Sun", "Moon")
|
| 30 |
+
return sentence.replace(" is ", " is not ") if " is " in sentence else sentence
|
| 31 |
elif level == "hard":
|
| 32 |
if "eight" in sentence:
|
| 33 |
return sentence.replace("eight", "ten")
|
|
|
|
| 48 |
break
|
| 49 |
return final
|
| 50 |
|
| 51 |
+
# ✅ MAIN BACKEND FUNCTION
|
| 52 |
+
def generate_true_false(context, num_questions, difficulty):
|
| 53 |
+
"""
|
| 54 |
+
Returns a list of (statement, label) pairs for Streamlit frontend.
|
| 55 |
+
label: 'ENTAILMENT' (True) or 'CONTRADICTION' (False)
|
| 56 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
sentences = validate_inputs(context, num_questions, difficulty)
|
| 58 |
questions = generate_statements(context, num_questions, difficulty, sentences)
|
| 59 |
+
result = []
|
| 60 |
+
|
| 61 |
+
for statement, _ in questions:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
input_text = f"{context} [SEP] {statement}"
|
| 63 |
+
nli_result = nli(input_text)[0]
|
| 64 |
+
label = "ENTAILMENT" if nli_result["label"] == "entailment" else "CONTRADICTION"
|
| 65 |
+
result.append((statement, label))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
return result
|
|
|
|
|
|
|
|
|