hackathon / app.py
com3dian's picture
Update app.py
c05e0a4 verified
raw
history blame
2.04 kB
import io
import random
from typing import List, Tuple
import aiohttp
import panel as pn
from PIL import Image
from transformers import CLIPModel, CLIPProcessor
pn.extension(design="bootstrap", sizing_mode="stretch_width")
import panel as pn
import pandas as pd
import os
import datetime
import io
from google_sheet import fetch_leaderboard
from google_drive import upload_to_drive
pn.extension()
# File upload widget
file_input = pn.widgets.FileInput(accept='.zip', multiple=False)
# Status message
status = pn.pane.Markdown("")
# Leaderboard display
leaderboard = pn.pane.DataFrame(pd.DataFrame(), width=600)
import tempfile
temp_dir = tempfile.gettempdir()
def submit_file(event):
if file_input.value is None:
status.object = "⚠️ Please upload a .zip file before submitting."
return
try:
# Extract filename
filename = file_input.filename
submission_path = os.path.join(temp_dir, filename)
# Save uploaded content to temp file
with open(submission_path, "wb") as f:
f.write(file_input.value)
# Upload to Google Drive
drive_file_id = upload_to_drive(submission_path, filename)
status.object = f"βœ… Uploaded to Google Drive [File ID: {drive_file_id}]"
except Exception as e:
status.object = f"❌ Failed to upload to Google Drive: {e}"
return # Exit early on failure
# Update leaderboard
try:
df = fetch_leaderboard()
if not df.empty:
df_sorted = df.sort_values(by="score", ascending=False)
leaderboard.object = df_sorted
else:
leaderboard.object = pd.DataFrame()
except Exception as e:
status.object += f"\n⚠️ Could not load leaderboard: {e}"
submit_button = pn.widgets.Button(name="Submit", button_type="primary")
submit_button.on_click(submit_file)
# Layout
app = pn.Column(
"## πŸ† Hackathon Leaderboard",
file_input,
submit_button,
status,
"### Leaderboard",
leaderboard
)
app.servable()