Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,6 +51,33 @@ def update_subdomains(domain, df):
|
|
| 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:
|
|
@@ -63,19 +90,6 @@ def launch_interface():
|
|
| 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)
|
|
|
|
| 51 |
subdomains = df[df["domain"] == domain]["subdomain"].dropna().unique().tolist()
|
| 52 |
return gr.Dropdown.update(choices=sorted(subdomains), value=None)
|
| 53 |
|
| 54 |
+
# Load and handle file upload
|
| 55 |
+
def load_and_update(file):
|
| 56 |
+
if file is not None:
|
| 57 |
+
try:
|
| 58 |
+
df = pd.read_csv(file.name) # Read the CSV file
|
| 59 |
+
if df.empty:
|
| 60 |
+
return "⚠️ The uploaded dataset is empty."
|
| 61 |
+
|
| 62 |
+
# Ensure expected columns exist
|
| 63 |
+
required_columns = ['domain', 'subdomain', 'keywords', 'question', 'option1', 'option2', 'option3', 'option4', 'correct_answer']
|
| 64 |
+
missing_columns = set(required_columns) - set(df.columns)
|
| 65 |
+
if missing_columns:
|
| 66 |
+
return f"⚠️ Missing required columns in the dataset: {', '.join(missing_columns)}"
|
| 67 |
+
|
| 68 |
+
# Normalize text to avoid case/whitespace mismatches
|
| 69 |
+
df['domain'] = df['domain'].str.strip().str.lower()
|
| 70 |
+
df['subdomain'] = df['subdomain'].str.strip()
|
| 71 |
+
|
| 72 |
+
# Update domain dropdown choices
|
| 73 |
+
domains = sorted(df['domain'].unique().tolist())
|
| 74 |
+
domain_dropdown.update(choices=domains)
|
| 75 |
+
|
| 76 |
+
return df
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return f"Error loading file: {str(e)}"
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
# Gradio UI
|
| 82 |
def launch_interface():
|
| 83 |
with gr.Blocks() as demo:
|
|
|
|
| 90 |
file_upload = gr.File(label="Upload MCQ Dataset (CSV)", type="file")
|
| 91 |
|
| 92 |
# Update domain list when file is uploaded
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
file_upload.change(fn=load_and_update, inputs=file_upload, outputs=None)
|
| 94 |
|
| 95 |
domain_dropdown.change(fn=update_subdomains, inputs=domain_dropdown, outputs=subdomain_dropdown)
|