File size: 1,217 Bytes
abb556a
 
 
 
 
 
 
f09d8a4
abb556a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f09d8a4
abb556a
17d2846
abb556a
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import random
import gradio as gr
import os

def group_names(file, members_per_group):
    # Read the CSV file
    df = pd.read_csv(file.name, encoding='utf-8')
    
    # Shuffle the DataFrame
    df = df.sample(frac=1).reset_index(drop=True)
    
    # Grouping
    groups = []
    for i in range(0, len(df), members_per_group):
        groups.append(df[i:i + members_per_group])

    # Creating a new DataFrame for grouped data
    grouped_df = pd.DataFrame({'Group': [f'Group {i+1}' for i, group in enumerate(groups)],
                               'Names': [', '.join(group['Names'].tolist()) for group in groups]})
    
    return grouped_df

def main_interface(file, members_per_group):
    grouped_df = group_names(file, members_per_group)
    # 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 output_filename

iface = gr.Interface(
    fn=main_interface,
    inputs=[gr.File(label="Upload CSV File"), gr.Number(label="Members per Group", value=5)],
    outputs=gr.File(label="Download Grouped Names CSV"),
    allow_flagging="never"
)

iface.launch()