Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,75 @@
|
|
| 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 torch
|
| 8 |
from diffusers import DiffusionPipeline
|
| 9 |
-
from PIL import Image
|
| 10 |
import warnings
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ๊ฒฝ๊ณ ๋ฉ์์ง ์จ๊ธฐ๊ธฐ
|
| 13 |
warnings.filterwarnings('ignore', category=UserWarning)
|
| 14 |
|
| 15 |
-
# ์ ์ฅ ๋๋ ํ ๋ฆฌ ์์ฑ
|
| 16 |
SAVE_DIR = "saved_images"
|
| 17 |
if not os.path.exists(SAVE_DIR):
|
| 18 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 19 |
|
| 20 |
-
# ์ฅ์น ์ค์
|
|
|
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
|
| 23 |
# ๋ชจ๋ธ ๋ก๋
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
MAX_SEED = np.iinfo(np.int32).max
|
| 29 |
MAX_IMAGE_SIZE = 2048
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Enhanced examples with more detailed prompts and specific styling
|
| 32 |
EXAMPLES = [
|
| 33 |
{
|
|
@@ -257,43 +299,85 @@ def generate_diagram(prompt, width=1024, height=1024):
|
|
| 257 |
except Exception as e:
|
| 258 |
raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 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 |
-
examples=GRADIO_EXAMPLES,
|
| 295 |
-
cache_examples=True
|
| 296 |
-
)
|
| 297 |
|
| 298 |
# ์ฑ ์คํ
|
| 299 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import random
|
| 4 |
import torch
|
| 5 |
from diffusers import DiffusionPipeline
|
|
|
|
| 6 |
import warnings
|
| 7 |
+
import os
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import uuid
|
| 10 |
|
| 11 |
# ๊ฒฝ๊ณ ๋ฉ์์ง ์จ๊ธฐ๊ธฐ
|
| 12 |
warnings.filterwarnings('ignore', category=UserWarning)
|
| 13 |
|
| 14 |
+
# ์ ์ฅ ๋๋ ํ ๋ฆฌ ์์ฑ
|
| 15 |
SAVE_DIR = "saved_images"
|
| 16 |
if not os.path.exists(SAVE_DIR):
|
| 17 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 18 |
|
| 19 |
+
# ์ฅ์น ๋ฐ dtype ์ค์
|
| 20 |
+
dtype = torch.float32 if torch.cuda.is_available() else torch.float32
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
|
| 23 |
# ๋ชจ๋ธ ๋ก๋
|
| 24 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 25 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 26 |
+
torch_dtype=dtype,
|
| 27 |
+
device_map="auto",
|
| 28 |
+
use_safetensors=True
|
| 29 |
+
).to(device)
|
| 30 |
+
|
| 31 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ
|
| 32 |
+
pipe.enable_attention_slicing()
|
| 33 |
+
if device == "cpu":
|
| 34 |
+
pipe.enable_sequential_cpu_offload()
|
| 35 |
|
| 36 |
MAX_SEED = np.iinfo(np.int32).max
|
| 37 |
MAX_IMAGE_SIZE = 2048
|
| 38 |
|
| 39 |
+
def generate_diagram(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4):
|
| 40 |
+
"""FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ด์ด๊ทธ๋จ ์์ฑ"""
|
| 41 |
+
try:
|
| 42 |
+
if randomize_seed:
|
| 43 |
+
seed = random.randint(0, MAX_SEED)
|
| 44 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 45 |
+
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
image = pipe(
|
| 48 |
+
prompt=prompt,
|
| 49 |
+
width=width,
|
| 50 |
+
height=height,
|
| 51 |
+
num_inference_steps=num_inference_steps,
|
| 52 |
+
generator=generator,
|
| 53 |
+
guidance_scale=0.0
|
| 54 |
+
).images[0]
|
| 55 |
+
|
| 56 |
+
# ์ด๋ฏธ์ง ์ ์ฅ
|
| 57 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 58 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 59 |
+
filename = f"diagram_{timestamp}_{unique_id}.png"
|
| 60 |
+
save_path = os.path.join(SAVE_DIR, filename)
|
| 61 |
+
image.save(save_path)
|
| 62 |
+
|
| 63 |
+
return image, seed
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 67 |
+
finally:
|
| 68 |
+
if torch.cuda.is_available():
|
| 69 |
+
torch.cuda.empty_cache()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
# Enhanced examples with more detailed prompts and specific styling
|
| 74 |
EXAMPLES = [
|
| 75 |
{
|
|
|
|
| 299 |
except Exception as e:
|
| 300 |
raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 301 |
|
| 302 |
+
# CSS ์คํ์ผ
|
| 303 |
+
css="""
|
| 304 |
+
#col-container {
|
| 305 |
+
margin: 0 auto;
|
| 306 |
+
max-width: 520px;
|
| 307 |
+
}
|
| 308 |
+
"""
|
| 309 |
+
|
| 310 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 311 |
+
with gr.Blocks(css=css) as demo:
|
| 312 |
+
with gr.Column(elem_id="col-container"):
|
| 313 |
+
gr.Markdown("""# FLUX ๋ค์ด์ด๊ทธ๋จ ์์ฑ๊ธฐ
|
| 314 |
+
FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆ๋ค์ด ์๊ทธ๋ฆผ ์คํ์ผ์ ๋ค์ด์ด๊ทธ๋จ์ ์์ฑํฉ๋๋ค
|
| 315 |
+
""")
|
| 316 |
+
|
| 317 |
+
with gr.Row():
|
| 318 |
+
prompt = gr.Text(
|
| 319 |
+
label="ํ๋กฌํํธ",
|
| 320 |
+
show_label=False,
|
| 321 |
+
max_lines=1,
|
| 322 |
+
placeholder="๋ค์ด์ด๊ทธ๋จ ๊ตฌ์กฐ๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 323 |
+
container=False,
|
| 324 |
+
)
|
| 325 |
+
run_button = gr.Button("์์ฑ", scale=0)
|
| 326 |
+
|
| 327 |
+
result = gr.Image(label="์์ฑ๋ ๋ค์ด์ด๊ทธ๋จ", show_label=False)
|
| 328 |
+
|
| 329 |
+
with gr.Accordion("๊ณ ๊ธ ์ค์ ", open=False):
|
| 330 |
+
seed = gr.Slider(
|
| 331 |
+
label="์๋",
|
| 332 |
+
minimum=0,
|
| 333 |
+
maximum=MAX_SEED,
|
| 334 |
+
step=1,
|
| 335 |
+
value=0,
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
randomize_seed = gr.Checkbox(label="๋๋ค ์๋", value=True)
|
| 339 |
+
|
| 340 |
+
with gr.Row():
|
| 341 |
+
width = gr.Slider(
|
| 342 |
+
label="๋๋น",
|
| 343 |
+
minimum=256,
|
| 344 |
+
maximum=MAX_IMAGE_SIZE,
|
| 345 |
+
step=32,
|
| 346 |
+
value=1024,
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
height = gr.Slider(
|
| 350 |
+
label="๋์ด",
|
| 351 |
+
minimum=256,
|
| 352 |
+
maximum=MAX_IMAGE_SIZE,
|
| 353 |
+
step=32,
|
| 354 |
+
value=1024,
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
num_inference_steps = gr.Slider(
|
| 358 |
+
label="์ถ๋ก ๋จ๊ณ ์",
|
| 359 |
+
minimum=1,
|
| 360 |
+
maximum=50,
|
| 361 |
+
step=1,
|
| 362 |
+
value=4,
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
# ์์ ์ถ๊ฐ
|
| 366 |
+
gr.Examples(
|
| 367 |
+
examples=EXAMPLES, # ์ด์ ์ ์ ์๋ ์์ ๋ค ์ฌ์ฉ
|
| 368 |
+
fn=generate_diagram,
|
| 369 |
+
inputs=[prompt],
|
| 370 |
+
outputs=[result, seed],
|
| 371 |
+
cache_examples=True
|
| 372 |
)
|
| 373 |
+
|
| 374 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 375 |
+
gr.on(
|
| 376 |
+
triggers=[run_button.click, prompt.submit],
|
| 377 |
+
fn=generate_diagram,
|
| 378 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 379 |
+
outputs=[result, seed]
|
| 380 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
# ์ฑ ์คํ
|
| 383 |
if __name__ == "__main__":
|