Spaces:
Running
Running
File size: 2,627 Bytes
de6fc01 0339ef7 de6fc01 0339ef7 de6fc01 0339ef7 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
import gradio as gr
import asyncio
import time
import threading
from src.retrieve_data import populate_lb_data
leaderboard_data = {}
async def fetch_data():
"""Fetch the leaderboard data asynchronously"""
global leaderboard_data
try:
data = await populate_lb_data()
leaderboard_data = data
return True
except Exception as e:
print(f"Error fetching data: {e}")
return False
def background_update():
"""Background thread function to update data every 5 minutes"""
while True:
print("Updating leaderboard data...")
asyncio.run(fetch_data())
time.sleep(300) # 5 minutes
def create_table_for_lb(lb_name):
"""Create a formatted table for a specific leaderboard and GPU"""
global leaderboard_data
lb_data = leaderboard_data[lb_name]
headers = ["Rank", "Submission Name", "User ID", "Score", "Date"]
rows = []
for i, result in enumerate(lb_data.results, 1):
rows.append(
[
i,
result.submission_name,
result.user_id,
f"{float(result.submission_score):.4f}",
result.submission_time,
]
)
return gr.Dataframe(
headers=headers,
datatype=["number", "str", "str", "str", "str"],
value=rows,
interactive=False,
)
def refresh_ui():
"""Force refresh the UI with latest data"""
asyncio.run(fetch_data())
return "Data refreshed!"
def build_ui():
"""Build the Gradio UI"""
global leaderboard_data
with gr.Blocks(title="ML Leaderboards") as app:
gr.Markdown("# Machine Learning Leaderboards")
with gr.Row():
refresh_btn = gr.Button("Refresh Data")
status = gr.Textbox(label="Status", value="Ready")
refresh_btn.click(fn=refresh_ui, outputs=status)
# Initial data fetch
asyncio.run(fetch_data())
# Create tabs for each leaderboard
if leaderboard_data:
with gr.Tabs():
for lb_name, lb_data in leaderboard_data.items():
with gr.Tab(lb_name):
gr.Markdown(f"## {lb_name} - {lb_data.gpu}")
create_table_for_lb(lb_name)
else:
gr.Markdown("No leaderboard data available. Please refresh.")
return app
if __name__ == "__main__":
# Start the background update thread
update_thread = threading.Thread(target=background_update, daemon=True)
update_thread.start()
# Launch the Gradio app
app = build_ui()
app.launch()
|