codelion commited on
Commit
915daef
·
verified ·
1 Parent(s): ffe3b64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -12
app.py CHANGED
@@ -268,7 +268,7 @@ def generate_item(user_input, ideas, generate_video=False, max_retries=3):
268
  while not operation.done:
269
  if time.time() - start_time > timeout_seconds:
270
  raise TimeoutError("Video generation timed out")
271
- time.sleep(5) # Reduced polling interval
272
  operation = client.operations.get(operation)
273
  if operation.error:
274
  raise ValueError(f"Video generation failed: {operation.error.message}")
@@ -376,22 +376,28 @@ def start_feed(user_input, generate_video, current_index, feed_items):
376
  current_user_input = user_input
377
  is_loading = True
378
  share_links = ""
 
379
 
380
  html_content = generate_html([], False, 0, user_input, is_loading, "initializing", random.choice(PROGRESS_STAGES["initializing"]), 10)
381
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
382
 
383
  try:
 
384
  html_content = generate_html([], False, 0, user_input, is_loading, "generating_ideas", random.choice(PROGRESS_STAGES["generating_ideas"]), 20)
385
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
386
  ideas = generate_ideas(user_input)
387
 
388
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
389
  for progress in item_generator:
 
 
 
390
  logging.debug(f"Progress update: {progress}")
391
  if isinstance(progress, dict) and "stage" in progress:
392
  html_content = generate_html([], False, 0, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
393
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
394
  else:
 
395
  item = progress
396
  feed_items = [item]
397
  current_index = 0
@@ -401,6 +407,29 @@ def start_feed(user_input, generate_video, current_index, feed_items):
401
  logging.debug("Feed generation complete")
402
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
403
  return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
  except Exception as e:
405
  logging.error(f"Error in start_feed: {e}")
406
  html_content = """
@@ -418,7 +447,7 @@ def start_feed(user_input, generate_video, current_index, feed_items):
418
  color: white;
419
  font-family: Arial, sans-serif;
420
  ">
421
- <p>Error generating content. Please try again!</p>
422
  </div>
423
  """
424
  is_loading = False
@@ -434,6 +463,7 @@ def load_next(user_input, generate_video, current_index, feed_items):
434
  user_input = current_user_input
435
  is_loading = True
436
  share_links = ""
 
437
 
438
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading, "initializing", random.choice(PROGRESS_STAGES["initializing"]), 10)
439
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
@@ -452,17 +482,22 @@ def load_next(user_input, generate_video, current_index, feed_items):
452
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
453
  return
454
  else:
 
455
  ideas = feed_items[-1]['ideas'] if feed_items else generate_ideas(user_input)
456
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading, "generating_ideas", random.choice(PROGRESS_STAGES["generating_ideas"]), 20)
457
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
458
 
459
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
460
  for progress in item_generator:
 
 
 
461
  logging.debug(f"Progress update: {progress}")
462
  if isinstance(progress, dict) and "stage" in progress:
463
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
464
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
465
  else:
 
466
  new_item = progress
467
  feed_items.append(new_item)
468
  current_index = len(feed_items) - 1
@@ -476,6 +511,29 @@ def load_next(user_input, generate_video, current_index, feed_items):
476
  logging.debug("New feed item generated")
477
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
478
  return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
  except Exception as e:
480
  logging.error(f"Error in load_next: {e}")
481
  html_content = """
@@ -493,7 +551,7 @@ def load_next(user_input, generate_video, current_index, feed_items):
493
  color: white;
494
  font-family: Arial, sans-serif;
495
  ">
496
- <p>Error generating content. Please try again!</p>
497
  </div>
498
  """
499
  is_loading = False
@@ -544,15 +602,15 @@ def generate_share_links(image_base64, video_base64, caption):
544
  video_data_url = f"data:video/mp4;base64,{video_base64}"
545
  download_links += f"""
546
  <a href="{video_data_url}" download="feed_video.mp4" style="
547
- background-color: #4CAF50;
548
- color: white;
549
- padding: 8px 16px;
550
- border-radius: 5px;
551
- text-decoration: none;
552
- font-size: 14px;
553
- font-weight: bold;
554
- transition: background-color 0.3s;
555
- " onmouseover="this.style.backgroundColor='#45a049'" onmouseout="this.style.backgroundColor='#4CAF50'">Download Video</a>
556
  """
557
  download_links += "</div>"
558
 
 
268
  while not operation.done:
269
  if time.time() - start_time > timeout_seconds:
270
  raise TimeoutError("Video generation timed out")
271
+ time.sleep(5)
272
  operation = client.operations.get(operation)
273
  if operation.error:
274
  raise ValueError(f"Video generation failed: {operation.error.message}")
 
376
  current_user_input = user_input
377
  is_loading = True
378
  share_links = ""
379
+ timeout_seconds = 120 # Timeout for entire generation process
380
 
381
  html_content = generate_html([], False, 0, user_input, is_loading, "initializing", random.choice(PROGRESS_STAGES["initializing"]), 10)
382
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
383
 
384
  try:
385
+ start_time = time.time()
386
  html_content = generate_html([], False, 0, user_input, is_loading, "generating_ideas", random.choice(PROGRESS_STAGES["generating_ideas"]), 20)
387
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
388
  ideas = generate_ideas(user_input)
389
 
390
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
391
  for progress in item_generator:
392
+ if time.time() - start_time > timeout_seconds:
393
+ logging.error("Generation timed out")
394
+ raise TimeoutError("Feed generation timed out")
395
  logging.debug(f"Progress update: {progress}")
396
  if isinstance(progress, dict) and "stage" in progress:
397
  html_content = generate_html([], False, 0, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
398
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
399
  else:
400
+ logging.debug("Received final item from generator")
401
  item = progress
402
  feed_items = [item]
403
  current_index = 0
 
407
  logging.debug("Feed generation complete")
408
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
409
  return
410
+ logging.warning("Generator exhausted without returning final item")
411
+ # Fallback if generator doesn't return
412
+ html_content = """
413
+ <div style="
414
+ display: flex;
415
+ flex-direction: column;
416
+ align-items: center;
417
+ justify-content: center;
418
+ max-width: 360px;
419
+ margin: 0 auto;
420
+ background-color: #000;
421
+ height: 640px;
422
+ border: 1px solid #333;
423
+ border-radius: 10px;
424
+ color: white;
425
+ font-family: Arial, sans-serif;
426
+ ">
427
+ <p>Generation incomplete. Please try again!</p>
428
+ </div>
429
+ """
430
+ is_loading = False
431
+ yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
432
+ return
433
  except Exception as e:
434
  logging.error(f"Error in start_feed: {e}")
435
  html_content = """
 
447
  color: white;
448
  font-family: Arial, sans-serif;
449
  ">
450
+ <p>Error generating content: {str(e)}. Please try again!</p>
451
  </div>
452
  """
453
  is_loading = False
 
463
  user_input = current_user_input
464
  is_loading = True
465
  share_links = ""
466
+ timeout_seconds = 120 # Timeout for entire generation process
467
 
468
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading, "initializing", random.choice(PROGRESS_STAGES["initializing"]), 10)
469
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
 
482
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
483
  return
484
  else:
485
+ start_time = time.time()
486
  ideas = feed_items[-1]['ideas'] if feed_items else generate_ideas(user_input)
487
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading, "generating_ideas", random.choice(PROGRESS_STAGES["generating_ideas"]), 20)
488
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
489
 
490
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
491
  for progress in item_generator:
492
+ if time.time() - start_time > timeout_seconds:
493
+ logging.error("Generation timed out")
494
+ raise TimeoutError("Feed generation timed out")
495
  logging.debug(f"Progress update: {progress}")
496
  if isinstance(progress, dict) and "stage" in progress:
497
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
498
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
499
  else:
500
+ logging.debug("Received final item from generator")
501
  new_item = progress
502
  feed_items.append(new_item)
503
  current_index = len(feed_items) - 1
 
511
  logging.debug("New feed item generated")
512
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
513
  return
514
+ logging.warning("Generator exhausted without returning final item")
515
+ # Fallback if generator doesn't return
516
+ html_content = """
517
+ <div style="
518
+ display: flex;
519
+ flex-direction: column;
520
+ align-items: center;
521
+ justify-content: center;
522
+ max-width: 360px;
523
+ margin: 0 auto;
524
+ background-color: #000;
525
+ height: 640px;
526
+ border: 1px solid #333;
527
+ border-radius: 10px;
528
+ color: white;
529
+ font-family: Arial, sans-serif;
530
+ ">
531
+ <p>Generation incomplete. Please try again!</p>
532
+ </div>
533
+ """
534
+ is_loading = False
535
+ yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
536
+ return
537
  except Exception as e:
538
  logging.error(f"Error in load_next: {e}")
539
  html_content = """
 
551
  color: white;
552
  font-family: Arial, sans-serif;
553
  ">
554
+ <p>Error generating content: {str(e)}. Please try again!</p>
555
  </div>
556
  """
557
  is_loading = False
 
602
  video_data_url = f"data:video/mp4;base64,{video_base64}"
603
  download_links += f"""
604
  <a href="{video_data_url}" download="feed_video.mp4" style="
605
+ background-color: #4CAF50;
606
+ color: white;
607
+ padding: 8px 16px;
608
+ border-radius: 5px;
609
+ text-decoration: none;
610
+ font-size: 14px;
611
+ font-weight: bold;
612
+ transition: background-color 0.3s;
613
+ " onmouseover="this.style.backgroundColor='#45a049'" onmouseout="this.style.backgroundColor='#4CAF50'">Download Video</a>
614
  """
615
  download_links += "</div>"
616