import gradio as gr import pandas as pd from datasets import Dataset, load_dataset from collections import Counter from datetime import datetime import os # Load the dataset DATASET_NAME = "jablonkagroup/lamalab-shoutouts-table" def load_shoutouts_dataset(): """Load the shoutouts dataset from Hugging Face Hub""" try: dataset = load_dataset(DATASET_NAME, split="train") return dataset except Exception as e: print(f"Error loading dataset: {e}") return None def create_received_leaderboard(dataset): """Create leaderboard for people who received most shoutouts""" if dataset is None: return pd.DataFrame(columns=["Rank", "Name", "Total Shoutouts", "Average per Week"]) all_received = [] for entry in dataset: all_received.extend(entry["shoutout_received"]) received_counts = Counter(all_received) # Calculate average per week (total unique dates) total_weeks = len(set([entry["date"] for entry in dataset])) leaderboard_data = [] for rank, (name, count) in enumerate(received_counts.most_common(), 1): avg_per_week = round(count / total_weeks, 2) if total_weeks > 0 else 0 leaderboard_data.append( {"Rank": rank, "Name": name, "Total Shoutouts": count, "Average per Week": avg_per_week} ) return pd.DataFrame(leaderboard_data) def create_given_leaderboard(dataset): """Create leaderboard for people who gave most shoutouts""" if dataset is None: return pd.DataFrame(columns=["Rank", "Name", "Total Shoutouts", "Average per Week"]) all_given = [] for entry in dataset: all_given.extend(entry["shoutout_given"]) given_counts = Counter(all_given) # Calculate average per week total_weeks = len(set([entry["date"] for entry in dataset])) leaderboard_data = [] for rank, (name, count) in enumerate(given_counts.most_common(), 1): avg_per_week = round(count / total_weeks, 2) if total_weeks > 0 else 0 leaderboard_data.append( {"Rank": rank, "Name": name, "Total Shoutouts": count, "Average per Week": avg_per_week} ) return pd.DataFrame(leaderboard_data) def get_all_people(dataset): """Get all unique people from the dataset""" if dataset is None: return [] all_people = set() for entry in dataset: all_people.update(entry["shoutout_received"]) all_people.update(entry["shoutout_given"]) return sorted(list(all_people)) def submit_shoutout(date, received_people, given_people, dataset): """Submit a new shoutout entry""" if not date: return "❌ Please enter a date!", dataset if not received_people and not given_people: return "❌ Please select at least one person who received or gave shoutouts!", dataset try: # Create new entry new_entry = { "date": date, "shoutout_received": received_people if received_people else [], "shoutout_given": given_people if given_people else [], } # Convert current dataset to list current_data = [] for entry in dataset: current_data.append( { "date": entry["date"], "shoutout_received": entry["shoutout_received"], "shoutout_given": entry["shoutout_given"], } ) # Add new entry current_data.append(new_entry) # Create new dataset new_dataset = Dataset.from_list(current_data) # Push to hub (you might need to handle authentication) try: new_dataset.push_to_hub(DATASET_NAME, token=os.getenv("HF_TOKEN")) return f"✅ Successfully added shoutout for {date}!", new_dataset except Exception as e: return f"❌ Error pushing to hub: {str(e)}", dataset except Exception as e: return f"❌ Error submitting shoutout: {str(e)}", dataset def refresh_leaderboards(): """Refresh the leaderboards with latest data""" dataset = load_shoutouts_dataset() received_df = create_received_leaderboard(dataset) given_df = create_given_leaderboard(dataset) people_list = get_all_people(dataset) return received_df, given_df, gr.Dropdown(choices=people_list), gr.Dropdown(choices=people_list), dataset # Initialize data initial_dataset = load_shoutouts_dataset() initial_received_df = create_received_leaderboard(initial_dataset) initial_given_df = create_given_leaderboard(initial_dataset) initial_people = get_all_people(initial_dataset) # Custom CSS custom_css = """ .tab-nav button { font-size: 16px; font-weight: bold; } .markdown-text { font-size: 16px; } .leaderboard-table { font-size: 14px; } """ # Create Gradio interface with gr.Blocks(css=custom_css, title="LamaLab Shoutouts Leaderboard") as demo: # Store dataset in state dataset_state = gr.State(initial_dataset) gr.HTML("