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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -53
app.py CHANGED
@@ -18,7 +18,7 @@ st.write("Waiting for image upload and prompt input...")
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 (add this before model initialization)
22
  st.write("Migrating the cache for model files...")
23
  try:
24
  from transformers.utils import move_cache
@@ -30,7 +30,6 @@ except Exception as e:
30
 
31
  if uploaded_file and prompt:
32
  try:
33
- # Debug: File and prompt received
34
  st.write(f"Uploaded file: {uploaded_file.name}")
35
  st.write(f"Prompt: {prompt}")
36
 
@@ -42,73 +41,49 @@ if uploaded_file and prompt:
42
 
43
  # Load the image
44
  st.write("Loading image...")
45
- try:
46
- image = load_image("uploaded_image.jpg")
47
- st.write("Image loaded successfully.")
48
- except Exception as e:
49
- st.error("Error loading the image.")
50
- st.write(f"Debug info: {e}")
51
- raise e
52
 
53
- # Initialize the CogVideoX pipeline
54
  st.write("Initializing the pipeline...")
55
- try:
56
- pipe = CogVideoXImageToVideoPipeline.from_pretrained(
57
- "THUDM/CogVideoX1.5-5B-I2V",
58
- torch_dtype=torch.bfloat16,
59
- cache_dir="./huggingface_cache" # Adjust path if necessary
60
- )
61
- st.write("Pipeline initialized successfully.")
62
- except Exception as e:
63
- st.error("Error during pipeline initialization.")
64
- st.write(f"Debug info: {e}")
65
- raise e
66
-
67
- # Enable optimizations for large models
68
  pipe.enable_sequential_cpu_offload()
69
  pipe.vae.enable_tiling()
70
  pipe.vae.enable_slicing()
71
 
72
- # Debug: Ready to generate video
73
- st.write("Pipeline setup complete. Ready to generate video.")
74
-
75
- # Generate the video
76
  st.write("Generating video... This may take a while.")
77
- try:
78
- video_frames = pipe(
79
- prompt=prompt,
80
- image=image,
81
- num_videos_per_prompt=1,
82
- num_inference_steps=50,
83
- num_frames=81,
84
- guidance_scale=6,
85
- generator=torch.Generator(device="cuda").manual_seed(42),
86
- ).frames[0]
87
- st.write("Video generated successfully.")
88
- except Exception as e:
89
- st.error("Error during video generation.")
90
- st.write(f"Debug info: {e}")
91
- raise e
92
 
93
  # Export video
94
  st.write("Exporting video...")
95
- try:
96
- video_path = "output.mp4"
97
- export_to_video(video_frames, video_path, fps=8)
98
- st.write("Video exported successfully.")
99
- except Exception as e:
100
- st.error("Error exporting video.")
101
- st.write(f"Debug info: {e}")
102
- raise e
103
 
104
- # Display video in Streamlit
105
  st.video(video_path)
106
 
107
  except Exception as e:
108
  st.error(f"An error occurred: {e}")
109
  st.write(f"Debug info: {e}")
110
  else:
111
- # Debug: Waiting for inputs
112
  st.write("Please upload an image and provide a prompt to get started.")
113
 
114
-
 
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
 
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
 
 
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