shaheerawan3 commited on
Commit
6667bef
·
verified ·
1 Parent(s): 451ed16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -154
app.py CHANGED
@@ -450,6 +450,29 @@ def main():
450
  st.error("Please fill in at least the topic and key message fields.")
451
  return
452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
453
  try:
454
  # Create progress tracker
455
  update_progress = create_progress_tracker()
@@ -472,163 +495,28 @@ def main():
472
  # Display results
473
  update_progress(100, "Complete!")
474
 
475
- # ... [display logic remains the same] ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
 
477
  except Exception as e:
478
  st.error(f"An error occurred: {str(e)}")
479
- finally:
480
- # Cleanup
481
- if 'video_path' in locals() and video_path and os.path.exists(video_path):
482
- try:
483
- os.remove(video_path)
484
- except:
485
- pass
486
-
487
- # Initialize style configuration
488
- style_config = ContentStyle(
489
- font_size=font_size,
490
- font_color=font_color,
491
- background_effect=background_effect,
492
- transition_effect=transition_effect,
493
- text_animation=text_animation,
494
- theme=theme,
495
- layout=layout
496
- )
497
-
498
- # Collect preferences
499
- preferences = {
500
- 'topic': topic,
501
- 'style': style,
502
- 'tone': tone,
503
- 'audience': audience,
504
- 'purpose': purpose,
505
- 'message': message,
506
- 'mood': mood,
507
- 'voice_type': voice_type
508
- }
509
-
510
- # Show progress
511
- with st.spinner("🎨 Generating your content..."):
512
- try:
513
- progress_bar = st.progress(0)
514
- status_text = st.empty() # For showing status updates
515
-
516
- # Initialize AI engine
517
- status_text.text("Initializing AI engine...")
518
- ai_engine = AIContentEngine()
519
- progress_bar.progress(10)
520
-
521
- # Generate content with better error handling
522
- status_text.text("Generating content...")
523
- try:
524
- content_package = ai_engine.generate_content_package(preferences)
525
- progress_bar.progress(30)
526
- except Exception as e:
527
- st.error(f"Error in content generation: {str(e)}")
528
- return
529
-
530
- # Preview generated content with validation
531
- if all(key in content_package for key in ['main_content', 'quote', 'tips', 'call_to_action', 'hashtags']):
532
- status_text.text("Preparing content preview...")
533
- st.subheader("📝 Generated Content Preview")
534
- with st.expander("View Content Details", expanded=True):
535
- st.markdown("### Main Content")
536
- st.write(content_package['main_content'])
537
-
538
- st.markdown("### Quote")
539
- st.write(content_package['quote'])
540
-
541
- st.markdown("### Tips")
542
- st.write(content_package['tips'])
543
-
544
- st.markdown("### Call to Action")
545
- st.write(content_package['call_to_action'])
546
-
547
- st.markdown("### Hashtags")
548
- st.write(content_package['hashtags'])
549
-
550
- progress_bar.progress(50)
551
- else:
552
- st.error("Content generation incomplete. Please try again.")
553
- return
554
-
555
- # Initialize video generator with progress tracking
556
- status_text.text("Preparing video generator...")
557
- video_gen = VideoGenerator()
558
- progress_bar.progress(60)
559
-
560
- # Generate video with enhanced error handling
561
- status_text.text("Creating video...")
562
- try:
563
- video_path = video_gen.create_video(content_package, preferences, style_config)
564
- progress_bar.progress(90)
565
- except Exception as e:
566
- st.error(f"Error in video generation: {str(e)}")
567
- return
568
-
569
- if video_path and os.path.exists(video_path):
570
- progress_bar.progress(100)
571
- status_text.text("Completed!")
572
-
573
- try:
574
- # Display video with error handling
575
- st.subheader("🎥 Generated Video")
576
- video_file = open(video_path, 'rb')
577
- video_bytes = video_file.read()
578
- video_file.close() # Properly close the file
579
-
580
- if len(video_bytes) > 0:
581
- st.video(video_bytes)
582
-
583
- # Download button
584
- st.download_button(
585
- label="Download Video",
586
- data=video_bytes,
587
- file_name=f"content_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4",
588
- mime="video/mp4"
589
- )
590
-
591
- # Analytics and metrics with validation
592
- st.subheader("📊 Content Analytics")
593
- col1, col2, col3 = st.columns(3)
594
-
595
- quality_score = ai_engine.analyze_content_quality(content_package['main_content'])
596
-
597
- with col1:
598
- st.metric("Content Quality Score",
599
- f"{quality_score:.2f}")
600
- with col2:
601
- video_duration = VideoFileClip(video_path).duration
602
- st.metric("Video Duration", f"{video_duration:.1f}s")
603
- with col3:
604
- engagement_score = "High" if quality_score > 0.7 else "Medium"
605
- st.metric("Estimated Engagement", engagement_score)
606
- else:
607
- st.error("Generated video file is empty. Please try again.")
608
-
609
- except Exception as e:
610
- st.error(f"Error displaying video: {str(e)}")
611
-
612
- finally:
613
- # Cleanup temporary files
614
- try:
615
- if 'video_file' in locals():
616
- video_file.close()
617
- if os.path.exists(video_path):
618
- os.remove(video_path)
619
- except Exception as e:
620
- st.warning(f"Warning: Could not clean up temporary files: {str(e)}")
621
- else:
622
- st.error("Video generation failed. Please try again.")
623
-
624
- except Exception as e:
625
- st.error(f"An unexpected error occurred: {str(e)}")
626
- if 'progress_bar' in locals():
627
- progress_bar.progress(0)
628
 
629
- finally:
630
- if 'status_text' in locals():
631
- status_text.empty()
632
-
633
  if __name__ == "__main__":
634
  main()
 
450
  st.error("Please fill in at least the topic and key message fields.")
451
  return
452
 
453
+ # Initialize style configuration
454
+ style_config = ContentStyle(
455
+ font_size=font_size,
456
+ font_color=font_color,
457
+ background_effect=background_effect,
458
+ transition_effect=transition_effect,
459
+ text_animation=text_animation,
460
+ theme=theme,
461
+ layout=layout
462
+ )
463
+
464
+ # Collect preferences before trying to use them
465
+ preferences = {
466
+ 'topic': topic,
467
+ 'style': style,
468
+ 'tone': tone,
469
+ 'audience': audience,
470
+ 'purpose': purpose,
471
+ 'message': message,
472
+ 'mood': mood,
473
+ 'voice_type': voice_type
474
+ }
475
+
476
  try:
477
  # Create progress tracker
478
  update_progress = create_progress_tracker()
 
495
  # Display results
496
  update_progress(100, "Complete!")
497
 
498
+ if video_path and os.path.exists(video_path):
499
+ try:
500
+ video_file = open(video_path, 'rb')
501
+ video_bytes = video_file.read()
502
+ st.video(video_bytes)
503
+ video_file.close()
504
+
505
+ # Download button
506
+ st.download_button(
507
+ label="Download Video",
508
+ data=video_bytes,
509
+ file_name=f"content_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4",
510
+ mime="video/mp4"
511
+ )
512
+ finally:
513
+ if 'video_file' in locals():
514
+ video_file.close()
515
+ if os.path.exists(video_path):
516
+ os.remove(video_path)
517
 
518
  except Exception as e:
519
  st.error(f"An error occurred: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
 
 
 
 
 
521
  if __name__ == "__main__":
522
  main()