fffiloni commited on
Commit
464d284
·
verified ·
1 Parent(s): fc0a183

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gc
3
+ import time
4
+ import random
5
+ import imageio
6
+ import torch
7
+ import gradio as gr
8
+ from diffusers.utils import load_image
9
+
10
+ from skyreels_v2_infer.modules import download_model
11
+ from skyreels_v2_infer.pipelines import Image2VideoPipeline, Text2VideoPipeline, PromptEnhancer, resizecrop
12
+
13
+ def generate_video(
14
+ prompt,
15
+ model_id,
16
+ resolution,
17
+ num_frames,
18
+ image=None,
19
+ guidance_scale=6.0,
20
+ shift=8.0,
21
+ inference_steps=30,
22
+ use_usp=False,
23
+ offload=False,
24
+ fps=24,
25
+ seed=None,
26
+ prompt_enhancer=False,
27
+ teacache=False,
28
+ teacache_thresh=0.2,
29
+ use_ret_steps=False
30
+ ):
31
+ model_id = download_model(model_id)
32
+
33
+ if resolution == "540P":
34
+ height, width = 544, 960
35
+ elif resolution == "720P":
36
+ height, width = 720, 1280
37
+ else:
38
+ raise ValueError(f"Invalid resolution: {resolution}")
39
+
40
+ if seed is None:
41
+ random.seed(time.time())
42
+ seed = int(random.randrange(4294967294))
43
+
44
+ if image is not None:
45
+ image = load_image(image).convert("RGB")
46
+ image_width, image_height = image.size
47
+ if image_height > image_width:
48
+ height, width = width, height
49
+ image = resizecrop(image, height, width)
50
+
51
+ negative_prompt = (
52
+ "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, "
53
+ "overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, "
54
+ "poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, "
55
+ "three legs, many people in the background, walking backwards"
56
+ )
57
+
58
+ prompt_input = prompt
59
+ if prompt_enhancer and image is None:
60
+ enhancer = PromptEnhancer()
61
+ prompt_input = enhancer(prompt_input)
62
+ del enhancer
63
+ gc.collect()
64
+ torch.cuda.empty_cache()
65
+
66
+ if image is None:
67
+ pipe = Text2VideoPipeline(model_path=model_id, dit_path=model_id, use_usp=use_usp, offload=offload)
68
+ else:
69
+ pipe = Image2VideoPipeline(model_path=model_id, dit_path=model_id, use_usp=use_usp, offload=offload)
70
+
71
+ if teacache:
72
+ pipe.transformer.initialize_teacache(
73
+ enable_teacache=True,
74
+ num_steps=inference_steps,
75
+ teacache_thresh=teacache_thresh,
76
+ use_ret_steps=use_ret_steps,
77
+ ckpt_dir=model_id,
78
+ )
79
+
80
+ kwargs = {
81
+ "prompt": prompt_input,
82
+ "negative_prompt": negative_prompt,
83
+ "num_frames": num_frames,
84
+ "num_inference_steps": inference_steps,
85
+ "guidance_scale": guidance_scale,
86
+ "shift": shift,
87
+ "generator": torch.Generator(device="cuda").manual_seed(seed),
88
+ "height": height,
89
+ "width": width,
90
+ }
91
+
92
+ if image is not None:
93
+ kwargs["image"] = image.convert("RGB")
94
+
95
+ with torch.cuda.amp.autocast(dtype=pipe.transformer.dtype), torch.no_grad():
96
+ video_frames = pipe(**kwargs)[0]
97
+
98
+ os.makedirs("gradio_videos", exist_ok=True)
99
+ timestamp = time.strftime("%Y%m%d_%H%M%S")
100
+ output_path = f"gradio_videos/{prompt[:50].replace('/', '')}_{seed}_{timestamp}.mp4"
101
+ imageio.mimwrite(output_path, video_frames, fps=fps, quality=8, output_params=["-loglevel", "error"])
102
+
103
+ return output_path
104
+
105
+ # Gradio UI
106
+ resolution_options = ["540P", "720P"]
107
+ model_options = MODEL_ID_CONFIG["text2video"] + MODEL_ID_CONFIG["image2video"]
108
+
109
+ app = gr.Interface(
110
+ fn=generate_video,
111
+ inputs=[
112
+ gr.Textbox(label="Prompt"),
113
+ gr.Dropdown(choices=model_options, value="Skywork/SkyReels-V2-T2V-14B-540P", label="Model ID"),
114
+ gr.Radio(choices=resolution_options, value="540P", label="Resolution"),
115
+ gr.Slider(minimum=16, maximum=200, value=97, step=1, label="Number of Frames"),
116
+ gr.Image(type="filepath", label="Input Image (optional)"),
117
+ gr.Slider(minimum=1.0, maximum=20.0, value=6.0, step=0.1, label="Guidance Scale"),
118
+ gr.Slider(minimum=0.0, maximum=20.0, value=8.0, step=0.1, label="Shift"),
119
+ gr.Slider(minimum=1, maximum=100, value=30, step=1, label="Inference Steps"),
120
+ gr.Checkbox(label="Use USP"),
121
+ gr.Checkbox(label="Offload"),
122
+ gr.Slider(minimum=1, maximum=60, value=24, step=1, label="FPS"),
123
+ gr.Number(label="Seed (optional, random if empty)", precision=0),
124
+ gr.Checkbox(label="Prompt Enhancer"),
125
+ gr.Checkbox(label="Use TeaCache"),
126
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.2, step=0.01, label="TeaCache Threshold"),
127
+ gr.Checkbox(label="Use Retention Steps"),
128
+ ],
129
+ outputs=gr.Video(label="Generated Video"),
130
+ title="SkyReels V2 Video Generator",
131
+ )
132
+
133
+ app.launch(show_api=False, show_error=True, ssr_mode=False)