Spaces:
Sleeping
Sleeping
Update src/main.py
Browse files- src/main.py +67 -68
src/main.py
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import uuid
|
|
@@ -8,10 +12,8 @@ from typing import Any, Dict, List, Tuple
|
|
| 8 |
import streamlit as st
|
| 9 |
import pandas as pd
|
| 10 |
from PIL import Image
|
| 11 |
-
from huggingface_hub import HfApi, hf_hub_download
|
| 12 |
from huggingface_hub.utils import HfHubHTTPError
|
| 13 |
-
from pathlib import Path
|
| 14 |
-
|
| 15 |
|
| 16 |
# =========================
|
| 17 |
# CONFIG
|
|
@@ -27,6 +29,8 @@ st.set_page_config(
|
|
| 27 |
#Set this to the private dataset repo that acts as the "database"
|
| 28 |
DB_REPO_ID = os.getenv("DB_REPO_ID", "NidhiS09/VizWiz-submissions-db")
|
| 29 |
DB_REPO_TYPE = "dataset"
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# This must exist as a Space Secret in the PUBLIC UI Space
|
| 32 |
SUBMISSIONS_TOKEN = os.getenv("SUBMISSIONS_TOKEN", "")
|
|
@@ -44,25 +48,6 @@ CHALLENGE_TYPES = ["Object Detection", "Instance Segmentation"]
|
|
| 44 |
LEADERBOARD_METRICS = ["bbox_mAP", "bbox_AP50", "segm_mAP", "segm_AP50"]
|
| 45 |
DEFAULT_SORT_METRIC = "segm_AP50"
|
| 46 |
|
| 47 |
-
# =========================
|
| 48 |
-
# COMMIT SCHEDULER SETUP
|
| 49 |
-
# =========================
|
| 50 |
-
|
| 51 |
-
# Local folder for scheduler to watch
|
| 52 |
-
SCHEDULER_FOLDER = Path("submissions_queue")
|
| 53 |
-
SCHEDULER_FOLDER.mkdir(exist_ok=True)
|
| 54 |
-
|
| 55 |
-
# Initialize CommitScheduler
|
| 56 |
-
scheduler = CommitScheduler(
|
| 57 |
-
repo_id=DB_REPO_ID,
|
| 58 |
-
repo_type=DB_REPO_TYPE,
|
| 59 |
-
folder_path=SCHEDULER_FOLDER,
|
| 60 |
-
token=SUBMISSIONS_TOKEN,
|
| 61 |
-
path_in_repo="submissions", # Where files go in the repo
|
| 62 |
-
every=5, # Commit every 5 minutes
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
# =========================
|
| 67 |
# HELPERS
|
| 68 |
# =========================
|
|
@@ -120,14 +105,25 @@ def _validate_submission_json(obj: Any) -> Tuple[bool, str]:
|
|
| 120 |
return True, "OK"
|
| 121 |
|
| 122 |
|
| 123 |
-
def
|
| 124 |
-
"""
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
|
| 133 |
def _create_submission_record(
|
|
@@ -140,10 +136,11 @@ def _create_submission_record(
|
|
| 140 |
original_filename: str,
|
| 141 |
) -> str:
|
| 142 |
"""
|
| 143 |
-
Writes pred/meta/status to the
|
| 144 |
Returns submission_id.
|
| 145 |
"""
|
| 146 |
_require_token()
|
|
|
|
| 147 |
|
| 148 |
submission_id = str(uuid.uuid4())
|
| 149 |
ts = int(time.time())
|
|
@@ -160,10 +157,10 @@ def _create_submission_record(
|
|
| 160 |
|
| 161 |
status = {"state": "queued", "timestamp": ts}
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
|
| 168 |
return submission_id
|
| 169 |
|
|
@@ -225,39 +222,7 @@ def _load_leaderboard_df() -> pd.DataFrame:
|
|
| 225 |
# =========================
|
| 226 |
# UI
|
| 227 |
# =========================
|
| 228 |
-
|
| 229 |
-
def render_overview():
|
| 230 |
-
with st.expander("βΉοΈ Overview of the AI Benchmark Arena"):
|
| 231 |
-
st.markdown(
|
| 232 |
-
"""
|
| 233 |
-
|
| 234 |
-
**Note:** This Hugging Face Space queues submissions for evaluation and persists results in a private database repo.
|
| 235 |
-
"""
|
| 236 |
-
)
|
| 237 |
-
# Keep this optional so missing image doesn't crash the Space
|
| 238 |
-
try:
|
| 239 |
-
overview_image = Image.open("src/overview_image.png").resize((600, 600))
|
| 240 |
-
st.image(overview_image, caption="Example of an object localization task")
|
| 241 |
-
except Exception:
|
| 242 |
-
st.info("Overview image not found at src/overview_image.png (optional).")
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
def render_eval_details():
|
| 246 |
-
with st.expander("π How is the Score Calculated?"):
|
| 247 |
-
st.markdown(
|
| 248 |
-
"""
|
| 249 |
-
Your submission is evaluated offline by a private evaluator against hidden ground-truth annotations.
|
| 250 |
-
The leaderboard reports:
|
| 251 |
-
|
| 252 |
-
- bbox_mAP
|
| 253 |
-
- bbox_AP50
|
| 254 |
-
- segm_mAP
|
| 255 |
-
- segm_AP50 (default ranking)
|
| 256 |
-
|
| 257 |
-
Raw submissions are kept private; only scores and metadata are shown.
|
| 258 |
-
"""
|
| 259 |
-
)
|
| 260 |
-
|
| 261 |
|
| 262 |
def page_submit():
|
| 263 |
st.header("π Submit your Predictions")
|
|
@@ -375,6 +340,9 @@ def page_leaderboard():
|
|
| 375 |
|
| 376 |
|
| 377 |
def main():
|
|
|
|
|
|
|
|
|
|
| 378 |
st.sidebar.title("AI Benchmark Arena π")
|
| 379 |
|
| 380 |
# Warn early if DB repo isn't configured
|
|
@@ -396,6 +364,37 @@ def main():
|
|
| 396 |
else:
|
| 397 |
page_leaderboard()
|
| 398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
if __name__ == "__main__":
|
| 401 |
main()
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# st.write("hello")
|
| 3 |
+
|
| 4 |
+
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
import uuid
|
|
|
|
| 12 |
import streamlit as st
|
| 13 |
import pandas as pd
|
| 14 |
from PIL import Image
|
| 15 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 16 |
from huggingface_hub.utils import HfHubHTTPError
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# =========================
|
| 19 |
# CONFIG
|
|
|
|
| 29 |
#Set this to the private dataset repo that acts as the "database"
|
| 30 |
DB_REPO_ID = os.getenv("DB_REPO_ID", "NidhiS09/VizWiz-submissions-db")
|
| 31 |
DB_REPO_TYPE = "dataset"
|
| 32 |
+
# print(DB_REPO_ID)
|
| 33 |
+
|
| 34 |
|
| 35 |
# This must exist as a Space Secret in the PUBLIC UI Space
|
| 36 |
SUBMISSIONS_TOKEN = os.getenv("SUBMISSIONS_TOKEN", "")
|
|
|
|
| 48 |
LEADERBOARD_METRICS = ["bbox_mAP", "bbox_AP50", "segm_mAP", "segm_AP50"]
|
| 49 |
DEFAULT_SORT_METRIC = "segm_AP50"
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# =========================
|
| 52 |
# HELPERS
|
| 53 |
# =========================
|
|
|
|
| 105 |
return True, "OK"
|
| 106 |
|
| 107 |
|
| 108 |
+
def _upload_json(api: HfApi, data: Dict[str, Any] | List[Any], path_in_repo: str) -> None:
|
| 109 |
+
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp:
|
| 110 |
+
json.dump(data, tmp, ensure_ascii=False)
|
| 111 |
+
tmp_path = tmp.name
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
api.upload_file(
|
| 115 |
+
path_or_fileobj=tmp_path,
|
| 116 |
+
path_in_repo=path_in_repo,
|
| 117 |
+
repo_id=DB_REPO_ID,
|
| 118 |
+
repo_type=DB_REPO_TYPE,
|
| 119 |
+
token=SUBMISSIONS_TOKEN,
|
| 120 |
+
commit_message=f"Add {path_in_repo}",
|
| 121 |
+
)
|
| 122 |
+
finally:
|
| 123 |
+
try:
|
| 124 |
+
os.remove(tmp_path)
|
| 125 |
+
except OSError:
|
| 126 |
+
pass
|
| 127 |
|
| 128 |
|
| 129 |
def _create_submission_record(
|
|
|
|
| 136 |
original_filename: str,
|
| 137 |
) -> str:
|
| 138 |
"""
|
| 139 |
+
Writes pred/meta/status to the private DB dataset repo.
|
| 140 |
Returns submission_id.
|
| 141 |
"""
|
| 142 |
_require_token()
|
| 143 |
+
api = HfApi()
|
| 144 |
|
| 145 |
submission_id = str(uuid.uuid4())
|
| 146 |
ts = int(time.time())
|
|
|
|
| 157 |
|
| 158 |
status = {"state": "queued", "timestamp": ts}
|
| 159 |
|
| 160 |
+
base = f"submissions/{submission_id}"
|
| 161 |
+
_upload_json(api, pred, f"{base}/pred.json")
|
| 162 |
+
_upload_json(api, meta, f"{base}/meta.json")
|
| 163 |
+
_upload_json(api, status, f"{base}/status.json")
|
| 164 |
|
| 165 |
return submission_id
|
| 166 |
|
|
|
|
| 222 |
# =========================
|
| 223 |
# UI
|
| 224 |
# =========================
|
| 225 |
+
#2 FUNCTIONS WERE HERE, RENDER_OVERVIEW AND RENDER EVAL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
def page_submit():
|
| 228 |
st.header("π Submit your Predictions")
|
|
|
|
| 340 |
|
| 341 |
|
| 342 |
def main():
|
| 343 |
+
|
| 344 |
+
# st.write("β
App booted and rendering UI")
|
| 345 |
+
|
| 346 |
st.sidebar.title("AI Benchmark Arena π")
|
| 347 |
|
| 348 |
# Warn early if DB repo isn't configured
|
|
|
|
| 364 |
else:
|
| 365 |
page_leaderboard()
|
| 366 |
|
| 367 |
+
def render_overview():
|
| 368 |
+
with st.expander("βΉοΈ Overview of the AI Benchmark Arena"):
|
| 369 |
+
st.markdown(
|
| 370 |
+
"""
|
| 371 |
+
**Note:** This Hugging Face Space queues submissions for evaluation and persists results in a private database repo.
|
| 372 |
+
"""
|
| 373 |
+
)
|
| 374 |
+
# Keep this optional so missing image doesn't crash the Space
|
| 375 |
+
try:
|
| 376 |
+
overview_image = Image.open("src/overview_image.png").resize((600, 600))
|
| 377 |
+
st.image(overview_image, caption="Example of an object localization task")
|
| 378 |
+
except Exception:
|
| 379 |
+
st.info("Overview image not found at src/overview_image.png (optional).")
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
def render_eval_details():
|
| 383 |
+
with st.expander("π How is the Score Calculated?"):
|
| 384 |
+
st.markdown(
|
| 385 |
+
"""
|
| 386 |
+
Your submission is evaluated offline by a private evaluator against hidden ground-truth annotations.
|
| 387 |
+
The leaderboard reports:
|
| 388 |
+
|
| 389 |
+
- bbox_mAP
|
| 390 |
+
- bbox_AP50
|
| 391 |
+
- segm_mAP
|
| 392 |
+
- segm_AP50 (default ranking)
|
| 393 |
+
|
| 394 |
+
Raw submissions are kept private; only scores and metadata are shown.
|
| 395 |
+
"""
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
|
| 399 |
if __name__ == "__main__":
|
| 400 |
main()
|