File size: 2,972 Bytes
abb556a 18294d1 abb556a f09d8a4 8cee5b9 18294d1 8cee5b9 18294d1 abb556a 8cee5b9 2f8379a 8cee5b9 abb556a 18294d1 c746d07 abb556a 18294d1 8cee5b9 93f3b9d 18294d1 c746d07 abb556a 8cee5b9 |
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 |
import pandas as pd
import gradio as gr
def group_names(file, members_per_group, fixed_groups_input):
# Read the CSV file
df = pd.read_csv(file.name, encoding='utf-8')
# Parse fixed groups input
fixed_groups = [group.strip() for group in fixed_groups_input.split(';') if group.strip()]
fixed_groups_df_list = []
remaining_df = df.copy()
# Process fixed groups and create a list for additional members to be added
for group in fixed_groups:
group_names = [name.strip() for name in group.split(',') if name.strip()]
# Find these names in the DataFrame
matched_rows = remaining_df[remaining_df['Names'].isin(group_names)]
fixed_groups_df_list.append(matched_rows)
# Remove these names from the pool of remaining names
remaining_df = remaining_df[~remaining_df['Names'].isin(group_names)]
# Shuffle the remaining DataFrame
remaining_df = remaining_df.sample(frac=1).reset_index(drop=True)
# Adjusting fixed groups to include additional members if they're under the specified group size
for i, group_df in enumerate(fixed_groups_df_list):
while len(group_df) < members_per_group and not remaining_df.empty:
group_df = pd.concat([group_df, remaining_df.iloc[[0]]])
remaining_df = remaining_df.iloc[1:].reset_index(drop=True)
fixed_groups_df_list[i] = group_df # Update the group with added members
# Grouping the remaining names
groups = fixed_groups_df_list # Start with adjusted fixed groups
for i in range(0, len(remaining_df), members_per_group):
groups.append(remaining_df[i:i + members_per_group])
# Determine the maximum group size
max_group_size = max(len(group) for group in groups)
# Creating a new DataFrame for grouped data with separate columns for each member
grouped_data = {'Group': [f'Group {i+1}' for i in range(len(groups))]}
# Add columns for each member
for i in range(max_group_size):
grouped_data[f'Member{i+1}'] = [group['Names'].tolist()[i] if i < len(group) else "" for group in groups]
grouped_df = pd.DataFrame(grouped_data)
return grouped_df
def main_interface(file, members_per_group, fixed_groups_input):
grouped_df = group_names(file, members_per_group, fixed_groups_input)
# Save the DataFrame to a CSV file with UTF-8 encoding
output_filename = '/tmp/grouped_names.csv'
grouped_df.to_csv(output_filename, index=False, encoding='utf-8-sig')
return grouped_df, output_filename
iface = gr.Interface(
fn=main_interface,
inputs=[
gr.File(label="Upload CSV File"),
gr.Number(label="Members per Group", value=5),
gr.Textbox(label="Fixed Groups (separated by semicolon;)", placeholder="ame1, Name2; Name1, Name2", value="4"),
],
outputs=[gr.Dataframe(label="Grouped Names"), gr.File(label="Download Grouped Names CSV")],
allow_flagging="never"
)
iface.launch()
|