Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import glob
|
| 5 |
from pathlib import Path
|
|
@@ -13,7 +11,10 @@ from huggingface_hub import HfApi
|
|
| 13 |
|
| 14 |
# ---------------- Config ----------------
|
| 15 |
IMAGE_DIR = os.getenv("IMAGE_DIR", "images")
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
SUPPORTED_EXTS = (".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tif", ".tiff", ".webp")
|
| 18 |
|
| 19 |
# Label names
|
|
@@ -22,7 +23,7 @@ LABEL_BLAST = os.getenv("LABEL_BLAST", "BLAST")
|
|
| 22 |
LABEL_UNCERTAIN = os.getenv("LABEL_UNCERTAIN", "UNCERTAIN")
|
| 23 |
LABEL_TRASH = os.getenv("LABEL_TRASH", "LOW_QUALITY")
|
| 24 |
|
| 25 |
-
# (Optional) Hugging Face sync
|
| 26 |
HF_TOKEN = os.getenv("HF_TOKEN") # set as a Secret in Space settings
|
| 27 |
SPACE_ID = os.getenv("SPACE_ID", "MichaelDeutges/LabelingTest") # set to your Space id
|
| 28 |
api = HfApi()
|
|
@@ -52,7 +53,7 @@ def rel_to_image_dir(p: str):
|
|
| 52 |
return p
|
| 53 |
|
| 54 |
def write_label(image_path: str, label: str, annotator: str):
|
| 55 |
-
"""Append one row to labels.csv
|
| 56 |
os.makedirs(os.path.dirname(LABELS_CSV) or ".", exist_ok=True)
|
| 57 |
record = {
|
| 58 |
"image": rel_to_image_dir(image_path),
|
|
@@ -64,18 +65,8 @@ def write_label(image_path: str, label: str, annotator: str):
|
|
| 64 |
exists = os.path.exists(LABELS_CSV)
|
| 65 |
pd.DataFrame([record]).to_csv(LABELS_CSV, mode="a", header=not exists, index=False)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
try:
|
| 70 |
-
api.upload_file(
|
| 71 |
-
path_or_fileobj=LABELS_CSV,
|
| 72 |
-
path_in_repo="labels.csv",
|
| 73 |
-
repo_id=SPACE_ID,
|
| 74 |
-
repo_type="space",
|
| 75 |
-
token=HF_TOKEN
|
| 76 |
-
)
|
| 77 |
-
except Exception as e:
|
| 78 |
-
print("Upload failed:", e)
|
| 79 |
|
| 80 |
# --------------- UI ---------------------
|
| 81 |
st.title("🏷️ Blast Cell Labeling App")
|
|
@@ -102,6 +93,24 @@ with st.sidebar:
|
|
| 102 |
help="Continue where you left off, based on your name in labels.csv",
|
| 103 |
)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# session state
|
| 106 |
st.session_state.setdefault("order", [])
|
| 107 |
st.session_state.setdefault("idx", 0)
|
|
@@ -187,5 +196,4 @@ else:
|
|
| 187 |
st.rerun()
|
| 188 |
|
| 189 |
st.divider()
|
| 190 |
-
st.caption("Labels are
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import glob
|
| 3 |
from pathlib import Path
|
|
|
|
| 11 |
|
| 12 |
# ---------------- Config ----------------
|
| 13 |
IMAGE_DIR = os.getenv("IMAGE_DIR", "images")
|
| 14 |
+
|
| 15 |
+
# ✅ write to the Space's persistent volume (no rebuilds)
|
| 16 |
+
LABELS_CSV = os.getenv("LABELS_CSV", "/data/labels.csv")
|
| 17 |
+
|
| 18 |
SUPPORTED_EXTS = (".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tif", ".tiff", ".webp")
|
| 19 |
|
| 20 |
# Label names
|
|
|
|
| 23 |
LABEL_UNCERTAIN = os.getenv("LABEL_UNCERTAIN", "UNCERTAIN")
|
| 24 |
LABEL_TRASH = os.getenv("LABEL_TRASH", "LOW_QUALITY")
|
| 25 |
|
| 26 |
+
# (Optional) Hugging Face sync (manual button below)
|
| 27 |
HF_TOKEN = os.getenv("HF_TOKEN") # set as a Secret in Space settings
|
| 28 |
SPACE_ID = os.getenv("SPACE_ID", "MichaelDeutges/LabelingTest") # set to your Space id
|
| 29 |
api = HfApi()
|
|
|
|
| 53 |
return p
|
| 54 |
|
| 55 |
def write_label(image_path: str, label: str, annotator: str):
|
| 56 |
+
"""Append one row to labels.csv."""
|
| 57 |
os.makedirs(os.path.dirname(LABELS_CSV) or ".", exist_ok=True)
|
| 58 |
record = {
|
| 59 |
"image": rel_to_image_dir(image_path),
|
|
|
|
| 65 |
exists = os.path.exists(LABELS_CSV)
|
| 66 |
pd.DataFrame([record]).to_csv(LABELS_CSV, mode="a", header=not exists, index=False)
|
| 67 |
|
| 68 |
+
# ❌ removed auto upload to the Space (this caused rebuilds)
|
| 69 |
+
# If you want to sync, use the manual button in the sidebar.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# --------------- UI ---------------------
|
| 72 |
st.title("🏷️ Blast Cell Labeling App")
|
|
|
|
| 93 |
help="Continue where you left off, based on your name in labels.csv",
|
| 94 |
)
|
| 95 |
|
| 96 |
+
st.markdown("---")
|
| 97 |
+
# ✅ Optional manual sync to Space Files tab
|
| 98 |
+
if st.button("⬆️ Sync labels.csv to Space Files"):
|
| 99 |
+
if HF_TOKEN and SPACE_ID and os.path.exists(LABELS_CSV):
|
| 100 |
+
try:
|
| 101 |
+
api.upload_file(
|
| 102 |
+
path_or_fileobj=LABELS_CSV,
|
| 103 |
+
path_in_repo="labels.csv",
|
| 104 |
+
repo_id=SPACE_ID,
|
| 105 |
+
repo_type="space",
|
| 106 |
+
token=HF_TOKEN
|
| 107 |
+
)
|
| 108 |
+
st.success("Synced labels.csv to Space Files.")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
st.error(f"Sync failed: {e}")
|
| 111 |
+
else:
|
| 112 |
+
st.warning("Set HF_TOKEN & SPACE_ID and ensure labels.csv exists.")
|
| 113 |
+
|
| 114 |
# session state
|
| 115 |
st.session_state.setdefault("order", [])
|
| 116 |
st.session_state.setdefault("idx", 0)
|
|
|
|
| 196 |
st.rerun()
|
| 197 |
|
| 198 |
st.divider()
|
| 199 |
+
st.caption("Labels are written to `/data/labels.csv`. Use **Sync** in the sidebar to push to the Space Files.")
|
|
|