cringo commited on
Commit
ade5ccc
Β·
1 Parent(s): 9f45c98

progress bar

Browse files
Files changed (2) hide show
  1. app.py +63 -5
  2. core +0 -1
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import uuid
3
  import time
 
4
  import shutil
5
  import tempfile
6
  import threading
@@ -18,9 +19,22 @@ JOB_MAX_AGE_SECONDS = 24 * 3600
18
  def _process_job_worker(job_id, work_dir, source_path, target_path, source_lang, target_lang):
19
  status_file = Path(work_dir) / "status.txt"
20
  out_epub = Path(work_dir) / "bilingual.epub"
 
21
 
22
  status_file.write_text("processing")
23
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  try:
25
  shutil.copy2(source_path, Path(work_dir) / "source.epub")
26
  shutil.copy2(target_path, Path(work_dir) / "target.epub")
@@ -34,6 +48,8 @@ def _process_job_worker(job_id, work_dir, source_path, target_path, source_lang,
34
  auto_match_chapter_threshold=0.6,
35
  model=MODEL,
36
  threads=CPU_CORES,
 
 
37
  )
38
  bbb.run()
39
  if out_epub.exists():
@@ -67,6 +83,7 @@ class JobManager:
67
  "output_path": None,
68
  "created_at": time.time(),
69
  "process": None,
 
70
  }
71
 
72
  p = multiprocessing.Process(
@@ -88,7 +105,17 @@ class JobManager:
88
  return None
89
  p = job.get("process")
90
  if p and p.is_alive():
 
 
 
 
 
 
 
 
 
91
  return job
 
92
  status_file = Path(job["work_dir"]) / "status.txt"
93
  if not status_file.exists():
94
  job["status"] = "error: process died unexpectedly"
@@ -178,7 +205,7 @@ def submit_job(source_file, target_file, source_lang, target_lang):
178
  )
179
 
180
  status = f"πŸ”„ Job **{job_id[:8]}** submitted. Processing `{src_name}` β†’ `{tgt_name}`."
181
- return job_id, status
182
 
183
 
184
  def load_job(job_id):
@@ -193,6 +220,7 @@ def load_job(job_id):
193
  gr.update(value=None, interactive=True),
194
  gr.update(interactive=True),
195
  gr.update(visible=False),
 
196
  )
197
 
198
  job = manager.get_job(job_id)
@@ -200,7 +228,29 @@ def load_job(job_id):
200
  return load_job("")
201
 
202
  if job["status"] == "processing":
203
- status = f"πŸ”„ Processing `{job['source_name']}` and `{job['target_name']}` …"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  output = gr.update(value=None, visible=False)
205
  clear_file = gr.update(value=None, interactive=False)
206
  cancel_visible = gr.update(visible=True)
@@ -214,6 +264,7 @@ def load_job(job_id):
214
  clear_file,
215
  gr.update(interactive=False),
216
  cancel_visible,
 
217
  )
218
 
219
  elif job["status"] == "done":
@@ -230,6 +281,7 @@ def load_job(job_id):
230
  clear_file,
231
  gr.update(interactive=True),
232
  gr.update(visible=False),
 
233
  )
234
  else:
235
  status = f"❌ Error: {job['status']}"
@@ -244,13 +296,14 @@ def load_job(job_id):
244
  clear_file,
245
  gr.update(interactive=True),
246
  gr.update(visible=False),
 
247
  )
248
 
249
 
250
  def cancel_current_job(job_id):
251
  if job_id:
252
  manager.cancel(job_id)
253
- return "", gr.update(visible=False)
254
 
255
 
256
  with gr.Blocks(title="πŸ“š Bilingual Book Builder") as demo:
@@ -284,12 +337,13 @@ with gr.Blocks(title="πŸ“š Bilingual Book Builder") as demo:
284
  cancel_btn = gr.Button("❌ Cancel", variant="stop", visible=False)
285
  refresh_btn = gr.Button("πŸ”„ Check Status")
286
  status_msg = gr.Markdown("")
 
287
  output_file = gr.File(label="Download Bilingual EPUB", interactive=False, visible=False)
288
 
289
  process_btn.click(
290
  fn=submit_job,
291
  inputs=[source_file, target_file, source_lang, target_lang],
292
- outputs=[job_state, status_msg],
293
  ).then(
294
  fn=load_job,
295
  inputs=job_state,
@@ -303,13 +357,14 @@ with gr.Blocks(title="πŸ“š Bilingual Book Builder") as demo:
303
  target_file,
304
  process_btn,
305
  cancel_btn,
 
306
  ],
307
  )
308
 
309
  cancel_btn.click(
310
  fn=cancel_current_job,
311
  inputs=job_state,
312
- outputs=[job_state, cancel_btn],
313
  ).then(
314
  fn=load_job,
315
  inputs=job_state,
@@ -323,6 +378,7 @@ with gr.Blocks(title="πŸ“š Bilingual Book Builder") as demo:
323
  target_file,
324
  process_btn,
325
  cancel_btn,
 
326
  ],
327
  )
328
 
@@ -339,6 +395,7 @@ with gr.Blocks(title="πŸ“š Bilingual Book Builder") as demo:
339
  target_file,
340
  process_btn,
341
  cancel_btn,
 
342
  ],
343
  )
344
 
@@ -355,6 +412,7 @@ with gr.Blocks(title="πŸ“š Bilingual Book Builder") as demo:
355
  target_file,
356
  process_btn,
357
  cancel_btn,
 
358
  ],
359
  )
360
 
 
1
  import os
2
  import uuid
3
  import time
4
+ import json
5
  import shutil
6
  import tempfile
7
  import threading
 
19
  def _process_job_worker(job_id, work_dir, source_path, target_path, source_lang, target_lang):
20
  status_file = Path(work_dir) / "status.txt"
21
  out_epub = Path(work_dir) / "bilingual.epub"
22
+ progress_file = Path(work_dir) / "progress.json"
23
 
24
  status_file.write_text("processing")
25
 
26
+ def progress_callback(phase: str, step: int, total: int, message: str = None):
27
+ """Called by BBB to report progress. Writes latest info to a file."""
28
+ data = {
29
+ "phase": phase,
30
+ "step": step,
31
+ "total": total,
32
+ "message": message,
33
+ "timestamp": time.time()
34
+ }
35
+ with open(progress_file, "w") as f:
36
+ json.dump(data, f)
37
+
38
  try:
39
  shutil.copy2(source_path, Path(work_dir) / "source.epub")
40
  shutil.copy2(target_path, Path(work_dir) / "target.epub")
 
48
  auto_match_chapter_threshold=0.6,
49
  model=MODEL,
50
  threads=CPU_CORES,
51
+ verbosity='progress',
52
+ progress_callback=progress_callback,
53
  )
54
  bbb.run()
55
  if out_epub.exists():
 
83
  "output_path": None,
84
  "created_at": time.time(),
85
  "process": None,
86
+ "progress": None,
87
  }
88
 
89
  p = multiprocessing.Process(
 
105
  return None
106
  p = job.get("process")
107
  if p and p.is_alive():
108
+ progress_file = Path(job["work_dir"]) / "progress.json"
109
+ if progress_file.exists():
110
+ try:
111
+ with open(progress_file) as f:
112
+ job["progress"] = json.load(f)
113
+ except Exception:
114
+ job["progress"] = None
115
+ else:
116
+ job["progress"] = None
117
  return job
118
+
119
  status_file = Path(job["work_dir"]) / "status.txt"
120
  if not status_file.exists():
121
  job["status"] = "error: process died unexpectedly"
 
205
  )
206
 
207
  status = f"πŸ”„ Job **{job_id[:8]}** submitted. Processing `{src_name}` β†’ `{tgt_name}`."
208
+ return job_id, status, gr.update(visible=False)
209
 
210
 
211
  def load_job(job_id):
 
220
  gr.update(value=None, interactive=True),
221
  gr.update(interactive=True),
222
  gr.update(visible=False),
223
+ gr.update(visible=False),
224
  )
225
 
226
  job = manager.get_job(job_id)
 
228
  return load_job("")
229
 
230
  if job["status"] == "processing":
231
+ progress = job.get("progress")
232
+ if progress and progress["total"] > 0:
233
+ phase = progress.get("phase", "processing")
234
+ step = progress["step"]
235
+ total = progress["total"]
236
+ msg = progress.get("message")
237
+ percent = (step / total) * 100
238
+ detail = f"{phase} β€” {step}/{total} ({percent:.0f}%)"
239
+ if msg:
240
+ detail += f" β€” {msg}"
241
+ status = f"πŸ”„ Processing `{job['source_name']}` and `{job['target_name']}`\n\n**{detail}**"
242
+ bar_html = f"""
243
+ <div style="width:100%; background-color:#eee; border-radius:4px;">
244
+ <div style="width:{percent}%; background-color:#4CAF50; height:20px; border-radius:4px; text-align:center; color:white; font-size:12px; line-height:20px;">
245
+ {percent:.0f}%
246
+ </div>
247
+ </div>
248
+ """
249
+ progress_bar_update = gr.update(value=bar_html, visible=True)
250
+ else:
251
+ status = f"πŸ”„ Processing `{job['source_name']}` and `{job['target_name']}` … (initializing)"
252
+ progress_bar_update = gr.update(visible=False)
253
+
254
  output = gr.update(value=None, visible=False)
255
  clear_file = gr.update(value=None, interactive=False)
256
  cancel_visible = gr.update(visible=True)
 
264
  clear_file,
265
  gr.update(interactive=False),
266
  cancel_visible,
267
+ progress_bar_update,
268
  )
269
 
270
  elif job["status"] == "done":
 
281
  clear_file,
282
  gr.update(interactive=True),
283
  gr.update(visible=False),
284
+ gr.update(visible=False),
285
  )
286
  else:
287
  status = f"❌ Error: {job['status']}"
 
296
  clear_file,
297
  gr.update(interactive=True),
298
  gr.update(visible=False),
299
+ gr.update(visible=False),
300
  )
301
 
302
 
303
  def cancel_current_job(job_id):
304
  if job_id:
305
  manager.cancel(job_id)
306
+ return "", gr.update(visible=False), gr.update(visible=False)
307
 
308
 
309
  with gr.Blocks(title="πŸ“š Bilingual Book Builder") as demo:
 
337
  cancel_btn = gr.Button("❌ Cancel", variant="stop", visible=False)
338
  refresh_btn = gr.Button("πŸ”„ Check Status")
339
  status_msg = gr.Markdown("")
340
+ progress_bar = gr.HTML(visible=False)
341
  output_file = gr.File(label="Download Bilingual EPUB", interactive=False, visible=False)
342
 
343
  process_btn.click(
344
  fn=submit_job,
345
  inputs=[source_file, target_file, source_lang, target_lang],
346
+ outputs=[job_state, status_msg, progress_bar],
347
  ).then(
348
  fn=load_job,
349
  inputs=job_state,
 
357
  target_file,
358
  process_btn,
359
  cancel_btn,
360
+ progress_bar,
361
  ],
362
  )
363
 
364
  cancel_btn.click(
365
  fn=cancel_current_job,
366
  inputs=job_state,
367
+ outputs=[job_state, cancel_btn, progress_bar],
368
  ).then(
369
  fn=load_job,
370
  inputs=job_state,
 
378
  target_file,
379
  process_btn,
380
  cancel_btn,
381
+ progress_bar,
382
  ],
383
  )
384
 
 
395
  target_file,
396
  process_btn,
397
  cancel_btn,
398
+ progress_bar,
399
  ],
400
  )
401
 
 
412
  target_file,
413
  process_btn,
414
  cancel_btn,
415
+ progress_bar,
416
  ],
417
  )
418
 
core DELETED
@@ -1 +0,0 @@
1
- Subproject commit 1bc7b180cd95cf2b00af4771eb5ca97d4b07a4dd