Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,81 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from typing import List, Tuple
|
4 |
-
|
5 |
-
import aiohttp
|
6 |
-
import panel as pn
|
7 |
-
from PIL import Image
|
8 |
-
from transformers import CLIPModel, CLIPProcessor
|
9 |
-
|
10 |
-
pn.extension(design="bootstrap", sizing_mode="stretch_width")
|
11 |
import panel as pn
|
12 |
import pandas as pd
|
13 |
-
import os
|
14 |
-
import datetime
|
15 |
-
import io
|
16 |
|
17 |
from google_sheet import fetch_leaderboard
|
18 |
from google_drive import upload_to_drive
|
19 |
|
20 |
-
pn.extension()
|
21 |
|
22 |
-
#
|
23 |
file_input = pn.widgets.FileInput(accept='.zip', multiple=False)
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
status = pn.pane.Markdown("")
|
27 |
-
|
28 |
-
# Leaderboard display
|
29 |
-
leaderboard = pn.pane.DataFrame(pd.DataFrame(), width=600)
|
30 |
-
import tempfile
|
31 |
-
|
32 |
temp_dir = tempfile.gettempdir()
|
33 |
|
|
|
34 |
def submit_file(event):
|
35 |
if file_input.value is None:
|
36 |
status.object = "β οΈ Please upload a .zip file before submitting."
|
|
|
|
|
37 |
return
|
38 |
|
39 |
try:
|
40 |
-
# Extract filename
|
41 |
filename = file_input.filename
|
42 |
submission_path = os.path.join(temp_dir, filename)
|
43 |
|
44 |
-
# Save uploaded content to temp file
|
45 |
with open(submission_path, "wb") as f:
|
46 |
f.write(file_input.value)
|
47 |
|
48 |
-
# Upload to Google Drive
|
49 |
drive_file_id = upload_to_drive(submission_path, filename)
|
50 |
-
status.object = f"β
Uploaded to Google Drive
|
|
|
|
|
51 |
except Exception as e:
|
52 |
-
status.object = f"β Failed to upload
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
|
|
56 |
try:
|
57 |
df = fetch_leaderboard()
|
58 |
if not df.empty:
|
59 |
-
|
60 |
-
leaderboard.object = df_sorted
|
61 |
else:
|
62 |
leaderboard.object = pd.DataFrame()
|
63 |
except Exception as e:
|
64 |
-
status.object
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
submit_button,
|
85 |
-
status,
|
86 |
-
"### Leaderboard",
|
87 |
-
leaderboard
|
88 |
)
|
89 |
|
90 |
app.servable()
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import panel as pn
|
4 |
import pandas as pd
|
|
|
|
|
|
|
5 |
|
6 |
from google_sheet import fetch_leaderboard
|
7 |
from google_drive import upload_to_drive
|
8 |
|
9 |
+
pn.extension(design="bootstrap", sizing_mode="stretch_width")
|
10 |
|
11 |
+
# Widgets
|
12 |
file_input = pn.widgets.FileInput(accept='.zip', multiple=False)
|
13 |
+
submit_button = pn.widgets.Button(name="π€ Submit", button_type="primary")
|
14 |
+
status = pn.pane.Alert("", alert_type="info", visible=False)
|
15 |
+
leaderboard = pn.pane.DataFrame(pd.DataFrame(), width=800, height=400, index=False)
|
16 |
|
17 |
+
# Temp dir
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
temp_dir = tempfile.gettempdir()
|
19 |
|
20 |
+
# --- Submission logic ---
|
21 |
def submit_file(event):
|
22 |
if file_input.value is None:
|
23 |
status.object = "β οΈ Please upload a .zip file before submitting."
|
24 |
+
status.alert_type = "warning"
|
25 |
+
status.visible = True
|
26 |
return
|
27 |
|
28 |
try:
|
|
|
29 |
filename = file_input.filename
|
30 |
submission_path = os.path.join(temp_dir, filename)
|
31 |
|
|
|
32 |
with open(submission_path, "wb") as f:
|
33 |
f.write(file_input.value)
|
34 |
|
|
|
35 |
drive_file_id = upload_to_drive(submission_path, filename)
|
36 |
+
status.object = f"β
Uploaded to Google Drive\n**File ID**: `{drive_file_id}`"
|
37 |
+
status.alert_type = "success"
|
38 |
+
status.visible = True
|
39 |
except Exception as e:
|
40 |
+
status.object = f"β Failed to upload: {e}"
|
41 |
+
status.alert_type = "danger"
|
42 |
+
status.visible = True
|
43 |
+
return
|
44 |
+
|
45 |
+
# Refresh leaderboard
|
46 |
+
update_leaderboard()
|
47 |
+
|
48 |
+
submit_button.on_click(submit_file)
|
49 |
|
50 |
+
# --- Leaderboard logic ---
|
51 |
+
def update_leaderboard():
|
52 |
try:
|
53 |
df = fetch_leaderboard()
|
54 |
if not df.empty:
|
55 |
+
leaderboard.object = df.sort_values(by="score", ascending=False)
|
|
|
56 |
else:
|
57 |
leaderboard.object = pd.DataFrame()
|
58 |
except Exception as e:
|
59 |
+
status.object = f"β οΈ Could not load leaderboard: {e}"
|
60 |
+
status.alert_type = "warning"
|
61 |
+
status.visible = True
|
62 |
+
|
63 |
+
# Initial load
|
64 |
+
update_leaderboard()
|
65 |
+
|
66 |
+
# --- Layout ---
|
67 |
+
app = pn.template.BootstrapTemplate(
|
68 |
+
title="π Hackathon Leaderboard",
|
69 |
+
sidebar=[
|
70 |
+
pn.pane.Markdown("### π Upload Your Submission (.zip)"),
|
71 |
+
file_input,
|
72 |
+
submit_button,
|
73 |
+
status,
|
74 |
+
],
|
75 |
+
main=[
|
76 |
+
pn.pane.Markdown("## π₯ Live Leaderboard"),
|
77 |
+
leaderboard
|
78 |
+
]
|
|
|
|
|
|
|
|
|
79 |
)
|
80 |
|
81 |
app.servable()
|