import gradio as gr import time from supabase import create_client, Client import os from dotenv import load_dotenv import pandas as pd # Load environment variables load_dotenv() # Initialize Supabase client SUPABASE_URL = os.getenv("SUPABASE_URL") SUPABASE_KEY = os.getenv("SUPABASE_KEY") supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) def get_active_round(): # Fetch the active round data and return both round ID and round number response = supabase.table("round_status").select("id, round").eq("is_eval_active", True).single().execute() if response.data: return response.data['id'], response.data['round'] # Return both round ID and round number return None, None def get_elo_ratings(round_id): # Query the ELO ratings based on the round_id response = supabase.table("elos").select("user_id, rating").eq("round", round_id).execute() print("get_elo_ratings: ", response.data) if response.data: df = pd.DataFrame(response.data) df = df.sort_values(by='rating', ascending=False) print(df.head()) return df return pd.DataFrame(columns=['user_id', 'rating']) def update_info(): # Get the active round ID and round number round_id, round_number = get_active_round() print("Active Round ID:", round_id, "Round Number:", round_number) # This will print both round ID and round number if round_id: # Fetch the ELO ratings based on the round ID elo_ratings = get_elo_ratings(round_id) return f"Active Round: {round_number}", elo_ratings # Display the round number in the UI else: return "No active round found", pd.DataFrame(columns=['user_id', 'rating']) with gr.Blocks() as demo: gr.Markdown("## Leaderboard") round_info = gr.Textbox(label="") elo_table = gr.DataFrame(label="ELO Ratings", headers=["User ID", "Rating"]) # Create a periodic update function def periodic_update(): round_status, ratings = update_info() return round_status, ratings # Load initial values demo.load(update_info, outputs=[round_info, elo_table]) # Use gr.Timer to trigger updates every 5 seconds timer = gr.Timer(value=5, active=True) # Set timer to tick every 5 seconds timer.tick(periodic_update, outputs=[round_info, elo_table]) if __name__ == "__main__": demo.queue() demo.launch()