Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install ipywidgets (if not already installed)
|
| 2 |
+
!pip install ipywidgets
|
| 3 |
+
|
| 4 |
+
# Imports
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import ipywidgets as widgets
|
| 7 |
+
from IPython.display import display
|
| 8 |
+
|
| 9 |
+
# Load your CSV file
|
| 10 |
+
df = pd.read_csv('mcq_dataset.csv')
|
| 11 |
+
|
| 12 |
+
# Unique domains
|
| 13 |
+
domains = df['domain'].unique().tolist()
|
| 14 |
+
|
| 15 |
+
# Create dropdown widgets
|
| 16 |
+
domain_dropdown = widgets.Dropdown(
|
| 17 |
+
options=domains,
|
| 18 |
+
description='Domain:',
|
| 19 |
+
style={'description_width': 'initial'}
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
subdomain_dropdown = widgets.Dropdown(
|
| 23 |
+
options=[],
|
| 24 |
+
description='Subdomain:',
|
| 25 |
+
style={'description_width': 'initial'}
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Update subdomains based on selected domain
|
| 29 |
+
def update_subdomains(*args):
|
| 30 |
+
selected_domain = domain_dropdown.value
|
| 31 |
+
filtered = df[df['domain'] == selected_domain]
|
| 32 |
+
subdomains = filtered['subdomain'].unique().tolist()
|
| 33 |
+
subdomain_dropdown.options = subdomains
|
| 34 |
+
|
| 35 |
+
domain_dropdown.observe(update_subdomains, 'value')
|
| 36 |
+
update_subdomains() # initial call
|
| 37 |
+
|
| 38 |
+
display(domain_dropdown)
|
| 39 |
+
display(subdomain_dropdown)
|
| 40 |
+
|
| 41 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 42 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 43 |
+
|
| 44 |
+
def get_top_mcqs(user_input, domain, subdomain, df, top_n=10):
|
| 45 |
+
# Filter dataset by domain and subdomain
|
| 46 |
+
filtered_df = df[(df['domain'] == domain) & (df['subdomain'] == subdomain)]
|
| 47 |
+
|
| 48 |
+
if filtered_df.empty:
|
| 49 |
+
print("No MCQs found for the selected domain/subdomain.")
|
| 50 |
+
return pd.DataFrame()
|
| 51 |
+
|
| 52 |
+
# Combine user input with keywords for similarity comparison
|
| 53 |
+
documents = filtered_df['keywords'].tolist()
|
| 54 |
+
documents.insert(0, user_input) # user input is first
|
| 55 |
+
|
| 56 |
+
# TF-IDF vectorization
|
| 57 |
+
vectorizer = TfidfVectorizer()
|
| 58 |
+
tfidf_matrix = vectorizer.fit_transform(documents)
|
| 59 |
+
|
| 60 |
+
# Compute cosine similarity between user input and all MCQs
|
| 61 |
+
cosine_sim = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:]).flatten()
|
| 62 |
+
|
| 63 |
+
# Get indices of top N most similar questions
|
| 64 |
+
top_indices = cosine_sim.argsort()[-top_n:][::-1]
|
| 65 |
+
|
| 66 |
+
# Retrieve top questions
|
| 67 |
+
top_questions = filtered_df.iloc[top_indices].copy()
|
| 68 |
+
top_questions['similarity_score'] = cosine_sim[top_indices]
|
| 69 |
+
|
| 70 |
+
return top_questions.reset_index(drop=True)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Assume the user selected values from the dropdowns
|
| 74 |
+
selected_domain = domain_dropdown.value
|
| 75 |
+
selected_subdomain = subdomain_dropdown.value
|
| 76 |
+
|
| 77 |
+
# User inputs a topic or keywords
|
| 78 |
+
user_input = input("Enter keywords or topic to focus on: ")
|
| 79 |
+
|
| 80 |
+
# Retrieve top 10 relevant MCQs
|
| 81 |
+
top_mcqs = get_top_mcqs(user_input, selected_domain, selected_subdomain, df)
|
| 82 |
+
|
| 83 |
+
# Display them
|
| 84 |
+
top_mcqs[['question', 'option1', 'option2', 'option3', 'option4', 'correct_answer', 'similarity_score']]
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def run_quiz(mcq_df):
|
| 88 |
+
score = 0
|
| 89 |
+
|
| 90 |
+
print("\n--- Starting the Quiz ---\n")
|
| 91 |
+
|
| 92 |
+
for i, row in mcq_df.iterrows():
|
| 93 |
+
print(f"Q{i+1}: {row['question']}")
|
| 94 |
+
print(f"A. {row['option1']}")
|
| 95 |
+
print(f"B. {row['option2']}")
|
| 96 |
+
print(f"C. {row['option3']}")
|
| 97 |
+
print(f"D. {row['option4']}")
|
| 98 |
+
|
| 99 |
+
answer_map = {
|
| 100 |
+
'A': row['option1'],
|
| 101 |
+
'B': row['option2'],
|
| 102 |
+
'C': row['option3'],
|
| 103 |
+
'D': row['option4']
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
user_ans = input("Your answer (A/B/C/D): ").strip().upper()
|
| 107 |
+
selected_option = answer_map.get(user_ans, None)
|
| 108 |
+
|
| 109 |
+
if selected_option == row['correct_answer']:
|
| 110 |
+
print("✅ Correct!\n")
|
| 111 |
+
score += 1
|
| 112 |
+
else:
|
| 113 |
+
print(f"❌ Incorrect! Correct answer: {row['correct_answer']}\n")
|
| 114 |
+
|
| 115 |
+
print(f"🎯 Quiz Complete! Your Score: {score} / 10")
|
| 116 |
+
return score
|
| 117 |
+
|
| 118 |
+
user_score = run_quiz(top_mcqs)
|