Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,19 +11,24 @@ from huggingface_hub import HfApi
|
|
| 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
|
| 21 |
LABEL_NONBLAST = os.getenv("LABEL_NONBLAST", "NON-BLAST")
|
| 22 |
LABEL_BLAST = os.getenv("LABEL_BLAST", "BLAST")
|
| 23 |
LABEL_UNCERTAIN = os.getenv("LABEL_UNCERTAIN", "UNCERTAIN")
|
| 24 |
LABEL_TRASH = os.getenv("LABEL_TRASH", "LOW_QUALITY")
|
| 25 |
|
| 26 |
-
# (Optional) Hugging Face sync
|
| 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,8 +58,12 @@ def rel_to_image_dir(p: str):
|
|
| 53 |
return p
|
| 54 |
|
| 55 |
def write_label(image_path: str, label: str, annotator: str):
|
| 56 |
-
"""Append one row to labels.csv."""
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
record = {
|
| 59 |
"image": rel_to_image_dir(image_path),
|
| 60 |
"label": label,
|
|
@@ -65,8 +74,18 @@ def write_label(image_path: str, label: str, annotator: str):
|
|
| 65 |
exists = os.path.exists(LABELS_CSV)
|
| 66 |
pd.DataFrame([record]).to_csv(LABELS_CSV, mode="a", header=not exists, index=False)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# --------------- UI ---------------------
|
| 72 |
st.title("🏷️ Blast Cell Labeling App")
|
|
@@ -93,24 +112,6 @@ with st.sidebar:
|
|
| 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,4 +197,4 @@ else:
|
|
| 196 |
st.rerun()
|
| 197 |
|
| 198 |
st.divider()
|
| 199 |
-
st.caption("Labels are
|
|
|
|
| 11 |
|
| 12 |
# ---------------- Config ----------------
|
| 13 |
IMAGE_DIR = os.getenv("IMAGE_DIR", "images")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
SUPPORTED_EXTS = (".png", ".jpg", ".jpeg", ".bmp", ".gif", ".tif", ".tiff", ".webp")
|
| 15 |
|
| 16 |
+
# pick a writable place for labels.csv
|
| 17 |
+
DATA_DIR = "/data"
|
| 18 |
+
DEFAULT_LABELS = (
|
| 19 |
+
f"{DATA_DIR}/labels.csv"
|
| 20 |
+
if os.path.isdir(DATA_DIR) and os.access(DATA_DIR, os.W_OK)
|
| 21 |
+
else "labels.csv"
|
| 22 |
+
)
|
| 23 |
+
LABELS_CSV = os.getenv("LABELS_CSV", DEFAULT_LABELS)
|
| 24 |
+
|
| 25 |
# Label names
|
| 26 |
LABEL_NONBLAST = os.getenv("LABEL_NONBLAST", "NON-BLAST")
|
| 27 |
LABEL_BLAST = os.getenv("LABEL_BLAST", "BLAST")
|
| 28 |
LABEL_UNCERTAIN = os.getenv("LABEL_UNCERTAIN", "UNCERTAIN")
|
| 29 |
LABEL_TRASH = os.getenv("LABEL_TRASH", "LOW_QUALITY")
|
| 30 |
|
| 31 |
+
# (Optional) Hugging Face sync
|
| 32 |
HF_TOKEN = os.getenv("HF_TOKEN") # set as a Secret in Space settings
|
| 33 |
SPACE_ID = os.getenv("SPACE_ID", "MichaelDeutges/LabelingTest") # set to your Space id
|
| 34 |
api = HfApi()
|
|
|
|
| 58 |
return p
|
| 59 |
|
| 60 |
def write_label(image_path: str, label: str, annotator: str):
|
| 61 |
+
"""Append one row to labels.csv and (optionally) push to the Space repo."""
|
| 62 |
+
# only create a directory if LABELS_CSV actually has one, and it's not already there
|
| 63 |
+
dirpath = os.path.dirname(LABELS_CSV)
|
| 64 |
+
if dirpath and not os.path.isdir(dirpath):
|
| 65 |
+
os.makedirs(dirpath, exist_ok=True)
|
| 66 |
+
|
| 67 |
record = {
|
| 68 |
"image": rel_to_image_dir(image_path),
|
| 69 |
"label": label,
|
|
|
|
| 74 |
exists = os.path.exists(LABELS_CSV)
|
| 75 |
pd.DataFrame([record]).to_csv(LABELS_CSV, mode="a", header=not exists, index=False)
|
| 76 |
|
| 77 |
+
# best-effort sync to Hugging Face repo (safe to skip if no token/space id)
|
| 78 |
+
if HF_TOKEN and SPACE_ID:
|
| 79 |
+
try:
|
| 80 |
+
api.upload_file(
|
| 81 |
+
path_or_fileobj=LABELS_CSV,
|
| 82 |
+
path_in_repo="labels.csv",
|
| 83 |
+
repo_id=SPACE_ID,
|
| 84 |
+
repo_type="space",
|
| 85 |
+
token=HF_TOKEN
|
| 86 |
+
)
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print("Upload failed:", e)
|
| 89 |
|
| 90 |
# --------------- UI ---------------------
|
| 91 |
st.title("🏷️ Blast Cell Labeling App")
|
|
|
|
| 112 |
help="Continue where you left off, based on your name in labels.csv",
|
| 113 |
)
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# session state
|
| 116 |
st.session_state.setdefault("order", [])
|
| 117 |
st.session_state.setdefault("idx", 0)
|
|
|
|
| 197 |
st.rerun()
|
| 198 |
|
| 199 |
st.divider()
|
| 200 |
+
st.caption(f"Labels are saved to `{LABELS_CSV}` (and synced to this Space if HF_TOKEN is set).")
|