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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import spaces
|
| 8 |
+
import torch
|
| 9 |
+
from diffusers import DiffusionPipeline
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
# Create permanent storage directory
|
| 13 |
+
SAVE_DIR = "saved_images" # Gradio will handle the persistence
|
| 14 |
+
if not os.path.exists(SAVE_DIR):
|
| 15 |
+
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
repo_id = "black-forest-labs/FLUX.1-dev"
|
| 19 |
+
adapter_id = "seawolf2357/kim-korea" # ํน์ ์ ์น์ธ์ ํ์ตํ LoRA ๋ชจ๋ธ
|
| 20 |
+
|
| 21 |
+
pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16)
|
| 22 |
+
pipeline.load_lora_weights(adapter_id)
|
| 23 |
+
pipeline = pipeline.to(device)
|
| 24 |
+
|
| 25 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 26 |
+
MAX_IMAGE_SIZE = 1024
|
| 27 |
+
|
| 28 |
+
def save_generated_image(image, prompt):
|
| 29 |
+
# Generate unique filename with timestamp
|
| 30 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 31 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 32 |
+
filename = f"{timestamp}_{unique_id}.png"
|
| 33 |
+
filepath = os.path.join(SAVE_DIR, filename)
|
| 34 |
+
|
| 35 |
+
# Save the image
|
| 36 |
+
image.save(filepath)
|
| 37 |
+
|
| 38 |
+
# Save metadata
|
| 39 |
+
metadata_file = os.path.join(SAVE_DIR, "metadata.txt")
|
| 40 |
+
with open(metadata_file, "a", encoding="utf-8") as f:
|
| 41 |
+
f.write(f"{filename}|{prompt}|{timestamp}\n")
|
| 42 |
+
|
| 43 |
+
return filepath
|
| 44 |
+
|
| 45 |
+
@spaces.GPU(duration=60)
|
| 46 |
+
def inference(
|
| 47 |
+
prompt,
|
| 48 |
+
seed=42,
|
| 49 |
+
randomize_seed=True,
|
| 50 |
+
width=1024,
|
| 51 |
+
height=768,
|
| 52 |
+
guidance_scale=3.5,
|
| 53 |
+
num_inference_steps=30,
|
| 54 |
+
lora_scale=1.0,
|
| 55 |
+
progress=None,
|
| 56 |
+
):
|
| 57 |
+
if randomize_seed:
|
| 58 |
+
seed = random.randint(0, MAX_SEED)
|
| 59 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 60 |
+
|
| 61 |
+
image = pipeline(
|
| 62 |
+
prompt=prompt,
|
| 63 |
+
guidance_scale=guidance_scale,
|
| 64 |
+
num_inference_steps=num_inference_steps,
|
| 65 |
+
width=width,
|
| 66 |
+
height=height,
|
| 67 |
+
generator=generator,
|
| 68 |
+
joint_attention_kwargs={"scale": lora_scale},
|
| 69 |
+
).images[0]
|
| 70 |
+
|
| 71 |
+
# Save the generated image
|
| 72 |
+
filepath = save_generated_image(image, prompt)
|
| 73 |
+
|
| 74 |
+
# Return just the image and seed
|
| 75 |
+
return image, seed
|
| 76 |
+
|
| 77 |
+
# ์์ ๋ฌธ๊ตฌ: ํน์ ์ ์น์ธ Mr. KIM์ ๋ค์ํ ์ํฉ์ ๋ฌ์ฌ
|
| 78 |
+
|
| 79 |
+
examples = [
|
| 80 |
+
"Mr. KIM holding up a 'Fighting!' banner with both hands, showing patriotic pride and determination for national excellence. ",
|
| 81 |
+
"Mr. KIM raising both arms in celebration with a triumphant expression, showing victory and hope for the future.",
|
| 82 |
+
"Mr. KIM jogging in a park wearing athletic gear, demonstrating healthy lifestyle and energetic leadership qualities.",
|
| 83 |
+
"Mr. KIM warmly shaking hands with female citizens in a crowded street, showing genuine care and connection with women voters. ",
|
| 84 |
+
"Mr. KIM at a campaign rally, pointing toward the horizon with an inspiring gesture while female and kids audience members applaud. ",
|
| 85 |
+
"Mr. KIM participating in a community event, surrounded by enthusiastic female supporters cheering ",
|
| 86 |
+
"Mr. KIM visiting a local market, engaging in friendly conversation with female vendors and shopkeepers. ",
|
| 87 |
+
"Mr. KIM walking through a university campus, discussing education policies with female students and professors. ",
|
| 88 |
+
"Mr. KIM delivering a powerful speech in front of a large crowd with confident gestures and determined expression. ",
|
| 89 |
+
"Mr. KIM in a dynamic interview setting, passionately outlining his visions for the future.",
|
| 90 |
+
"Mr. KIM preparing for an important debate, surrounded by paperwork, looking focused and resolute. ",
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
# UI๋ฅผ ๋ถ์ ๊ณ์ด ๊ทธ๋ผ๋์์ด์
์ผ๋ก ๋์์ธ
|
| 94 |
+
custom_css = """
|
| 95 |
+
:root {
|
| 96 |
+
--color-primary: #8F1A3A; /* ๋ถ์ ํค์ ๋ฉ์ธ ์ปฌ๋ฌ */
|
| 97 |
+
--color-secondary: #FF4B4B; /* ํฌ์ธํธ ์ปฌ๋ฌ(๋ฐ์ ๋นจ๊ฐ) */
|
| 98 |
+
--background-fill-primary: linear-gradient(to right, #FFF5F5, #FED7D7, #FEB2B2);
|
| 99 |
+
}
|
| 100 |
+
footer {
|
| 101 |
+
visibility: hidden;
|
| 102 |
+
}
|
| 103 |
+
.gradio-container {
|
| 104 |
+
background: var(--background-fill-primary);
|
| 105 |
+
}
|
| 106 |
+
.title {
|
| 107 |
+
color: var(--color-primary) !important;
|
| 108 |
+
font-size: 3rem !important;
|
| 109 |
+
font-weight: 700 !important;
|
| 110 |
+
text-align: center;
|
| 111 |
+
margin: 1rem 0;
|
| 112 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.05);
|
| 113 |
+
font-family: 'Playfair Display', serif;
|
| 114 |
+
}
|
| 115 |
+
.subtitle {
|
| 116 |
+
color: #4A5568 !important;
|
| 117 |
+
font-size: 1.2rem !important;
|
| 118 |
+
text-align: center;
|
| 119 |
+
margin-bottom: 1.5rem;
|
| 120 |
+
font-style: italic;
|
| 121 |
+
}
|
| 122 |
+
.collection-link {
|
| 123 |
+
text-align: center;
|
| 124 |
+
margin-bottom: 2rem;
|
| 125 |
+
font-size: 1.1rem;
|
| 126 |
+
}
|
| 127 |
+
.collection-link a {
|
| 128 |
+
color: var(--color-primary);
|
| 129 |
+
text-decoration: underline;
|
| 130 |
+
transition: color 0.3s ease;
|
| 131 |
+
}
|
| 132 |
+
.collection-link a:hover {
|
| 133 |
+
color: var(--color-secondary);
|
| 134 |
+
}
|
| 135 |
+
.model-description {
|
| 136 |
+
background-color: rgba(255, 255, 255, 0.8);
|
| 137 |
+
border-radius: 12px;
|
| 138 |
+
padding: 24px;
|
| 139 |
+
margin: 20px 0;
|
| 140 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
|
| 141 |
+
border-left: 5px solid var(--color-primary);
|
| 142 |
+
}
|
| 143 |
+
button.primary {
|
| 144 |
+
background-color: var(--color-primary) !important;
|
| 145 |
+
transition: all 0.3s ease;
|
| 146 |
+
color: #fff !important;
|
| 147 |
+
}
|
| 148 |
+
button:hover {
|
| 149 |
+
transform: translateY(-2px);
|
| 150 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
|
| 151 |
+
}
|
| 152 |
+
.input-container {
|
| 153 |
+
border-radius: 10px;
|
| 154 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
| 155 |
+
background-color: rgba(255, 255, 255, 0.6);
|
| 156 |
+
padding: 20px;
|
| 157 |
+
margin-bottom: 1rem;
|
| 158 |
+
}
|
| 159 |
+
.advanced-settings {
|
| 160 |
+
margin-top: 1rem;
|
| 161 |
+
padding: 1rem;
|
| 162 |
+
border-radius: 10px;
|
| 163 |
+
background-color: rgba(255, 255, 255, 0.6);
|
| 164 |
+
}
|
| 165 |
+
.example-region {
|
| 166 |
+
background-color: rgba(255, 255, 255, 0.5);
|
| 167 |
+
border-radius: 10px;
|
| 168 |
+
padding: 1rem;
|
| 169 |
+
margin-top: 1rem;
|
| 170 |
+
}
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
with gr.Blocks(css=custom_css, analytics_enabled=False) as demo:
|
| 174 |
+
gr.HTML('<div class="title">Mr. KIM in KOREA</div>')
|
| 175 |
+
|
| 176 |
+
# ์ปฌ๋ ์
๋งํฌ ๋๋ ์๋ด๋ฌธ์ ํ์ ์ ์์ /์ญ์
|
| 177 |
+
gr.HTML('<div class="collection-link"><a href="https://huggingface.co/collections/openfree/painting-art-ai-681453484ec15ef5978bbeb1" target="_blank">Visit the LoRA Model Collection</a></div>')
|
| 178 |
+
|
| 179 |
+
# ๋ชจ๋ธ ์ค๋ช
: ํน์ ์ ์น์ธ์ ๋ํ LoRA ๋ชจ๋ธ์์ ์ธ๊ธ
|
| 180 |
+
with gr.Group(elem_classes="model-description"):
|
| 181 |
+
gr.HTML("""
|
| 182 |
+
<p>
|
| 183 |
+
๋ณธ ๋ชจ๋ธ์ ์ฐ๊ตฌ ๋ชฉ์ ์ผ๋ก ํน์ ์ธ์ ์ผ๊ตด๊ณผ ์ธ๋ชจ๋ฅผ ํ์ตํ LoRA ๋ชจ๋ธ์
๋๋ค.<br>
|
| 184 |
+
๋ชฉ์ ์ธ์ ์ฉ๋๋ก ๋ฌด๋จ ์ฌ์ฉ ์๋๋ก ์ ์ํด ์ฃผ์ธ์.<br>
|
| 185 |
+
(์์ prompt ์ฌ์ฉ ์ ๋ฐ๋์ 'kim'์ ํฌํจํ์ฌ์ผ ์ต์ ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.)
|
| 186 |
+
</p>
|
| 187 |
+
""")
|
| 188 |
+
|
| 189 |
+
# ๋ฉ์ธ UI
|
| 190 |
+
with gr.Column(elem_id="col-container"):
|
| 191 |
+
with gr.Row(elem_classes="input-container"):
|
| 192 |
+
prompt = gr.Text(
|
| 193 |
+
label="Prompt",
|
| 194 |
+
max_lines=1,
|
| 195 |
+
placeholder="Enter your prompt (add [trigger] at the end)",
|
| 196 |
+
value=examples[0] # ๊ธฐ๋ณธ ์์
|
| 197 |
+
)
|
| 198 |
+
run_button = gr.Button("Generate", variant="primary", scale=0)
|
| 199 |
+
|
| 200 |
+
result = gr.Image(label="Generated Image")
|
| 201 |
+
seed_output = gr.Number(label="Seed", visible=True)
|
| 202 |
+
|
| 203 |
+
with gr.Accordion("Advanced Settings", open=False, elem_classes="advanced-settings"):
|
| 204 |
+
seed = gr.Slider(
|
| 205 |
+
label="Seed",
|
| 206 |
+
minimum=0,
|
| 207 |
+
maximum=MAX_SEED,
|
| 208 |
+
step=1,
|
| 209 |
+
value=42,
|
| 210 |
+
)
|
| 211 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 212 |
+
|
| 213 |
+
with gr.Row():
|
| 214 |
+
width = gr.Slider(
|
| 215 |
+
label="Width",
|
| 216 |
+
minimum=256,
|
| 217 |
+
maximum=MAX_IMAGE_SIZE,
|
| 218 |
+
step=32,
|
| 219 |
+
value=1024,
|
| 220 |
+
)
|
| 221 |
+
height = gr.Slider(
|
| 222 |
+
label="Height",
|
| 223 |
+
minimum=256,
|
| 224 |
+
maximum=MAX_IMAGE_SIZE,
|
| 225 |
+
step=32,
|
| 226 |
+
value=768,
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
with gr.Row():
|
| 230 |
+
guidance_scale = gr.Slider(
|
| 231 |
+
label="Guidance scale",
|
| 232 |
+
minimum=0.0,
|
| 233 |
+
maximum=10.0,
|
| 234 |
+
step=0.1,
|
| 235 |
+
value=3.5,
|
| 236 |
+
)
|
| 237 |
+
num_inference_steps = gr.Slider(
|
| 238 |
+
label="Number of inference steps",
|
| 239 |
+
minimum=1,
|
| 240 |
+
maximum=50,
|
| 241 |
+
step=1,
|
| 242 |
+
value=30,
|
| 243 |
+
)
|
| 244 |
+
lora_scale = gr.Slider(
|
| 245 |
+
label="LoRA scale",
|
| 246 |
+
minimum=0.0,
|
| 247 |
+
maximum=1.0,
|
| 248 |
+
step=0.1,
|
| 249 |
+
value=1.0,
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
with gr.Group(elem_classes="example-region"):
|
| 253 |
+
gr.Markdown("### Examples")
|
| 254 |
+
gr.Examples(
|
| 255 |
+
examples=examples,
|
| 256 |
+
inputs=prompt,
|
| 257 |
+
outputs=None, # Don't auto-run examples
|
| 258 |
+
fn=None, # No function to run for examples - just fill the prompt
|
| 259 |
+
cache_examples=False,
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 263 |
+
gr.on(
|
| 264 |
+
triggers=[run_button.click, prompt.submit],
|
| 265 |
+
fn=inference,
|
| 266 |
+
inputs=[
|
| 267 |
+
prompt,
|
| 268 |
+
seed,
|
| 269 |
+
randomize_seed,
|
| 270 |
+
width,
|
| 271 |
+
height,
|
| 272 |
+
guidance_scale,
|
| 273 |
+
num_inference_steps,
|
| 274 |
+
lora_scale,
|
| 275 |
+
],
|
| 276 |
+
outputs=[result, seed_output],
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
demo.queue()
|
| 280 |
+
demo.launch()
|