Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
|
| 2 |
# force rebuild 1
|
| 3 |
import os
|
|
@@ -42,9 +43,10 @@ def cosine(a: np.ndarray, b: np.ndarray) -> float:
|
|
| 42 |
return float(np.clip(np.dot(a, b), -1.0, 1.0))
|
| 43 |
|
| 44 |
def score_percent(a: np.ndarray, b: np.ndarray) -> float:
|
| 45 |
-
sim = cosine(a, b)
|
| 46 |
return float(np.clip(sim * 100.0, 0.0, 100.0))
|
| 47 |
|
|
|
|
| 48 |
# Map cosine similarity (-1..1) into a more intuitive 0..100,
|
| 49 |
# but for images you'll usually be in ~0.15..0.95 range.
|
| 50 |
sim = cosine(a, b)
|
|
@@ -144,10 +146,10 @@ def gallery_items(conn, challenge_id: int, limit: int = 24):
|
|
| 144 |
|
| 145 |
# --- Gradio App ---
|
| 146 |
def teacher_panel(admin_key: str, title: str, target_img: Image.Image):
|
| 147 |
-
|
| 148 |
-
if admin_key != required:
|
| 149 |
return gr.update(value="❌ Wrong admin key."), None, None, None
|
| 150 |
|
|
|
|
| 151 |
if not title or target_img is None:
|
| 152 |
return gr.update(value="❌ Please provide a title and a target image."), None, None, None
|
| 153 |
|
|
@@ -159,10 +161,9 @@ def teacher_panel(admin_key: str, title: str, target_img: Image.Image):
|
|
| 159 |
return gr.update(value=msg), ch, ch[1], target_embed
|
| 160 |
|
| 161 |
def teacher_reset(admin_key: str):
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
if admin_key != required:
|
| 165 |
return gr.update(value="❌ Wrong admin key."), None, None, None
|
|
|
|
| 166 |
conn = db()
|
| 167 |
msg = reset_all(conn)
|
| 168 |
return gr.update(value=msg), None, "", None
|
|
@@ -230,7 +231,11 @@ with gr.Blocks(title="TwinClass: Image Likeness Leaderboard") as demo:
|
|
| 230 |
demo.load(refresh, outputs=[target, ch_label, challenge_id, target_embed, leaderboard, gallery])
|
| 231 |
|
| 232 |
with gr.Tab("Teacher Admin"):
|
| 233 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
admin_key = gr.Textbox(label="Admin key", type="password", placeholder="Enter admin key")
|
| 235 |
title = gr.Textbox(label="Challenge title", placeholder="e.g., Neon mirror eagles", max_lines=1)
|
| 236 |
target_img = gr.Image(label="Upload target image", type="pil")
|
|
|
|
| 1 |
+
ADMIN_KEY = "teacher123"
|
| 2 |
|
| 3 |
# force rebuild 1
|
| 4 |
import os
|
|
|
|
| 43 |
return float(np.clip(np.dot(a, b), -1.0, 1.0))
|
| 44 |
|
| 45 |
def score_percent(a: np.ndarray, b: np.ndarray) -> float:
|
| 46 |
+
sim = cosine(a, b) # usually ~0.5–1.0 for similar images
|
| 47 |
return float(np.clip(sim * 100.0, 0.0, 100.0))
|
| 48 |
|
| 49 |
+
|
| 50 |
# Map cosine similarity (-1..1) into a more intuitive 0..100,
|
| 51 |
# but for images you'll usually be in ~0.15..0.95 range.
|
| 52 |
sim = cosine(a, b)
|
|
|
|
| 146 |
|
| 147 |
# --- Gradio App ---
|
| 148 |
def teacher_panel(admin_key: str, title: str, target_img: Image.Image):
|
| 149 |
+
if admin_key != ADMIN_KEY:
|
|
|
|
| 150 |
return gr.update(value="❌ Wrong admin key."), None, None, None
|
| 151 |
|
| 152 |
+
|
| 153 |
if not title or target_img is None:
|
| 154 |
return gr.update(value="❌ Please provide a title and a target image."), None, None, None
|
| 155 |
|
|
|
|
| 161 |
return gr.update(value=msg), ch, ch[1], target_embed
|
| 162 |
|
| 163 |
def teacher_reset(admin_key: str):
|
| 164 |
+
if admin_key != ADMIN_KEY:
|
|
|
|
|
|
|
| 165 |
return gr.update(value="❌ Wrong admin key."), None, None, None
|
| 166 |
+
|
| 167 |
conn = db()
|
| 168 |
msg = reset_all(conn)
|
| 169 |
return gr.update(value=msg), None, "", None
|
|
|
|
| 231 |
demo.load(refresh, outputs=[target, ch_label, challenge_id, target_embed, leaderboard, gallery])
|
| 232 |
|
| 233 |
with gr.Tab("Teacher Admin"):
|
| 234 |
+
gr.Markdown(
|
| 235 |
+
"### Teacher: set the target image\n"
|
| 236 |
+
"Enter the **teacher admin key** to create or reset a challenge."
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
admin_key = gr.Textbox(label="Admin key", type="password", placeholder="Enter admin key")
|
| 240 |
title = gr.Textbox(label="Challenge title", placeholder="e.g., Neon mirror eagles", max_lines=1)
|
| 241 |
target_img = gr.Image(label="Upload target image", type="pil")
|