multimodalart HF Staff commited on
Commit
4917153
·
verified ·
1 Parent(s): 877340d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -62
app.py CHANGED
@@ -68,71 +68,66 @@ def process_video(
68
  manual_height,
69
  manual_width,
70
  guidance_scale,
71
- progress=gr.Progress()
72
  ):
73
- """Process the video with Lucy Edit"""
74
- try:
75
- # Load and preprocess video
76
- progress(0.2, desc="Loading video...")
 
 
 
 
77
 
78
- # Get video dimensions
79
- temp_video = load_video(video_path)
80
- if temp_video and len(temp_video) > 0:
81
- original_width, original_height = temp_video[0].size
82
-
83
- # Calculate dimensions
84
- if auto_resize:
85
- width, height = calculate_resolution(original_width, original_height)
86
- gr.Info(f"Auto-resized from {original_width}x{original_height} to {width}x{height} (preserving aspect ratio)")
87
- else:
88
- width, height = manual_width, manual_height
89
- if abs((original_width/original_height) - (width/height)) > 0.1:
90
- gr.Warning(f"Output aspect ratio ({width}x{height}) differs significantly from input ({original_width}x{original_height}). Video may appear stretched.")
91
  else:
92
- raise ValueError("Could not load video or video is empty")
93
-
94
- # Convert video function
95
- def convert_video(video: List[Image.Image]) -> List[Image.Image]:
96
- # Ensure we don't exceed the video length
97
- frames_to_load = min(len(video), num_frames)
98
- video_frames = video[:frames_to_load]
99
- # Resize frames
100
- video_frames = [frame.resize((width, height)) for frame in video_frames]
101
- return video_frames
102
-
103
- # Load video from file path
104
- video = load_video(video_path, convert_method=convert_video)
105
-
106
- # Ensure we have the right number of frames
107
- if len(video) < num_frames:
108
- gr.Warning(f"Video has only {len(video)} frames, using all available frames.")
109
- num_frames = len(video)
110
-
111
- # Generate edited video
112
- progress(0.5, desc="Generating edited video...")
113
- output = pipe(
114
- prompt=prompt,
115
- video=video,
116
- negative_prompt=negative_prompt,
117
- height=height,
118
- width=width,
119
- num_frames=num_frames,
120
- guidance_scale=guidance_scale,
121
- ).frames[0]
122
-
123
- # Export to temporary file
124
- progress(0.9, desc="Exporting video...")
125
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_file:
126
- output_path = tmp_file.name
127
-
128
- export_to_video(output, output_path, fps=24)
129
-
130
- progress(1.0, desc="Complete!")
131
- return output_path
132
-
133
- except Exception as e:
134
- gr.Error(f"An error occurred: {str(e)}")
135
- return None
136
 
137
  # Create Gradio interface
138
  with gr.Blocks(title="Lucy Edit - Video Editing with Text") as demo:
 
68
  manual_height,
69
  manual_width,
70
  guidance_scale,
71
+ progress=gr.Progress(track_tqdm=True)
72
  ):
73
+ # Load and preprocess video
74
+ progress(0.2, desc="Loading video...")
75
+
76
+ # Get video dimensions
77
+ temp_video = load_video(video_path)
78
+ print(len(temp_video))
79
+ if temp_video and len(temp_video) > 0:
80
+ original_width, original_height = temp_video[0].size
81
 
82
+ # Calculate dimensions
83
+ if auto_resize:
84
+ width, height = calculate_resolution(original_width, original_height)
85
+ gr.Info(f"Auto-resized from {original_width}x{original_height} to {width}x{height} (preserving aspect ratio)")
 
 
 
 
 
 
 
 
 
86
  else:
87
+ width, height = manual_width, manual_height
88
+ if abs((original_width/original_height) - (width/height)) > 0.1:
89
+ gr.Warning(f"Output aspect ratio ({width}x{height}) differs significantly from input ({original_width}x{original_height}). Video may appear stretched.")
90
+ else:
91
+ raise gr.Error("Could not load video or video is empty")
92
+
93
+ # Convert video function
94
+ def convert_video(video: List[Image.Image]) -> List[Image.Image]:
95
+ # Ensure we don't exceed the video length
96
+ frames_to_load = min(len(video), num_frames)
97
+ video_frames = video[:frames_to_load]
98
+ # Resize frames
99
+ video_frames = [frame.resize((width, height)) for frame in video_frames]
100
+ return video_frames
101
+
102
+ # Load video from file path
103
+ video = load_video(video_path, convert_method=convert_video)
104
+
105
+ # Ensure we have the right number of frames
106
+ if len(video) < num_frames:
107
+ gr.Warning(f"Video has only {len(video)} frames, using all available frames.")
108
+ num_frames = len(video)
109
+
110
+ # Generate edited video
111
+ progress(0.5, desc="Generating edited video...")
112
+ output = pipe(
113
+ prompt=prompt,
114
+ video=video,
115
+ negative_prompt=negative_prompt,
116
+ height=height,
117
+ width=width,
118
+ num_frames=num_frames,
119
+ guidance_scale=guidance_scale,
120
+ ).frames[0]
121
+
122
+ # Export to temporary file
123
+ progress(0.9, desc="Exporting video...")
124
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_file:
125
+ output_path = tmp_file.name
126
+
127
+ export_to_video(output, output_path, fps=24)
128
+
129
+ progress(1.0, desc="Complete!")
130
+ return output_path
131
 
132
  # Create Gradio interface
133
  with gr.Blocks(title="Lucy Edit - Video Editing with Text") as demo: