File size: 6,192 Bytes
2af7c18
eaa76b7
2af7c18
 
 
5c5e329
6f6d790
2af7c18
 
b766640
2af7c18
 
 
 
 
 
 
 
 
 
 
6a17503
 
 
2af7c18
 
 
194b0bd
2af7c18
eaa76b7
 
2af7c18
bcdfcef
194b0bd
4596569
2af7c18
 
 
 
 
 
c7e9ad1
2af7c18
 
16a1e1b
6a17503
 
2af7c18
5c5e329
2af7c18
 
5c5e329
103dcd4
1c85335
2af7c18
50ded98
1c85335
50ded98
 
 
 
 
 
 
 
2af7c18
 
69cab2c
50ded98
 
 
 
 
 
 
 
 
103dcd4
2af7c18
50ded98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8af109
82172b4
 
b8af109
 
 
82172b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8af109
 
 
 
 
 
69cab2c
67ae5ad
 
 
 
 
 
 
 
 
 
 
 
 
 
82172b4
 
 
 
 
 
 
 
 
67ae5ad
b8af109
1c85335
50ded98
 
 
 
 
 
 
 
 
 
2af7c18
 
1c85335
 
 
 
 
 
 
 
 
 
2af7c18
67ae5ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c85335
2af7c18
 
 
 
 
 
 
 
 
 
1c85335
 
 
 
 
2af7c18
 
 
1c85335
 
 
4218146
67ae5ad
 
2af7c18
1c85335
2af7c18
8097377
1c85335
 
 
2af7c18
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import gradio as gr
import numpy as np
import torch
import spaces
from diffusers import FluxPipeline, FluxTransformer2DModel
from diffusers.utils import export_to_gif
from huggingface_hub import hf_hub_download
from PIL import Image
import uuid
import random

device = "cuda" if torch.cuda.is_available() else "cpu"

if torch.cuda.is_available():
    torch_dtype = torch.bfloat16
else:
    torch_dtype = torch.float32

def split_image(input_image, num_splits=4):
    output_images = []
    for i in range(num_splits):
        left = i * 320
        right = (i + 1) * 320
        box = (left, 0, right, 320)
        output_images.append(input_image.crop(box))
    return output_images

pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch_dtype).to(device)

MAX_SEED = np.iinfo(np.int32).max

@spaces.GPU 
def infer(prompt, seed=1, randomize_seed=False, num_inference_steps=28):
    print('entered the function')
    prompt_template = f"A side by side 4 frame image showing high quality consecutive stills from a looped gif animation moving from left to right. The scene has motion. The stills are of {prompt}"
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
        
    generator = torch.Generator().manual_seed(seed)
    
    image = pipe(
        prompt=prompt_template,
        num_inference_steps=num_inference_steps,
        num_images_per_prompt=1,
        generator=generator,
        height=320,
        width=1280
    ).images[0]
    
    gif_name = f"{uuid.uuid4().hex}-flux.gif"
    export_to_gif(split_image(image, 4), gif_name, fps=4)
    
    return gif_name, image, seed

examples = [
    "a red panda playing with a bamboo stick in the snow",
    "an astronaut breakdancing on the moon",
    "a magical butterfly transforming into sparkles",
    "a robot learning to paint like Van Gogh",
    "a dragon hatching from a crystal egg",
    "a time traveler stepping through a portal",
    "a mermaid playing with bioluminescent fish",
    "a steampunk clock with moving gears",
    "a flower blooming in timelapse",
    "a wizard casting a colorful spell"
]

css = """
footer {visibility: hidden}

#col-container {
    max-width: 1200px;
    margin: auto;
    padding: 20px;
    background-color: #ffffff;
    border-radius: 20px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.gr-button {
    background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
    border: none;
    color: white;
    font-weight: 600;
    border-radius: 8px;
}

.gr-button:hover {
    background: linear-gradient(45deg, #4ECDC4, #FF6B6B);
    transform: translateY(-2px);
    transition: all 0.3s ease;
}

.gr-input {
    border-radius: 8px;
    border: 2px solid #e0e0e0;
}

.gr-accordion {
    border-radius: 8px;
    background-color: #f8f9fa;
}

/* Examples 텍스트 색상 관련 스타일 수정 */
.gr-examples-text {
    color: black !important;
}

.gr-examples button {
    color: black !important;
}

.gr-examples span {
    color: black !important;
}

.gr-examples div {
    color: black !important;
}

.gr-examples p {
    color: black !important;
}

.gr-examples h3 {
    color: black !important;
}

.gr-sample-text {
    color: black !important;
}

#main-output {
    height: 500px !important;
    width: 100% !important;
}

#preview-output {
    height: 200px !important;
    width: auto !important;
}

#strip-output {
    height: 150px !important;
    width: auto !important;
}

/* 추가적인 Examples 관련 스타일 */
.example-text {
    color: black !important;
}

.example-label {
    color: black !important;
}
"""

with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
    gr.HTML("""
        <div style="text-align: center; max-width: 800px; margin: 0 auto;">
            <h1 style="font-size: 3rem; font-weight: 700; margin-bottom: 1rem;">
                FLUX Animation Creator
            </h1>
            <p style="font-size: 1.2rem; color: #666; margin-bottom: 2rem;">
                Create amazing animated GIFs with AI - Just describe what you want to see!
            </p>
        </div>
    """)
    
    with gr.Column(elem_id="col-container"):
        with gr.Row():
            prompt = gr.Text(
                label="Your Animation Prompt",
                show_label=True,
                max_lines=1,
                placeholder="Describe the animation you want to create...",
                container=True,
                elem_id="prompt-input"
            )
            run_button = gr.Button("✨ Generate", scale=0, variant="primary")
        
        # GIF 결과 출력 (큰 크기)
        result = gr.Image(
            label="Generated Animation", 
            show_label=True,
            elem_id="main-output",
            height=500
        )
        
        with gr.Row():
            # 미리보기 이미지들 (작은 크기)
            result_full = gr.Image(
                label="Preview", 
                elem_id="preview-output",
                height=200
            )
            strip_image = gr.Image(
                label="Animation Strip", 
                elem_id="strip-output",
                height=150
            )
        
        with gr.Accordion("Advanced Settings", open=False):
            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=0,
            )
            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
            num_inference_steps = gr.Slider(
                label="Number of inference steps",
                minimum=1,
                maximum=32,
                step=1,
                value=28, 
            )    
        
        gr.Examples(
            examples=examples,
            inputs=[prompt],
            outputs=[result, result_full, seed],
            fn=infer,
            cache_examples=True,
            label="Click on any example to try it out"
        )
    
    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[prompt, seed, randomize_seed, num_inference_steps],
        outputs=[result, result_full, seed]
    )

demo.queue().launch()