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