File size: 5,804 Bytes
40462a0
7aafe2f
 
 
 
 
a30ff0a
7aafe2f
 
a30ff0a
 
 
 
 
 
 
 
 
40462a0
7aafe2f
 
 
 
 
 
40462a0
7aafe2f
 
 
a30ff0a
7aafe2f
 
 
a30ff0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aafe2f
 
23fd89e
7aafe2f
a30ff0a
 
7aafe2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a30ff0a
7aafe2f
 
 
 
a30ff0a
 
 
 
 
 
7aafe2f
 
d4545dc
 
 
 
 
 
7aafe2f
d4545dc
7aafe2f
 
 
 
a30ff0a
7aafe2f
 
a30ff0a
 
7aafe2f
 
 
 
a30ff0a
7aafe2f
a30ff0a
7aafe2f
 
 
a30ff0a
 
7aafe2f
a30ff0a
 
 
7aafe2f
 
a30ff0a
7aafe2f
 
 
 
 
 
 
 
 
 
 
 
97d3c4e
7aafe2f
 
 
 
 
 
 
 
 
23fd89e
7aafe2f
 
 
 
 
 
 
 
a30ff0a
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
import gradio as gr
import numpy as np
import random
import spaces
import torch
import time
import os
from diffusers import DiffusionPipeline
from custom_pipeline import FLUXPipelineWithIntermediateOutputs
from transformers import pipeline

# Hugging Face 토큰 κ°€μ Έμ˜€κΈ°
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
    raise ValueError("HF_TOKEN environment variable is not set. Please set it to your Hugging Face token.")

# λ²ˆμ—­ λͺ¨λΈ λ‘œλ“œ (토큰 인증 μΆ”κ°€)
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", use_auth_token=hf_token)

# Constants
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 2048
DEFAULT_WIDTH = 1024
DEFAULT_HEIGHT = 1024
DEFAULT_INFERENCE_STEPS = 1

# Device and model setup
dtype = torch.float16
pipe = FLUXPipelineWithIntermediateOutputs.from_pretrained(
    "black-forest-labs/FLUX.1-schnell", torch_dtype=dtype, use_auth_token=hf_token
).to("cuda")
torch.cuda.empty_cache()

# ν•œκΈ€ 메뉴 이름 dictionary
korean_labels = {
    "Generated Image": "μƒμ„±λœ 이미지",
    "Prompt": "ν”„λ‘¬ν”„νŠΈ",
    "Enhance Image": "이미지 ν–₯상",
    "Advanced Options": "κ³ κΈ‰ μ˜΅μ…˜",
    "Seed": "μ‹œλ“œ",
    "Randomize Seed": "μ‹œλ“œ λ¬΄μž‘μœ„ν™”",
    "Width": "λ„ˆλΉ„",
    "Height": "높이",
    "Inference Steps": "μΆ”λ‘  단계",
    "Inspiration Gallery": "영감 가러리"
}

def translate_if_korean(text):
    if any('\u3131' <= char <= '\u3163' or '\uac00' <= char <= '\ud7a3' for char in text):
        return translator(text, use_auth_token=hf_token)[0]['translation_text']
    return text

# Inference function
@spaces.GPU(duration=25)
def generate_image(prompt, seed=42, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, randomize_seed=False, num_inference_steps=DEFAULT_INFERENCE_STEPS):
    
    prompt = translate_if_korean(prompt)
    
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    generator = torch.Generator().manual_seed(seed)

    start_time = time.time()

    # Only generate the last image in the sequence
    for img in pipe.generate_images(  
            prompt=prompt,
            guidance_scale=0,
            num_inference_steps=num_inference_steps,
            width=width,
            height=height,
            generator=generator
        ): 
        latency = f"처리 μ‹œκ°„: {(time.time()-start_time):.2f} 초"    
        yield img, seed, latency

# Example prompts
examples = [
    "λ‹¬μ—μ„œ μ•Œμ—μ„œ λΆ€ν™”ν•˜λŠ” μž‘μ€ 우주 비행사",
    "μ•ˆλ…•ν•˜μ„Έμš” 세상이라고 쓰인 ν‘œμ§€νŒμ„ λ“€κ³  μžˆλŠ” 고양이",
    "λΉ„λ„ˆ μŠˆλ‹ˆμ²Όμ˜ μ• λ‹ˆλ©”μ΄μ…˜ μΌλŸ¬μŠ€νŠΈλ ˆμ΄μ…˜",
    "ν•˜λŠ˜μ„ λ‚˜λŠ” μžλ™μ°¨μ™€ λ„€μ˜¨ λΆˆλΉ›μ΄ μžˆλŠ” 미래적인 λ„μ‹œ 풍경",
    "κΈ΄ κ°ˆμƒ‰ μ›¨μ΄λΈŒ 머리λ₯Ό 올렀 λ¬Άκ³  μ•ˆκ²½μ„ μ“΄ μ Šμ€ μ—¬μ„±μ˜ 사진. κ·Έλ…€λŠ” 흰 피뢀에 눈과 μž…μˆ μ„ κ°•μ‘°ν•œ μ€μ€ν•œ ν™”μž₯을 ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έλ…€λŠ” 검은색 μƒμ˜λ₯Ό μž…μ—ˆμŠ΅λ‹ˆλ‹€. 배경은 λ„μ‹œ 건물 μ™Έκ΄€μœΌλ‘œ 보이며, 햇빛이 κ·Έλ…€μ˜ 얼꡴에 λ”°λœ»ν•œ 빛을 λΉ„μΆ”κ³  μžˆμŠ΅λ‹ˆλ‹€.",
    "μŠ€ν‹°λΈŒ 작슀λ₯Ό μŠ€νƒ€μ›Œμ¦ˆ μ˜ν™” μΊλ¦­ν„°λ‘œ μƒμƒν•΄λ³΄μ„Έμš”"
]

css = """
footer {
    visibility: hidden;
}
"""

# --- Gradio UI ---
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
    with gr.Column(elem_id="app-container"):

        with gr.Row():
            with gr.Column(scale=3):
                result = gr.Image(label=korean_labels["Generated Image"], show_label=False, interactive=False)
            with gr.Column(scale=1):
                prompt = gr.Text(
                    label=korean_labels["Prompt"],
                    placeholder="μƒμ„±ν•˜κ³  싢은 이미지λ₯Ό μ„€λͺ…ν•˜μ„Έμš”...",
                    lines=3,
                    show_label=False,
                    container=False,
                )
                enhanceBtn = gr.Button(f"πŸš€ {korean_labels['Enhance Image']}")

                with gr.Column(korean_labels["Advanced Options"]):
                    with gr.Row():
                        latency = gr.Text(show_label=False)
                    with gr.Row():
                        seed = gr.Number(label=korean_labels["Seed"], value=42, precision=0)
                        randomize_seed = gr.Checkbox(label=korean_labels["Randomize Seed"], value=False)
                    with gr.Row():
                        width = gr.Slider(label=korean_labels["Width"], minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_WIDTH)
                        height = gr.Slider(label=korean_labels["Height"], minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=DEFAULT_HEIGHT)
                        num_inference_steps = gr.Slider(label=korean_labels["Inference Steps"], minimum=1, maximum=4, step=1, value=DEFAULT_INFERENCE_STEPS)

        with gr.Row():
            gr.Markdown(f"### 🌟 {korean_labels['Inspiration Gallery']}")
        with gr.Row():
            gr.Examples(
                examples=examples,
                fn=generate_image,
                inputs=[prompt],
                outputs=[result, seed],
                cache_examples="lazy" 
            )

    # Event handling - Trigger image generation on button click or input change
    enhanceBtn.click(
        fn=generate_image,
        inputs=[prompt, seed, width, height],
        outputs=[result, seed, latency],
        show_progress="hidden",
        show_api=False,
        queue=False
    )

    gr.on(
        triggers=[prompt.input, width.input, height.input, num_inference_steps.input],
        fn=generate_image,
        inputs=[prompt, seed, width, height, randomize_seed, num_inference_steps],
        outputs=[result, seed, latency],
        show_progress="hidden",
        show_api=False,
        trigger_mode="always_last",
        queue=False
    )

# Launch the app
demo.launch()