Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
| 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 |
# Load dataset
|
| 7 |
-
df = pd.read_csv(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def get_top_mcqs(user_input, domain, subdomain, top_n=10):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
filtered_df = df[(df['domain'] == domain) & (df['subdomain'] == subdomain)]
|
| 12 |
|
| 13 |
if filtered_df.empty:
|
|
@@ -27,47 +41,52 @@ def get_top_mcqs(user_input, domain, subdomain, top_n=10):
|
|
| 27 |
|
| 28 |
return top_questions.reset_index(drop=True)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
def run_quiz(domain, subdomain, keyword_input):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return f"❌ Runtime Error: {str(e)}"
|
| 50 |
-
|
| 51 |
-
# Function to update subdomain choices
|
| 52 |
def update_subdomains(domain):
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Gradio UI
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
gr.Markdown("## 🧠 Domain-Based MCQ Quiz System")
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
-
domain_dropdown = gr.Dropdown(label="Select Domain", choices=sorted(df['domain'].
|
| 62 |
-
subdomain_dropdown = gr.Dropdown(label="Select Subdomain"
|
| 63 |
|
| 64 |
domain_dropdown.change(fn=update_subdomains, inputs=domain_dropdown, outputs=subdomain_dropdown)
|
| 65 |
|
| 66 |
keyword_input = gr.Textbox(label="Enter keywords or topic")
|
| 67 |
quiz_button = gr.Button("Get Top MCQs")
|
| 68 |
-
quiz_output = gr.Textbox(label="Quiz Questions", lines=20
|
| 69 |
|
| 70 |
quiz_button.click(fn=run_quiz, inputs=[domain_dropdown, subdomain_dropdown, keyword_input], outputs=quiz_output)
|
| 71 |
|
| 72 |
-
# Launch
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
+
# Install dependencies (if needed)
|
| 2 |
+
# !pip install gradio pandas scikit-learn
|
| 3 |
+
|
| 4 |
import pandas as pd
|
| 5 |
import gradio as gr
|
| 6 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
|
| 9 |
# Load dataset
|
| 10 |
+
df = pd.read_csv("mcq_dataset.csv")
|
| 11 |
+
|
| 12 |
+
# Clean domain/subdomain for consistent matching
|
| 13 |
+
df['domain'] = df['domain'].astype(str).str.strip().str.lower()
|
| 14 |
+
df['subdomain'] = df['subdomain'].astype(str).str.strip()
|
| 15 |
|
| 16 |
+
# Function to get top N MCQs using cosine similarity
|
| 17 |
def get_top_mcqs(user_input, domain, subdomain, top_n=10):
|
| 18 |
+
if not domain or not subdomain:
|
| 19 |
+
return pd.DataFrame()
|
| 20 |
+
|
| 21 |
+
# Normalize input for comparison
|
| 22 |
+
domain = domain.strip().lower()
|
| 23 |
+
subdomain = subdomain.strip()
|
| 24 |
+
|
| 25 |
filtered_df = df[(df['domain'] == domain) & (df['subdomain'] == subdomain)]
|
| 26 |
|
| 27 |
if filtered_df.empty:
|
|
|
|
| 41 |
|
| 42 |
return top_questions.reset_index(drop=True)
|
| 43 |
|
| 44 |
+
# Quiz execution logic
|
| 45 |
def run_quiz(domain, subdomain, keyword_input):
|
| 46 |
+
mcq_df = get_top_mcqs(keyword_input, domain, subdomain)
|
| 47 |
+
|
| 48 |
+
if mcq_df.empty:
|
| 49 |
+
return "⚠️ No questions found for the selected domain/subdomain."
|
| 50 |
+
|
| 51 |
+
quiz_output = ""
|
| 52 |
+
for i, row in mcq_df.iterrows():
|
| 53 |
+
quiz_output += f"Q{i+1}: {row['question']}\n"
|
| 54 |
+
quiz_output += f"A. {row['option1']}\n"
|
| 55 |
+
quiz_output += f"B. {row['option2']}\n"
|
| 56 |
+
quiz_output += f"C. {row['option3']}\n"
|
| 57 |
+
quiz_output += f"D. {row['option4']}\n"
|
| 58 |
+
quiz_output += f"(✅ Correct Answer: {row['correct_answer']})\n\n"
|
| 59 |
+
|
| 60 |
+
return quiz_output
|
| 61 |
+
|
| 62 |
+
# Update subdomains dynamically
|
|
|
|
|
|
|
|
|
|
| 63 |
def update_subdomains(domain):
|
| 64 |
+
if not domain:
|
| 65 |
+
return gr.Dropdown.update(choices=[], value=None)
|
| 66 |
+
|
| 67 |
+
domain = domain.strip().lower()
|
| 68 |
+
subdomains = df[df["domain"] == domain]["subdomain"].dropna().unique().tolist()
|
| 69 |
+
|
| 70 |
+
if not subdomains:
|
| 71 |
+
subdomains = ["No Subdomains Available"]
|
| 72 |
+
|
| 73 |
+
return gr.Dropdown.update(choices=sorted(subdomains), value=subdomains[0])
|
| 74 |
|
| 75 |
# Gradio UI
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
gr.Markdown("## 🧠 Domain-Based MCQ Quiz System")
|
| 78 |
|
| 79 |
with gr.Row():
|
| 80 |
+
domain_dropdown = gr.Dropdown(label="Select Domain", choices=sorted(df['domain'].unique().tolist()))
|
| 81 |
+
subdomain_dropdown = gr.Dropdown(label="Select Subdomain")
|
| 82 |
|
| 83 |
domain_dropdown.change(fn=update_subdomains, inputs=domain_dropdown, outputs=subdomain_dropdown)
|
| 84 |
|
| 85 |
keyword_input = gr.Textbox(label="Enter keywords or topic")
|
| 86 |
quiz_button = gr.Button("Get Top MCQs")
|
| 87 |
+
quiz_output = gr.Textbox(label="Quiz Questions", lines=20)
|
| 88 |
|
| 89 |
quiz_button.click(fn=run_quiz, inputs=[domain_dropdown, subdomain_dropdown, keyword_input], outputs=quiz_output)
|
| 90 |
|
| 91 |
+
# Launch app
|
| 92 |
demo.launch()
|