MK-316 commited on
Commit
18294d1
1 Parent(s): c746d07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -1,37 +1,63 @@
1
  import pandas as pd
2
- import random
3
  import gradio as gr
4
- import os
5
 
6
- def group_names(file, members_per_group):
7
  # Read the CSV file
8
  df = pd.read_csv(file.name, encoding='utf-8')
9
 
10
- # Shuffle the DataFrame
11
- df = df.sample(frac=1).reset_index(drop=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Grouping
14
- groups = []
15
- for i in range(0, len(df), members_per_group):
16
- groups.append(df[i:i + members_per_group])
 
 
 
 
 
 
 
17
 
18
  # Creating a new DataFrame for grouped data
19
- grouped_df = pd.DataFrame({'Group': [f'Group {i+1}' for i, group in enumerate(groups)],
20
- 'Names': [', '.join(group['Names'].tolist()) for group in groups]})
 
 
 
 
21
 
22
  return grouped_df
23
 
24
- def main_interface(file, members_per_group):
25
- grouped_df = group_names(file, members_per_group)
26
  # Save the DataFrame to a CSV file with UTF-8 encoding
27
  output_filename = '/tmp/grouped_names.csv'
28
  grouped_df.to_csv(output_filename, index=False, encoding='utf-8-sig')
29
- # Return both the DataFrame for display and the file path for downloading
30
  return grouped_df, output_filename
31
 
32
  iface = gr.Interface(
33
  fn=main_interface,
34
- inputs=[gr.File(label="Upload CSV File"), gr.Number(label="Members per Group", value=5)],
 
 
 
 
35
  outputs=[gr.Dataframe(label="Grouped Names"), gr.File(label="Download Grouped Names CSV")],
36
  allow_flagging="never"
37
  )
 
1
  import pandas as pd
 
2
  import gradio as gr
 
3
 
4
+ def group_names(file, members_per_group, fixed_groups_input):
5
  # Read the CSV file
6
  df = pd.read_csv(file.name, encoding='utf-8')
7
 
8
+ # Parse fixed groups input
9
+ fixed_groups = [group.strip() for group in fixed_groups_input.split(';') if group.strip()]
10
+ fixed_groups_df_list = []
11
+ remaining_df = df.copy()
12
+
13
+ # Process fixed groups and create a list for additional members to be added
14
+ for group in fixed_groups:
15
+ group_names = [name.strip() for name in group.split(',') if name.strip()]
16
+ # Find these names in the DataFrame
17
+ matched_rows = remaining_df[remaining_df['Names'].isin(group_names)]
18
+ fixed_groups_df_list.append(matched_rows)
19
+ # Remove these names from the pool of remaining names
20
+ remaining_df = remaining_df[~remaining_df['Names'].isin(group_names)]
21
+
22
+ # Shuffle the remaining DataFrame
23
+ remaining_df = remaining_df.sample(frac=1).reset_index(drop=True)
24
 
25
+ # Adjusting fixed groups to include additional members if they're under the specified group size
26
+ for i, group_df in enumerate(fixed_groups_df_list):
27
+ while len(group_df) < members_per_group and not remaining_df.empty:
28
+ group_df = pd.concat([group_df, remaining_df.iloc[[0]]])
29
+ remaining_df = remaining_df.iloc[1:].reset_index(drop=True)
30
+ fixed_groups_df_list[i] = group_df # Update the group with added members
31
+
32
+ # Grouping the remaining names
33
+ groups = fixed_groups_df_list # Start with adjusted fixed groups
34
+ for i in range(0, len(remaining_df), members_per_group):
35
+ groups.append(remaining_df[i:i + members_per_group])
36
 
37
  # Creating a new DataFrame for grouped data
38
+ grouped_data = []
39
+ for i, group in enumerate(groups):
40
+ group_name = f'Group {i+1}'
41
+ names = ', '.join(group['Names'].tolist())
42
+ grouped_data.append({'Group': group_name, 'Members': names})
43
+ grouped_df = pd.DataFrame(grouped_data)
44
 
45
  return grouped_df
46
 
47
+ def main_interface(file, members_per_group, fixed_groups_input):
48
+ grouped_df = group_names(file, members_per_group, fixed_groups_input)
49
  # Save the DataFrame to a CSV file with UTF-8 encoding
50
  output_filename = '/tmp/grouped_names.csv'
51
  grouped_df.to_csv(output_filename, index=False, encoding='utf-8-sig')
 
52
  return grouped_df, output_filename
53
 
54
  iface = gr.Interface(
55
  fn=main_interface,
56
+ inputs=[
57
+ gr.File(label="Upload CSV File"),
58
+ gr.Number(label="Members per Group", value=4),
59
+ gr.Textbox(label="Fixed Groups (separated by semicolon)", placeholder="Name1, Name2; Name3, Name4", value=""),
60
+ ],
61
  outputs=[gr.Dataframe(label="Grouped Names"), gr.File(label="Download Grouped Names CSV")],
62
  allow_flagging="never"
63
  )