yukee1992 commited on
Commit
582136f
·
verified ·
1 Parent(s): b3e89b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -5
app.py CHANGED
@@ -366,29 +366,73 @@ def update_job_status(job_id: str, status: JobStatus, progress: int, message: st
366
 
367
  if request_data.get("callback_url"):
368
  try:
 
 
 
369
  callback_data = {
370
  "job_id": job_id,
371
  "status": status.value,
372
  "progress": progress,
373
  "message": message,
374
  "story_title": request_data["story_title"],
375
- "timestamp": time.time()
 
 
 
376
  }
377
 
 
378
  if status == JobStatus.COMPLETED and result:
379
- callback_data["result"] = result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380
 
381
  response = requests.post(
382
- request_data["callback_url"],
383
  json=callback_data,
384
- timeout=10
 
385
  )
386
- print(f"📢 Callback sent to n8n: Status {response.status_code}")
 
 
387
  except Exception as e:
388
  print(f"⚠️ Callback failed: {str(e)}")
389
 
390
  return True
391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392
  # BACKGROUND TASK FOR COMPLETE STORYBOOK GENERATION (n8n API)
393
  def generate_storybook_background(job_id: str):
394
  """Background task to generate complete storybook with images and text"""
 
366
 
367
  if request_data.get("callback_url"):
368
  try:
369
+ callback_url = request_data["callback_url"]
370
+
371
+ # Enhanced callback data for better Telegram notifications
372
  callback_data = {
373
  "job_id": job_id,
374
  "status": status.value,
375
  "progress": progress,
376
  "message": message,
377
  "story_title": request_data["story_title"],
378
+ "total_scenes": len(request_data["scenes"]),
379
+ "timestamp": time.time(),
380
+ "source": "huggingface-storybook-generator",
381
+ "estimated_time_remaining": calculate_remaining_time(job_id, progress)
382
  }
383
 
384
+ # Add result data for completed jobs
385
  if status == JobStatus.COMPLETED and result:
386
+ callback_data["result"] = {
387
+ "total_pages": result.get("total_pages", 0),
388
+ "generation_time": result.get("generation_time", 0),
389
+ "oci_bucket_url": result.get("oci_bucket_url", ""),
390
+ "pages_generated": result.get("generated_pages", 0)
391
+ }
392
+
393
+ # Add current scene info for processing jobs
394
+ if status == JobStatus.PROCESSING:
395
+ current_scene = progress // (100 // len(request_data["scenes"])) + 1
396
+ callback_data["current_scene"] = current_scene
397
+ callback_data["total_scenes"] = len(request_data["scenes"])
398
+ callback_data["scene_description"] = request_data["scenes"][current_scene-1]["visual"][:100] + "..."
399
+
400
+ headers = {
401
+ 'Content-Type': 'application/json',
402
+ 'User-Agent': 'Storybook-Generator/1.0'
403
+ }
404
 
405
  response = requests.post(
406
+ callback_url,
407
  json=callback_data,
408
+ headers=headers,
409
+ timeout=30
410
  )
411
+
412
+ print(f"📢 Callback sent: Status {response.status_code}")
413
+
414
  except Exception as e:
415
  print(f"⚠️ Callback failed: {str(e)}")
416
 
417
  return True
418
 
419
+ def calculate_remaining_time(job_id, progress):
420
+ """Calculate estimated time remaining"""
421
+ if progress == 0:
422
+ return "Calculating..."
423
+
424
+ job_data = job_storage.get(job_id)
425
+ if not job_data:
426
+ return "Unknown"
427
+
428
+ time_elapsed = time.time() - job_data["created_at"]
429
+ if progress > 0:
430
+ total_estimated = (time_elapsed / progress) * 100
431
+ remaining = total_estimated - time_elapsed
432
+ return f"{int(remaining // 60)}m {int(remaining % 60)}s"
433
+
434
+ return "Unknown"
435
+
436
  # BACKGROUND TASK FOR COMPLETE STORYBOOK GENERATION (n8n API)
437
  def generate_storybook_background(job_id: str):
438
  """Background task to generate complete storybook with images and text"""