File size: 7,036 Bytes
69620c8 6960db5 69620c8 31c0b50 6960db5 69620c8 3e46e09 4f076f3 69620c8 c1bd24e 69620c8 c401dbb 6960db5 69620c8 4f076f3 69620c8 6960db5 69620c8 4f076f3 69620c8 4f076f3 3e46e09 69620c8 669c8e5 69620c8 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 c43703f 4f076f3 69620c8 6960db5 69620c8 |
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 |
import spaces
import gradio as gr
import torch
from PIL import Image
from diffusers import DiffusionPipeline
import random
from transformers import pipeline
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cuda.matmul.allow_tf32 = True
# ๋ฒ์ญ ๋ชจ๋ธ ์ด๊ธฐํ
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
base_model = "black-forest-labs/FLUX.1-dev"
pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
lora_repo = "strangerzonehf/Flux-Pixel-Background-LoRA"
trigger_word = ""
pipe.load_lora_weights(lora_repo)
pipe.to("cuda")
MAX_SEED = 2**32-1
@spaces.GPU()
def translate_and_generate(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
# ํ๊ธ ๊ฐ์ง ๋ฐ ๋ฒ์ญ
def contains_korean(text):
return any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text)
if contains_korean(prompt):
# ํ๊ธ์ ์์ด๋ก ๋ฒ์ญ
translated = translator(prompt)[0]['translation_text']
actual_prompt = translated
else:
actual_prompt = prompt
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator(device="cuda").manual_seed(seed)
progress(0, "Starting image generation...")
for i in range(1, steps + 1):
if i % (steps // 10) == 0:
progress(i / steps * 100, f"Processing step {i} of {steps}...")
image = pipe(
prompt=f"{actual_prompt} {trigger_word}",
num_inference_steps=steps,
guidance_scale=cfg_scale,
width=width,
height=height,
generator=generator,
joint_attention_kwargs={"scale": lora_scale},
).images[0]
progress(100, "Completed!")
return image, seed
example_image_path = "example0.webp"
example_prompt = """Pixel Background, a silhouette of a surfer is seen riding a wave on a red surfboard. The surfers shadow is cast on the left side of the image, adding a touch of depth to the composition. The background is a vibrant orange, pink, and blue, with a sun setting in the upper right corner of the frame. The silhouette of the surfer, a palm tree casts a shadow onto the wave, adding depth and contrast to the scene."""
example_cfg_scale = 3.2
example_steps = 32
example_width = 1152
example_height = 896
example_seed = 3981632454
example_lora_scale = 0.85
def load_example():
example_image = Image.open(example_image_path)
return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image
css = """
.container {max-width: 1400px; margin: auto; padding: 20px;}
.header {text-align: center; margin-bottom: 30px;}
.generate-btn {background-color: #2ecc71 !important; color: white !important; margin: 20px auto !important; display: block !important; width: 200px !important;}
.generate-btn:hover {background-color: #27ae60 !important;}
.parameter-box {background-color: #f5f6fa; padding: 20px; border-radius: 10px; margin: 10px 0;}
.result-box {background-color: #f5f6fa; padding: 20px; border-radius: 10px; margin: 0 auto 20px auto; text-align: center;}
.image-output {margin: 0 auto; display: block; max-width: 800px !important;}
.accordion {margin-top: 20px;}
"""
with gr.Blocks(css=css) as app:
with gr.Column(elem_classes="container"):
gr.Markdown("# ๐จ Flux ART Image Generator", elem_classes="header")
# ์ด๋ฏธ์ง ์ถ๋ ฅ ์์ญ์ ๋จผ์ ๋ฐฐ์น
with gr.Group(elem_classes="result-box"):
gr.Markdown("### ๐ผ๏ธ Generated Image")
result = gr.Image(label="Result", elem_classes="image-output")
# ์์ฑ ๋ฒํผ
generate_button = gr.Button(
"๐ Generate Image",
elem_classes="generate-btn"
)
# ์ต์
๋ค์ ์์ฝ๋์ธ์ผ๋ก ๊ตฌ์ฑ
with gr.Accordion("๐จ Generation Options", open=False, elem_classes="accordion"):
with gr.Group(elem_classes="parameter-box"):
prompt = gr.TextArea(
label="โ๏ธ Your Prompt (ํ๊ธ ๋๋ ์์ด)",
placeholder="์ด๋ฏธ์ง๋ฅผ ์ค๋ช
ํ์ธ์... (ํ๊ธ ์
๋ ฅ์ ์๋์ผ๋ก ์์ด๋ก ๋ฒ์ญ๋ฉ๋๋ค)",
lines=5
)
with gr.Group(elem_classes="parameter-box"):
gr.Markdown("### ๐๏ธ Generation Parameters")
with gr.Row():
with gr.Column():
cfg_scale = gr.Slider(
label="CFG Scale",
minimum=1,
maximum=20,
step=0.5,
value=example_cfg_scale
)
steps = gr.Slider(
label="Steps",
minimum=1,
maximum=100,
step=1,
value=example_steps
)
lora_scale = gr.Slider(
label="LoRA Scale",
minimum=0,
maximum=1,
step=0.01,
value=example_lora_scale
)
with gr.Group(elem_classes="parameter-box"):
gr.Markdown("### ๐ Image Dimensions")
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=1536,
step=64,
value=example_width
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=1536,
step=64,
value=example_height
)
with gr.Group(elem_classes="parameter-box"):
gr.Markdown("### ๐ฒ Seed Settings")
with gr.Row():
randomize_seed = gr.Checkbox(
True,
label="Randomize seed"
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=example_seed
)
app.load(
load_example,
inputs=[],
outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result]
)
generate_button.click(
translate_and_generate,
inputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale],
outputs=[result, seed]
)
app.queue()
app.launch() |