Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import os | |
import datetime | |
from google_sheet import fetch_leaderboard | |
from google_drive import upload_to_drive | |
os.makedirs("submissions", exist_ok=True) | |
def get_leaderboard_html(): | |
try: | |
df = fetch_leaderboard() | |
if df.empty: | |
return "<p>No submissions yet.</p>" | |
df_sorted = df.sort_values(by="score", ascending=False) | |
return df_sorted.to_html(index=False) | |
except Exception as e: | |
return f"<p>Could not load leaderboard: {e}</p>" | |
def handle_submission(file): | |
if file is None: | |
return "β No file uploaded.", "<p>No leaderboard to show.</p>" | |
timestamp = datetime.datetime.now().isoformat().replace(":", "_") | |
submission_filename = f"{timestamp}_{file.name}" | |
submission_path = os.path.join("submissions", submission_filename) | |
with open(submission_path, "wb") as f: | |
f.write(file.read()) | |
try: | |
drive_file_id = upload_to_drive(submission_path, submission_filename) | |
status = f"β Uploaded to Google Drive [File ID: {drive_file_id}]" | |
except Exception as e: | |
status = f"β οΈ Failed to upload to Google Drive: {e}" | |
leaderboard_html = get_leaderboard_html() | |
return status, leaderboard_html | |
with gr.Blocks(title="π Hackathon Leaderboard") as demo: | |
gr.Markdown("## π Hackathon Leaderboard") | |
with gr.Row(): | |
file_input = gr.File(label="Upload your submission (.zip)", file_types=[".zip"]) | |
submit_btn = gr.Button("Submit") | |
status_output = gr.Markdown() | |
leaderboard_output = gr.HTML(get_leaderboard_html()) | |
submit_btn.click( | |
fn=handle_submission, | |
inputs=file_input, | |
outputs=[status_output, leaderboard_output] | |
) | |
demo.launch(share=True) | |