Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +50 -30
src/streamlit_app.py
CHANGED
|
@@ -1,48 +1,68 @@
|
|
| 1 |
-
import
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import datetime
|
| 5 |
-
import dill as pickle
|
| 6 |
-
import inspect
|
| 7 |
|
| 8 |
-
from google_sheet import
|
| 9 |
from google_drive import upload_to_drive
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
timestamp = datetime.datetime.now().isoformat()
|
| 21 |
-
submission_filename = f"{timestamp.replace(':', '_')}_{uploaded_file.name}"
|
| 22 |
submission_path = os.path.join("submissions", submission_filename)
|
| 23 |
-
os.makedirs("submissions", exist_ok=True)
|
| 24 |
|
| 25 |
-
# Save
|
| 26 |
with open(submission_path, "wb") as f:
|
| 27 |
-
f.write(
|
| 28 |
|
| 29 |
try:
|
| 30 |
drive_file_id = upload_to_drive(submission_path, submission_filename)
|
| 31 |
-
|
| 32 |
except Exception as e:
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# ========================
|
| 36 |
-
# Always Show Leaderboard
|
| 37 |
-
# ========================
|
| 38 |
-
st.subheader("Leaderboard")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
df_sorted = df.sort_values(by="score", ascending=False)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import datetime
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
from google_sheet import fetch_leaderboard
|
| 7 |
from google_drive import upload_to_drive
|
| 8 |
|
| 9 |
+
# Ensure the submissions folder exists
|
| 10 |
+
os.makedirs("submissions", exist_ok=True)
|
| 11 |
|
| 12 |
+
# --- Submission Logic ---
|
| 13 |
+
def handle_submission(file):
|
| 14 |
+
if file is None:
|
| 15 |
+
return "β No file uploaded.", None
|
| 16 |
|
| 17 |
+
# Generate timestamped filename
|
| 18 |
+
timestamp = datetime.datetime.now().isoformat().replace(":", "_")
|
| 19 |
+
submission_filename = f"{timestamp}_{file.name}"
|
|
|
|
|
|
|
| 20 |
submission_path = os.path.join("submissions", submission_filename)
|
|
|
|
| 21 |
|
| 22 |
+
# Save file
|
| 23 |
with open(submission_path, "wb") as f:
|
| 24 |
+
f.write(file.read())
|
| 25 |
|
| 26 |
try:
|
| 27 |
drive_file_id = upload_to_drive(submission_path, submission_filename)
|
| 28 |
+
status = f"β
Uploaded to Google Drive [File ID: {drive_file_id}]"
|
| 29 |
except Exception as e:
|
| 30 |
+
status = f"β οΈ Failed to upload to Google Drive: {e}"
|
| 31 |
+
|
| 32 |
+
# Return status and updated leaderboard
|
| 33 |
+
return status, get_leaderboard_html()
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# --- Leaderboard Logic ---
|
| 37 |
+
def get_leaderboard_html():
|
| 38 |
+
try:
|
| 39 |
+
df = fetch_leaderboard()
|
| 40 |
+
if df.empty:
|
| 41 |
+
return "<p>No submissions yet.</p>"
|
| 42 |
df_sorted = df.sort_values(by="score", ascending=False)
|
| 43 |
+
return df_sorted.to_html(index=False)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"<p>Could not load leaderboard: {e}</p>"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# --- Gradio Interface ---
|
| 49 |
+
with gr.Blocks(title="π Hackathon Leaderboard") as demo:
|
| 50 |
+
gr.Markdown("## π Hackathon Leaderboard")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
file_input = gr.File(label="Upload your submission (.zip)", file_types=[".zip"])
|
| 54 |
+
submit_btn = gr.Button("Submit")
|
| 55 |
+
|
| 56 |
+
status_output = gr.Markdown()
|
| 57 |
+
leaderboard_output = gr.HTML(get_leaderboard_html())
|
| 58 |
+
|
| 59 |
+
def submit_action(file):
|
| 60 |
+
return handle_submission(file)
|
| 61 |
+
|
| 62 |
+
submit_btn.click(
|
| 63 |
+
fn=submit_action,
|
| 64 |
+
inputs=file_input,
|
| 65 |
+
outputs=[status_output, leaderboard_output]
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.launch()
|