mariagrandury's picture
extract team scores calculation
8b57f56
raw
history blame
4.21 kB
import json
import os
import time
from collections import defaultdict
from functools import lru_cache
import argilla as rg
import gradio as gr
import pandas as pd
from dotenv import load_dotenv
from fastapi import FastAPI
from calculate_personal_scores import calculate_personal_scores
from calculate_team_scores import calculate_team_scores
load_dotenv()
# FastAPI app
app = FastAPI()
# Global variables for caching
last_update_time = 0
cached_data = None
def create_leaderboard_ui():
"""Create the leaderboard UI with caching."""
global cached_data, last_update_time
current_time = time.time()
if cached_data is not None and current_time - last_update_time < 300:
df = cached_data
else:
print("Recalculating scores...")
df = calculate_personal_scores()
cached_data = df
last_update_time = current_time
calculate_team_scores()
if not df.empty:
df = df.reset_index(drop=True)
df.index = df.index + 1
df = df.rename_axis("Rank").reset_index()
df_html = df.to_html(classes="leaderboard-table", border=0, index=False)
return f"""
<div style="margin: 20px 0;">
<p>Última Actualización: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_update_time))}</p>
<style>
.leaderboard-table {{
width: 100%;
border-collapse: collapse;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
border-radius: 8px;
overflow: hidden;
}}
.leaderboard-table th {{
background-color: #1a1a2e;
color: white;
font-weight: bold;
text-align: left;
padding: 14px;
border-bottom: 2px solid #16213e;
}}
.leaderboard-table td {{
padding: 12px 14px;
border-bottom: 1px solid #333;
background-color: #222;
color: #fff;
}}
.leaderboard-table tr:hover td {{
background-color: #2a2a3a;
}}
.leaderboard-table tr:nth-child(1) td:first-child {{
background-color: #ffd700;
color: #333;
font-weight: bold;
text-align: center;
border-right: 1px solid #333;
}}
.leaderboard-table tr:nth-child(2) td:first-child {{
background-color: #c0c0c0;
color: #333;
font-weight: bold;
text-align: center;
border-right: 1px solid #333;
}}
.leaderboard-table tr:nth-child(3) td:first-child {{
background-color: #cd7f32;
color: #333;
font-weight: bold;
text-align: center;
border-right: 1px solid #333;
}}
.leaderboard-table tr:nth-child(1) td:nth-child(2) {{
font-weight: bold;
color: #ffd700;
}}
.leaderboard-table tr:nth-child(2) td:nth-child(2) {{
font-weight: bold;
color: #c0c0c0;
}}
.leaderboard-table tr:nth-child(3) td:nth-child(2) {{
font-weight: bold;
color: #cd7f32;
}}
</style>
{df_html}
</div>
"""
def refresh_data():
"""Refresh the leaderboard data."""
global cached_data, last_update_time
cached_data = None
last_update_time = 0
return create_leaderboard_ui()
# Gradio interface
with gr.Blocks(theme=gr.themes.Default()) as demo:
with gr.Column(scale=1):
gr.Markdown("# 🏆 Leaderboard Personal Retos Hackathon 2025")
leaderboard_html = gr.HTML(create_leaderboard_ui)
refresh_btn = gr.Button("🔄 Actualizar Datos", variant="primary")
refresh_btn.click(fn=refresh_data, outputs=leaderboard_html)
gr.mount_gradio_app(app, demo, path="/")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)