Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,57 @@
|
|
1 |
-
import
|
|
|
2 |
from diffusers import CogVideoXImageToVideoPipeline
|
3 |
from diffusers.utils import export_to_video, load_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
"
|
9 |
-
torch_dtype=torch.bfloat16
|
10 |
-
)
|
11 |
-
|
12 |
-
pipe.enable_sequential_cpu_offload()
|
13 |
-
pipe.vae.enable_tiling()
|
14 |
-
pipe.vae.enable_slicing()
|
15 |
-
|
16 |
-
video = pipe(
|
17 |
-
prompt=prompt,
|
18 |
-
image=image,
|
19 |
-
num_videos_per_prompt=1,
|
20 |
-
num_inference_steps=50,
|
21 |
-
num_frames=81,
|
22 |
-
guidance_scale=6,
|
23 |
-
generator=torch.Generator(device="cuda").manual_seed(42),
|
24 |
-
).frames[0]
|
25 |
-
|
26 |
-
export_to_video(video, "output.mp4", fps=8)
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
from diffusers import CogVideoXImageToVideoPipeline
|
4 |
from diffusers.utils import export_to_video, load_image
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Streamlit interface for uploading an image and inputting a prompt
|
8 |
+
st.title("Image to Video with Hugging Face")
|
9 |
+
st.write("Upload an image and provide a prompt to generate a video.")
|
10 |
+
|
11 |
+
# File uploader for the input image
|
12 |
+
uploaded_file = st.file_uploader("Upload an image (JPG or PNG):", type=["jpg", "jpeg", "png"])
|
13 |
+
prompt = st.text_input("Enter your prompt:", "A little girl is riding a bicycle at high speed. Focused, detailed, realistic.")
|
14 |
+
|
15 |
+
if uploaded_file and prompt:
|
16 |
+
try:
|
17 |
+
# Save the uploaded file to a temporary location
|
18 |
+
with open("uploaded_image.jpg", "wb") as f:
|
19 |
+
f.write(uploaded_file.read())
|
20 |
+
|
21 |
+
# Load the image
|
22 |
+
image = load_image("uploaded_image.jpg")
|
23 |
+
|
24 |
+
# Initialize the CogVideoX pipeline
|
25 |
+
st.write("Initializing the pipeline...")
|
26 |
+
pipe = CogVideoXImageToVideoPipeline.from_pretrained(
|
27 |
+
"THUDM/CogVideoX1.5-5B-I2V",
|
28 |
+
torch_dtype=torch.bfloat16
|
29 |
+
)
|
30 |
+
|
31 |
+
pipe.enable_sequential_cpu_offload()
|
32 |
+
pipe.vae.enable_tiling()
|
33 |
+
pipe.vae.enable_slicing()
|
34 |
+
|
35 |
+
# Generate the video
|
36 |
+
st.write("Generating video... this may take a while.")
|
37 |
+
video_frames = pipe(
|
38 |
+
prompt=prompt,
|
39 |
+
image=image,
|
40 |
+
num_videos_per_prompt=1,
|
41 |
+
num_inference_steps=50,
|
42 |
+
num_frames=81,
|
43 |
+
guidance_scale=6,
|
44 |
+
generator=torch.Generator(device="cuda").manual_seed(42),
|
45 |
+
).frames[0]
|
46 |
+
|
47 |
+
# Export the video
|
48 |
+
video_path = "output.mp4"
|
49 |
+
export_to_video(video_frames, video_path, fps=8)
|
50 |
+
|
51 |
+
# Display the video in Streamlit
|
52 |
+
st.video(video_path)
|
53 |
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"An error occurred: {e}")
|
56 |
+
else:
|
57 |
+
st.write("Please upload an image and provide a prompt to get started.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|