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()