jrrribeiro commited on
Commit
7ea05c5
·
1 Parent(s): e87da82

feat(ui): add human-readable percentage progress for segments upload controls

Browse files
Files changed (1) hide show
  1. src/ui/upload_app_factory.py +66 -27
src/ui/upload_app_factory.py CHANGED
@@ -60,6 +60,31 @@ def _resolve_segments_root(
60
  return None, None
61
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  class SegmentsUploadSession:
64
  def __init__(self) -> None:
65
  self._lock = threading.Lock()
@@ -87,20 +112,21 @@ class SegmentsUploadSession:
87
  with self._lock:
88
  return self._cancel
89
 
90
- def start(self, dataset_repo: str, archive_path: str | None, token: str, batch_size: int) -> tuple[str, str]:
91
  repo = (dataset_repo or "").strip()
92
  if not repo or "/" not in repo:
93
- return "❌ Informe dataset repo no formato owner/repo", "{}"
94
  if not archive_path:
95
- return "❌ Envie um arquivo (.tar, .tar.gz, ou .zip) de segmentos", "{}"
96
 
97
  with self._lock:
98
  if self._is_running():
99
- return "⏳ Ja existe upload em andamento", _as_pretty_json(self.snapshot())
 
100
 
101
  segments_root, temp_dir = _extract_compressed_segments(archive_path)
102
  if segments_root is None or temp_dir is None:
103
- return "❌ Arquivo compactado invalido", "{}"
104
 
105
  self._pause = False
106
  self._cancel = False
@@ -149,35 +175,42 @@ class SegmentsUploadSession:
149
  self._thread = threading.Thread(target=worker, daemon=True)
150
  self._thread.start()
151
 
152
- return "🚀 Upload iniciado em background", _as_pretty_json(self.snapshot())
 
153
 
154
- def pause(self) -> tuple[str, str]:
155
  with self._lock:
156
  if not self._is_running():
157
- return "ℹ️ Nenhum upload em andamento", _as_pretty_json(self.snapshot())
 
158
  self._pause = True
159
  self._status = "paused"
160
  self._message = "Upload pausado"
161
- return "⏸️ Upload pausado", _as_pretty_json(self.snapshot())
 
162
 
163
- def resume(self) -> tuple[str, str]:
164
  with self._lock:
165
  if not self._is_running():
166
- return "ℹ️ Nenhum upload em andamento", _as_pretty_json(self.snapshot())
 
167
  self._pause = False
168
  self._status = "running"
169
  self._message = "Upload retomado"
170
- return "▶️ Upload retomado", _as_pretty_json(self.snapshot())
 
171
 
172
- def cancel(self) -> tuple[str, str]:
173
  with self._lock:
174
  if not self._is_running():
175
- return "ℹ️ Nenhum upload em andamento", _as_pretty_json(self.snapshot())
 
176
  self._cancel = True
177
  self._pause = False
178
  self._status = "cancelling"
179
  self._message = "Cancelamento solicitado"
180
- return "🛑 Cancelamento solicitado", _as_pretty_json(self.snapshot())
 
181
 
182
  def snapshot(self) -> dict[str, Any]:
183
  return {
@@ -189,7 +222,7 @@ class SegmentsUploadSession:
189
  "timestamp": int(time.time()),
190
  }
191
 
192
- def status(self) -> tuple[str, str]:
193
  snap = self.snapshot()
194
  state = snap.get("status", "idle")
195
  if state == "running":
@@ -206,29 +239,34 @@ class SegmentsUploadSession:
206
  msg = "🛑 Cancelando upload..."
207
  else:
208
  msg = "ℹ️ Pronto"
209
- return msg, _as_pretty_json(snap)
210
 
211
 
212
  _SEGMENTS_UPLOAD_SESSION = SegmentsUploadSession()
213
 
214
 
215
- def _start_segments_upload(dataset_repo: str, archive_path: str | None, token: str, batch_size: float) -> tuple[str, str]:
 
 
 
 
 
216
  return _SEGMENTS_UPLOAD_SESSION.start(dataset_repo, archive_path, token, int(batch_size))
217
 
218
 
219
- def _pause_segments_upload() -> tuple[str, str]:
220
  return _SEGMENTS_UPLOAD_SESSION.pause()
221
 
222
 
223
- def _resume_segments_upload() -> tuple[str, str]:
224
  return _SEGMENTS_UPLOAD_SESSION.resume()
225
 
226
 
227
- def _cancel_segments_upload() -> tuple[str, str]:
228
  return _SEGMENTS_UPLOAD_SESSION.cancel()
229
 
230
 
231
- def _refresh_segments_upload_status() -> tuple[str, str]:
232
  return _SEGMENTS_UPLOAD_SESSION.status()
233
 
234
 
@@ -281,6 +319,7 @@ def build_upload_app() -> gr.Blocks:
281
  segments_upload_cancel = gr.Button("Cancelar", variant="stop")
282
  segments_upload_refresh = gr.Button("Atualizar Status", variant="secondary")
283
  segments_upload_status = gr.Markdown(value="Pronto")
 
284
  segments_upload_result = gr.Code(label="Resultado JSON", language="json")
285
 
286
  with gr.Accordion("Configuracao avancada", open=False):
@@ -416,31 +455,31 @@ def build_upload_app() -> gr.Blocks:
416
  segments_upload_start.click(
417
  fn=_start_segments_upload,
418
  inputs=[segments_upload_repo, segments_upload_file, segments_upload_token, segments_upload_batch_size],
419
- outputs=[segments_upload_status, segments_upload_result],
420
  )
421
 
422
  segments_upload_pause.click(
423
  fn=_pause_segments_upload,
424
  inputs=[],
425
- outputs=[segments_upload_status, segments_upload_result],
426
  )
427
 
428
  segments_upload_resume.click(
429
  fn=_resume_segments_upload,
430
  inputs=[],
431
- outputs=[segments_upload_status, segments_upload_result],
432
  )
433
 
434
  segments_upload_cancel.click(
435
  fn=_cancel_segments_upload,
436
  inputs=[],
437
- outputs=[segments_upload_status, segments_upload_result],
438
  )
439
 
440
  segments_upload_refresh.click(
441
  fn=_refresh_segments_upload_status,
442
  inputs=[],
443
- outputs=[segments_upload_status, segments_upload_result],
444
  )
445
 
446
  upload_button.click(
 
60
  return None, None
61
 
62
 
63
+ def _format_segments_upload_progress(snapshot: dict[str, Any]) -> str:
64
+ progress = snapshot.get("progress", {}) if isinstance(snapshot, dict) else {}
65
+ result = snapshot.get("result", {}) if isinstance(snapshot, dict) else {}
66
+
67
+ pending_total = int(progress.get("pending_total") or result.get("pending_files") or 0)
68
+ processed_pending = int(progress.get("processed_pending") or 0)
69
+ uploaded = int(progress.get("uploaded") or result.get("uploaded") or 0)
70
+ failed = int(progress.get("failed") or result.get("failed") or 0)
71
+ skipped = int(progress.get("skipped_existing") or result.get("skipped_existing") or 0)
72
+
73
+ if pending_total > 0:
74
+ percent = min(100.0, (processed_pending / pending_total) * 100.0)
75
+ return (
76
+ f"**Progresso:** {percent:.1f}% | "
77
+ f"Processados: {processed_pending}/{pending_total} | "
78
+ f"Enviados: {uploaded} | Falhas: {failed} | Ja existentes: {skipped}"
79
+ )
80
+
81
+ total_files = int(progress.get("total_files") or result.get("total_files") or 0)
82
+ if total_files > 0:
83
+ return f"**Progresso:** preparando upload ({total_files} arquivos detectados)"
84
+
85
+ return "**Progresso:** aguardando inicio"
86
+
87
+
88
  class SegmentsUploadSession:
89
  def __init__(self) -> None:
90
  self._lock = threading.Lock()
 
112
  with self._lock:
113
  return self._cancel
114
 
115
+ def start(self, dataset_repo: str, archive_path: str | None, token: str, batch_size: int) -> tuple[str, str, str]:
116
  repo = (dataset_repo or "").strip()
117
  if not repo or "/" not in repo:
118
+ return "❌ Informe dataset repo no formato owner/repo", "{}", "**Progresso:** aguardando inicio"
119
  if not archive_path:
120
+ return "❌ Envie um arquivo (.tar, .tar.gz, ou .zip) de segmentos", "{}", "**Progresso:** aguardando inicio"
121
 
122
  with self._lock:
123
  if self._is_running():
124
+ snap = self.snapshot()
125
+ return "⏳ Ja existe upload em andamento", _as_pretty_json(snap), _format_segments_upload_progress(snap)
126
 
127
  segments_root, temp_dir = _extract_compressed_segments(archive_path)
128
  if segments_root is None or temp_dir is None:
129
+ return "❌ Arquivo compactado invalido", "{}", "**Progresso:** aguardando inicio"
130
 
131
  self._pause = False
132
  self._cancel = False
 
175
  self._thread = threading.Thread(target=worker, daemon=True)
176
  self._thread.start()
177
 
178
+ snap = self.snapshot()
179
+ return "🚀 Upload iniciado em background", _as_pretty_json(snap), _format_segments_upload_progress(snap)
180
 
181
+ def pause(self) -> tuple[str, str, str]:
182
  with self._lock:
183
  if not self._is_running():
184
+ snap = self.snapshot()
185
+ return "ℹ️ Nenhum upload em andamento", _as_pretty_json(snap), _format_segments_upload_progress(snap)
186
  self._pause = True
187
  self._status = "paused"
188
  self._message = "Upload pausado"
189
+ snap = self.snapshot()
190
+ return "⏸️ Upload pausado", _as_pretty_json(snap), _format_segments_upload_progress(snap)
191
 
192
+ def resume(self) -> tuple[str, str, str]:
193
  with self._lock:
194
  if not self._is_running():
195
+ snap = self.snapshot()
196
+ return "ℹ️ Nenhum upload em andamento", _as_pretty_json(snap), _format_segments_upload_progress(snap)
197
  self._pause = False
198
  self._status = "running"
199
  self._message = "Upload retomado"
200
+ snap = self.snapshot()
201
+ return "▶️ Upload retomado", _as_pretty_json(snap), _format_segments_upload_progress(snap)
202
 
203
+ def cancel(self) -> tuple[str, str, str]:
204
  with self._lock:
205
  if not self._is_running():
206
+ snap = self.snapshot()
207
+ return "ℹ️ Nenhum upload em andamento", _as_pretty_json(snap), _format_segments_upload_progress(snap)
208
  self._cancel = True
209
  self._pause = False
210
  self._status = "cancelling"
211
  self._message = "Cancelamento solicitado"
212
+ snap = self.snapshot()
213
+ return "🛑 Cancelamento solicitado", _as_pretty_json(snap), _format_segments_upload_progress(snap)
214
 
215
  def snapshot(self) -> dict[str, Any]:
216
  return {
 
222
  "timestamp": int(time.time()),
223
  }
224
 
225
+ def status(self) -> tuple[str, str, str]:
226
  snap = self.snapshot()
227
  state = snap.get("status", "idle")
228
  if state == "running":
 
239
  msg = "🛑 Cancelando upload..."
240
  else:
241
  msg = "ℹ️ Pronto"
242
+ return msg, _as_pretty_json(snap), _format_segments_upload_progress(snap)
243
 
244
 
245
  _SEGMENTS_UPLOAD_SESSION = SegmentsUploadSession()
246
 
247
 
248
+ def _start_segments_upload(
249
+ dataset_repo: str,
250
+ archive_path: str | None,
251
+ token: str,
252
+ batch_size: float,
253
+ ) -> tuple[str, str, str]:
254
  return _SEGMENTS_UPLOAD_SESSION.start(dataset_repo, archive_path, token, int(batch_size))
255
 
256
 
257
+ def _pause_segments_upload() -> tuple[str, str, str]:
258
  return _SEGMENTS_UPLOAD_SESSION.pause()
259
 
260
 
261
+ def _resume_segments_upload() -> tuple[str, str, str]:
262
  return _SEGMENTS_UPLOAD_SESSION.resume()
263
 
264
 
265
+ def _cancel_segments_upload() -> tuple[str, str, str]:
266
  return _SEGMENTS_UPLOAD_SESSION.cancel()
267
 
268
 
269
+ def _refresh_segments_upload_status() -> tuple[str, str, str]:
270
  return _SEGMENTS_UPLOAD_SESSION.status()
271
 
272
 
 
319
  segments_upload_cancel = gr.Button("Cancelar", variant="stop")
320
  segments_upload_refresh = gr.Button("Atualizar Status", variant="secondary")
321
  segments_upload_status = gr.Markdown(value="Pronto")
322
+ segments_upload_progress = gr.Markdown(value="**Progresso:** aguardando inicio")
323
  segments_upload_result = gr.Code(label="Resultado JSON", language="json")
324
 
325
  with gr.Accordion("Configuracao avancada", open=False):
 
455
  segments_upload_start.click(
456
  fn=_start_segments_upload,
457
  inputs=[segments_upload_repo, segments_upload_file, segments_upload_token, segments_upload_batch_size],
458
+ outputs=[segments_upload_status, segments_upload_result, segments_upload_progress],
459
  )
460
 
461
  segments_upload_pause.click(
462
  fn=_pause_segments_upload,
463
  inputs=[],
464
+ outputs=[segments_upload_status, segments_upload_result, segments_upload_progress],
465
  )
466
 
467
  segments_upload_resume.click(
468
  fn=_resume_segments_upload,
469
  inputs=[],
470
+ outputs=[segments_upload_status, segments_upload_result, segments_upload_progress],
471
  )
472
 
473
  segments_upload_cancel.click(
474
  fn=_cancel_segments_upload,
475
  inputs=[],
476
+ outputs=[segments_upload_status, segments_upload_result, segments_upload_progress],
477
  )
478
 
479
  segments_upload_refresh.click(
480
  fn=_refresh_segments_upload_status,
481
  inputs=[],
482
+ outputs=[segments_upload_status, segments_upload_result, segments_upload_progress],
483
  )
484
 
485
  upload_button.click(