File size: 1,350 Bytes
9688f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import streamlit as st
import torch
from diffusers import DiffusionPipeline
import imageio
from io import BytesIO
from PIL import Image

# Title of the app
st.title("Text-to-Video Generator")

# Load the model (only once when the app starts)
@st.cache_resource
def load_model():
    pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16)
    pipe.enable_model_cpu_offload()
    pipe.unet.enable_forward_chunking(chunk_size=1, dim=1)
    pipe.enable_vae_slicing()
    return pipe

pipe = load_model()

# Text input for the prompt
prompt = st.text_input("Enter a text prompt for the video")

# If a prompt is entered, generate the video
if prompt:
    st.write("Generating video... please wait.")

    # Generate video frames
    video_frames = pipe(prompt, num_frames=24).frames[0]

    # Save video to in-memory buffer
    video_buffer = BytesIO()
    with imageio.get_writer(video_buffer, fps=10, format="mp4") as writer:
        for frame in video_frames:
            writer.append_data(frame)

    video_buffer.seek(0)

    # Display the video
    st.video(video_buffer, format="video/mp4")

    # Optional: Allow users to download the video
    st.download_button(label="Download Video", data=video_buffer, file_name="generated_video.mp4", mime="video/mp4")