guardiancc commited on
Commit
e7c6544
·
verified ·
1 Parent(s): 8f6e4b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -59
app.py CHANGED
@@ -1,60 +1,63 @@
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
-
44
- # Generate video
45
- with st.spinner("Generating video... This may take a while."):
46
- try:
47
- # Attempt to generate the video
48
- video_frames = pipe(
49
- prompt=prompt,
50
- image=image,
51
- num_videos_per_prompt=1,
52
- num_inference_steps=50,
53
- num_frames=81,
54
- guidance_scale=6,
55
- generator=torch.Generator(device=device).manual_seed(42),
56
- ).frames[0]
57
- except Exception as e:
58
- # Handle errors gracefully
59
- st.error(f"An error occurred during video generation: {e}")
60
-
 
 
 
 
 
 
 
 
1
  import torch
2
+ import gradio as gr
3
+ import time
4
+ from diffusers import CogVideoXImageToVideoPipeline
5
+ from diffusers.utils import export_to_video, load_image
6
+
7
+ # Load model once
8
+ pipe = CogVideoXImageToVideoPipeline.from_pretrained(
9
+ "THUDM/CogVideoX1.5-5B-I2V",
10
+ torch_dtype=torch.bfloat16
11
+ )
12
+ pipe.enable_sequential_cpu_offload()
13
+ pipe.vae.enable_tiling()
14
+ pipe.vae.enable_slicing()
15
+
16
+ def generate_video(image, prompt):
17
+ if image is None:
18
+ raise gr.Error("Please upload an input image")
19
+ if not prompt:
20
+ raise gr.Error("Please enter a text prompt")
21
+
22
+ # Load uploaded image
23
+ input_image = load_image(image)
24
+
25
+ # Generate video
26
+ video_frames = pipe(
27
+ prompt=prompt,
28
+ image=input_image,
29
+ num_videos_per_prompt=1,
30
+ num_inference_steps=50,
31
+ num_frames=81,
32
+ guidance_scale=6,
33
+ generator=torch.Generator(device="cuda").manual_seed(42),
34
+ ).frames[0]
35
+
36
+ # Save to temporary file
37
+ output_path = f"output_{int(time.time())}.mp4"
38
+ export_to_video(video_frames, output_path, fps=8)
39
+
40
+ return output_path
41
+
42
+ with gr.Blocks(title="CogVideoX Image-to-Video") as demo:
43
+ gr.Markdown("# 🎥 CogVideoX Image-to-Video Generation")
44
+ gr.Markdown("Transform images into videos using AI! Upload an image and enter a description to generate a video.")
45
+
46
+ with gr.Row():
47
+ with gr.Column():
48
+ image_input = gr.Image(label="Input Image", type="filepath")
49
+ prompt_input = gr.Textbox(label="Prompt", placeholder="Describe the video you want to generate...")
50
+ submit_btn = gr.Button("Generate Video")
51
+
52
+ with gr.Column():
53
+ video_output = gr.Video(label="Generated Video")
54
+ gr.Examples(examples=examples, inputs=[image_input, prompt_input])
55
+
56
+ submit_btn.click(
57
+ fn=generate_video,
58
+ inputs=[image_input, prompt_input],
59
+ outputs=video_output,
60
+ )
61
+
62
+ if __name__ == "__main__":
63
+ demo.launch()