kartik2627 commited on
Commit
8a800d5
·
verified ·
1 Parent(s): add0ae7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from diffusers import StableDiffusionImg2ImgPipeline
3
+ from moviepy.editor import ImageSequenceClip
4
+ from PIL import Image
5
+ import torch
6
+ import os
7
+
8
+ # Title and instructions
9
+ st.title("Image-to-Video Conversion")
10
+ st.write("Upload an image, provide a prompt, and generate a video using AI.")
11
+
12
+ # Sidebar for user input
13
+ st.sidebar.title("Settings")
14
+ num_frames = st.sidebar.slider("Number of Frames", 5, 50, 10)
15
+ fps = st.sidebar.slider("Frames Per Second (FPS)", 1, 30, 12)
16
+ guidance_scale = st.sidebar.slider("Guidance Scale", 5.0, 15.0, 7.5)
17
+ strength_base = st.sidebar.slider("Base Strength (Image Influence)", 0.1, 1.0, 0.5)
18
+
19
+ # File uploader
20
+ uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
21
+ prompt = st.text_input("Enter a Prompt", value="A cinematic animation of a sunset over mountains")
22
+
23
+ # Load the pre-trained model
24
+ @st.cache_resource
25
+ def load_model():
26
+ return StableDiffusionImg2ImgPipeline.from_pretrained(
27
+ "CompVis/stable-diffusion-v1-4",
28
+ torch_dtype=torch.float16,
29
+ revision="fp16"
30
+ ).to("cuda")
31
+
32
+ pipe = load_model()
33
+
34
+ # Process the uploaded image and generate video frames
35
+ if uploaded_image and st.button("Generate Video"):
36
+ # Load the input image
37
+ input_image = Image.open(uploaded_image).convert("RGB")
38
+ st.image(input_image, caption="Uploaded Image", use_column_width=True)
39
+
40
+ st.write("Generating video frames... This might take a few minutes.")
41
+ progress = st.progress(0)
42
+
43
+ frames = []
44
+ for i in range(num_frames):
45
+ progress.progress((i + 1) / num_frames)
46
+ result = pipe(
47
+ prompt=prompt,
48
+ image=input_image,
49
+ strength=strength_base + (i * 0.05), # Incremental strength
50
+ guidance_scale=guidance_scale
51
+ )
52
+ frames.append(result.images[0])
53
+
54
+ # Save video frames as a video file
55
+ video_path = "./output_video.mp4"
56
+ video_clip = ImageSequenceClip([frame for frame in frames], fps=fps)
57
+ video_clip.write_videofile(video_path, codec="libx264")
58
+ st.success("Video generated successfully!")
59
+
60
+ # Display video
61
+ st.video(video_path)
62
+
63
+ # Download link for the video
64
+ with open(video_path, "rb") as file:
65
+ btn = st.download_button(
66
+ label="Download Video",
67
+ data=file,
68
+ file_name="output_video.mp4",
69
+ mime="video/mp4"
70
+ )
71
+
72
+ # Footer
73
+ st.write("Powered by Hugging Face Diffusers and Streamlit")