shaheerawan3 commited on
Commit
f46c0bb
·
verified ·
1 Parent(s): c0dfc6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +235 -214
app.py CHANGED
@@ -1084,94 +1084,110 @@ class VideoGeneratorUI:
1084
 
1085
  def setup_ui(self):
1086
  st.set_page_config(layout="wide")
 
 
 
 
 
 
 
 
1087
 
1088
- # Custom CSS
1089
- st.markdown("""
1090
- <style>
1091
- .stApp {
1092
- max-width: 1200px;
1093
- margin: 0 auto;
1094
- }
1095
- .image-category {
1096
- margin-top: 2rem;
1097
- padding: 1rem;
1098
- border-radius: 0.5rem;
1099
- background: #f8f9fa;
1100
- }
1101
- .image-metadata {
1102
- font-size: 0.8rem;
1103
- color: #666;
1104
- margin-top: 0.5rem;
1105
- }
1106
- .submit-btn {
1107
- margin-top: 1rem;
1108
- padding: 0.5rem 1rem;
1109
- }
1110
- </style>
1111
- """, unsafe_allow_html=True)
1112
-
1113
  st.title("VaultGenix Video Generator")
1114
  st.markdown("Create professional videos for your digital legacy management platform")
1115
 
 
 
 
 
 
 
 
 
1116
  with st.container():
1117
- # Add form for prompt submission
1118
- with st.form(key='prompt_form'):
1119
- prompt = st.text_area("Enter your video script", height=200)
1120
- submit_button = st.form_submit_button(label='Analyze Script & Find Images')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1121
 
1122
  if submit_button and prompt:
1123
- # First show AI-selected images
1124
- with st.spinner("AI analyzing script and selecting relevant images..."):
1125
- try:
1126
- # Get AI-selected images first
1127
- keywords = self.generator.image_scraper.extract_key_topics(prompt)
1128
- st.write("🤖 AI-detected keywords:", ", ".join(keywords))
1129
-
1130
- image_categories = self.generator.image_scraper.get_images(prompt)
1131
-
1132
- # Store selections in session state
1133
- if 'selected_images' not in st.session_state:
1134
- st.session_state.selected_images = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1135
 
1136
- if image_categories and isinstance(image_categories, dict):
1137
- # Display AI-selected primary matches first
1138
- if 'primary' in image_categories and image_categories['primary']:
1139
- st.subheader("🎯 AI-Selected Most Relevant Images")
1140
- self.display_image_grid(image_categories['primary'])
1141
-
1142
- # Display secondary matches
1143
- if 'secondary' in image_categories and image_categories['secondary']:
1144
- st.subheader("🔄 AI-Selected Related Images")
1145
- self.display_image_grid(image_categories['secondary'])
1146
-
1147
- # Collect selected images
1148
- selected_images = []
1149
- for category in image_categories.values():
1150
- if isinstance(category, list):
1151
- for img in category:
1152
- key = f"img_{img['url']}"
1153
- if st.session_state.get(key, False):
1154
- selected_images.append(img['url'])
1155
-
1156
- st.session_state.selected_images = selected_images
1157
-
1158
- # Video generation section
1159
- if selected_images:
1160
- self.show_video_settings(prompt, selected_images)
1161
- else:
1162
- st.warning("Please select at least one image to generate the video.")
1163
 
 
 
 
 
1164
  else:
1165
- st.warning("No images found. Please try a different prompt.")
1166
-
1167
- except Exception as e:
1168
- st.error(f"An error occurred: {str(e)}")
1169
- print(f"Error in UI: {str(e)}")
1170
-
1171
- def display_image_grid(self, images: List[Dict[str, str]], cols: int = 3):
1172
- """Display images in a grid with metadata and confidence scores"""
1173
- if not images or not isinstance(images, list):
1174
- return
 
 
 
1175
 
1176
  n_images = len(images)
1177
  n_rows = (n_images + cols - 1) // cols
@@ -1187,40 +1203,48 @@ class VideoGeneratorUI:
1187
  try:
1188
  st.image(img['url'], use_container_width=True)
1189
 
1190
- # Add confidence score to checkbox label
1191
- confidence = img.get('relevance_score', 0) * 100
1192
- checkbox_label = f"Select (AI Confidence: {confidence:.1f}%)"
1193
 
1194
- st.checkbox(
1195
- checkbox_label,
1196
- key=f"img_{img['url']}",
1197
- help=f"Keywords: {img['keyword']}\nTags: {img['tags']}"
1198
- )
1199
 
1200
- # Show relevance metadata
1201
  st.markdown(
1202
- f"<div class='image-metadata'>"
1203
- f"<b>AI Relevance:</b> {img['relevance']}<br>"
1204
- f"<b>Keywords:</b> {img['keyword']}<br>"
1205
- f"<b>Match Type:</b> {img.get('category', 'General')}"
1206
- f"</div>",
1207
  unsafe_allow_html=True
1208
  )
1209
  except Exception as e:
1210
- print(f"Error displaying image: {e}")
 
 
 
1211
 
1212
- def show_video_settings(self, prompt: str, selected_images: List[str]):
1213
- """Show video generation settings and controls with effects"""
1214
- st.subheader("Video Settings")
 
 
 
 
1215
 
 
 
 
 
 
 
 
 
1216
  col1, col2 = st.columns(2)
 
1217
  with col1:
1218
- style = st.selectbox(
1219
- "Choose style",
1220
- options=["Professional", "Creative", "Educational"],
1221
- index=0
1222
- )
1223
- with col2:
1224
  duration = st.slider(
1225
  "Video duration (seconds)",
1226
  min_value=30,
@@ -1228,148 +1252,145 @@ class VideoGeneratorUI:
1228
  value=60,
1229
  step=30
1230
  )
1231
-
1232
- # Video effects controls
1233
- st.subheader("Video Effects")
1234
- effect_col1, effect_col2 = st.columns(2)
 
1235
 
1236
- with effect_col1:
1237
- zoom = st.slider(
1238
- "Zoom effect",
1239
- min_value=1.0,
1240
- max_value=1.5,
1241
- value=1.0,
1242
- step=0.1,
1243
- help="Gradually zoom in during each image segment"
1244
  )
1245
 
1246
- brightness = st.slider(
1247
- "Brightness",
1248
- min_value=0.5,
1249
- max_value=1.5,
1250
- value=1.0,
1251
  step=0.1
1252
  )
1253
 
1254
- with effect_col2:
1255
- contrast = st.slider(
1256
- "Contrast",
1257
- min_value=0.5,
1258
- max_value=1.5,
1259
- value=1.0,
1260
- step=0.1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1261
  )
1262
 
1263
- enable_blur = st.checkbox(
1264
- "Enable subtle blur effect",
1265
- value=False,
1266
- help="Add a subtle blur effect to transitions"
1267
  )
1268
-
1269
- video_effects = {
1270
- 'zoom': zoom,
1271
- 'brightness': brightness,
1272
- 'contrast': contrast,
1273
- 'blur': enable_blur
1274
- }
1275
-
1276
  if st.button("🎬 Generate Video", type="primary"):
1277
- if not selected_images:
1278
- st.error("Please select at least one image before generating the video.")
1279
  return
1280
-
1281
  try:
 
 
 
 
 
 
 
 
 
 
 
 
1282
  output_dir = "temp_videos"
1283
  os.makedirs(output_dir, exist_ok=True)
1284
  output_path = os.path.join(output_dir, f"vaultgenix_video_{int(time.time())}.mp4")
1285
 
1286
- with st.spinner("🎥 Generating your video..."):
1287
- video_path = self.generator.create_video(
1288
- prompt,
1289
- style,
1290
- duration,
1291
- output_path,
1292
- selected_images,
1293
- video_effects
1294
- )
1295
 
1296
- if os.path.exists(video_path):
1297
- st.success("✨ Video generated successfully!")
1298
-
1299
- # Display video
1300
- with open(video_path, 'rb') as video_file:
1301
- video_bytes = video_file.read()
1302
- st.video(video_bytes)
1303
-
1304
- # Download button
1305
- st.download_button(
1306
- label="⬇️ Download Video",
1307
- data=video_bytes,
1308
- file_name=os.path.basename(video_path),
1309
- mime="video/mp4"
1310
- )
1311
- else:
1312
- st.error("Video generation failed. Please try again.")
1313
-
1314
- except Exception as e:
1315
- st.error(f"Error generating video: {str(e)}")
1316
- print(f"Video generation error: {str(e)}")
1317
-
1318
- def generate_video(self, prompt: str, style: str, duration: int, selected_images: List[str]):
1319
- """Handle video generation with improved error handling"""
1320
- if not selected_images:
1321
- st.error("Please select at least one image before generating the video.")
1322
- return
1323
-
1324
- with st.spinner("🎥 Generating your video..."):
1325
- try:
1326
- # Create temp directory if it doesn't exist
1327
- temp_dir = Path("temp_videos")
1328
- temp_dir.mkdir(exist_ok=True)
1329
 
1330
- # Generate unique output path
1331
- output_path = str(temp_dir / f"vaultgenix_video_{int(time.time())}.mp4")
1332
-
1333
- # Generate video
1334
  video_path = self.generator.create_video(
1335
- prompt,
1336
- style,
1337
  duration,
1338
  output_path,
1339
- selected_images
 
 
1340
  )
1341
 
1342
- if video_path and os.path.exists(video_path):
1343
  st.success("✨ Video generated successfully!")
1344
 
1345
  # Display video
1346
- video_file = open(video_path, 'rb')
1347
- video_bytes = video_file.read()
1348
- st.video(video_bytes)
1349
-
1350
- # Download button
1351
- st.download_button(
1352
- label="⬇️ Download Video",
1353
- data=video_bytes,
1354
- file_name=os.path.basename(video_path),
1355
- mime="video/mp4"
1356
- )
1357
-
1358
- video_file.close()
 
 
 
 
 
 
 
 
1359
  else:
1360
  st.error("Video generation failed. Please try again.")
1361
 
1362
  except Exception as e:
1363
- st.error(f"Failed to generate video: {str(e)}")
1364
  print(f"Video generation error: {str(e)}")
1365
-
1366
- finally:
1367
- # Cleanup temporary files
1368
- try:
1369
- if 'video_file' in locals():
1370
- video_file.close()
1371
- except Exception as e:
1372
- print(f"Cleanup error: {e}")
1373
-
1374
  if __name__ == "__main__":
1375
  ui = VideoGeneratorUI()
 
1084
 
1085
  def setup_ui(self):
1086
  st.set_page_config(layout="wide")
1087
+
1088
+ # Initialize session state
1089
+ if 'selected_images' not in st.session_state:
1090
+ st.session_state.selected_images = []
1091
+ if 'processing_complete' not in st.session_state:
1092
+ st.session_state.processing_complete = False
1093
+ if 'current_step' not in st.session_state:
1094
+ st.session_state.current_step = 'input'
1095
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1096
  st.title("VaultGenix Video Generator")
1097
  st.markdown("Create professional videos for your digital legacy management platform")
1098
 
1099
+ # Add sidebar for advanced settings
1100
+ with st.sidebar:
1101
+ st.subheader("Advanced Settings")
1102
+ st.session_state.enable_transitions = st.checkbox("Enable Transitions", value=True)
1103
+ st.session_state.enable_captions = st.checkbox("Enable Captions", value=True)
1104
+ st.session_state.high_quality = st.checkbox("High Quality Export", value=True)
1105
+
1106
+ # Main content area
1107
  with st.container():
1108
+ if st.session_state.current_step == 'input':
1109
+ self.show_input_form()
1110
+ elif st.session_state.current_step == 'image_selection':
1111
+ self.show_image_selection()
1112
+ elif st.session_state.current_step == 'video_generation':
1113
+ self.show_video_generation()
1114
+
1115
+ def show_input_form(self):
1116
+ with st.form(key='prompt_form'):
1117
+ prompt = st.text_area("Enter your video script", height=200)
1118
+ col1, col2 = st.columns(2)
1119
+ with col1:
1120
+ st.session_state.video_style = st.selectbox(
1121
+ "Choose style",
1122
+ ["Professional", "Creative", "Educational"]
1123
+ )
1124
+ with col2:
1125
+ st.session_state.voice_style = st.selectbox(
1126
+ "Choose voice style",
1127
+ ["Professional Male", "Professional Female", "Casual Male", "Casual Female"]
1128
+ )
1129
+
1130
+ submit_button = st.form_submit_button(label='Find Images')
1131
 
1132
  if submit_button and prompt:
1133
+ st.session_state.prompt = prompt
1134
+ st.session_state.current_step = 'image_selection'
1135
+ st.rerun()
1136
+
1137
+ def show_image_selection(self):
1138
+ st.subheader("Select Images for Your Video")
1139
+
1140
+ # Display back button
1141
+ if st.button("← Back to Script"):
1142
+ st.session_state.current_step = 'input'
1143
+ st.rerun()
1144
+
1145
+ try:
1146
+ with st.spinner("AI analyzing script and selecting relevant images..."):
1147
+ keywords = self.generator.image_scraper.extract_key_topics(st.session_state.prompt)
1148
+ st.write("🤖 AI-detected keywords:", ", ".join(keywords))
1149
+
1150
+ image_categories = self.generator.image_scraper.get_images(st.session_state.prompt)
1151
+
1152
+ if image_categories and isinstance(image_categories, dict):
1153
+ # Create columns for image selection
1154
+ col1, col2 = st.columns([3, 1])
1155
+
1156
+ with col1:
1157
+ # Display images with selection
1158
+ selected_count = 0
1159
+ st.session_state.selected_images = [] # Reset selected images
1160
 
1161
+ for category, images in image_categories.items():
1162
+ if images:
1163
+ st.subheader(f"{category.title()} Images")
1164
+ selected = self.display_image_grid(images)
1165
+ selected_count += len(selected)
1166
+ st.session_state.selected_images.extend(selected)
1167
+
1168
+ with col2:
1169
+ # Show selection summary
1170
+ st.subheader("Selection Summary")
1171
+ st.write(f"Selected: {selected_count} images")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1172
 
1173
+ if selected_count > 0:
1174
+ if st.button("Continue to Video Generation →", type="primary"):
1175
+ st.session_state.current_step = 'video_generation'
1176
+ st.rerun()
1177
  else:
1178
+ st.warning("Please select at least one image")
1179
+
1180
+ else:
1181
+ st.warning("No images found. Please try a different prompt.")
1182
+
1183
+ except Exception as e:
1184
+ st.error(f"An error occurred: {str(e)}")
1185
+ print(f"Error in image selection: {str(e)}")
1186
+
1187
+
1188
+ def display_image_grid(self, images: List[Dict[str, str]], cols: int = 3) -> List[str]:
1189
+ """Display images in a grid and return selected image URLs"""
1190
+ selected_urls = []
1191
 
1192
  n_images = len(images)
1193
  n_rows = (n_images + cols - 1) // cols
 
1203
  try:
1204
  st.image(img['url'], use_container_width=True)
1205
 
1206
+ # Create unique key for each checkbox
1207
+ checkbox_key = f"img_select_{row}_{col}_{img['url'][-10:]}"
 
1208
 
1209
+ if st.checkbox(
1210
+ f"Select (Confidence: {img.get('relevance_score', 0)*100:.1f}%)",
1211
+ key=checkbox_key
1212
+ ):
1213
+ selected_urls.append(img['url'])
1214
 
1215
+ # Show image metadata
1216
  st.markdown(
1217
+ f"""<div style='font-size: 0.8em;'>
1218
+ Keywords: {img['keyword']}<br>
1219
+ Category: {img.get('category', 'General')}
1220
+ </div>""",
 
1221
  unsafe_allow_html=True
1222
  )
1223
  except Exception as e:
1224
+ st.error(f"Error displaying image: {str(e)}")
1225
+
1226
+ return selected_urls
1227
+
1228
 
1229
+ def show_video_generation(self):
1230
+ st.subheader("Video Generation Settings")
1231
+
1232
+ # Display back button
1233
+ if st.button("← Back to Image Selection"):
1234
+ st.session_state.current_step = 'image_selection'
1235
+ st.rerun()
1236
 
1237
+ # Show selected images preview
1238
+ st.write("Selected Images:")
1239
+ cols = st.columns(4)
1240
+ for idx, img_url in enumerate(st.session_state.selected_images):
1241
+ with cols[idx % 4]:
1242
+ st.image(img_url, width=150)
1243
+
1244
+ # Video settings
1245
  col1, col2 = st.columns(2)
1246
+
1247
  with col1:
 
 
 
 
 
 
1248
  duration = st.slider(
1249
  "Video duration (seconds)",
1250
  min_value=30,
 
1252
  value=60,
1253
  step=30
1254
  )
1255
+
1256
+ background_music = st.selectbox(
1257
+ "Background Music",
1258
+ ["None", "Corporate", "Upbeat", "Inspirational", "Technology"]
1259
+ )
1260
 
1261
+ with col2:
1262
+ transition_style = st.selectbox(
1263
+ "Transition Style",
1264
+ ["Fade", "Slide", "Zoom", "None"]
 
 
 
 
1265
  )
1266
 
1267
+ music_volume = st.slider(
1268
+ "Music Volume",
1269
+ min_value=0.0,
1270
+ max_value=1.0,
1271
+ value=0.3,
1272
  step=0.1
1273
  )
1274
 
1275
+ # Advanced video effects
1276
+ with st.expander("Advanced Video Effects"):
1277
+ effect_col1, effect_col2 = st.columns(2)
1278
+
1279
+ with effect_col1:
1280
+ zoom = st.slider(
1281
+ "Zoom Effect",
1282
+ min_value=1.0,
1283
+ max_value=1.5,
1284
+ value=1.0,
1285
+ step=0.1
1286
+ )
1287
+
1288
+ brightness = st.slider(
1289
+ "Brightness",
1290
+ min_value=0.5,
1291
+ max_value=1.5,
1292
+ value=1.0,
1293
+ step=0.1
1294
+ )
1295
+
1296
+ with effect_col2:
1297
+ contrast = st.slider(
1298
+ "Contrast",
1299
+ min_value=0.5,
1300
+ max_value=1.5,
1301
+ value=1.0,
1302
+ step=0.1
1303
+ )
1304
+
1305
+ enable_blur = st.checkbox(
1306
+ "Enable Blur Effect",
1307
+ value=False
1308
+ )
1309
+
1310
+ # Text overlay settings
1311
+ with st.expander("Text Overlay Settings"):
1312
+ subtitle_style = st.selectbox(
1313
+ "Subtitle Style",
1314
+ ["Modern", "Classic", "Minimal", "None"]
1315
  )
1316
 
1317
+ caption_position = st.selectbox(
1318
+ "Caption Position",
1319
+ ["Bottom", "Top", "Middle"]
 
1320
  )
1321
+
1322
+ # Generate video button
 
 
 
 
 
 
1323
  if st.button("🎬 Generate Video", type="primary"):
1324
+ if not st.session_state.selected_images:
1325
+ st.error("No images selected. Please go back and select images.")
1326
  return
1327
+
1328
  try:
1329
+ video_effects = {
1330
+ 'zoom': zoom,
1331
+ 'brightness': brightness,
1332
+ 'contrast': contrast,
1333
+ 'blur': enable_blur,
1334
+ 'transition_style': transition_style,
1335
+ 'subtitle_style': subtitle_style,
1336
+ 'caption_position': caption_position,
1337
+ 'background_music': background_music,
1338
+ 'music_volume': music_volume
1339
+ }
1340
+
1341
  output_dir = "temp_videos"
1342
  os.makedirs(output_dir, exist_ok=True)
1343
  output_path = os.path.join(output_dir, f"vaultgenix_video_{int(time.time())}.mp4")
1344
 
1345
+ progress_text = st.empty()
1346
+ progress_bar = st.progress(0)
 
 
 
 
 
 
 
1347
 
1348
+ # Generate video with progress updates
1349
+ def progress_callback(progress):
1350
+ progress_bar.progress(progress)
1351
+ progress_text.text(f"Generating video: {progress}% complete")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1352
 
 
 
 
 
1353
  video_path = self.generator.create_video(
1354
+ st.session_state.prompt,
1355
+ st.session_state.video_style,
1356
  duration,
1357
  output_path,
1358
+ st.session_state.selected_images,
1359
+ video_effects,
1360
+ progress_callback
1361
  )
1362
 
1363
+ if os.path.exists(video_path):
1364
  st.success("✨ Video generated successfully!")
1365
 
1366
  # Display video
1367
+ with open(video_path, 'rb') as video_file:
1368
+ video_bytes = video_file.read()
1369
+ st.video(video_bytes)
1370
+
1371
+ # Download options
1372
+ col1, col2 = st.columns(2)
1373
+ with col1:
1374
+ st.download_button(
1375
+ label="⬇️ Download Video",
1376
+ data=video_bytes,
1377
+ file_name=os.path.basename(video_path),
1378
+ mime="video/mp4"
1379
+ )
1380
+ with col2:
1381
+ if st.session_state.high_quality:
1382
+ st.download_button(
1383
+ label="⬇️ Download High Quality",
1384
+ data=video_bytes, # You'll need to implement high quality export
1385
+ file_name=f"high_quality_{os.path.basename(video_path)}",
1386
+ mime="video/mp4"
1387
+ )
1388
  else:
1389
  st.error("Video generation failed. Please try again.")
1390
 
1391
  except Exception as e:
1392
+ st.error(f"Error generating video: {str(e)}")
1393
  print(f"Video generation error: {str(e)}")
1394
+
 
 
 
 
 
 
 
 
1395
  if __name__ == "__main__":
1396
  ui = VideoGeneratorUI()