File size: 6,938 Bytes
90ee73b
 
 
 
 
 
 
 
 
 
 
31a1088
 
 
 
 
 
90ee73b
 
f3a1f2e
 
 
 
 
764d87d
337bc14
90ee73b
 
f3a1f2e
90ee73b
 
f3a1f2e
 
 
 
 
09f95d8
 
 
 
 
 
 
220d1de
09f95d8
 
220d1de
09f95d8
90ee73b
 
20b95d1
5114719
 
1514a70
5114719
90ee73b
f3a1f2e
87d5fe9
 
 
f3a1f2e
 
 
 
 
90ee73b
337bc14
 
1514a70
a67daee
1514a70
337bc14
 
20b95d1
 
 
 
4fc9767
09f95d8
220d1de
09f95d8
 
 
 
90ee73b
31a1088
764d87d
31a1088
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90ee73b
 
 
 
 
24e347a
7aaac90
d9b0c2c
 
24e347a
90ee73b
 
87d5fe9
337bc14
87d5fe9
337bc14
f3a1f2e
 
 
 
 
 
 
 
 
337bc14
27f6e5d
337bc14
27f6e5d
a67daee
 
006c2e8
 
 
 
 
 
337bc14
1514a70
337bc14
 
f3a1f2e
 
87d5fe9
 
 
 
 
7096730
87d5fe9
 
 
 
 
 
 
 
 
7096730
13600b0
 
87d5fe9
90ee73b
 
 
337bc14
90ee73b
 
 
 
337bc14
90ee73b
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import gradio as gr
import torch
import os
import spaces
import uuid

from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
from diffusers.utils import export_to_video
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
from PIL import Image
from gradio_client import Client, file
from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips


# using tango2 via Gradio python client 
client = Client("declare-lab/tango2")

# Constants
bases = {
    "ToonYou": "frankjoshua/toonyou_beta6",
    "epiCRealism": "emilianJR/epiCRealism"
}
step_loaded = None
base_loaded = "epiCRealism"
motion_loaded = None

# Ensure model and scheduler are initialized in GPU-enabled function
if not torch.cuda.is_available():
    raise NotImplementedError("No GPU detected!")

device = "cuda"
dtype = torch.float16
pipe = AnimateDiffPipeline.from_pretrained(bases[base_loaded], torch_dtype=dtype).to(device)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")

# Safety checkers
from safety_checker import StableDiffusionSafetyChecker
from transformers import CLIPFeatureExtractor

safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-safety-checker").to(device)
feature_extractor = CLIPFeatureExtractor.from_pretrained("openai/clip-vit-base-patch32")

def check_nsfw_images(images: list[Image.Image]) -> list[bool]:
    safety_checker_input = feature_extractor(images, return_tensors="pt").to(device)
    has_nsfw_concepts = safety_checker(images=[images], clip_input=safety_checker_input.pixel_values.to(device))
    return has_nsfw_concepts

# Function 
@spaces.GPU(enable_queue=True)
def generate_image(prompt, base, motion, step, progress=gr.Progress()):
    global step_loaded
    global base_loaded
    global motion_loaded
    print(prompt, base, step)

    if step_loaded != step:
        repo = "ByteDance/AnimateDiff-Lightning"
        ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
        pipe.unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device), strict=False)
        step_loaded = step

    if base_loaded != base:
        pipe.unet.load_state_dict(torch.load(hf_hub_download(bases[base], "unet/diffusion_pytorch_model.bin"), map_location=device), strict=False)
        base_loaded = base

    if motion_loaded != motion:
        pipe.unload_lora_weights()
        if motion != "":
            pipe.load_lora_weights(motion, adapter_name="motion")
            pipe.set_adapters(["motion"], [0.7])
        motion_loaded = motion

    progress((0, step))
    def progress_callback(i, t, z):
        progress((i+1, step))

    output = pipe(prompt=prompt, guidance_scale=1.0, num_inference_steps=step, callback=progress_callback, callback_steps=1)

    has_nsfw_concepts = check_nsfw_images([output.frames[0][0]])
    if has_nsfw_concepts[0]:
        gr.Warning("NSFW content detected.")
        return None

    name = str(uuid.uuid4()).replace("-", "")
    video_path = f"/tmp/{name}.mp4"
    export_to_video(output.frames[0], video_path, fps=10)
    
    audio_path = tango2(prompt)
    final_video_path = fuse_together(audio_path, video_path)
    
    return final_video_path


def tango2(prompt):
    results = client.predict(
      prompt=prompt,
      steps=100,
      guidance=3,
      api_name="/predict"
    )
    return results

def fuse_together(audio, video):
    
    # Load your video and audio files
    video_clip = VideoFileClip(video)
    audio_clip = AudioFileClip(audio)
    
    # Loop the video twice
    looped_video = concatenate_videoclips([video_clip, video_clip])
    
    # Cut the audio to match the duration of the looped video
    looped_audio = audio_clip.subclip(0, looped_video.duration)
    
    # Set the audio of the looped video to the adjusted audio clip
    final_video = looped_video.set_audio(looped_audio)
    
    # Write the result to a file (output will be twice the length of the original video)
    name = str(uuid.uuid4()).replace("-", "")
    path = f"/tmp/{name}.mp4"
    final_video.write_videofile(path, codec="libx264", audio_codec="aac")

    return path


# Gradio Interface
with gr.Blocks(css="style.css") as demo:
    gr.HTML(
        "<h1><center>AnimateDiff-Lightning⚡ + TANGO 2</center></h1>" +
        "<p><center>Using Gradio Python Client to combine <b>AnimateDiff Lightning</b> with <b>Tango2</b> to give Voice to your Generated Videos</center></p>" +
        "<p><center>Refer Gradio Guide for Python Clients here :<a href='https://www.gradio.app/guides/getting-started-with-the-python-client'>Getting Started with the Gradio Python client</a></center></p>"
    )
    with gr.Group():
        with gr.Row():
            prompt = gr.Textbox(
                label='Prompt (English)'
            )
        with gr.Row():
            select_base = gr.Dropdown(
                label='Base model',
                choices=[
                    "ToonYou", 
                    "epiCRealism",
                ],
                value=base_loaded,
                interactive=True
            )
            select_motion = gr.Dropdown(
                label='Motion',
                choices=[
                    ("Default", ""),
                    ("Zoom in", "guoyww/animatediff-motion-lora-zoom-in"),
                    ("Zoom out", "guoyww/animatediff-motion-lora-zoom-out"),
                    ("Tilt up", "guoyww/animatediff-motion-lora-tilt-up"),
                    ("Tilt down", "guoyww/animatediff-motion-lora-tilt-down"),
                    ("Pan left", "guoyww/animatediff-motion-lora-pan-left"),
                    ("Pan right", "guoyww/animatediff-motion-lora-pan-right"),
                    ("Roll left", "guoyww/animatediff-motion-lora-rolling-anticlockwise"),
                    ("Roll right", "guoyww/animatediff-motion-lora-rolling-clockwise"),
                ],
                value="",
                interactive=True
            )
            select_step = gr.Dropdown(
                label='Inference steps',
                choices=[
                    ('1-Step', 1), 
                    ('2-Step', 2),
                    ('4-Step', 4),
                    ('8-Step', 8)],
                value=4,
                interactive=True
            )
            submit = gr.Button(
                scale=1,
                variant='primary'
            )
    video = gr.Video(
        label='AnimateDiff-Lightning',
        autoplay=True,
        height=512,
        width=512,
        elem_id="video_output"
    )

    prompt.submit(
        fn=generate_image,
        inputs=[prompt, select_base, select_motion, select_step],
        outputs=video,
    )
    submit.click(
        fn=generate_image,
        inputs=[prompt, select_base, select_motion, select_step],
        outputs=video,
    )

demo.queue().launch()