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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 |
+
# ---------- ์ด๊ธฐ ์ค์ ๋ฐ ๋ชจ๋ธ ๋ก๋ ----------
|
| 13 |
+
SAVE_DIR = "saved_images" # Gradio๊ฐ ์ ์ฅ์ ๊ด๋ฆฌ๋ฅผ ์ํ
|
| 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 = "openfree/pepe"
|
| 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 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 30 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 31 |
+
filename = f"{timestamp}_{unique_id}.png"
|
| 32 |
+
filepath = os.path.join(SAVE_DIR, filename)
|
| 33 |
+
|
| 34 |
+
# ์ด๋ฏธ์ง ์ ์ฅ
|
| 35 |
+
image.save(filepath)
|
| 36 |
+
|
| 37 |
+
# ๋ฉํ๋ฐ์ดํฐ ์ ์ฅ
|
| 38 |
+
metadata_file = os.path.join(SAVE_DIR, "metadata.txt")
|
| 39 |
+
with open(metadata_file, "a", encoding="utf-8") as f:
|
| 40 |
+
f.write(f"{filename}|{prompt}|{timestamp}\n")
|
| 41 |
+
|
| 42 |
+
return filepath
|
| 43 |
+
|
| 44 |
+
def load_generated_images():
|
| 45 |
+
if not os.path.exists(SAVE_DIR):
|
| 46 |
+
return []
|
| 47 |
+
|
| 48 |
+
# ๋๋ ํ ๋ฆฌ ๋ด ์ด๋ฏธ์ง ํ์ผ ๋ก๋
|
| 49 |
+
image_files = [
|
| 50 |
+
os.path.join(SAVE_DIR, f)
|
| 51 |
+
for f in os.listdir(SAVE_DIR)
|
| 52 |
+
if f.endswith(('.png', '.jpg', '.jpeg', '.webp'))
|
| 53 |
+
]
|
| 54 |
+
# ์์ฑ ์๊ฐ ๊ธฐ์ค ์ ๋ ฌ (์ต์ ํ์ผ ์ฐ์ )
|
| 55 |
+
image_files.sort(key=lambda x: os.path.getctime(x), reverse=True)
|
| 56 |
+
return image_files
|
| 57 |
+
|
| 58 |
+
def load_predefined_images():
|
| 59 |
+
# ๋ณ๋ ์ฌ์ ์ด๋ฏธ์ง ์์
|
| 60 |
+
return []
|
| 61 |
+
|
| 62 |
+
css = """
|
| 63 |
+
/* ๋ฐฐ๊ฒฝ ๊ทธ๋ผ๋์ธํธ๋ฅผ ์ฃผ๊ฑฐ๋, ํฐํธ/ํ์ดํ ํฌ๊ธฐ ๋ฑ์ ์ํ๋ ๋๋ก ๊พธ๋ฐ ์ ์์ต๋๋ค. */
|
| 64 |
+
body {
|
| 65 |
+
font-family: 'Open Sans', sans-serif;
|
| 66 |
+
background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
|
| 67 |
+
margin: 0; /* ๊ธฐ๋ณธ ์ฌ๋ฐฑ ์ ๊ฑฐ */
|
| 68 |
+
padding: 0;
|
| 69 |
+
}
|
| 70 |
+
.title {
|
| 71 |
+
font-size: 1.8em;
|
| 72 |
+
font-weight: bold;
|
| 73 |
+
text-align: center;
|
| 74 |
+
margin: 20px 0;
|
| 75 |
+
}
|
| 76 |
+
footer {
|
| 77 |
+
visibility: hidden;
|
| 78 |
+
}
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
@spaces.GPU(duration=120)
|
| 82 |
+
def inference(
|
| 83 |
+
prompt: str,
|
| 84 |
+
seed: int,
|
| 85 |
+
randomize_seed: bool,
|
| 86 |
+
width: int,
|
| 87 |
+
height: int,
|
| 88 |
+
guidance_scale: float,
|
| 89 |
+
num_inference_steps: int,
|
| 90 |
+
lora_scale: float,
|
| 91 |
+
progress: gr.Progress = gr.Progress(track_tqdm=True),
|
| 92 |
+
):
|
| 93 |
+
if randomize_seed:
|
| 94 |
+
seed = random.randint(0, MAX_SEED)
|
| 95 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 96 |
+
|
| 97 |
+
image = pipeline(
|
| 98 |
+
prompt=prompt,
|
| 99 |
+
guidance_scale=guidance_scale,
|
| 100 |
+
num_inference_steps=num_inference_steps,
|
| 101 |
+
width=width,
|
| 102 |
+
height=height,
|
| 103 |
+
generator=generator,
|
| 104 |
+
joint_attention_kwargs={"scale": lora_scale},
|
| 105 |
+
).images[0]
|
| 106 |
+
|
| 107 |
+
filepath = save_generated_image(image, prompt)
|
| 108 |
+
return image, seed, load_generated_images()
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# ---------- ์์ ํ๋กฌํํธ ----------
|
| 112 |
+
examples = [
|
| 113 |
+
"Pepe the frog playing fetch with a golden retriever in a sunny park. He wears casual weekend clothes and tosses a bright red frisbee with a goofy grin. The dog leaps gracefully through the air, tail wagging with excitement. The warm afternoon sunlight filters through the trees, creating a humorous meme-like atmosphere. [pepe]",
|
| 114 |
+
"Pepe the frog dressed in full military gear, standing at attention with a standard-issue rifle. His crisp uniform is adorned with cartoonish medals. Other frog soldiers march in formation behind him during a grand meme parade, conveying discipline mixed with comical charm. [pepe]",
|
| 115 |
+
"A medieval Pepe knight in gleaming armor, proudly holding an ornate sword and shield. He stands in front of a majestic castle with a swirling moat. His shield features a cartoon frog crest, and sunlight gleams off his polished armor, adding a humorous yet epic feel. [pepe]",
|
| 116 |
+
"A charismatic Pepe the frog addressing a crowd from a podium. He wears a well-fitted suit and gestures with exaggerated cartoon expressions while speaking. The audience is filled with fellow frog characters holding supportive banners. Cameras capture this grand meme moment. [pepe]",
|
| 117 |
+
"Pepe the frog enjoying a peaceful morning at home, reading a newspaper at his kitchen table. He wears comfy pajamas and sips coffee from a novelty frog mug. Sunlight streams through the window, illuminating a quaint plant on the countertop in this cozy, meme-inspired scene. [pepe]",
|
| 118 |
+
"Businessman Pepe walking confidently through a sleek office lobby, briefcase in hand. He wears a tailored navy suit, and his wide frog eyes convey determination. Floor-to-ceiling windows reveal a bustling cityscape behind him, blending corporate professionalism with meme humor. [pepe]"
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# ---------- UI ----------
|
| 123 |
+
# ์ํ๋ ๊ทธ๋ผ๋์ค ํ
๋ง๋ฅผ ์ ํํด ์ ์ฉํฉ๋๋ค. ์๋๋ Soft ํ
๋ง์ primary_hue="emerald"๋ฅผ ์ง์ ํ ๏ฟฝ๏ฟฝ๏ฟฝ์์
๋๋ค.
|
| 124 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft(primary_hue="emerald"), analytics_enabled=False) as demo:
|
| 125 |
+
gr.HTML('<div class="title">PEPE Meme Generator</div>')
|
| 126 |
+
|
| 127 |
+
gr.HTML("""
|
| 128 |
+
<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fopenfree-pepe.hf.space">
|
| 129 |
+
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fopenfree-pepe.hf.space&countColor=%23263759" />
|
| 130 |
+
</a>
|
| 131 |
+
""")
|
| 132 |
+
|
| 133 |
+
with gr.Tabs() as tabs:
|
| 134 |
+
with gr.Tab("Generation"):
|
| 135 |
+
with gr.Column():
|
| 136 |
+
with gr.Row():
|
| 137 |
+
prompt = gr.Text(
|
| 138 |
+
label="Prompt",
|
| 139 |
+
show_label=False,
|
| 140 |
+
max_lines=1,
|
| 141 |
+
placeholder="Enter your prompt",
|
| 142 |
+
container=False,
|
| 143 |
+
)
|
| 144 |
+
run_button = gr.Button("Run", scale=0)
|
| 145 |
+
|
| 146 |
+
result = gr.Image(label="Result", show_label=False)
|
| 147 |
+
|
| 148 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 149 |
+
seed = gr.Slider(
|
| 150 |
+
label="Seed",
|
| 151 |
+
minimum=0,
|
| 152 |
+
maximum=MAX_SEED,
|
| 153 |
+
step=1,
|
| 154 |
+
value=42,
|
| 155 |
+
)
|
| 156 |
+
randomize_seed = gr.Checkbox(
|
| 157 |
+
label="Randomize seed",
|
| 158 |
+
value=True
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
with gr.Row():
|
| 162 |
+
width = gr.Slider(
|
| 163 |
+
label="Width",
|
| 164 |
+
minimum=256,
|
| 165 |
+
maximum=MAX_IMAGE_SIZE,
|
| 166 |
+
step=32,
|
| 167 |
+
value=1024,
|
| 168 |
+
)
|
| 169 |
+
height = gr.Slider(
|
| 170 |
+
label="Height",
|
| 171 |
+
minimum=256,
|
| 172 |
+
maximum=MAX_IMAGE_SIZE,
|
| 173 |
+
step=32,
|
| 174 |
+
value=768,
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
with gr.Row():
|
| 178 |
+
guidance_scale = gr.Slider(
|
| 179 |
+
label="Guidance scale",
|
| 180 |
+
minimum=0.0,
|
| 181 |
+
maximum=10.0,
|
| 182 |
+
step=0.1,
|
| 183 |
+
value=3.5,
|
| 184 |
+
)
|
| 185 |
+
num_inference_steps = gr.Slider(
|
| 186 |
+
label="Number of inference steps",
|
| 187 |
+
minimum=1,
|
| 188 |
+
maximum=50,
|
| 189 |
+
step=1,
|
| 190 |
+
value=30,
|
| 191 |
+
)
|
| 192 |
+
lora_scale = gr.Slider(
|
| 193 |
+
label="LoRA scale",
|
| 194 |
+
minimum=0.0,
|
| 195 |
+
maximum=1.0,
|
| 196 |
+
step=0.1,
|
| 197 |
+
value=1.0,
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
gr.Examples(
|
| 201 |
+
examples=examples,
|
| 202 |
+
inputs=[prompt],
|
| 203 |
+
outputs=[result, seed],
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
with gr.Tab("Gallery"):
|
| 207 |
+
gr.Markdown("### Generated Images Gallery")
|
| 208 |
+
generated_gallery = gr.Gallery(
|
| 209 |
+
label="Generated Images",
|
| 210 |
+
columns=6,
|
| 211 |
+
show_label=False,
|
| 212 |
+
value=load_generated_images(),
|
| 213 |
+
elem_id="generated_gallery",
|
| 214 |
+
height="auto"
|
| 215 |
+
)
|
| 216 |
+
refresh_btn = gr.Button("๐ Refresh Gallery")
|
| 217 |
+
|
| 218 |
+
# Gallery ์๋ก๊ณ ์นจ ํธ๋ค๋ฌ
|
| 219 |
+
def refresh_gallery():
|
| 220 |
+
return load_generated_images()
|
| 221 |
+
|
| 222 |
+
refresh_btn.click(
|
| 223 |
+
fn=refresh_gallery,
|
| 224 |
+
inputs=None,
|
| 225 |
+
outputs=generated_gallery,
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# Run ๋ฒํผ & ํ๋กฌํํธ ์
๋ ฅ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
| 229 |
+
gr.on(
|
| 230 |
+
triggers=[run_button.click, prompt.submit],
|
| 231 |
+
fn=inference,
|
| 232 |
+
inputs=[
|
| 233 |
+
prompt,
|
| 234 |
+
seed,
|
| 235 |
+
randomize_seed,
|
| 236 |
+
width,
|
| 237 |
+
height,
|
| 238 |
+
guidance_scale,
|
| 239 |
+
num_inference_steps,
|
| 240 |
+
lora_scale,
|
| 241 |
+
],
|
| 242 |
+
outputs=[result, seed, generated_gallery],
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
demo.queue()
|
| 246 |
+
demo.launch()
|