pm6six commited on
Commit
e986028
·
verified ·
1 Parent(s): dc76dee

Update app.py

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