File size: 2,989 Bytes
5e75573
 
1890b5d
 
 
 
 
5e75573
1890b5d
5e75573
1890b5d
5e75573
 
 
 
 
 
 
 
 
 
 
 
1890b5d
 
5e75573
 
1890b5d
5e75573
 
 
1890b5d
5e75573
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1890b5d
5e75573
 
 
 
 
 
 
 
 
 
1890b5d
 
5e75573
 
 
 
1890b5d
5e75573
 
1890b5d
5e75573
 
 
 
 
 
 
1890b5d
 
5e75573
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# streamlit_app.py
import streamlit as st
import torch
from diffusers import WanPipeline
from diffusers.utils import export_to_video
import os

st.set_page_config(page_title="Text-to-Video Generator", page_icon="🎬", layout="wide")

@st.cache_resource
def load_model():
    """Load and cache the model"""
    pipe = WanPipeline.from_pretrained(
        "alibaba-pai/Wan2.2-Fun-A14B-Control",
        torch_dtype=torch.float16,
        variant="fp16"
    )
    
    device = "cuda" if torch.cuda.is_available() else "cpu"
    pipe = pipe.to(device)
    pipe.enable_model_cpu_offload()
    pipe.enable_vae_slicing()
    
    return pipe

st.title("🎬 Text-to-Video Generator")
st.markdown("### Powered by Wan2.2-Fun-A14B-Control")

# Sidebar for settings
with st.sidebar:
    st.header("βš™οΈ Settings")
    
    num_frames = st.slider("Number of Frames", 16, 81, 81)
    height = st.slider("Height", 256, 1024, 768, step=64)
    width = st.slider("Width", 256, 1536, 1360, step=64)
    num_inference_steps = st.slider("Inference Steps", 20, 100, 50)
    guidance_scale = st.slider("Guidance Scale", 1.0, 20.0, 7.5, step=0.5)
    seed = st.number_input("Seed (-1 for random)", value=-1, step=1)

# Main content
prompt = st.text_area(
    "Prompt",
    value="A cat playing piano in a sunlit room, cinematic lighting, 4k",
    height=100
)

negative_prompt = st.text_area(
    "Negative Prompt",
    value="blurry, low quality, distorted",
    height=80
)

if st.button("🎬 Generate Video", type="primary"):
    with st.spinner("Loading model..."):
        pipe = load_model()
    
    with st.spinner("Generating video... This may take a few minutes."):
        try:
            # Set seed
            generator = None
            if seed != -1:
                generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu")
                generator.manual_seed(int(seed))
            
            # Generate
            output = pipe(
                prompt=prompt,
                negative_prompt=negative_prompt,
                num_frames=num_frames,
                height=height,
                width=width,
                num_inference_steps=num_inference_steps,
                guidance_scale=guidance_scale,
                generator=generator,
            )
            
            # Save video
            frames = output.frames[0]
            output_path = "output_video.mp4"
            export_to_video(frames, output_path, fps=8)
            
            st.success("βœ… Video generated successfully!")
            st.video(output_path)
            
            # Download button
            with open(output_path, "rb") as file:
                st.download_button(
                    label="πŸ“₯ Download Video",
                    data=file,
                    file_name="generated_video.mp4",
                    mime="video/mp4"
                )
                
        except Exception as e:
            st.error(f"❌ Error: {str(e)}")