Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import os | |
import datetime | |
import pickle | |
import inspect | |
from google_sheet import * | |
st.title("π Hackathon Leaderboard") | |
# ======================== | |
# Submission Form | |
# ======================== | |
uploaded_file = st.file_uploader("Upload your model/class (.pkl)", type=["pkl"]) | |
def is_valid_model(obj): | |
""" | |
Check if the object is a class instance with a 'predict' method. | |
""" | |
return hasattr(obj, 'get_team') and inspect.ismethod(getattr(obj, 'get_team', None)) or callable(getattr(obj, 'get_team', None)) | |
if uploaded_file and st.button("Submit"): | |
timestamp = datetime.datetime.now().isoformat() | |
submission_filename = f"{timestamp.replace(':', '_')}_{uploaded_file.name}" | |
submission_path = os.path.join("submissions", submission_filename) | |
os.makedirs("submissions", exist_ok=True) | |
# Save uploaded file | |
with open(submission_path, "wb") as f: | |
f.write(uploaded_file.read()) | |
team = "fail" | |
# Load and evaluate model | |
try: | |
with open(submission_path, "rb") as f: | |
model = pickle.load(f) | |
if is_valid_model(model): | |
team = model.get_team() | |
score = 100 # Example valid score | |
st.success("Valid model uploaded. β ") | |
else: | |
team = "fail" | |
score = 0 | |
st.error("Uploaded object does not implement a `predict` method. β") | |
except Exception as e: | |
score = 0 | |
st.error(f"Failed to load or validate pickle file: {e}") | |
# Append result to leaderboard | |
append_score(timestamp, score, submission_filename) | |
st.success(f"Submission received! Score: {score}; Team {team}") | |
# ======================== | |
# Always Show Leaderboard | |
# ======================== | |
st.subheader("Leaderboard") | |
try: | |
df = fetch_leaderboard() | |
if not df.empty: | |
df_sorted = df.sort_values(by="score", ascending=False) | |
st.dataframe(df_sorted) | |
else: | |
st.info("No submissions yet.") | |
except Exception as e: | |
st.warning(f"Could not load leaderboard: {e}") | |