File size: 9,682 Bytes
c5d553f
8e294e8
c5d553f
498c646
 
 
c5d553f
8e294e8
 
 
 
 
 
c5d553f
8e294e8
c5d553f
8e294e8
c5d553f
 
8e294e8
 
498c646
8e294e8
 
 
 
 
 
 
 
498c646
 
8e294e8
 
 
 
498c646
 
8e294e8
 
498c646
 
8e294e8
 
 
 
 
498c646
8e294e8
 
498c646
c5d553f
8e294e8
c5d553f
 
 
498c646
c5d553f
498c646
c5d553f
 
498c646
c5d553f
 
498c646
c5d553f
a906e23
 
 
deb6de0
 
 
c5d553f
498c646
c5d553f
 
8e294e8
 
c5d553f
8e294e8
498c646
8e294e8
c5d553f
8e294e8
498c646
8e294e8
498c646
8e294e8
 
 
498c646
8e294e8
 
498c646
c5d553f
 
8e294e8
498c646
c5d553f
8e294e8
c5d553f
8e294e8
 
 
 
 
 
 
c5d553f
 
8e294e8
498c646
 
 
8e294e8
498c646
 
 
 
 
 
8e294e8
 
 
 
 
498c646
8e294e8
c5d553f
8e294e8
 
 
c5d553f
8e294e8
c5d553f
498c646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5d553f
 
 
8e294e8
deb6de0
498c646
 
a797a11
8e294e8
498c646
 
c5d553f
 
498c646
c5d553f
8e294e8
 
c5d553f
 
deb6de0
8e294e8
c5d553f
 
498c646
8e294e8
 
 
 
 
498c646
 
8e294e8
 
 
 
 
 
 
 
498c646
8e294e8
498c646
 
c5d553f
8e294e8
c5d553f
498c646
 
8e294e8
498c646
 
 
8e294e8
 
c5d553f
 
 
8e294e8
498c646
c5d553f
 
 
498c646
8e294e8
498c646
8e294e8
 
 
 
498c646
 
c5d553f
 
8e294e8
c5d553f
 
 
 
 
8e294e8
 
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
237
238
239
240
241
import gradio as gr
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel
import torch
import gc
import time
import os

# --- Конфигурация SDXL-Lightning ---
BASE_MODEL_ID = "stabilityai/stable-diffusion-xl-base-1.0"
LIGHTNING_MODEL_ID = "ByteDance/SDXL-Lightning"
LIGHTNING_CHECKPOINT = "sdxl_lightning_4step_unet.safetensors"
DEVICE = "cpu"
DTYPE = torch.float32

print(f"INFO: Starting with SDXL-Lightning on {DEVICE}")

# --- Загрузка модели ---
pipe = None
try:
    print("INFO: Loading SDXL-Lightning model...")
    load_start = time.time()
    
    # Загружаем базовую модель SDXL
    pipe = StableDiffusionXLPipeline.from_pretrained(
        BASE_MODEL_ID,
        torch_dtype=DTYPE,
        use_safetensors=True,
        variant="fp16",  # Используем fp16 вариант для экономии памяти
        low_cpu_mem_usage=True,
        safety_checker=None
    )
    
    # Загружаем оптимизированный UNet из SDXL-Lightning
    lightning_unet = UNet2DConditionModel.from_pretrained(
        LIGHTNING_MODEL_ID,
        subfolder="unet",
        torch_dtype=DTYPE,
        use_safetensors=True,
        variant=LIGHTNING_CHECKPOINT,
        low_cpu_mem_usage=True
    )
    
    # Заменяем UNet в пайплайне на Lightning версию
    pipe.unet = lightning_unet
    
    # Оптимизации для CPU
    pipe.enable_attention_slicing()
    
    load_end = time.time()
    print(f"INFO: Model loaded successfully in {load_end - load_start:.2f} seconds")
    
except Exception as e:
    print(f"ERROR: Failed to load model: {e}")

# --- Функция генерации промпта ---
def create_prompt_and_negative(user_description, style_choice):
    base_description = f"emoji of {user_description}, icon"
    style_keywords = ""
    negative_prompt = "ugly, tiling, poorly drawn, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, signature, cut off, draft, text, words, low quality, lowres, multiple emojis, multiple objects, borders, frame, artifacts, noise, text, letters, signature, username, complex background"

    if style_choice == "Apple (iOS) Style":
        style_keywords = "apple emoji style, ios emoji, 3D icon, glossy finish, smooth gradients, vibrant colors, rounded, clean vector art look, product render, high detail, playful, expressive, centered, iconic, simple background"
        negative_prompt += ", flat, 2d, sketch, hand-drawn, dark, dull, messy, text"
    elif style_choice == "Google (Noto) Style":
        style_keywords = "google emoji style, noto color emoji, flat 2d design, bold black outline, simple geometric shapes, minimalist, cartoonish, expressive, clean lines, vibrant solid colors, centered, iconic, simple background"
        negative_prompt += ", 3d, realistic, shadows, complex textures, messy, text"
    elif style_choice == "Hand-Drawn Style":
        style_keywords = "hand-drawn emoji, doodle style, simple line art, sketch, minimalist drawing, black and white with color accents, MS Paint style, childlike drawing, centered, iconic, simple background"
        negative_prompt += ", 3d, realistic, complex, detailed, professional, polished, text"
    elif style_choice == "Anime Style":
        style_keywords = "anime emoji, manga style, kawaii, cute japanese anime, big eyes, simple shapes, colorful, 2D, flat colors, centered, iconic, simple background"
        negative_prompt += ", realistic, 3d, photorealistic, complex, detailed, western, text"

    final_prompt = f"{base_description}, {style_keywords}, high quality, centered composition"
    return final_prompt, negative_prompt

# --- Функция генерации ---
def generate_emoji(description, style, guidance_scale_val, seed_val, size_choice):
    if pipe is None:
        raise gr.Error("Model not loaded. Please check logs.")
    if not description.strip():
        raise gr.Error("Please enter a description!")

    # Размер
    if size_choice == "Small (faster)":
        height, width = 512, 512  # SDXL лучше работает с размерами кратными 512
    elif size_choice == "Medium":
        height, width = 768, 768
    else:
        height, width = 1024, 1024

    # Сид
    if seed_val is None or seed_val == -1:
        seed = torch.randint(0, 2**32 - 1, (1,)).item()
    else:
        seed = int(seed_val)
    
    generator = torch.Generator(device='cpu').manual_seed(seed)

    # Промпт
    prompt, negative_prompt = create_prompt_and_negative(description, style)
    print(f"INFO: Generating: '{description}' | Style: '{style}' | Size: {width}x{height}")
    print(f"INFO: Guidance: {guidance_scale_val}, Seed: {seed}")
    
    # Генерация
    gc.collect()
    start_time = time.time()
    
    try:
        with torch.no_grad():
            # SDXL-Lightning оптимизирован для 4 шагов
            image = pipe(
                prompt,
                negative_prompt=negative_prompt,
                num_inference_steps=4,  # Lightning оптимизирован для 4 шагов
                guidance_scale=float(guidance_scale_val),
                generator=generator,
                height=height,
                width=width
            ).images[0]
        
        end_time = time.time()
        generation_time = end_time - start_time
        print(f"INFO: Generated in {generation_time:.2f} seconds")
        
        gc.collect()
        return image, seed, f"Generated in {generation_time:.1f} seconds"
    
    except Exception as e:
        print(f"ERROR: Generation failed: {e}")
        gc.collect()
        raise gr.Error(f"Generation failed: {e}")

# --- Интерфейс ---
css = """
#title_custom { 
    text-align: center; 
    margin-bottom: 10px; 
    font-family: 'Poppins', sans-serif; 
    background: linear-gradient(90deg, #FF8C00, #FF2D55);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
.container {
    max-width: 900px;
    margin: 0 auto;
}
.gr-button-primary { 
    background: linear-gradient(90deg, #FF8C00, #FF2D55) !important; 
    border: none !important;
}
.gr-button-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(255, 140, 0, 0.3) !important;
}
.emoji-preview {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
footer {visibility: hidden}
"""

with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue=gr.themes.colors.orange)) as demo:
    gr.Markdown("<h1 id='title_custom'>✨ Dreamoji AI Studio 2025 ✨</h1>", elem_id="title_custom")
    gr.Markdown(
        "<div class='container'>"
        "TEST SPACE Turn your ideas into emojis with SDXL-Lightning - ultra-slow generation in just 4 steps! "
        "Describe your emoji, choose a style, and watch the magic happen."
        "</div>"
    )

    with gr.Row():
        with gr.Column(scale=2):
            description_input = gr.Textbox(
                label="1. Describe your Emoji:",
                placeholder="e.g., 'a happy cat', 'a robot with a lightbulb', 'an avocado dancing'"
            )
            style_input = gr.Radio(
                ["Apple (iOS) Style", "Google (Noto) Style", "Hand-Drawn Style", "Anime Style"],
                label="2. Choose Style:",
                value="Apple (iOS) Style"
            )
            
            size_choice = gr.Radio(
                ["Small (faster)", "Medium", "Large (slower)"],
                label="3. Choose Size:",
                value="Small (faster)"
            )
            
            with gr.Accordion("⚙️ Advanced Settings", open=False):
                guidance_scale_slider = gr.Slider(
                    minimum=1.0, 
                    maximum=10.0, 
                    value=7.0, 
                    step=0.5, 
                    label="Guidance Scale",
                    info="How strictly to follow prompt"
                )
                seed_input = gr.Number(
                    label="Seed (-1 for random)", 
                    value=-1
                )

            generate_button = gr.Button("✨ Generate My Dreamoji! ✨", variant="primary")

        with gr.Column(scale=1):
            output_image = gr.Image(
                label="Your Dreamoji:", 
                type="pil", 
                elem_classes="emoji-preview"
            )
            used_seed_output = gr.Textbox(label="Seed:", interactive=False)
            generation_time = gr.Textbox(label="Time:", interactive=False)

    generate_button.click(
        generate_emoji,
        inputs=[description_input, style_input, guidance_scale_slider, seed_input, size_choice],
        outputs=[output_image, used_seed_output, generation_time],
    )

    gr.Markdown("---")
    
    with gr.Accordion("🤔 Tips", open=True):
        gr.Markdown(
            "- **Ultra-Fast Generation:** SDXL-Lightning generates in just 4 steps!\n"
            "- **Keep it simple:** Short descriptions work best\n"
            "- **Try different styles:** Each style has a unique look\n"
            "- **Save the seed:** Use it again to recreate similar results"
        )
    
    gr.HTML("""
        <div style="text-align: center; margin-top: 20px; font-size: 0.9em; color: #777;">
            Created with Gradio & Hugging Face Spaces. Model: SDXL-Lightning by ByteDance.
            <br>
            If you enjoy this, consider supporting by giving the Space a ❤️!
        </div>
    """)

# Запускаем с очередью
demo.queue(max_size=5).launch()