Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
@@ -6,15 +10,12 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 6 |
# Load dataset
|
| 7 |
df = pd.read_csv("mcq_dataset.csv")
|
| 8 |
|
| 9 |
-
# Normalize
|
| 10 |
-
df['domain'] = df['domain'].
|
| 11 |
-
df['subdomain'] = df['subdomain'].
|
| 12 |
|
| 13 |
-
#
|
| 14 |
def get_top_mcqs(user_input, domain, subdomain, top_n=10):
|
| 15 |
-
if not domain or not subdomain:
|
| 16 |
-
return pd.DataFrame()
|
| 17 |
-
|
| 18 |
domain = domain.strip().lower()
|
| 19 |
subdomain = subdomain.strip()
|
| 20 |
|
|
@@ -37,10 +38,10 @@ def get_top_mcqs(user_input, domain, subdomain, top_n=10):
|
|
| 37 |
|
| 38 |
return top_questions.reset_index(drop=True)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def run_quiz(domain, subdomain, keyword_input):
|
| 42 |
mcq_df = get_top_mcqs(keyword_input, domain, subdomain)
|
| 43 |
-
|
| 44 |
if mcq_df.empty:
|
| 45 |
return "⚠️ No questions found for the selected domain/subdomain."
|
| 46 |
|
|
@@ -57,29 +58,17 @@ def run_quiz(domain, subdomain, keyword_input):
|
|
| 57 |
|
| 58 |
# Dynamic subdomain update
|
| 59 |
def update_subdomains(domain):
|
| 60 |
-
if not domain:
|
| 61 |
-
return gr.Dropdown.update(choices=[], value=None)
|
| 62 |
-
|
| 63 |
domain = domain.strip().lower()
|
| 64 |
subdomains = df[df["domain"] == domain]["subdomain"].dropna().unique().tolist()
|
| 65 |
-
|
| 66 |
-
subdomains = ["No Subdomains Available"]
|
| 67 |
-
|
| 68 |
-
return gr.Dropdown.update(choices=sorted(subdomains), value=subdomains[0])
|
| 69 |
|
| 70 |
-
# Gradio
|
| 71 |
with gr.Blocks() as demo:
|
| 72 |
gr.Markdown("## 🧠 Domain-Based MCQ Quiz System")
|
| 73 |
|
| 74 |
-
with gr.
|
| 75 |
-
domain_dropdown = gr.Dropdown(
|
| 76 |
-
|
| 77 |
-
choices=sorted(df['domain'].unique().tolist())
|
| 78 |
-
)
|
| 79 |
-
subdomain_dropdown = gr.Dropdown(
|
| 80 |
-
label="Select Subdomain",
|
| 81 |
-
filterable=True # 👈 Enables searching subdomains
|
| 82 |
-
)
|
| 83 |
|
| 84 |
domain_dropdown.change(fn=update_subdomains, inputs=domain_dropdown, outputs=subdomain_dropdown)
|
| 85 |
|
|
@@ -89,5 +78,5 @@ with gr.Blocks() as demo:
|
|
| 89 |
|
| 90 |
quiz_button.click(fn=run_quiz, inputs=[domain_dropdown, subdomain_dropdown, keyword_input], outputs=quiz_output)
|
| 91 |
|
| 92 |
-
# Launch
|
| 93 |
demo.launch()
|
|
|
|
| 1 |
+
# Install dependencies (only needed once)
|
| 2 |
+
!pip install gradio pandas scikit-learn
|
| 3 |
+
|
| 4 |
+
# Imports
|
| 5 |
import pandas as pd
|
| 6 |
import gradio as gr
|
| 7 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
|
|
| 10 |
# Load dataset
|
| 11 |
df = pd.read_csv("mcq_dataset.csv")
|
| 12 |
|
| 13 |
+
# Normalize text to avoid case/whitespace mismatches
|
| 14 |
+
df['domain'] = df['domain'].str.strip().str.lower()
|
| 15 |
+
df['subdomain'] = df['subdomain'].str.strip()
|
| 16 |
|
| 17 |
+
# Core logic to retrieve top MCQs
|
| 18 |
def get_top_mcqs(user_input, domain, subdomain, top_n=10):
|
|
|
|
|
|
|
|
|
|
| 19 |
domain = domain.strip().lower()
|
| 20 |
subdomain = subdomain.strip()
|
| 21 |
|
|
|
|
| 38 |
|
| 39 |
return top_questions.reset_index(drop=True)
|
| 40 |
|
| 41 |
+
# Quiz generation logic
|
| 42 |
def run_quiz(domain, subdomain, keyword_input):
|
| 43 |
mcq_df = get_top_mcqs(keyword_input, domain, subdomain)
|
| 44 |
+
|
| 45 |
if mcq_df.empty:
|
| 46 |
return "⚠️ No questions found for the selected domain/subdomain."
|
| 47 |
|
|
|
|
| 58 |
|
| 59 |
# Dynamic subdomain update
|
| 60 |
def update_subdomains(domain):
|
|
|
|
|
|
|
|
|
|
| 61 |
domain = domain.strip().lower()
|
| 62 |
subdomains = df[df["domain"] == domain]["subdomain"].dropna().unique().tolist()
|
| 63 |
+
return gr.Dropdown.update(choices=sorted(subdomains), value=None)
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Gradio UI
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("## 🧠 Domain-Based MCQ Quiz System")
|
| 68 |
|
| 69 |
+
with gr.Row():
|
| 70 |
+
domain_dropdown = gr.Dropdown(label="Select Domain", choices=sorted(df['domain'].unique().tolist()))
|
| 71 |
+
subdomain_dropdown = gr.Dropdown(label="Select Subdomain", choices=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
domain_dropdown.change(fn=update_subdomains, inputs=domain_dropdown, outputs=subdomain_dropdown)
|
| 74 |
|
|
|
|
| 78 |
|
| 79 |
quiz_button.click(fn=run_quiz, inputs=[domain_dropdown, subdomain_dropdown, keyword_input], outputs=quiz_output)
|
| 80 |
|
| 81 |
+
# Launch interface
|
| 82 |
demo.launch()
|