Spaces:
Sleeping
Sleeping
File size: 10,619 Bytes
e3e0b69 f48260f e3e0b69 ef0959f f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f e3e0b69 f48260f 6b20de6 f48260f 7811d4c f48260f 1fd1f1d f48260f 7811d4c 6b20de6 f48260f 7811d4c f48260f 7811d4c f48260f 7811d4c f48260f 7811d4c e3e0b69 f48260f 7811d4c 620aa52 c8109a0 d363c2f c8109a0 f48260f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
import gradio as gr
import pandas as pd
from pathlib import Path
from typing import Dict, List, Tuple
import os
import base64
PROCESSED_DATA_DIR = Path(".")
# Embed logo as a base64 data URI to avoid Gradio toolbar interactions
logo_path = "rowsquared-logo-large.png"
with open(logo_path, "rb") as f:
logo_b64 = base64.b64encode(f.read()).decode("utf-8")
# ----------------------------
# Data loading & preprocessing
# ----------------------------
df_isco = (
pd.read_excel(
PROCESSED_DATA_DIR / "isco_imperfect.xlsx",
converters={"major": str, "sub_major": str, "minor": str, "unit": str},
)[["major_label", "sub_major_label", "minor_label", "unit_label"]]
.dropna()
.drop_duplicates()
.reset_index(drop=True)
)
# Build nested hierarchy dict: {major: {sub: {minor: [units]}}}
hierarchy: Dict[str, Dict[str, Dict[str, List[str]]]] = {}
for _, r in df_isco.iterrows():
hierarchy.setdefault(r.major_label, {}) \
.setdefault(r.sub_major_label, {}) \
.setdefault(r.minor_label, []) \
.append(r.unit_label)
# Ensure uniqueness & sorting at leaf lists
for maj in hierarchy:
for sub in hierarchy[maj]:
for mn in hierarchy[maj][sub]:
hierarchy[maj][sub][mn] = sorted(list(dict.fromkeys(hierarchy[maj][sub][mn])))
# Fast helpers for children
def majors() -> List[str]:
return sorted(hierarchy.keys())
def submajors(maj: str) -> List[str]:
return sorted(hierarchy.get(maj, {}).keys())
def minors(maj: str, sub: str) -> List[str]:
return sorted(hierarchy.get(maj, {}).get(sub, {}).keys())
def units(maj: str, sub: str, mn: str) -> List[str]:
return hierarchy.get(maj, {}).get(sub, {}).get(mn, [])
# ----------------------------
# Records to annotate
# ----------------------------
records = pd.read_excel(PROCESSED_DATA_DIR / "isco_predictions.xlsx").copy()
for col in ["major_label", "sub_major_label", "minor_label", "unit_label"]:
if col not in records:
records[col] = ""
if "annotated" not in records:
records["annotated"] = False
# ensure not views
for col in ["major_label", "sub_major_label", "minor_label", "unit_label", "annotated"]:
records[col] = records[col].copy()
records.reset_index(drop=True, inplace=True)
# -----------------------------------
# Core logic: clamp & state management
# -----------------------------------
def clamp_path(maj: str, sub: str, mn: str, un: str
) -> Tuple[str, str, str, str, List[str], List[str], List[str], List[str]]:
"""Return a valid (maj, sub, mn, un) tuple + their choices lists.
Only replace a level if it's invalid for the hierarchy."""
maj_choices = majors()
if maj not in maj_choices:
maj = maj_choices[0] if maj_choices else ""
sub_choices = submajors(maj) if maj else []
if sub not in sub_choices:
sub = sub_choices[0] if sub_choices else ""
mn_choices = minors(maj, sub) if sub else []
if mn not in mn_choices:
mn = mn_choices[0] if mn_choices else ""
un_choices = units(maj, sub, mn) if mn else []
if un not in un_choices:
un = un_choices[0] if un_choices else ""
return maj, sub, mn, un, maj_choices, sub_choices, mn_choices, un_choices
def save_record(i: int, maj: str, sub: str, mn: str, un: str) -> None:
records.loc[i, ["major_label", "sub_major_label", "minor_label", "unit_label"]] = [maj, sub, mn, un]
records.loc[i, "annotated"] = True
def status_text(i: int) -> str:
return f"**Status**: {'β
Annotated' if records.loc[i, 'annotated'] else 'β Not Annotated'}"
def load_record(i: int):
rec = records.loc[i]
maj, sub, mn, un, maj_c, sub_c, mn_c, un_c = clamp_path(
rec["major_label"], rec["sub_major_label"], rec["minor_label"], rec["unit_label"]
)
# Persist clamped values back (only if changed)
save_record(i, maj, sub, mn, un)
record_md = f"## Occupation: {rec['occupation_title_main']}\n## Industry: {rec['industry_title_main']}"
return (
record_md,
status_text(i),
gr.update(choices=maj_c, value=maj),
gr.update(choices=sub_c, value=sub),
gr.update(choices=mn_c, value=mn),
gr.update(choices=un_c, value=un),
)
# ---------------------
# Event handler helpers
# ---------------------
def on_major_change(new_major: str, i: int):
sub_c = submajors(new_major)
sub = sub_c[0] if sub_c else ""
mn_c = minors(new_major, sub) if sub else []
mn = mn_c[0] if mn_c else ""
un_c = units(new_major, sub, mn) if mn else []
un = un_c[0] if un_c else ""
save_record(i, new_major, sub, mn, un)
return (
gr.update(choices=majors(), value=new_major),
gr.update(choices=sub_c, value=sub),
gr.update(choices=mn_c, value=mn),
gr.update(choices=un_c, value=un),
status_text(i),
)
def on_sub_change(new_sub: str, i: int, major: str):
mn_c = minors(major, new_sub)
mn = mn_c[0] if mn_c else ""
un_c = units(major, new_sub, mn) if mn else []
un = un_c[0] if un_c else ""
records.loc[i, ["sub_major_label", "minor_label", "unit_label"]] = [new_sub, mn, un]
records.loc[i, "annotated"] = True
return (
gr.update(choices=submajors(major), value=new_sub),
gr.update(choices=mn_c, value=mn),
gr.update(choices=un_c, value=un),
status_text(i),
)
def on_minor_change(new_minor: str, i: int, major: str, sub: str):
un_c = units(major, sub, new_minor)
un = un_c[0] if un_c else ""
records.loc[i, ["minor_label", "unit_label"]] = [new_minor, un]
records.loc[i, "annotated"] = True
return (
gr.update(choices=minors(major, sub), value=new_minor),
gr.update(choices=un_c, value=un),
status_text(i),
)
def on_unit_change(new_unit: str, i: int, major: str, sub: str, mn: str):
un_c = units(major, sub, mn)
if new_unit not in un_c:
new_unit = un_c[0] if un_c else ""
records.loc[i, "unit_label"] = new_unit
records.loc[i, "annotated"] = True
return gr.update(choices=un_c, value=new_unit), status_text(i)
def go_next(i: int) -> int:
return (i + 1) % len(records)
def go_prev(i: int) -> int:
return (i - 1) % len(records)
# ---- NAVIGATION: save + move + reload in ONE callback ----
def save_and_jump(i: int, direction: str):
# Final safety net: clamp and persist whatever is currently stored
rec = records.loc[i]
maj, sub, mn, un, *_ = clamp_path(
rec["major_label"], rec["sub_major_label"], rec["minor_label"], rec["unit_label"]
)
save_record(i, maj, sub, mn, un)
new_i = go_next(i) if direction == "next" else go_prev(i)
return (new_i,) + load_record(new_i)
def download_annotations() -> str:
path = PROCESSED_DATA_DIR / "annotated_output.csv"
records.to_csv(path, index=False)
return str(path)
# --------------
# Build the UI
# --------------
def build_gradio_app():
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=1):
# Static logo, non-interactive
gr.HTML(
f'<img src="data:image/png;base64,{logo_b64}" width="200" style="pointer-events:none; user-select:none; display:block;" />'
)
with gr.Row():
gr.Markdown("# ISCO Annotation", elem_id="isco-title")
gr.HTML("""
<style>
#isco-title {
text-align: center;
width: 100%;
margin: 0.5em 0;
}
footer { display: none !important; }
.gradio-container .api-link, .gradio-container .share-link { display: none !important; }
</style>
""")
idx_state = gr.State(0)
with gr.Group():
record_md = gr.Markdown()
status_md = gr.Markdown()
with gr.Row():
prev_btn = gr.Button("β¬
Previous")
next_btn = gr.Button("β
Next")
with gr.Row():
with gr.Column():
major_radio = gr.Radio(label="Level 1: Major", choices=[], interactive=True)
with gr.Column():
sub_radio = gr.Radio(label="Level 2: Sub-major", choices=[], interactive=True)
with gr.Column():
minor_radio = gr.Radio(label="Level 3: Minor", choices=[], interactive=True)
with gr.Column():
unit_radio = gr.Radio(label="Level 4: Unit", choices=[], interactive=True)
download_btn = gr.Button("π₯ Download Annotations")
download_file = gr.File(label="Annotated CSV", visible=False)
# Initial load
demo.load(
lambda: (0,) + load_record(0),
outputs=[idx_state, record_md, status_md, major_radio, sub_radio, minor_radio, unit_radio],
)
next_btn.click(lambda i: save_and_jump(i, "next"),
inputs=[idx_state],
outputs=[idx_state, record_md, status_md, major_radio, sub_radio, minor_radio, unit_radio])
prev_btn.click(lambda i: save_and_jump(i, "prev"),
inputs=[idx_state],
outputs=[idx_state, record_md, status_md, major_radio, sub_radio, minor_radio, unit_radio])
# Change handlers (also update status)
major_radio.change(
on_major_change,
inputs=[major_radio, idx_state],
outputs=[major_radio, sub_radio, minor_radio, unit_radio, status_md],
)
sub_radio.change(
on_sub_change,
inputs=[sub_radio, idx_state, major_radio],
outputs=[sub_radio, minor_radio, unit_radio, status_md],
)
minor_radio.change(
on_minor_change,
inputs=[minor_radio, idx_state, major_radio, sub_radio],
outputs=[minor_radio, unit_radio, status_md],
)
unit_radio.change(
on_unit_change,
inputs=[unit_radio, idx_state, major_radio, sub_radio, minor_radio],
outputs=[unit_radio, status_md],
)
# Download
download_btn.click(download_annotations, outputs=[download_file]).then(
lambda: gr.update(visible=True), None, [download_file]
)
return demo
if __name__=="__main__":
demo = build_gradio_app()
demo.queue().launch(
show_api=False,
ssr_mode=False, # β disable experimental SSR
auth=(os.getenv("APP_USER",""), os.getenv("APP_PASS","")),
server_name="0.0.0.0", # optional, but explicit
server_port=int(os.getenv("PORT", 7860)),
)
|