Dhan98 commited on
Commit
8d4cb5f
Β·
verified Β·
1 Parent(s): 8321365

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -49
app.py CHANGED
@@ -1,35 +1,17 @@
1
  import streamlit as st
2
  import torch
3
- from transformers import pipeline, AutoProcessor, AutoModel
4
  from PIL import Image
5
  import numpy as np
6
  import cv2
7
  import tempfile
8
  import os
9
-
10
- def save_video_frames(frames, output_path, fps=30):
11
- """
12
- Save video frames using OpenCV instead of moviepy
13
- """
14
- # Get frame dimensions
15
- height, width = frames[0].shape[:2]
16
-
17
- # Initialize video writer
18
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
19
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
20
-
21
- # Write frames
22
- for frame in frames:
23
- # Convert from RGB to BGR (OpenCV uses BGR)
24
- frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
25
- out.write(frame_bgr)
26
-
27
- # Release video writer
28
- out.release()
29
 
30
  def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
31
  """
32
- Generate a video from an image using LTX-Video and image captioning.
33
  """
34
  try:
35
  if progress_bar:
@@ -43,44 +25,44 @@ def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
43
  st.write(f"Generated caption: *{caption}*")
44
 
45
  if progress_bar:
46
- progress_bar.progress(0.3, "Loading LTX-Video model...")
47
 
48
- # Initialize LTX-Video components
49
- processor = AutoProcessor.from_pretrained("Lightricks/ltx-video")
50
- model = AutoModel.from_pretrained("Lightricks/ltx-video")
 
 
51
 
52
  if progress_bar:
53
  progress_bar.progress(0.4, "Processing image...")
54
 
55
- # Process image
56
- inputs = processor(images=image, return_tensors="pt")
 
 
57
 
58
  if progress_bar:
59
  progress_bar.progress(0.5, "Generating video frames...")
60
 
61
- # Generate video frames
62
- num_frames = duration_seconds * 30 # 30 FPS
63
- with torch.no_grad():
64
- video_frames = model.generate(
65
- **inputs,
66
- num_frames=num_frames,
67
- num_inference_steps=50,
68
- guidance_scale=7.5,
69
- prompt=caption,
70
- ).videos[0]
71
 
72
  if progress_bar:
73
  progress_bar.progress(0.8, "Creating final video...")
74
 
75
- # Convert frames to numpy arrays
76
- frames = [np.array(frame) for frame in video_frames]
77
-
78
  # Create temporary file for video
79
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
80
  output_path = tmp_file.name
81
 
82
- # Save video using OpenCV
83
- save_video_frames(frames, output_path)
84
 
85
  if progress_bar:
86
  progress_bar.progress(1.0, "Video generation complete!")
@@ -89,23 +71,25 @@ def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
89
 
90
  except Exception as e:
91
  st.error(f"Error generating video: {str(e)}")
92
- raise # Re-raise the exception to see the full error message
93
- return None, None
94
 
95
  def main():
96
- st.set_page_config(page_title="Video Generator", page_icon="πŸŽ₯")
97
 
98
- st.title("πŸŽ₯ AI Video Generator")
99
  st.write("""
100
  Upload an image to generate a video with AI-powered motion and transitions.
101
  The app will automatically generate a caption for your image and use it as inspiration for the video.
102
  """)
103
 
 
 
 
104
  # File uploader
105
  uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'])
106
 
107
- # Duration selector
108
- duration = st.slider("Video duration (seconds)", min_value=1, max_value=30, value=10)
109
 
110
  if uploaded_file is not None:
111
  # Display uploaded image
 
1
  import streamlit as st
2
  import torch
3
+ from transformers import pipeline
4
  from PIL import Image
5
  import numpy as np
6
  import cv2
7
  import tempfile
8
  import os
9
+ from diffusers import VideoToVideoSDPipeline
10
+ from diffusers.utils import export_to_video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def generate_video_from_image(image, duration_seconds=10, progress_bar=None):
13
  """
14
+ Generate a video from an image using VideoToVideoSDPipeline.
15
  """
16
  try:
17
  if progress_bar:
 
25
  st.write(f"Generated caption: *{caption}*")
26
 
27
  if progress_bar:
28
+ progress_bar.progress(0.3, "Loading Video Generation model...")
29
 
30
+ # Initialize Video Generation pipeline
31
+ pipeline = VideoToVideoSDPipeline.from_pretrained(
32
+ "cerspense/zeroscope_v2_576w",
33
+ torch_dtype=torch.float16
34
+ ).to("cuda" if torch.cuda.is_available() else "cpu")
35
 
36
  if progress_bar:
37
  progress_bar.progress(0.4, "Processing image...")
38
 
39
+ # Prepare image
40
+ if image.mode != "RGB":
41
+ image = image.convert("RGB")
42
+ image = image.resize((576, 320)) # Resize to model's expected size
43
 
44
  if progress_bar:
45
  progress_bar.progress(0.5, "Generating video frames...")
46
 
47
+ # Generate video
48
+ num_frames = duration_seconds * 8 # 8 FPS for this model
49
+ video_frames = pipeline(
50
+ image,
51
+ num_inference_steps=50,
52
+ num_frames=num_frames,
53
+ guidance_scale=7.5,
54
+ prompt=caption,
55
+ ).videos[0]
 
56
 
57
  if progress_bar:
58
  progress_bar.progress(0.8, "Creating final video...")
59
 
 
 
 
60
  # Create temporary file for video
61
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
62
  output_path = tmp_file.name
63
 
64
+ # Export video frames
65
+ export_to_video(video_frames, output_path, fps=8)
66
 
67
  if progress_bar:
68
  progress_bar.progress(1.0, "Video generation complete!")
 
71
 
72
  except Exception as e:
73
  st.error(f"Error generating video: {str(e)}")
74
+ raise
 
75
 
76
  def main():
77
+ st.set_page_config(page_title="AI Video Generator", page_icon="πŸŽ₯")
78
 
79
+ st.title("πŸŽ₯ Video Generator")
80
  st.write("""
81
  Upload an image to generate a video with AI-powered motion and transitions.
82
  The app will automatically generate a caption for your image and use it as inspiration for the video.
83
  """)
84
 
85
+ # Add warning about computational requirements
86
+ st.warning("Note: Video generation may take several minutes depending on the duration and available computing resources.")
87
+
88
  # File uploader
89
  uploaded_file = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'])
90
 
91
+ # Duration selector (adjusted for this model's capabilities)
92
+ duration = st.slider("Video duration (seconds)", min_value=1, max_value=15, value=5)
93
 
94
  if uploaded_file is not None:
95
  # Display uploaded image