Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,44 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import torch
|
|
|
4 |
|
5 |
try:
|
6 |
from diffusers import CogVideoXImageToVideoPipeline
|
7 |
pipeline_available = True
|
8 |
-
except ImportError
|
9 |
pipeline_available = False
|
10 |
-
st.error("Failed to import CogVideoXImageToVideoPipeline
|
11 |
-
st.write(f"Debug info: {e}")
|
12 |
|
13 |
-
|
14 |
-
# Streamlit interface
|
15 |
st.title("Image to Video with Hugging Face")
|
16 |
st.write("Upload an image and provide a prompt to generate a video.")
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
subprocess.run(["streamlit", "config", "set", "browser.gatherUsageStats", "false"])
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# Check if the pipeline is available before proceeding
|
25 |
-
if not pipeline_available:
|
26 |
-
st.error("The required pipeline is unavailable. Please ensure you have the correct version of the diffusers library.")
|
27 |
-
else:
|
28 |
-
# File uploader for the input image
|
29 |
uploaded_file = st.file_uploader("Upload an image (JPG or PNG):", type=["jpg", "jpeg", "png"])
|
30 |
prompt = st.text_input("Enter your prompt:", "A little girl is riding a bicycle at high speed. Focused, detailed, realistic.")
|
31 |
|
32 |
-
# Cache migration step
|
33 |
-
st.write("Migrating the cache for model files...")
|
34 |
-
try:
|
35 |
-
from transformers.utils import move_cache
|
36 |
-
move_cache()
|
37 |
-
st.write("Cache migration completed successfully.")
|
38 |
-
except Exception as e:
|
39 |
-
st.error(f"Cache migration failed: {e}")
|
40 |
-
st.write("Proceeding without cache migration...")
|
41 |
-
|
42 |
if uploaded_file and prompt:
|
43 |
try:
|
44 |
-
st.write(f"Uploaded file: {uploaded_file.name}")
|
45 |
-
st.write(f"Prompt: {prompt}")
|
46 |
-
|
47 |
# Save uploaded file
|
48 |
-
|
49 |
-
|
|
|
50 |
f.write(uploaded_file.read())
|
51 |
st.write("Uploaded image saved successfully.")
|
52 |
|
53 |
# Load the image
|
54 |
-
|
55 |
-
st.write("Loading image...")
|
56 |
-
image = load_image("uploaded_image.jpg")
|
57 |
-
st.write("Image loaded successfully.")
|
58 |
|
59 |
-
# Initialize
|
60 |
-
|
61 |
pipe = CogVideoXImageToVideoPipeline.from_pretrained(
|
62 |
"THUDM/CogVideoX1.5-5B-I2V",
|
63 |
torch_dtype=torch.bfloat16,
|
64 |
cache_dir="./huggingface_cache",
|
65 |
-
force_download=True # Ensure fresh download
|
66 |
)
|
67 |
-
st.write("Pipeline initialized successfully.")
|
68 |
-
|
69 |
-
# Enable optimizations
|
70 |
pipe.enable_sequential_cpu_offload()
|
71 |
pipe.vae.enable_tiling()
|
72 |
pipe.vae.enable_slicing()
|
73 |
|
74 |
# Generate video
|
75 |
-
st.
|
76 |
-
video_frames = pipe(
|
77 |
-
prompt=prompt,
|
78 |
-
image=image,
|
79 |
-
num_videos_per_prompt=1,
|
80 |
-
num_inference_steps=50,
|
81 |
-
num_frames=81,
|
82 |
-
guidance_scale=6,
|
83 |
-
generator=torch.Generator(device="cuda").manual_seed(42),
|
84 |
-
).frames[0]
|
85 |
-
st.write("Video generated successfully.")
|
86 |
-
|
87 |
-
# Export video
|
88 |
-
st.write("Exporting video...")
|
89 |
-
from diffusers.utils import export_to_video
|
90 |
-
video_path = "output.mp4"
|
91 |
-
export_to_video(video_frames, video_path, fps=8)
|
92 |
-
st.write("Video exported successfully.")
|
93 |
-
|
94 |
-
# Display video
|
95 |
-
st.video(video_path)
|
96 |
-
|
97 |
-
except Exception as e:
|
98 |
-
st.error(f"An error occurred: {e}")
|
99 |
-
st.write(f"Debug info: {e}")
|
100 |
-
else:
|
101 |
-
st.write("Please upload an image and provide a prompt to get started.")
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import torch
|
4 |
+
from diffusers.utils import load_image
|
5 |
|
6 |
try:
|
7 |
from diffusers import CogVideoXImageToVideoPipeline
|
8 |
pipeline_available = True
|
9 |
+
except ImportError:
|
10 |
pipeline_available = False
|
11 |
+
st.error("Failed to import `CogVideoXImageToVideoPipeline`. Please run `pip install diffusers`.")
|
|
|
12 |
|
|
|
|
|
13 |
st.title("Image to Video with Hugging Face")
|
14 |
st.write("Upload an image and provide a prompt to generate a video.")
|
15 |
|
16 |
+
if pipeline_available:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
uploaded_file = st.file_uploader("Upload an image (JPG or PNG):", type=["jpg", "jpeg", "png"])
|
18 |
prompt = st.text_input("Enter your prompt:", "A little girl is riding a bicycle at high speed. Focused, detailed, realistic.")
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
if uploaded_file and prompt:
|
21 |
try:
|
|
|
|
|
|
|
22 |
# Save uploaded file
|
23 |
+
import uuid
|
24 |
+
file_name = f"{uuid.uuid4()}_uploaded_image.jpg"
|
25 |
+
with open(file_name, "wb") as f:
|
26 |
f.write(uploaded_file.read())
|
27 |
st.write("Uploaded image saved successfully.")
|
28 |
|
29 |
# Load the image
|
30 |
+
image = load_image(file_name)
|
|
|
|
|
|
|
31 |
|
32 |
+
# Initialize pipeline
|
33 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
34 |
pipe = CogVideoXImageToVideoPipeline.from_pretrained(
|
35 |
"THUDM/CogVideoX1.5-5B-I2V",
|
36 |
torch_dtype=torch.bfloat16,
|
37 |
cache_dir="./huggingface_cache",
|
|
|
38 |
)
|
|
|
|
|
|
|
39 |
pipe.enable_sequential_cpu_offload()
|
40 |
pipe.vae.enable_tiling()
|
41 |
pipe.vae.enable_slicing()
|
42 |
|
43 |
# Generate video
|
44 |
+
with st.spinner("Generating video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|