Spaces:
Build error
Build error
| import gradio as gr | |
| import pandas as pd | |
| import random | |
| # Default list of participants | |
| participants_list = [ | |
| ["Darsh", "π"], ["Jazmine", "π₯"], ["Gnanam", "π₯"], ["DT", "π₯"], | |
| ["Rajen", "π"], ["Thiru", "π"], ["Shageenderan", "π"], ["Nirthanjali", "π"], | |
| ["Shyahana", "π"], ["Rischendera", "π"], ["Sothie", "π€©"], ["Niranjan", "π€©"], | |
| ["Sonali", "π€©"], ["Rehana", "π€©"], ["Tanisha", "π€©"], ["Rohan", "π€©"], | |
| ["Secret male", "π₯"], ["Pansy", "π₯"], ["Chandran", "π"], ["Vasanthi", "π"], | |
| ["Ashok", "ππ½"], ["Jane", "ππ½"] | |
| ] | |
| # Function to add a participant | |
| def add_participant(name, emoji): | |
| if not name or not emoji: | |
| return participants_list, "Please provide both a name and select an emoji." | |
| participants_list.append([name.strip(), emoji]) | |
| return participants_list, f"{name} added successfully!" | |
| # Function to remove a participant | |
| def remove_participant(name): | |
| global participants_list | |
| participants_list = [p for p in participants_list if p[0] != name] | |
| return participants_list, f"{name} removed successfully!" | |
| # Function to generate pairings ensuring emoji constraints | |
| def generate_pairings(): | |
| if len(participants_list) < 2: | |
| return "Please add at least two participants.", None | |
| # Extract names and emojis | |
| names = [name for name, emoji in participants_list] | |
| emojis = {name: emoji for name, emoji in participants_list} | |
| # Shuffle names for randomization | |
| receivers = names[:] | |
| random.shuffle(receivers) | |
| # Retry until all constraints are satisfied | |
| max_attempts = 1000 | |
| attempts = 0 | |
| while any( | |
| giver == receiver or emojis[giver] == emojis[receiver] | |
| for giver, receiver in zip(names, receivers) | |
| ): | |
| random.shuffle(receivers) | |
| attempts += 1 | |
| if attempts > max_attempts: | |
| return "Failed to generate valid pairings. Please adjust the participant list.", None | |
| # Create pairings | |
| pairings = [{"Giver": giver, "Receiver": receiver} for giver, receiver in zip(names, receivers)] | |
| # Return pairings as a DataFrame for display and download | |
| pairings_df = pd.DataFrame(pairings) | |
| return None, pairings_df | |
| # Function to download pairings as CSV | |
| def download_pairings(pairings_df): | |
| if pairings_df is None or len(pairings_df) == 0: | |
| return None | |
| # Save the DataFrame to a CSV file and return the file path | |
| csv_path = "pairings.csv" | |
| pairings_df.to_csv(csv_path, index=False) | |
| return csv_path | |
| # Gradio Interface | |
| with gr.Blocks() as interface: | |
| gr.Markdown("## π Secret Santa Pairing Generator π") | |
| gr.Markdown( | |
| "Add participants by entering their name and selecting their emoji group. " | |
| "Generate pairings and download the results as a CSV. \nCreated by Shageenderan Sapai" | |
| ) | |
| # Section for adding participants | |
| with gr.Row(): | |
| name_input = gr.Textbox(label="Participant Name", placeholder="Enter name") | |
| emoji_dropdown = gr.Dropdown( | |
| label="Select Emoji Group", | |
| choices=["π", "π₯", "π€©", "π", "ππ½"], | |
| value="π", | |
| ) | |
| add_button = gr.Button("Add Participant") | |
| # Section for displaying participants | |
| participant_table = gr.Dataframe( | |
| headers=["Name", "Emoji"], | |
| datatype=["str", "str"], | |
| value=participants_list, | |
| label="Current Participants" | |
| ) | |
| # Section for removing participants | |
| remove_name_input = gr.Textbox(label="Name to Remove", placeholder="Enter name to remove") | |
| remove_button = gr.Button("Remove Participant") | |
| # Section for generating pairings | |
| generate_button = gr.Button("Generate Pairings", variant="primary") | |
| pairing_output = gr.Dataframe( | |
| headers=["Giver", "Receiver"], | |
| datatype=["str", "str"], | |
| label="Generated Pairings" | |
| ) | |
| download_pairings_button = gr.Button("Download Pairings as CSV") | |
| download_file = gr.File(label="CSV File") | |
| # Interactivity | |
| add_button.click( | |
| add_participant, | |
| inputs=[name_input, emoji_dropdown], | |
| outputs=[participant_table, gr.Text(label="Message")] | |
| ) | |
| remove_button.click( | |
| remove_participant, | |
| inputs=[remove_name_input], | |
| outputs=[participant_table, gr.Text(label="Message")] | |
| ) | |
| generate_button.click( | |
| generate_pairings, | |
| inputs=[], | |
| outputs=[gr.Text(label="Error Message"), pairing_output] | |
| ) | |
| download_pairings_button.click( | |
| download_pairings, | |
| inputs=[pairing_output], | |
| outputs=[download_file] | |
| ) | |
| # Launch the interface | |
| interface.launch() | |