Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +150 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,152 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import hashlib
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from huggingface_hub import CommitScheduler
|
| 8 |
+
from localization_eval import evaluate_submission
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
# --- PERSISTENCE CONFIGURATION ---
|
| 12 |
+
DATA_FILENAME = "submissions.csv"
|
| 13 |
+
USER_FILENAME = "users.csv"
|
| 14 |
+
DATA_DIR = Path("data")
|
| 15 |
+
DATA_DIR.mkdir(exist_ok=True)
|
| 16 |
+
|
| 17 |
+
DATA_PATH = DATA_DIR / DATA_FILENAME
|
| 18 |
+
USER_PATH = DATA_DIR / USER_FILENAME
|
| 19 |
+
|
| 20 |
+
# Initialize CommitScheduler
|
| 21 |
+
# This will automatically sync everything in the /data folder to your HF Dataset
|
| 22 |
+
repo_id = "your-username/your-private-dataset-name" # TODO: Change this
|
| 23 |
+
|
| 24 |
+
scheduler = CommitScheduler(
|
| 25 |
+
repo_id=repo_id,
|
| 26 |
+
repo_type="dataset",
|
| 27 |
+
folder_path=DATA_DIR,
|
| 28 |
+
path_in_repo="data",
|
| 29 |
+
every=5,
|
| 30 |
+
token=os.getenv("HF_TOKEN")
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# --- DB HELPERS (Replaced SQLite with Pandas/CSV) ---
|
| 34 |
+
|
| 35 |
+
def init_db():
|
| 36 |
+
# Create files if they don't exist
|
| 37 |
+
if not USER_PATH.exists():
|
| 38 |
+
pd.DataFrame(columns=["username", "password"]).to_csv(USER_PATH, index=False)
|
| 39 |
+
if not DATA_PATH.exists():
|
| 40 |
+
pd.DataFrame(columns=["username", "bbox_mAP", "bbox_AP50", "segm_mAP", "segm_AP50", "timestamp"]).to_csv(DATA_PATH, index=False)
|
| 41 |
+
|
| 42 |
+
def make_hashes(password):
|
| 43 |
+
return hashlib.sha256(str.encode(password)).hexdigest()
|
| 44 |
+
|
| 45 |
+
def add_user(username, password):
|
| 46 |
+
with scheduler.lock:
|
| 47 |
+
df = pd.read_csv(USER_PATH)
|
| 48 |
+
if username in df['username'].values:
|
| 49 |
+
return False
|
| 50 |
+
new_user = pd.DataFrame([{"username": username, "password": make_hashes(password)}])
|
| 51 |
+
df = pd.concat([df, new_user], ignore_index=True)
|
| 52 |
+
df.to_csv(USER_PATH, index=False)
|
| 53 |
+
return True
|
| 54 |
+
|
| 55 |
+
def login_user(username, password):
|
| 56 |
+
df = pd.read_csv(USER_PATH)
|
| 57 |
+
user_row = df[(df['username'] == username) & (df['password'] == make_hashes(password))]
|
| 58 |
+
return not user_row.empty
|
| 59 |
+
|
| 60 |
+
def save_submission(username, bbox_mAP, bbox_AP50, segm_mAP, segm_AP50):
|
| 61 |
+
with scheduler.lock:
|
| 62 |
+
df = pd.read_csv(DATA_PATH)
|
| 63 |
+
new_sub = pd.DataFrame([{
|
| 64 |
+
"username": username,
|
| 65 |
+
"bbox_mAP": bbox_mAP,
|
| 66 |
+
"bbox_AP50": bbox_AP50,
|
| 67 |
+
"segm_mAP": segm_mAP,
|
| 68 |
+
"segm_AP50": segm_AP50,
|
| 69 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 70 |
+
}])
|
| 71 |
+
df = pd.concat([df, new_sub], ignore_index=True)
|
| 72 |
+
df.to_csv(DATA_PATH, index=False)
|
| 73 |
+
|
| 74 |
+
def get_leaderboard_data():
|
| 75 |
+
if not DATA_PATH.exists():
|
| 76 |
+
return pd.DataFrame()
|
| 77 |
+
|
| 78 |
+
df = pd.read_csv(DATA_PATH)
|
| 79 |
+
if df.empty:
|
| 80 |
+
return df
|
| 81 |
+
|
| 82 |
+
# Logic to get the BEST score per user
|
| 83 |
+
# Sorting by segm_mAP (desc) and timestamp (asc) to get best/earliest
|
| 84 |
+
df = df.sort_values(by=['segm_mAP', 'timestamp'], ascending=[False, True])
|
| 85 |
+
df_best = df.drop_duplicates(subset='username', keep='first')
|
| 86 |
+
|
| 87 |
+
df_best = df_best.rename(columns={'segm_mAP': 'Best_segm_mAP'})
|
| 88 |
+
return df_best
|
| 89 |
+
|
| 90 |
+
# --- CONFIGURATION & SETUP ---
|
| 91 |
+
st.set_page_config(page_title="AI Benchmark Arena", page_icon="๐", layout="wide")
|
| 92 |
+
|
| 93 |
+
# (The rest of your ui_login_signup and main_app functions remain largely the same)
|
| 94 |
+
# Just ensure you call the new CSV-based functions.
|
| 95 |
+
|
| 96 |
+
# ... [KEEP YOUR ui_login_signup() and UI code here] ...
|
| 97 |
+
def ui_login_signup():
|
| 98 |
+
|
| 99 |
+
st.title("Welcome to Benchmark Arena ๐")
|
| 100 |
+
|
| 101 |
+
tab1, tab2 = st.tabs(["Login", "Sign Up"])
|
| 102 |
+
|
| 103 |
+
with tab1:
|
| 104 |
+
st.subheader("Sign In")
|
| 105 |
+
username = st.text_input("Username", key="login_user")
|
| 106 |
+
password = st.text_input("Password", type='password', key="login_pass")
|
| 107 |
+
|
| 108 |
+
if st.button("Login"):
|
| 109 |
+
if login_user(username, password):
|
| 110 |
+
st.session_state['logged_in'] = True
|
| 111 |
+
st.session_state['username'] = username
|
| 112 |
+
st.rerun()
|
| 113 |
+
else:
|
| 114 |
+
st.error("Username or Password incorrect")
|
| 115 |
+
|
| 116 |
+
with tab2:
|
| 117 |
+
st.subheader("Create New Account")
|
| 118 |
+
new_user = st.text_input("Username", key="new_user")
|
| 119 |
+
new_pass = st.text_input("Password", type='password', key="new_pass")
|
| 120 |
+
|
| 121 |
+
if st.button("Sign Up"):
|
| 122 |
+
if add_user(new_user, new_pass):
|
| 123 |
+
st.success("Account created! Please navigate to Login.")
|
| 124 |
+
else:
|
| 125 |
+
st.warning("Username already exists.")
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def main_app():
|
| 130 |
+
# (Sidebar and Expanders code here - same as your original)
|
| 131 |
+
|
| 132 |
+
# In your "Evaluate" button logic:
|
| 133 |
+
# Ensure variables are mapped correctly to the new save_submission()
|
| 134 |
+
if st.button("Evaluate"):
|
| 135 |
+
with st.spinner('Calculating...'):
|
| 136 |
+
bbox_mAP, bbox_AP50, segm_mAP, segm_AP50 = evaluate_submission("src/biv_query.json", save_path)
|
| 137 |
+
if bbox_mAP is not None:
|
| 138 |
+
save_submission(st.session_state['username'], bbox_mAP, bbox_AP50, segm_mAP, segm_AP50)
|
| 139 |
+
st.balloons()
|
| 140 |
+
st.success("Submission Successful!")
|
| 141 |
+
|
| 142 |
+
if __name__ == '__main__':
|
| 143 |
+
init_db()
|
| 144 |
+
|
| 145 |
+
if 'logged_in' not in st.session_state:
|
| 146 |
+
st.session_state['logged_in'] = False
|
| 147 |
+
st.session_state['username'] = None
|
| 148 |
|
| 149 |
+
if not st.session_state['logged_in']:
|
| 150 |
+
ui_login_signup()
|
| 151 |
+
else:
|
| 152 |
+
main_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|