Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,10 @@
|
|
| 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
|
| 8 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
|
| 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 |
|
|
@@ -39,8 +28,8 @@ def get_top_mcqs(user_input, domain, subdomain, top_n=10):
|
|
| 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."
|
|
@@ -57,26 +46,47 @@ def run_quiz(domain, subdomain, keyword_input):
|
|
| 57 |
return quiz_output
|
| 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 |
-
|
| 67 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
quiz_button = gr.Button("Get Top MCQs")
|
| 77 |
-
quiz_output = gr.Textbox(label="Quiz Questions", lines=20)
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Core logic to retrieve top MCQs
|
| 7 |
+
def get_top_mcqs(user_input, domain, subdomain, df, top_n=10):
|
| 8 |
domain = domain.strip().lower()
|
| 9 |
subdomain = subdomain.strip()
|
| 10 |
|
|
|
|
| 28 |
return top_questions.reset_index(drop=True)
|
| 29 |
|
| 30 |
# Quiz generation logic
|
| 31 |
+
def run_quiz(domain, subdomain, keyword_input, df):
|
| 32 |
+
mcq_df = get_top_mcqs(keyword_input, domain, subdomain, df)
|
| 33 |
|
| 34 |
if mcq_df.empty:
|
| 35 |
return "⚠️ No questions found for the selected domain/subdomain."
|
|
|
|
| 46 |
return quiz_output
|
| 47 |
|
| 48 |
# Dynamic subdomain update
|
| 49 |
+
def update_subdomains(domain, df):
|
| 50 |
domain = domain.strip().lower()
|
| 51 |
subdomains = df[df["domain"] == domain]["subdomain"].dropna().unique().tolist()
|
| 52 |
return gr.Dropdown.update(choices=sorted(subdomains), value=None)
|
| 53 |
|
| 54 |
# Gradio UI
|
| 55 |
+
def launch_interface():
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("## 🧠 Domain-Based MCQ Quiz System")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
domain_dropdown = gr.Dropdown(label="Select Domain", choices=[])
|
| 61 |
+
subdomain_dropdown = gr.Dropdown(label="Select Subdomain", choices=[])
|
| 62 |
+
|
| 63 |
+
file_upload = gr.File(label="Upload MCQ Dataset (CSV)", type="file")
|
| 64 |
+
|
| 65 |
+
# Update domain list when file is uploaded
|
| 66 |
+
def load_and_update(file):
|
| 67 |
+
if file is not None:
|
| 68 |
+
df = pd.read_csv(file.name)
|
| 69 |
+
# Normalize text to avoid case/whitespace mismatches
|
| 70 |
+
df['domain'] = df['domain'].str.strip().str.lower()
|
| 71 |
+
df['subdomain'] = df['subdomain'].str.strip()
|
| 72 |
+
|
| 73 |
+
domains = sorted(df['domain'].unique().tolist())
|
| 74 |
+
domain_dropdown.update(choices=domains)
|
| 75 |
+
|
| 76 |
+
return df
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
file_upload.change(fn=load_and_update, inputs=file_upload, outputs=None)
|
| 80 |
|
| 81 |
+
domain_dropdown.change(fn=update_subdomains, inputs=domain_dropdown, outputs=subdomain_dropdown)
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
keyword_input = gr.Textbox(label="Enter keywords or topic")
|
| 84 |
+
quiz_button = gr.Button("Get Top MCQs")
|
| 85 |
+
quiz_output = gr.Textbox(label="Quiz Questions", lines=20)
|
| 86 |
|
| 87 |
+
quiz_button.click(fn=run_quiz, inputs=[domain_dropdown, subdomain_dropdown, keyword_input, file_upload], outputs=quiz_output)
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
demo.launch()
|
| 90 |
|
| 91 |
+
# Run the interface
|
| 92 |
+
launch_interface()
|