Spaces:
Sleeping
Sleeping
File size: 2,894 Bytes
456b5c5 c350084 456b5c5 9171978 aeaf1c9 9171978 aeaf1c9 c350084 aeaf1c9 7e464a8 195a41f 101501e 7e464a8 c350084 7e464a8 9171978 195a41f 7e464a8 c350084 7e464a8 9171978 7e464a8 9171978 c350084 9171978 c350084 7e464a8 98732f8 c350084 98732f8 c350084 7e464a8 98732f8 c350084 7e464a8 c350084 7e464a8 c350084 195a41f c350084 101501e 456b5c5 7e464a8 195a41f 7e464a8 456b5c5 c350084 7e464a8 c350084 456b5c5 7e464a8 456b5c5 195a41f c350084 7e464a8 456b5c5 7e464a8 aeaf1c9 456b5c5 195a41f 456b5c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import gradio as gr
import pandas as pd
import tempfile
import os
# ---------------------------------------------------
# Main Processing Function
# ---------------------------------------------------
def group_students(file_path):
try:
if file_path is None:
return pd.DataFrame({"Status": ["Upload a file first"]}), None
# Read CSV or Excel
if file_path.lower().endswith((".xlsx", ".xls")):
df = pd.read_excel(file_path)
else:
df = pd.read_csv(file_path)
# Clean column names
df.columns = df.columns.str.strip().str.lower()
# Auto detect columns
name_col = None
dob_col = None
for col in df.columns:
if "name" in col:
name_col = col
if "dob" in col or "birth" in col or "date" in col:
dob_col = col
if name_col is None or dob_col is None:
return pd.DataFrame({
"Error": ["Name or Date of Birth column not found"]
}), None
# Convert DOB
df[dob_col] = pd.to_datetime(df[dob_col], errors="coerce", dayfirst=True)
df = df.dropna(subset=[dob_col])
# Extract month
df["Birth_Month"] = df[dob_col].dt.month_name()
grouped = (
df.groupby("Birth_Month")[name_col]
.apply(lambda x: ", ".join(x.astype(str)))
.reset_index()
)
# Sort calendar order
month_order = [
"January","February","March","April","May","June",
"July","August","September","October","November","December"
]
grouped["Birth_Month"] = pd.Categorical(
grouped["Birth_Month"],
categories=month_order,
ordered=True
)
grouped = grouped.sort_values("Birth_Month")
# Create downloadable CSV
output_path = os.path.join(
tempfile.gettempdir(),
"birth_month_grouped.csv"
)
grouped.to_csv(output_path, index=False)
return grouped, output_path
except Exception as e:
# show error directly in table
return pd.DataFrame({"Error": [str(e)]}), None
# ---------------------------------------------------
# Gradio UI
# ---------------------------------------------------
with gr.Blocks() as demo:
gr.Markdown("""
# 🎂 Student Birth Month Grouping
Upload CSV or Excel file containing:
- Student Name
- Date of Birth
""")
file_input = gr.File(
label="Upload File",
type="filepath"
)
run_btn = gr.Button("Generate List")
table_output = gr.Dataframe(label="Grouped Students")
download_output = gr.File(label="Download CSV")
run_btn.click(
group_students,
inputs=file_input,
outputs=[table_output, download_output]
)
demo.launch() |