subramaniansrc commited on
Commit
195a41f
Β·
verified Β·
1 Parent(s): 98732f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -40
app.py CHANGED
@@ -4,57 +4,50 @@ import tempfile
4
  import os
5
 
6
  # ---------------------------------------------------
7
- # Auto column detection
8
  # ---------------------------------------------------
9
  def detect_columns(df):
10
 
11
- cols = [c.strip().lower() for c in df.columns]
 
12
 
13
- name_col, dob_col = None, None
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 = original
19
 
20
  if any(k in c for k in ["dob", "birth", "date"]):
21
- dob_col = original
22
 
23
  return name_col, dob_col
24
 
25
 
26
  # ---------------------------------------------------
27
- # Read file safely
28
  # ---------------------------------------------------
29
- def read_file(uploaded_file):
30
-
31
- if isinstance(uploaded_file, dict):
32
- file_path = uploaded_file["name"]
33
- else:
34
- file_path = uploaded_file
35
 
36
- if file_path.endswith((".xlsx", ".xls")):
37
- return pd.read_excel(file_path)
38
- else:
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
- {"Error": ["Name/DOB column not detected"]}
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
- # βœ… IMPORTANT FIX β€” create persistent file
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
- # Returning filepath ENABLES download button
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
- Upload CSV/Excel containing Name and DOB columns.
 
109
  """)
110
 
111
  file_input = gr.File(
112
- label="Upload Student List",
113
- file_types=[".csv", ".xlsx", ".xls"],
114
- type="filepath"
115
  )
116
 
117
  btn = gr.Button("Generate Month-wise List")
118
 
119
- output_table = gr.Dataframe(label="Grouped Students")
120
 
121
- download_file = gr.File(label="⬇ Download Grouped CSV")
 
 
 
122
 
123
  btn.click(
124
  group_by_birth_month,
125
  inputs=file_input,
126
- outputs=[output_table, download_file]
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()