Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,57 +4,50 @@ import tempfile
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# ---------------------------------------------------
|
| 7 |
-
#
|
| 8 |
# ---------------------------------------------------
|
| 9 |
def detect_columns(df):
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
for original, c in zip(df.columns, cols):
|
| 16 |
|
| 17 |
if any(k in c for k in ["name", "student"]):
|
| 18 |
-
name_col =
|
| 19 |
|
| 20 |
if any(k in c for k in ["dob", "birth", "date"]):
|
| 21 |
-
dob_col =
|
| 22 |
|
| 23 |
return name_col, dob_col
|
| 24 |
|
| 25 |
|
| 26 |
# ---------------------------------------------------
|
| 27 |
-
#
|
| 28 |
# ---------------------------------------------------
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
if isinstance(uploaded_file, dict):
|
| 32 |
-
file_path = uploaded_file["name"]
|
| 33 |
-
else:
|
| 34 |
-
file_path = uploaded_file
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return pd.read_csv(file_path)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# ---------------------------------------------------
|
| 43 |
-
# Main Processing
|
| 44 |
-
# ---------------------------------------------------
|
| 45 |
-
def group_by_birth_month(uploaded_file):
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
df = read_file(uploaded_file)
|
| 49 |
df.columns = df.columns.str.strip()
|
| 50 |
|
| 51 |
name_col, dob_col = detect_columns(df)
|
| 52 |
|
| 53 |
if name_col is None or dob_col is None:
|
| 54 |
-
return pd.DataFrame(
|
| 55 |
-
|
| 56 |
-
), None
|
| 57 |
|
|
|
|
| 58 |
df[dob_col] = pd.to_datetime(
|
| 59 |
df[dob_col],
|
| 60 |
errors="coerce",
|
|
@@ -83,7 +76,7 @@ def group_by_birth_month(uploaded_file):
|
|
| 83 |
|
| 84 |
grouped = grouped.sort_values("Birth_Month")
|
| 85 |
|
| 86 |
-
# β
|
| 87 |
output_path = os.path.join(
|
| 88 |
tempfile.gettempdir(),
|
| 89 |
"birth_month_grouped.csv"
|
|
@@ -91,39 +84,43 @@ def group_by_birth_month(uploaded_file):
|
|
| 91 |
|
| 92 |
grouped.to_csv(output_path, index=False)
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
return grouped, output_path
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
return pd.DataFrame({"Error": [str(e)]}), None
|
| 99 |
|
| 100 |
|
| 101 |
# ---------------------------------------------------
|
| 102 |
-
# UI
|
| 103 |
# ---------------------------------------------------
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
|
| 106 |
gr.Markdown("""
|
| 107 |
-
# π Student Birth Month Grouping
|
| 108 |
-
|
|
|
|
| 109 |
""")
|
| 110 |
|
| 111 |
file_input = gr.File(
|
| 112 |
-
label="Upload Student
|
| 113 |
-
|
| 114 |
-
|
| 115 |
)
|
| 116 |
|
| 117 |
btn = gr.Button("Generate Month-wise List")
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
btn.click(
|
| 124 |
group_by_birth_month,
|
| 125 |
inputs=file_input,
|
| 126 |
-
outputs=[
|
| 127 |
)
|
| 128 |
|
| 129 |
demo.launch()
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# ---------------------------------------------------
|
| 7 |
+
# Detect columns automatically
|
| 8 |
# ---------------------------------------------------
|
| 9 |
def detect_columns(df):
|
| 10 |
|
| 11 |
+
name_col = None
|
| 12 |
+
dob_col = None
|
| 13 |
|
| 14 |
+
for col in df.columns:
|
| 15 |
+
c = col.strip().lower()
|
|
|
|
| 16 |
|
| 17 |
if any(k in c for k in ["name", "student"]):
|
| 18 |
+
name_col = col
|
| 19 |
|
| 20 |
if any(k in c for k in ["dob", "birth", "date"]):
|
| 21 |
+
dob_col = col
|
| 22 |
|
| 23 |
return name_col, dob_col
|
| 24 |
|
| 25 |
|
| 26 |
# ---------------------------------------------------
|
| 27 |
+
# Main Function
|
| 28 |
# ---------------------------------------------------
|
| 29 |
+
def group_by_birth_month(file_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
try:
|
| 32 |
+
if file_path is None:
|
| 33 |
+
return pd.DataFrame({"Error": ["Upload a file first"]}), None
|
|
|
|
| 34 |
|
| 35 |
+
# Read file
|
| 36 |
+
if file_path.endswith((".xlsx", ".xls")):
|
| 37 |
+
df = pd.read_excel(file_path)
|
| 38 |
+
else:
|
| 39 |
+
df = pd.read_csv(file_path)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
df.columns = df.columns.str.strip()
|
| 42 |
|
| 43 |
name_col, dob_col = detect_columns(df)
|
| 44 |
|
| 45 |
if name_col is None or dob_col is None:
|
| 46 |
+
return pd.DataFrame({
|
| 47 |
+
"Error": ["Name/DOB column not detected"]
|
| 48 |
+
}), None
|
| 49 |
|
| 50 |
+
# Convert DOB
|
| 51 |
df[dob_col] = pd.to_datetime(
|
| 52 |
df[dob_col],
|
| 53 |
errors="coerce",
|
|
|
|
| 76 |
|
| 77 |
grouped = grouped.sort_values("Birth_Month")
|
| 78 |
|
| 79 |
+
# β
Create downloadable CSV
|
| 80 |
output_path = os.path.join(
|
| 81 |
tempfile.gettempdir(),
|
| 82 |
"birth_month_grouped.csv"
|
|
|
|
| 84 |
|
| 85 |
grouped.to_csv(output_path, index=False)
|
| 86 |
|
| 87 |
+
# β CRITICAL FIX: return as LIST
|
| 88 |
+
return grouped, [output_path]
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
return pd.DataFrame({"Error": [str(e)]}), None
|
| 92 |
|
| 93 |
|
| 94 |
# ---------------------------------------------------
|
| 95 |
+
# Gradio UI
|
| 96 |
# ---------------------------------------------------
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
|
| 99 |
gr.Markdown("""
|
| 100 |
+
# π Student Birth Month Grouping (HF Stable Version)
|
| 101 |
+
|
| 102 |
+
Upload CSV or Excel containing Name and Date of Birth.
|
| 103 |
""")
|
| 104 |
|
| 105 |
file_input = gr.File(
|
| 106 |
+
label="Upload Student File",
|
| 107 |
+
type="filepath",
|
| 108 |
+
file_types=[".csv", ".xlsx", ".xls"]
|
| 109 |
)
|
| 110 |
|
| 111 |
btn = gr.Button("Generate Month-wise List")
|
| 112 |
|
| 113 |
+
table_output = gr.Dataframe(label="Grouped Students")
|
| 114 |
|
| 115 |
+
download_output = gr.File(
|
| 116 |
+
label="β¬ Download CSV Result",
|
| 117 |
+
file_count="single"
|
| 118 |
+
)
|
| 119 |
|
| 120 |
btn.click(
|
| 121 |
group_by_birth_month,
|
| 122 |
inputs=file_input,
|
| 123 |
+
outputs=[table_output, download_output]
|
| 124 |
)
|
| 125 |
|
| 126 |
demo.launch()
|