Nightfury16 commited on
Commit
081c164
·
1 Parent(s): 5d8078a

Initial commit

Browse files
Files changed (2) hide show
  1. app.py +16 -28
  2. requirements.txt +3 -2
app.py CHANGED
@@ -1,7 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import requests
4
- import os
5
  import csv
6
  import threading
7
  from io import BytesIO
@@ -27,9 +37,8 @@ ROOM_CLASSES = ["living_room", "bedroom", "kitchen", "bathroom", "dining_room",
27
 
28
  def sync_pull():
29
  if not HF_TOKEN:
30
- print("⚠️ HF_TOKEN not found. Data will not be saved to dataset.")
31
  return
32
-
33
  print(f"🔄 Syncing from {DATASET_REPO_ID}...")
34
  for filename in ["annotations.csv", "verifications.csv", "skipped.csv"]:
35
  try:
@@ -46,7 +55,6 @@ def sync_pull():
46
 
47
  def sync_push_background(local_path, remote_filename):
48
  if not HF_TOKEN: return
49
-
50
  def _push():
51
  try:
52
  api = HfApi(token=HF_TOKEN)
@@ -60,13 +68,10 @@ def sync_push_background(local_path, remote_filename):
60
  print(f"☁️ Synced {remote_filename}")
61
  except Exception as e:
62
  print(f"❌ Sync failed: {e}")
63
-
64
- thread = threading.Thread(target=_push)
65
- thread.start()
66
 
67
  def init_files():
68
  sync_pull()
69
-
70
  for f in [LABEL_FILE, VERIFY_FILE, SKIP_FILE]:
71
  if not os.path.exists(f):
72
  if f == LABEL_FILE: cols = ["timestamp", "user", "group_id", "url", "score", "label"]
@@ -75,7 +80,7 @@ def init_files():
75
  pd.DataFrame(columns=cols).to_csv(f, index=False)
76
 
77
  if not os.path.exists(URL_FILE):
78
- print("⚠️ urls.txt not found! Please upload it.")
79
 
80
  init_files()
81
 
@@ -167,7 +172,6 @@ def render_workspace(mode, history, specific_index=None, move_back=False):
167
  found = False
168
  for i, gid in enumerate(all_groups):
169
  if gid in s_done: continue
170
-
171
  is_ready = False
172
  if mode == "label" and gid not in l_done: is_ready = True
173
  elif mode == "verify" and gid in l_done and gid not in v_done: is_ready = True
@@ -182,7 +186,6 @@ def render_workspace(mode, history, specific_index=None, move_back=False):
182
  return {screen_menu: gr.update(visible=True), screen_work: gr.update(visible=False), log_box: "No more tasks found."}
183
 
184
  urls = get_group_urls(target_gid)
185
-
186
  if not history or history[-1] != target_gid:
187
  history.append(target_gid)
188
 
@@ -195,7 +198,7 @@ def render_workspace(mode, history, specific_index=None, move_back=False):
195
  for future in futures:
196
  processed_images[futures[future]] = future.result()
197
 
198
- header = f"# Property #{target_idx + 1} <span style='font-size:14px;color:gray;'>(ID: {target_gid})</span>"
199
 
200
  updates = {
201
  screen_menu: gr.update(visible=False),
@@ -216,7 +219,6 @@ def render_workspace(mode, history, specific_index=None, move_back=False):
216
  if i < len(urls):
217
  u = urls[i]
218
  img_data = processed_images[i]
219
-
220
  updates[img_c] = gr.update(value=img_data, visible=True)
221
  v_sc = saved_vals.get(u, {}).get('score', 5)
222
  v_lbl = saved_vals.get(u, {}).get('label', ROOM_CLASSES[0])
@@ -239,7 +241,6 @@ def render_workspace(mode, history, specific_index=None, move_back=False):
239
  updates[c_drp] = gr.update(visible=False)
240
  updates[c_chk] = gr.update(visible=False)
241
  updates[c_lbl] = gr.update(visible=False)
242
-
243
  return updates
244
 
245
  def save_data(mode, history, urls, *args):
@@ -247,7 +248,6 @@ def save_data(mode, history, urls, *args):
247
  gid = history[-1]
248
  ts = datetime.now().isoformat()
249
  rows = []
250
-
251
  for i, u in enumerate(urls):
252
  base = i * 4
253
  sc, lbl, chk = args[base], args[base+1], args[base+2]
@@ -255,14 +255,11 @@ def save_data(mode, history, urls, *args):
255
  else: rows.append([ts, "user", gid, u, chk, lbl])
256
 
257
  fname = LABEL_FILE if mode == "label" else VERIFY_FILE
258
-
259
  with FileLock(LOCK_FILE):
260
  with open(fname, "a", newline="") as f:
261
  csv.writer(f).writerows(rows)
262
-
263
  remote_filename = os.path.basename(fname)
264
  sync_push_background(fname, remote_filename)
265
-
266
  return render_workspace(mode, history)
267
 
268
  def skip_group(idx, history, mode):
@@ -272,7 +269,6 @@ def skip_group(idx, history, mode):
272
  with open(SKIP_FILE, "a", newline="") as f:
273
  csv.writer(f).writerow([datetime.now().isoformat(), "user", gid])
274
  sync_push_background(SKIP_FILE, "skipped.csv")
275
-
276
  return render_workspace(mode, history, specific_index=idx + 1)
277
 
278
  def refresh_cat():
@@ -281,7 +277,6 @@ def refresh_cat():
281
  except: l_set = set()
282
  try: v_set = set(pd.read_csv(VERIFY_FILE)['group_id'].unique())
283
  except: v_set = set()
284
-
285
  data = []
286
  for i, gid in enumerate(all_gids):
287
  s = "⚪ Pending"
@@ -291,7 +286,6 @@ def refresh_cat():
291
  return pd.DataFrame(data, columns=["#", "Status", "ID"])
292
 
293
  with gr.Blocks(theme=gr.themes.Soft(), title="Labeling Tool") as demo:
294
-
295
  state_mode = gr.State("label")
296
  state_hist = gr.State([])
297
  state_urls = gr.State([])
@@ -305,7 +299,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Labeling Tool") as demo:
305
  with gr.Tab("Workspace", id=0):
306
  with gr.Group() as screen_menu:
307
  gr.Markdown("# Welcome! 👋")
308
- gr.Markdown("Connected to HF Dataset for free persistence.")
309
  with gr.Row():
310
  b_start_l = gr.Button("Start Labeling", variant="primary")
311
  b_start_v = gr.Button("Start Verification")
@@ -313,7 +307,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Labeling Tool") as demo:
313
  with gr.Group(visible=False) as screen_work:
314
  header_md = gr.Markdown()
315
  img_objs, input_objs = [], []
316
-
317
  with gr.Row():
318
  for i in range(MAX_IMAGES):
319
  with gr.Column(min_width=250):
@@ -325,7 +318,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Labeling Tool") as demo:
325
  chk = gr.Checkbox(label="Correct?", value=True, visible=False)
326
  img_objs.append(img)
327
  input_objs.extend([sld, drp, chk, lbl])
328
-
329
  with gr.Row():
330
  b_back = gr.Button("⬅ Back")
331
  b_skip = gr.Button("Skip ➡")
@@ -344,16 +336,12 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Labeling Tool") as demo:
344
 
345
  b_start_l.click(lambda: "label", None, state_mode).then(render_workspace, [state_mode, state_hist], ALL_IO)
346
  b_start_v.click(lambda: "verify", None, state_mode).then(render_workspace, [state_mode, state_hist], ALL_IO)
347
-
348
  b_save.click(save_data, [state_mode, state_hist, state_urls] + input_objs, ALL_IO)
349
  b_skip.click(skip_group, [state_idx, state_hist, state_mode], ALL_IO)
350
  b_back.click(lambda m, h: render_workspace(m, h, move_back=True), [state_mode, state_hist], ALL_IO)
351
-
352
  btn_home.click(lambda: {screen_menu: gr.update(visible=True), screen_work: gr.update(visible=False), state_hist: []}, None, [screen_menu, screen_work, state_hist])
353
-
354
  b_go_l.click(lambda: "label", None, state_mode).then(lambda n, m, h: render_workspace(m, h, specific_index=int(n)-1), [num_in, state_mode, state_hist], ALL_IO)
355
  b_go_v.click(lambda: "verify", None, state_mode).then(lambda n, m, h: render_workspace(m, h, specific_index=int(n)-1), [num_in, state_mode, state_hist], ALL_IO)
356
-
357
  b_ref_cat.click(refresh_cat, None, df_cat)
358
  demo.load(refresh_cat, None, df_cat).then(get_stats_text, None, top_stats)
359
 
 
1
+ import os
2
+
3
+ import huggingface_hub
4
+ if not hasattr(huggingface_hub, "HfFolder"):
5
+ class HfFolder:
6
+ @staticmethod
7
+ def get_token(): return os.environ.get("HF_TOKEN")
8
+ @staticmethod
9
+ def save_token(t): pass
10
+ huggingface_hub.HfFolder = HfFolder
11
+
12
  import gradio as gr
13
  import pandas as pd
14
  import requests
 
15
  import csv
16
  import threading
17
  from io import BytesIO
 
37
 
38
  def sync_pull():
39
  if not HF_TOKEN:
40
+ print("⚠️ HF_TOKEN not set. Persistence disabled.")
41
  return
 
42
  print(f"🔄 Syncing from {DATASET_REPO_ID}...")
43
  for filename in ["annotations.csv", "verifications.csv", "skipped.csv"]:
44
  try:
 
55
 
56
  def sync_push_background(local_path, remote_filename):
57
  if not HF_TOKEN: return
 
58
  def _push():
59
  try:
60
  api = HfApi(token=HF_TOKEN)
 
68
  print(f"☁️ Synced {remote_filename}")
69
  except Exception as e:
70
  print(f"❌ Sync failed: {e}")
71
+ threading.Thread(target=_push).start()
 
 
72
 
73
  def init_files():
74
  sync_pull()
 
75
  for f in [LABEL_FILE, VERIFY_FILE, SKIP_FILE]:
76
  if not os.path.exists(f):
77
  if f == LABEL_FILE: cols = ["timestamp", "user", "group_id", "url", "score", "label"]
 
80
  pd.DataFrame(columns=cols).to_csv(f, index=False)
81
 
82
  if not os.path.exists(URL_FILE):
83
+ print("⚠️ urls.txt not found in root! Please upload it.")
84
 
85
  init_files()
86
 
 
172
  found = False
173
  for i, gid in enumerate(all_groups):
174
  if gid in s_done: continue
 
175
  is_ready = False
176
  if mode == "label" and gid not in l_done: is_ready = True
177
  elif mode == "verify" and gid in l_done and gid not in v_done: is_ready = True
 
186
  return {screen_menu: gr.update(visible=True), screen_work: gr.update(visible=False), log_box: "No more tasks found."}
187
 
188
  urls = get_group_urls(target_gid)
 
189
  if not history or history[-1] != target_gid:
190
  history.append(target_gid)
191
 
 
198
  for future in futures:
199
  processed_images[futures[future]] = future.result()
200
 
201
+ header = f"# Property #{target_idx + 1} (ID: {target_gid})"
202
 
203
  updates = {
204
  screen_menu: gr.update(visible=False),
 
219
  if i < len(urls):
220
  u = urls[i]
221
  img_data = processed_images[i]
 
222
  updates[img_c] = gr.update(value=img_data, visible=True)
223
  v_sc = saved_vals.get(u, {}).get('score', 5)
224
  v_lbl = saved_vals.get(u, {}).get('label', ROOM_CLASSES[0])
 
241
  updates[c_drp] = gr.update(visible=False)
242
  updates[c_chk] = gr.update(visible=False)
243
  updates[c_lbl] = gr.update(visible=False)
 
244
  return updates
245
 
246
  def save_data(mode, history, urls, *args):
 
248
  gid = history[-1]
249
  ts = datetime.now().isoformat()
250
  rows = []
 
251
  for i, u in enumerate(urls):
252
  base = i * 4
253
  sc, lbl, chk = args[base], args[base+1], args[base+2]
 
255
  else: rows.append([ts, "user", gid, u, chk, lbl])
256
 
257
  fname = LABEL_FILE if mode == "label" else VERIFY_FILE
 
258
  with FileLock(LOCK_FILE):
259
  with open(fname, "a", newline="") as f:
260
  csv.writer(f).writerows(rows)
 
261
  remote_filename = os.path.basename(fname)
262
  sync_push_background(fname, remote_filename)
 
263
  return render_workspace(mode, history)
264
 
265
  def skip_group(idx, history, mode):
 
269
  with open(SKIP_FILE, "a", newline="") as f:
270
  csv.writer(f).writerow([datetime.now().isoformat(), "user", gid])
271
  sync_push_background(SKIP_FILE, "skipped.csv")
 
272
  return render_workspace(mode, history, specific_index=idx + 1)
273
 
274
  def refresh_cat():
 
277
  except: l_set = set()
278
  try: v_set = set(pd.read_csv(VERIFY_FILE)['group_id'].unique())
279
  except: v_set = set()
 
280
  data = []
281
  for i, gid in enumerate(all_gids):
282
  s = "⚪ Pending"
 
286
  return pd.DataFrame(data, columns=["#", "Status", "ID"])
287
 
288
  with gr.Blocks(theme=gr.themes.Soft(), title="Labeling Tool") as demo:
 
289
  state_mode = gr.State("label")
290
  state_hist = gr.State([])
291
  state_urls = gr.State([])
 
299
  with gr.Tab("Workspace", id=0):
300
  with gr.Group() as screen_menu:
301
  gr.Markdown("# Welcome! 👋")
302
+ gr.Markdown("Persistent & Compatible Version (Gradio 3.x)")
303
  with gr.Row():
304
  b_start_l = gr.Button("Start Labeling", variant="primary")
305
  b_start_v = gr.Button("Start Verification")
 
307
  with gr.Group(visible=False) as screen_work:
308
  header_md = gr.Markdown()
309
  img_objs, input_objs = [], []
 
310
  with gr.Row():
311
  for i in range(MAX_IMAGES):
312
  with gr.Column(min_width=250):
 
318
  chk = gr.Checkbox(label="Correct?", value=True, visible=False)
319
  img_objs.append(img)
320
  input_objs.extend([sld, drp, chk, lbl])
 
321
  with gr.Row():
322
  b_back = gr.Button("⬅ Back")
323
  b_skip = gr.Button("Skip ➡")
 
336
 
337
  b_start_l.click(lambda: "label", None, state_mode).then(render_workspace, [state_mode, state_hist], ALL_IO)
338
  b_start_v.click(lambda: "verify", None, state_mode).then(render_workspace, [state_mode, state_hist], ALL_IO)
 
339
  b_save.click(save_data, [state_mode, state_hist, state_urls] + input_objs, ALL_IO)
340
  b_skip.click(skip_group, [state_idx, state_hist, state_mode], ALL_IO)
341
  b_back.click(lambda m, h: render_workspace(m, h, move_back=True), [state_mode, state_hist], ALL_IO)
 
342
  btn_home.click(lambda: {screen_menu: gr.update(visible=True), screen_work: gr.update(visible=False), state_hist: []}, None, [screen_menu, screen_work, state_hist])
 
343
  b_go_l.click(lambda: "label", None, state_mode).then(lambda n, m, h: render_workspace(m, h, specific_index=int(n)-1), [num_in, state_mode, state_hist], ALL_IO)
344
  b_go_v.click(lambda: "verify", None, state_mode).then(lambda n, m, h: render_workspace(m, h, specific_index=int(n)-1), [num_in, state_mode, state_hist], ALL_IO)
 
345
  b_ref_cat.click(refresh_cat, None, df_cat)
346
  demo.load(refresh_cat, None, df_cat).then(get_stats_text, None, top_stats)
347
 
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
- gradio==4.44.1
2
- huggingface_hub==0.24.6
 
3
  pandas
4
  requests
5
  Pillow
 
1
+ gradio==3.50.2
2
+ huggingface_hub==0.23.0
3
+ pydantic<2.0
4
  pandas
5
  requests
6
  Pillow