Spaces:
Sleeping
Sleeping
optimize
Browse files
app.py
CHANGED
@@ -1,56 +1,67 @@
|
|
1 |
-
import torch
|
2 |
-
import gradio as gr
|
3 |
-
from diffusers import DiffusionPipeline
|
|
|
4 |
|
5 |
-
def load_video_model():
|
6 |
-
try:
|
7 |
-
# Ensure all necessary libraries are imported
|
8 |
-
import sentencepiece
|
9 |
|
10 |
-
# Load the model with specific error handling
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
21 |
-
# Load the model when the script starts
|
22 |
-
pipe = load_video_model()
|
23 |
|
24 |
-
def generate_video(prompt):
|
25 |
-
if pipe is None:
|
26 |
-
return "Error: Model could not be loaded. Check your dependencies."
|
27 |
|
28 |
-
try:
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
#
|
33 |
-
|
|
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
imageio.mimsave(output_path, images, fps=5)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
# Create Gradio interface
|
44 |
-
demo = gr.Interface(
|
45 |
-
fn=generate_video,
|
46 |
-
inputs=gr.Textbox(label="Enter Video Generation Prompt"),
|
47 |
-
outputs=gr.Video(label="Generated Video"),
|
48 |
-
title="LTX-Video Generation",
|
49 |
-
description="Generate a video using Lightricks Video Diffusion Model"
|
50 |
-
)
|
51 |
|
52 |
-
if __name__ == "__main__":
|
53 |
-
if pipe is not None:
|
54 |
-
demo.launch()
|
55 |
-
else:
|
56 |
print("Could not launch app due to model loading failure.")
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
+
import time # For monitoring time
|
5 |
|
6 |
+
def load_video_model():
|
7 |
+
try:
|
8 |
+
# Ensure all necessary libraries are imported
|
9 |
+
import sentencepiece
|
10 |
|
11 |
+
# Load the model with specific error handling
|
12 |
+
print("Loading model...")
|
13 |
+
pipe = DiffusionPipeline.from_pretrained("Lightricks/LTX-Video")
|
14 |
+
print("Model loaded successfully!")
|
15 |
+
return pipe
|
16 |
+
except ImportError as e:
|
17 |
+
print(f"Dependency Error: {e}")
|
18 |
+
print("Please install required libraries: pip install sentencepiece diffusers transformers torch gradio imageio")
|
19 |
+
return None
|
20 |
+
except Exception as e:
|
21 |
+
print(f"Error loading model: {e}")
|
22 |
+
return None
|
23 |
|
24 |
+
# Load the model when the script starts
|
25 |
+
pipe = load_video_model()
|
26 |
|
27 |
+
def generate_video(prompt):
|
28 |
+
if pipe is None:
|
29 |
+
return "Error: Model could not be loaded. Check your dependencies."
|
30 |
|
31 |
+
try:
|
32 |
+
print("Starting video generation...")
|
33 |
+
start_time = time.time()
|
34 |
|
35 |
+
# Generate video frames with a limit of 24 frames (approx. 5 seconds at 5 FPS)
|
36 |
+
images = pipe(prompt, num_inference_steps=24).images
|
37 |
+
print("Video frames generated successfully!")
|
38 |
|
39 |
+
# Save the generated images as a video
|
40 |
+
output_path = "generated_video.mp4"
|
|
|
41 |
|
42 |
+
# Convert images to video
|
43 |
+
import imageio
|
44 |
+
imageio.mimsave(output_path, images, fps=5)
|
45 |
+
|
46 |
+
end_time = time.time()
|
47 |
+
print(f"Video saved to {output_path} in {end_time - start_time:.2f} seconds")
|
48 |
+
|
49 |
+
return output_path
|
50 |
+
except Exception as e:
|
51 |
+
print(f"Error during video generation: {str(e)}")
|
52 |
+
return f"Error generating video: {str(e)}"
|
53 |
|
54 |
+
# Create Gradio interface
|
55 |
+
demo = gr.Interface(
|
56 |
+
fn=generate_video,
|
57 |
+
inputs=gr.Textbox(label="Enter Video Generation Prompt"),
|
58 |
+
outputs=gr.Video(label="Generated Video"),
|
59 |
+
title="LTX-Video Generation",
|
60 |
+
description="Generate a video using Lightricks Video Diffusion Model"
|
61 |
+
)
|
62 |
|
63 |
+
if __name__ == "__main__":
|
64 |
+
if pipe is not None:
|
65 |
+
demo.launch(timeout=300) # Increase timeout to 300 seconds (5 minutes)
|
66 |
+
else:
|
67 |
print("Could not launch app due to model loading failure.")
|