File size: 15,004 Bytes
5dea862
 
 
 
64806f8
 
5dea862
 
64806f8
c83763a
64806f8
 
 
 
 
5dea862
7cea496
64806f8
5dea862
64806f8
 
 
 
5dea862
64806f8
 
 
 
 
5dea862
64806f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cea496
 
 
 
 
 
 
 
 
 
 
 
 
 
52df737
 
c23ced1
c699db9
5dea862
c699db9
 
5035677
7511d53
c699db9
 
 
5035677
 
7511d53
 
5035677
7511d53
5035677
 
 
7511d53
 
5035677
c699db9
 
 
 
52df737
c699db9
 
a0e7bff
 
 
 
 
 
 
 
 
7fb2129
 
a0e7bff
 
 
 
 
c699db9
 
a0e7bff
c699db9
64806f8
5dea862
a0e7bff
 
52df737
c23ced1
7cea496
5dea862
 
 
 
 
 
 
 
 
 
64806f8
c23ced1
 
a0fcee4
5035677
7cea496
5dea862
 
64806f8
5dea862
64806f8
a0e7bff
7fb2129
a0e7bff
5035677
5dea862
a0fcee4
5dea862
 
 
 
 
c23ced1
5dea862
a0fcee4
 
a0e7bff
5dea862
64806f8
 
5035677
a0e7bff
c23ced1
5035677
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64806f8
 
5dea862
64806f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dea862
 
 
 
 
 
 
 
 
 
 
 
 
64806f8
 
 
 
 
 
 
5dea862
28d91b7
 
5dea862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28d91b7
b885f28
783b884
 
 
 
 
 
 
 
 
 
b885f28
 
783b884
 
 
b885f28
 
 
783b884
b885f28
783b884
 
 
 
 
 
 
b885f28
 
 
 
 
 
 
 
64806f8
 
 
52df737
5dea862
64806f8
 
 
5dea862
 
 
 
 
 
783b884
 
5dea862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64806f8
5dea862
 
 
 
 
28d91b7
b1228ec
 
 
 
 
 
28d91b7
5dea862
 
 
 
 
 
 
 
 
 
 
 
 
28d91b7
5dea862
 
 
 
 
 
 
 
 
 
 
 
28d91b7
5dea862
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64806f8
 
5dea862
 
64806f8
5dea862
 
 
 
 
 
 
64806f8
5dea862
64806f8
 
 
 
 
5dea862
28d91b7
64806f8
 
 
 
 
 
b1228ec
64806f8
 
5dea862
28d91b7
64806f8
783b884
 
 
 
 
 
 
 
 
 
 
64806f8
 
 
 
 
 
b1228ec
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import os
import gc
import uuid
import random
import tempfile
import time
from datetime import datetime
from typing import Any
from huggingface_hub import login, hf_hub_download
import spaces

import gradio as gr
import numpy as np
import torch
from PIL import Image, ImageDraw, ImageFont
from diffusers import FluxPipeline
from transformers import pipeline

# ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ ํ•จ์ˆ˜
def clear_memory():
    gc.collect()
    try:
        if torch.cuda.is_available():
            with torch.cuda.device(0):
                torch.cuda.empty_cache()
    except:
        pass

# GPU ์„ค์ •
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

if torch.cuda.is_available():
    try:
        with torch.cuda.device(0):
            torch.cuda.empty_cache()
            torch.backends.cudnn.benchmark = True
            torch.backends.cuda.matmul.allow_tf32 = True
    except:
        print("Warning: Could not configure CUDA settings")

# HF ํ† ํฐ ์„ค์ •
HF_TOKEN = os.getenv("HF_TOKEN")
if HF_TOKEN is None:
    raise ValueError("Please set the HF_TOKEN environment variable")

try:
    login(token=HF_TOKEN)
except Exception as e:
    raise ValueError(f"Failed to login to Hugging Face: {str(e)}")



translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device=-1)  # CPU์—์„œ ์‹คํ–‰

def translate_to_english(text: str) -> str:
    """ํ•œ๊ธ€ ํ…์ŠคํŠธ๋ฅผ ์˜์–ด๋กœ ๋ฒˆ์—ญ"""
    try:
        if any(ord('๊ฐ€') <= ord(char) <= ord('ํžฃ') for char in text):
            translated = translator(text, max_length=128)[0]['translation_text']
            print(f"Translated '{text}' to '{translated}'")
            return translated
        return text
    except Exception as e:
        print(f"Translation error: {str(e)}")
        return text


# FLUX ํŒŒ์ดํ”„๋ผ์ธ ์ดˆ๊ธฐํ™” ๋ถ€๋ถ„ ์ˆ˜์ •
print("Initializing FLUX pipeline...")
try:
    pipe = FluxPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-dev",
        torch_dtype=torch.float16,
        use_auth_token=HF_TOKEN
    )
    print("FLUX pipeline initialized successfully")
    
    # ๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™” ์„ค์ •
    pipe.enable_attention_slicing(slice_size=1)
    
    # GPU ์„ค์ •
    if torch.cuda.is_available():
        pipe = pipe.to("cuda:0")
        torch.cuda.empty_cache()
        torch.backends.cudnn.benchmark = True
        torch.backends.cuda.matmul.allow_tf32 = True
    
    print("Pipeline optimization settings applied")

except Exception as e:
    print(f"Error initializing FLUX pipeline: {str(e)}")
    raise

# LoRA ๊ฐ€์ค‘์น˜ ๋กœ๋“œ ๋ถ€๋ถ„ ์ˆ˜์ •
print("Loading LoRA weights...")
try:
    # ๋กœ์ปฌ LoRA ํŒŒ์ผ์˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ ํ™•์ธ
    current_dir = os.path.dirname(os.path.abspath(__file__))
    lora_path = os.path.join(current_dir, "myt-flux-fantasy.safetensors")
    
    if not os.path.exists(lora_path):
        raise FileNotFoundError(f"LoRA file not found at: {lora_path}")
    
    print(f"Loading LoRA weights from: {lora_path}")
    
    # LoRA ๊ฐ€์ค‘์น˜ ๋กœ๋“œ
    pipe.load_lora_weights(lora_path)
    pipe.fuse_lora(lora_scale=0.75)  # lora_scale ๊ฐ’ ์กฐ์ •
    
    # ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ
    torch.cuda.empty_cache()
    gc.collect()
    
    print("LoRA weights loaded and fused successfully")
    print(f"Current device: {pipe.device}")

except Exception as e:
    print(f"Error loading LoRA weights: {str(e)}")
    print(f"Full error details: {repr(e)}")
    raise ValueError(f"Failed to load LoRA weights: {str(e)}")
    

@spaces.GPU(duration=60)
def generate_image(
    prompt: str,
    seed: int,
    randomize_seed: bool,
    width: int,
    height: int,
    guidance_scale: float,
    num_inference_steps: int,
    progress: gr.Progress = gr.Progress()
):
    try:
        clear_memory()
        
        translated_prompt = translate_to_english(prompt)
        print(f"Processing prompt: {translated_prompt}")
        
        if randomize_seed:
            seed = random.randint(0, MAX_SEED)
        
        generator = torch.Generator(device=device).manual_seed(seed)
        
        print(f"Current device: {pipe.device}")
        print(f"Starting image generation...")
        
        with torch.inference_mode(), torch.cuda.amp.autocast(enabled=True):
            image = pipe(
                prompt=translated_prompt,
                width=width,
                height=height,
                num_inference_steps=num_inference_steps,
                guidance_scale=guidance_scale,
                generator=generator,
                num_images_per_prompt=1,
            ).images[0]
        
        filepath = save_generated_image(image, translated_prompt)
        print(f"Image generated and saved to: {filepath}")
        return image, seed
        
    except Exception as e:
        print(f"Generation error: {str(e)}")
        print(f"Full error details: {repr(e)}")
        raise gr.Error(f"Image generation failed: {str(e)}")
    finally:
        clear_memory()

# ์ €์žฅ ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •
SAVE_DIR = "saved_images"
if not os.path.exists(SAVE_DIR):
    os.makedirs(SAVE_DIR, exist_ok=True)

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

def save_generated_image(image, prompt):
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    unique_id = str(uuid.uuid4())[:8]
    filename = f"{timestamp}_{unique_id}.png"
    filepath = os.path.join(SAVE_DIR, filename)
    image.save(filepath)
    return filepath



def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
    """ํ…์ŠคํŠธ์— ์™ธ๊ณฝ์„ ์„ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜"""
    for adj_x in range(-stroke_width, stroke_width + 1):
        for adj_y in range(-stroke_width, stroke_width + 1):
            draw.text((x + adj_x, y + adj_y), text, font=font, fill=text_color)

def add_text_to_image(
    input_image,
    text,
    font_size,
    color,
    opacity,
    x_position,
    y_position,
    thickness,
    text_position_type,
    font_choice
):
    try:
        if input_image is None or text.strip() == "":
            return input_image

        if not isinstance(input_image, Image.Image):
            if isinstance(input_image, np.ndarray):
                image = Image.fromarray(input_image)
            else:
                raise ValueError("Unsupported image type")
        else:
            image = input_image.copy()

        if image.mode != 'RGBA':
            image = image.convert('RGBA')

        font_files = {
            "Default": "DejaVuSans.ttf",
            "Korean Regular": "ko-Regular.ttf"
        }
        
        try:
            font_file = font_files.get(font_choice, "DejaVuSans.ttf")
            font = ImageFont.truetype(font_file, int(font_size))
        except Exception as e:
            print(f"Font loading error ({font_choice}): {str(e)}")
            font = ImageFont.load_default()
        
        color_map = {
            'White': (255, 255, 255),
            'Black': (0, 0, 0),
            'Red': (255, 0, 0),
            'Green': (0, 255, 0),
            'Blue': (0, 0, 255),
            'Yellow': (255, 255, 0),
            'Purple': (128, 0, 128)
        }
        rgb_color = color_map.get(color, (255, 255, 255))
        
        temp_draw = ImageDraw.Draw(image)
        text_bbox = temp_draw.textbbox((0, 0), text, font=font)
        text_width = text_bbox[2] - text_bbox[0]
        text_height = text_bbox[3] - text_bbox[1]

        actual_x = int((image.width - text_width) * (x_position / 100))
        actual_y = int((image.height - text_height) * (y_position / 100))

        text_color = (*rgb_color, int(opacity))
        
        txt_overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
        draw = ImageDraw.Draw(txt_overlay)
        
        add_text_with_stroke(
            draw,
            text,
            actual_x,
            actual_y,
            font,
            text_color,
            int(thickness)
        )
        output_image = Image.alpha_composite(image, txt_overlay)
        
        output_image = output_image.convert('RGB')
        
        return output_image

    except Exception as e:
        print(f"Error in add_text_to_image: {str(e)}")
        return input_image    


css = """
footer {display: none}
.main-title {
    text-align: center;
    margin: 1em 0;
    padding: 1.5em;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
    border-radius: 15px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.main-title h1 {
    color: #2196F3;
    font-size: 2.8em;
    margin-bottom: 0.3em;
    font-weight: 700;
}
.main-title p {
    color: #555;
    font-size: 1.3em;
    line-height: 1.4;
}
.container {
    max-width: 1200px;
    margin: auto;
    padding: 20px;
}
.input-panel, .output-panel {
    background: white;
    padding: 1.5em;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    margin-bottom: 1em;
}
"""

import requests

def enhance_prompt(prompt: str) -> str:
    """ํ”„๋กฌํ”„ํŠธ๋ฅผ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์Šคํƒ€์ผ๋กœ ์ฆ๊ฐ•"""
    try:
        # ๊ธฐ๋ณธ ํ’ˆ์งˆ ํ–ฅ์ƒ ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€
        enhancements = [
            "masterpiece, best quality, highly detailed",
            "anime style, animation style",
            "vibrant colors, perfect lighting",
            "professional composition",
            "dynamic pose, expressive features",
            "detailed background, perfect shadows",
            "[trigger]"
        ]
        
        # ์• ๋‹ˆ๋ฉ”์ด์…˜ ์Šคํƒ€์ผ ํ”„๋กฌํ”„ํŠธ ๋ณ€ํ™˜
        anime_style_prompt = f"an animated {prompt}, detailed anime art style"
        
        # ์ตœ์ข… ํ”„๋กฌํ”„ํŠธ ๊ตฌ์„ฑ
        final_prompt = f"{anime_style_prompt}, {', '.join(enhancements)}"
        print(f"Enhanced prompt: {final_prompt}")
        
        return final_prompt
    except Exception as e:
        print(f"Prompt enhancement failed: {str(e)}")
        return prompt

# ๊ธฐ์กด์˜ pipeline ์ดˆ๊ธฐํ™” ๋ถ€๋ถ„ ์ œ๊ฑฐ
# try:
#     prompt_enhancer = pipeline(...)
# except Exception as e:
#     print(f"Error initializing prompt enhancer: {str(e)}")
#     prompt_enhancer = None


with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
    gr.HTML("""
        <div class="main-title">
            <h1>๐ŸŽจ Webtoon Studio</h1>
            <p>Generate webtoon-style images and add text with various styles and positions.</p>
        </div>
    """)

    with gr.Row():
        with gr.Column(scale=1):
            gen_prompt = gr.Textbox(
                label="Generation Prompt",
                placeholder="Enter your image generation prompt..."
            )
            enhance_btn = gr.Button("โœจ Enhance Prompt", variant="secondary")

            with gr.Row():
                gen_width = gr.Slider(512, 1024, 768, step=64, label="Width")
                gen_height = gr.Slider(512, 1024, 768, step=64, label="Height")
            
            with gr.Row():
                guidance_scale = gr.Slider(1, 20, 7.5, step=0.5, label="Guidance Scale")
                num_steps = gr.Slider(1, 50, 30, step=1, label="Number of Steps")
            
            with gr.Row():
                seed = gr.Number(label="Seed", value=-1)
                randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
            
            generate_btn = gr.Button("Generate Image", variant="primary")
            
            output_image = gr.Image(
                label="Generated Image",
                type="pil",
                show_download_button=True
            )
            output_seed = gr.Number(label="Used Seed", interactive=False)

            # ํ…์ŠคํŠธ ์ถ”๊ฐ€ ์„น์…˜
            with gr.Accordion("Text Options", open=False):
                text_input = gr.Textbox(
                    label="Text Content",
                    placeholder="Enter text to add..."
                )
                text_position_type = gr.Radio(
                    choices=["Text Over Image"],
                    value="Text Over Image",
                    label="Text Position",
                    visible=True
                )
                with gr.Row():
                    font_choice = gr.Dropdown(
                        choices=["Default", "Korean Regular"],
                        value="Default",
                        label="Font Selection",
                        interactive=True
                    )
                    font_size = gr.Slider(
                        minimum=10,
                        maximum=200,
                        value=40,
                        step=5,
                        label="Font Size"
                    )
                with gr.Row():
                    color_dropdown = gr.Dropdown(
                        choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
                        value="White",
                        label="Text Color"
                    )
                    thickness = gr.Slider(
                        minimum=0,
                        maximum=10,
                        value=1,
                        step=1,
                        label="Text Thickness"
                    )
                with gr.Row():
                    opacity_slider = gr.Slider(
                        minimum=0,
                        maximum=255,
                        value=255,
                        step=1,
                        label="Opacity"
                    )
                with gr.Row():
                    x_position = gr.Slider(
                        minimum=0,
                        maximum=100,
                        value=50,
                        step=1,
                        label="Left(0%)~Right(100%)"
                    )
                    y_position = gr.Slider(
                        minimum=0,
                        maximum=100,
                        value=50,
                        step=1,
                        label="High(0%)~Low(100%)"
                    )
                add_text_btn = gr.Button("Apply Text", variant="primary")

    # ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
    generate_btn.click(
        fn=generate_image,
        inputs=[
            gen_prompt,
            seed,
            randomize_seed,
            gen_width,
            gen_height,
            guidance_scale,
            num_steps,
        ],
        outputs=[output_image, output_seed]
    )

    add_text_btn.click(
        fn=add_text_to_image,
        inputs=[
            output_image,
            text_input,
            font_size,
            color_dropdown,
            opacity_slider,
            x_position,
            y_position,
            thickness,
            text_position_type,
            font_choice
        ],
        outputs=output_image
    )

    # ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ ์ถ”๊ฐ€
    def update_prompt(prompt):
        enhanced = enhance_prompt(prompt)
        return enhanced

    enhance_btn.click(
        fn=update_prompt,
        inputs=[gen_prompt],
        outputs=[gen_prompt]
    )

demo.queue(max_size=5)
demo.launch(
    server_name="0.0.0.0",
    server_port=7860,
    share=False,
    max_threads=2
)