Spaces:
Sleeping
Sleeping
Paul Gavrikov
commited on
Commit
Β·
0e48243
1
Parent(s):
e476c3e
initial app
Browse files- README.md +3 -1
- app.py +77 -40
- eval_utils.py +1 -1
- ground_truth.json +1 -0
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: gray
|
|
@@ -7,6 +7,8 @@ sdk: gradio
|
|
| 7 |
sdk_version: 5.38.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: VisualOverload
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: gray
|
|
|
|
| 7 |
sdk_version: 5.38.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
hf_oauth: true
|
| 11 |
+
app_file: app.py
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,57 +1,94 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import json
|
| 3 |
-
import os
|
| 4 |
-
import uuid
|
| 5 |
from eval_utils import evaluate_submission
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
os.makedirs(SUBMISSION_DIR, exist_ok=True)
|
| 12 |
-
user_last_submission = {}
|
| 13 |
|
| 14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
username = request.username # Hugging Face username
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Evaluate
|
| 33 |
-
score = evaluate_submission(submission_data, ground_truth)
|
| 34 |
-
|
| 35 |
-
# Save submission
|
| 36 |
-
submission_id = str(uuid.uuid4())
|
| 37 |
-
with open(f"{SUBMISSION_DIR}/{submission_id}_{username}_{identifier}.json", "w") as f:
|
| 38 |
json.dump({
|
| 39 |
"username": username,
|
| 40 |
-
"identifier":
|
| 41 |
-
"score": score,
|
| 42 |
"timestamp": now,
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
submit_btn = gr.Button("Submit")
|
| 53 |
-
output = gr.Textbox()
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json, os, time, uuid
|
|
|
|
|
|
|
| 3 |
from eval_utils import evaluate_submission
|
| 4 |
+
from threading import Lock
|
| 5 |
|
| 6 |
+
# Setup
|
| 7 |
+
GROUND_TRUTH = "ground_truth.json"
|
| 8 |
+
SUB_DIR = "submissions"
|
| 9 |
+
RATE_LIMIT = 60
|
| 10 |
+
os.makedirs(SUB_DIR, exist_ok=True)
|
| 11 |
+
user_last = {}
|
| 12 |
+
lock = Lock()
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def show_form(profile: gr.OAuthProfile | None):
|
| 16 |
+
visible = profile is not None
|
| 17 |
+
welcome = (f"## π Welcome, {profile.username}! Ready to submit?"
|
| 18 |
+
if visible else "## π Please sign in to submit.")
|
| 19 |
+
return welcome, gr.Textbox(visible=visible) , gr.File(visible=visible), \
|
| 20 |
+
gr.Checkbox(visible=visible), gr.Button(visible=visible)
|
| 21 |
|
|
|
|
| 22 |
|
| 23 |
+
def submit(submission_id: str, submission_file: str, is_private: bool, profile: gr.OAuthProfile | None):
|
| 24 |
+
if not profile:
|
| 25 |
+
raise gr.Error("π Please sign in first.")
|
| 26 |
+
|
| 27 |
+
username = profile.username
|
| 28 |
+
with lock:
|
| 29 |
+
now = time.time()
|
| 30 |
+
if now - user_last.get(username, 0) < RATE_LIMIT:
|
| 31 |
+
remaining = int(RATE_LIMIT - (now - user_last[username]))
|
| 32 |
+
raise gr.Error(f"β±οΈ Wait {remaining}s before retrying.")
|
| 33 |
+
user_last[username] = now
|
| 34 |
|
| 35 |
+
with open(submission_file.name, "rb") as file:
|
| 36 |
+
prediction_json = json.load(file)
|
| 37 |
+
|
| 38 |
+
with open(GROUND_TRUTH, "rb") as file:
|
| 39 |
+
ground_truth_json = json.load(file)
|
| 40 |
+
|
| 41 |
+
score = evaluate_submission(prediction_json, ground_truth_json)
|
| 42 |
|
| 43 |
+
if not is_private:
|
| 44 |
+
sid = str(uuid.uuid4())
|
| 45 |
+
submission_record = f"{username}_{sid}_{submission_id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
json.dump({
|
| 47 |
"username": username,
|
| 48 |
+
"identifier": submission_id,
|
|
|
|
| 49 |
"timestamp": now,
|
| 50 |
+
"submission": prediction_json
|
| 51 |
+
}, open(os.path.join(SUB_DIR, submission_record + ".json"), "w"))
|
| 52 |
+
|
| 53 |
+
return gr.Text(f"{score:.4f}", visible=True)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def get_leaderboard():
|
| 57 |
+
records = []
|
| 58 |
+
# for fn in os.listdir(SUB_DIR):
|
| 59 |
+
# r = json.load(open(os.path.join(SUB_DIR, fn)))
|
| 60 |
+
# if not r.get("private", False):
|
| 61 |
+
# records.append(r)
|
| 62 |
+
records.sort(key=lambda x: -x["score"])
|
| 63 |
+
if not records:
|
| 64 |
+
return "**No submissions yet**"
|
| 65 |
+
lines = ["| User | Score | Time |", "|---|---|---|"]
|
| 66 |
+
for r in records[:10]:
|
| 67 |
+
t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(r["timestamp"]))
|
| 68 |
+
lines.append(f"| {r['username']} | {r['score']:.4f} | {t} |")
|
| 69 |
+
return "\n".join(lines)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.LoginButton()
|
| 74 |
|
| 75 |
+
welcome_md = gr.Markdown("Please sign in to be able to submit.")
|
| 76 |
+
submission_file = gr.File(label="Prediction (.json)", visible=False)
|
| 77 |
+
submission_id = gr.Textbox(label="Submission identifier", visible=False)
|
| 78 |
+
private_flag = gr.Checkbox(label="Do not save my submission", value=False, visible=False)
|
| 79 |
+
submit_btn = gr.Button("Submit", visible=False)
|
| 80 |
+
result = gr.Textbox(label="β
Your score", visible=False)
|
| 81 |
+
leaderboard_heading_md = gr.Markdown("## π Public Leaderboard")
|
| 82 |
+
leaderboard_md = gr.Markdown(get_leaderboard())
|
| 83 |
|
| 84 |
+
# Load login state β show/hide components
|
| 85 |
+
demo.load(fn=show_form,
|
| 86 |
+
inputs=[],
|
| 87 |
+
outputs=[welcome_md, submission_id, submission_file, private_flag, submit_btn])
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Submit button only shown post-login
|
| 90 |
+
submit_btn.click(fn=submit,
|
| 91 |
+
inputs=[submission_id, submission_file, private_flag],
|
| 92 |
+
outputs=result)
|
| 93 |
|
| 94 |
demo.launch()
|
eval_utils.py
CHANGED
|
@@ -6,4 +6,4 @@ def evaluate_submission(preds, ground_truth):
|
|
| 6 |
if key in preds and preds[key] == ground_truth[key]:
|
| 7 |
correct += 1
|
| 8 |
|
| 9 |
-
return correct / total
|
|
|
|
| 6 |
if key in preds and preds[key] == ground_truth[key]:
|
| 7 |
correct += 1
|
| 8 |
|
| 9 |
+
return correct / total if total > 0 else 0.0
|
ground_truth.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{}
|
requirements.txt
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
gradio
|
|
|
|
| 1 |
+
gradio==5.38.1
|