cringo commited on
Commit
e4c8fbf
·
1 Parent(s): b56dbe4
Files changed (1) hide show
  1. app.py +98 -143
app.py CHANGED
@@ -184,45 +184,18 @@ class JobManager:
184
  manager = JobManager()
185
 
186
 
187
- def submit_job(source_file, target_file, source_lang, target_lang):
188
- if source_file is None or target_file is None:
189
- raise gr.Error("Please upload both EPUB files.")
190
-
191
- src_name = Path(source_file).name
192
- tgt_name = Path(target_file).name
193
-
194
- upload_dir = tempfile.mkdtemp(prefix="bbb_uploads_")
195
- src_path = os.path.join(upload_dir, src_name)
196
- tgt_path = os.path.join(upload_dir, tgt_name)
197
- shutil.copy2(source_file, src_path)
198
- shutil.copy2(target_file, tgt_path)
199
-
200
- job_id = manager.submit(
201
- source_path=src_path,
202
- target_path=tgt_path,
203
- source_lang=source_lang,
204
- target_lang=target_lang,
205
- source_name=src_name,
206
- target_name=tgt_name,
207
- upload_dir=upload_dir,
208
- )
209
-
210
- status = f"🔄 Job **{job_id[:8]}** submitted. Processing `{src_name}` → `{tgt_name}`."
211
- return job_id, status
212
-
213
-
214
- def load_job(job_id):
215
  if not job_id:
216
  return (
217
- "",
218
- gr.update(value=""),
219
- gr.update(value="", interactive=True),
220
- gr.update(value="", interactive=True),
221
- gr.update(value=None, visible=False),
222
- gr.update(value=None, interactive=True),
223
- gr.update(value=None, interactive=True),
224
- gr.update(interactive=True),
225
- gr.update(visible=False),
226
  )
227
 
228
  job = manager.get_job(job_id)
@@ -230,79 +203,96 @@ def load_job(job_id):
230
  return load_job("")
231
 
232
  if job["status"] == "processing":
233
- status = f"Processing `{job['source_name']}` and `{job['target_name']}`…"
234
  progress = job.get("progress")
235
- if progress and progress["total"] > 0:
236
- phase_id = progress.get("phase_id", "processing")
237
  step = progress["step"]
238
  total = progress["total"]
239
- description = progress["description"]
240
- percent = (step / total) * 100
241
- status += f"\n\n{description} — {step}/{total} ({percent:.0f}%)"
242
  else:
243
- status += f" (initializing)"
244
 
245
- output = gr.update(value=None, visible=False)
246
- clear_file = gr.update(value=None, interactive=False)
247
- cancel_visible = gr.update(visible=True)
248
  return (
249
  job_id,
250
  status,
251
- gr.update(value=job["source_lang"], interactive=False),
252
- gr.update(value=job["target_lang"], interactive=False),
253
- output,
254
- clear_file,
255
- clear_file,
256
- gr.update(interactive=False),
257
- cancel_visible,
258
  )
259
 
260
  elif job["status"] == "done":
261
  status = f"✅ Bilingual EPUB ready (source: `{job['source_name']}`, target: `{job['target_name']}`)"
262
- output = gr.update(value=job["output_path"], visible=True)
263
- clear_file = gr.update(value=None, interactive=True)
264
  return (
265
  job_id,
266
  status,
267
- gr.update(value=job["source_lang"], interactive=True),
268
- gr.update(value=job["target_lang"], interactive=True),
269
- output,
270
- clear_file,
271
- clear_file,
272
- gr.update(interactive=True),
273
- gr.update(visible=False),
274
  )
 
275
  else:
276
  status = f"❌ Error: {job['status']}"
277
- clear_file = gr.update(value=None, interactive=True)
278
  return (
279
  job_id,
280
  status,
281
- gr.update(value=job["source_lang"], interactive=True),
282
- gr.update(value=job["target_lang"], interactive=True),
283
- gr.update(value=None, visible=False),
284
- clear_file,
285
- clear_file,
286
- gr.update(interactive=True),
287
- gr.update(visible=False),
288
  )
289
 
290
 
291
- def cancel_current_job(job_id):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  if job_id:
293
  manager.cancel(job_id)
294
- return "", gr.update(visible=False)
295
 
296
 
297
  with gr.Blocks(title="📚 Bilingual Book Builder") as demo:
298
  storage_hours = JOB_MAX_AGE_SECONDS // 3600
299
  gr.Markdown(f"""
300
  # Bilingual Book Builder
301
-
302
  Upload a **source** (original) EPUB and a **target** (translation) EPUB to create a side‑by‑side bilingual book.
303
-
304
  Processing may take hours – you can close this page and return later.
305
-
306
  When ready, the bilingual EPUB will be stored for **{storage_hours} hours**.
307
  """)
308
 
@@ -313,95 +303,60 @@ with gr.Blocks(title="📚 Bilingual Book Builder") as demo:
313
 
314
  with gr.Row():
315
  with gr.Column():
316
- with gr.Row():
317
- source_file = gr.File(label="Source EPUB", file_types=[".epub"], value=None)
318
- target_file = gr.File(label="Target EPUB", file_types=[".epub"], value=None)
319
- with gr.Row():
320
- source_lang = gr.Textbox(label="Source language (en, ru, de)", placeholder="Auto‑detect")
321
- target_lang = gr.Textbox(label="Target language (en, ru, de)", placeholder="Auto‑detect")
322
  with gr.Column():
323
- with gr.Row():
324
- process_btn = gr.Button("🚀 Build Bilingual EPUB", variant="primary")
325
- cancel_btn = gr.Button("❌ Cancel", variant="stop", visible=False)
 
 
 
 
326
  refresh_btn = gr.Button("🔄 Check Status")
 
327
  status_msg = gr.Markdown("")
328
  output_file = gr.File(label="Download Bilingual EPUB", interactive=False, visible=False)
329
-
 
 
 
 
 
 
 
 
 
 
 
 
330
  process_btn.click(
331
- fn=submit_job,
332
  inputs=[source_file, target_file, source_lang, target_lang],
333
- outputs=[job_state, status_msg],
334
- ).then(
335
- fn=load_job,
336
- inputs=job_state,
337
- outputs=[
338
- job_id_hidden,
339
- status_msg,
340
- source_lang,
341
- target_lang,
342
- output_file,
343
- source_file,
344
- target_file,
345
- process_btn,
346
- cancel_btn,
347
- ],
348
  )
349
 
350
  cancel_btn.click(
351
- fn=cancel_current_job,
352
- inputs=job_state,
353
- outputs=[job_state, cancel_btn],
354
- ).then(
355
- fn=load_job,
356
  inputs=job_state,
357
- outputs=[
358
- job_id_hidden,
359
- status_msg,
360
- source_lang,
361
- target_lang,
362
- output_file,
363
- source_file,
364
- target_file,
365
- process_btn,
366
- cancel_btn,
367
- ],
368
  )
369
 
370
- demo.load(
371
  fn=load_job,
372
  inputs=job_state,
373
- outputs=[
374
- job_id_hidden,
375
- status_msg,
376
- source_lang,
377
- target_lang,
378
- output_file,
379
- source_file,
380
- target_file,
381
- process_btn,
382
- cancel_btn,
383
- ],
384
  )
385
 
386
- refresh_btn.click(
387
  fn=load_job,
388
  inputs=job_state,
389
- outputs=[
390
- job_id_hidden,
391
- status_msg,
392
- source_lang,
393
- target_lang,
394
- output_file,
395
- source_file,
396
- target_file,
397
- process_btn,
398
- cancel_btn,
399
- ],
400
  )
401
 
 
402
  if __name__ == "__main__":
403
  handler = logging.StreamHandler(sys.stdout)
404
-
405
  for module in LOG_MODULES:
406
  logger = logging.getLogger(module)
407
  logger.setLevel(logging.INFO)
 
184
  manager = JobManager()
185
 
186
 
187
+ def load_job(job_id: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  if not job_id:
189
  return (
190
+ "", # job_id_hidden
191
+ "", # status_msg
192
+ gr.Textbox(value="", interactive=True), # source_lang
193
+ gr.Textbox(value="", interactive=True), # target_lang
194
+ gr.File(value=None, visible=False), # output_file
195
+ gr.File(value=None, interactive=True), # source_file
196
+ gr.File(value=None, interactive=True), # target_file
197
+ gr.Button(interactive=True), # process_btn
198
+ gr.Button(visible=False), # cancel_btn
199
  )
200
 
201
  job = manager.get_job(job_id)
 
203
  return load_job("")
204
 
205
  if job["status"] == "processing":
 
206
  progress = job.get("progress")
207
+ if progress and progress.get("total", 0) > 0:
 
208
  step = progress["step"]
209
  total = progress["total"]
210
+ desc = progress["description"]
211
+ status = f"Processing `{job['source_name']}` and `{job['target_name']}`…\n\n{desc} — {step}/{total} ({step/total*100:.0f}%)"
 
212
  else:
213
+ status = f"Processing `{job['source_name']}` and `{job['target_name']}`… (initializing)"
214
 
 
 
 
215
  return (
216
  job_id,
217
  status,
218
+ gr.Textbox(value=job["source_lang"], interactive=False),
219
+ gr.Textbox(value=job["target_lang"], interactive=False),
220
+ gr.File(value=None, visible=False), # output_file
221
+ gr.File(value=None, interactive=False), # source_file
222
+ gr.File(value=None, interactive=False), # target_file
223
+ gr.Button(interactive=False), # process_btn
224
+ gr.Button(visible=True), # cancel_btn
225
  )
226
 
227
  elif job["status"] == "done":
228
  status = f"✅ Bilingual EPUB ready (source: `{job['source_name']}`, target: `{job['target_name']}`)"
 
 
229
  return (
230
  job_id,
231
  status,
232
+ gr.Textbox(value=job["source_lang"], interactive=True),
233
+ gr.Textbox(value=job["target_lang"], interactive=True),
234
+ gr.File(value=job["output_path"], visible=True), # output_file
235
+ gr.File(value=None, interactive=True), # source_file
236
+ gr.File(value=None, interactive=True), # target_file
237
+ gr.Button(interactive=True), # process_btn
238
+ gr.Button(visible=False), # cancel_btn
239
  )
240
+
241
  else:
242
  status = f"❌ Error: {job['status']}"
 
243
  return (
244
  job_id,
245
  status,
246
+ gr.Textbox(value=job["source_lang"], interactive=True),
247
+ gr.Textbox(value=job["target_lang"], interactive=True),
248
+ gr.File(value=None, visible=False), # output_file
249
+ gr.File(value=None, interactive=True), # source_file
250
+ gr.File(value=None, interactive=True), # target_file
251
+ gr.Button(interactive=True), # process_btn
252
+ gr.Button(visible=False), # cancel_btn
253
  )
254
 
255
 
256
+ def start_job(source_file, target_file, source_lang, target_lang):
257
+ if source_file is None or target_file is None:
258
+ raise gr.Error("Please upload both EPUB files.")
259
+
260
+ src_name = Path(source_file).name
261
+ tgt_name = Path(target_file).name
262
+
263
+ upload_dir = tempfile.mkdtemp(prefix="bbb_uploads_")
264
+ src_path = os.path.join(upload_dir, src_name)
265
+ tgt_path = os.path.join(upload_dir, tgt_name)
266
+ shutil.copy2(source_file, src_path)
267
+ shutil.copy2(target_file, tgt_path)
268
+
269
+ job_id = manager.submit(
270
+ source_path=src_path,
271
+ target_path=tgt_path,
272
+ source_lang=source_lang,
273
+ target_lang=target_lang,
274
+ source_name=src_name,
275
+ target_name=tgt_name,
276
+ upload_dir=upload_dir,
277
+ )
278
+
279
+ return load_job(job_id)
280
+
281
+ def cancel_job(job_id):
282
  if job_id:
283
  manager.cancel(job_id)
284
+ return load_job("")
285
 
286
 
287
  with gr.Blocks(title="📚 Bilingual Book Builder") as demo:
288
  storage_hours = JOB_MAX_AGE_SECONDS // 3600
289
  gr.Markdown(f"""
290
  # Bilingual Book Builder
291
+
292
  Upload a **source** (original) EPUB and a **target** (translation) EPUB to create a side‑by‑side bilingual book.
293
+
294
  Processing may take hours – you can close this page and return later.
295
+
296
  When ready, the bilingual EPUB will be stored for **{storage_hours} hours**.
297
  """)
298
 
 
303
 
304
  with gr.Row():
305
  with gr.Column():
306
+ source_file = gr.File(label="Source EPUB", file_types=[".epub"], value=None)
307
+ target_file = gr.File(label="Target EPUB", file_types=[".epub"], value=None)
 
 
 
 
308
  with gr.Column():
309
+ source_lang = gr.Textbox(label="Source language (en, ru, de)", placeholder="Auto‑detect")
310
+ target_lang = gr.Textbox(label="Target language (en, ru, de)", placeholder="Auto‑detect")
311
+
312
+ with gr.Row():
313
+ with gr.Column(scale=1):
314
+ process_btn = gr.Button("🚀 Build Bilingual EPUB", variant="primary")
315
+ cancel_btn = gr.Button("❌ Cancel", variant="stop", visible=False)
316
  refresh_btn = gr.Button("🔄 Check Status")
317
+ with gr.Column(scale=2):
318
  status_msg = gr.Markdown("")
319
  output_file = gr.File(label="Download Bilingual EPUB", interactive=False, visible=False)
320
+
321
+ all_outputs = [
322
+ job_state, # 0
323
+ status_msg, # 1
324
+ source_lang, # 2
325
+ target_lang, # 3
326
+ output_file, # 4
327
+ source_file, # 5
328
+ target_file, # 6
329
+ process_btn, # 7
330
+ cancel_btn, # 8
331
+ ]
332
+
333
  process_btn.click(
334
+ fn=start_job,
335
  inputs=[source_file, target_file, source_lang, target_lang],
336
+ outputs=all_outputs,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
  )
338
 
339
  cancel_btn.click(
340
+ fn=cancel_job,
 
 
 
 
341
  inputs=job_state,
342
+ outputs=all_outputs,
 
 
 
 
 
 
 
 
 
 
343
  )
344
 
345
+ refresh_btn.click(
346
  fn=load_job,
347
  inputs=job_state,
348
+ outputs=all_outputs,
 
 
 
 
 
 
 
 
 
 
349
  )
350
 
351
+ demo.load(
352
  fn=load_job,
353
  inputs=job_state,
354
+ outputs=all_outputs,
 
 
 
 
 
 
 
 
 
 
355
  )
356
 
357
+
358
  if __name__ == "__main__":
359
  handler = logging.StreamHandler(sys.stdout)
 
360
  for module in LOG_MODULES:
361
  logger = logging.getLogger(module)
362
  logger.setLevel(logging.INFO)