FLUX.2-dev / app.py
tchung1970's picture
Make prompt field full width
80ddbec
import os
import subprocess
import sys
import io
import gradio as gr
import numpy as np
import random
import spaces
import torch
from diffusers import Flux2Pipeline, Flux2Transformer2DModel
from diffusers import BitsAndBytesConfig as DiffBitsAndBytesConfig
from optimization import optimize_pipeline_
import requests
from PIL import Image
import json
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024
def remote_text_encoder(prompts):
from gradio_client import Client
client = Client("multimodalart/mistral-text-encoder")
result = client.predict(
prompt=prompts,
api_name="/encode_text"
)
prompt_embeds = torch.load(result[0])
return prompt_embeds
# Load model
repo_id = "black-forest-labs/FLUX.2-dev"
dit = Flux2Transformer2DModel.from_pretrained(
repo_id,
subfolder="transformer",
torch_dtype=torch.bfloat16
)
pipe = Flux2Pipeline.from_pretrained(
repo_id,
text_encoder=None,
transformer=dit,
torch_dtype=torch.bfloat16
)
pipe.to("cuda")
pipe.transformer.set_attention_backend("_flash_3_hub")
optimize_pipeline_(
pipe,
image=[Image.new("RGB", (1024, 1024))],
prompt_embeds = remote_text_encoder("prompt").to("cuda"),
guidance_scale=2.5,
width=1024,
height=1024,
num_inference_steps=1
)
def get_duration(prompt, input_images=None, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=50, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)):
num_images = 0 if input_images is None else len(input_images)
step_duration = 1 + 0.7 * num_images
return max(65, num_inference_steps * step_duration + 10)
@spaces.GPU(duration=get_duration)
def infer(prompt, input_images=None, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=50, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
# Get prompt embeddings from remote text encoder
progress(0.1, desc="ํ”„๋กฌํ”„ํŠธ ์ธ์ฝ”๋”ฉ ์ค‘...")
prompt_embeds = remote_text_encoder(prompt).to("cuda")
# Prepare image list (convert None or empty gallery to None)
image_list = None
if input_images is not None and len(input_images) > 0:
image_list = []
for item in input_images:
image_list.append(item[0])
# Generate image
progress(0.3, desc="์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘...")
generator = torch.Generator(device=device).manual_seed(seed)
image = pipe(
prompt_embeds=prompt_embeds,
image=image_list,
width=width,
height=height,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
generator=generator,
).images[0]
return image, seed
examples = [
["๊ฑฐ์‹ค ํƒ์ž ์œ„์— ๊ฝƒ๋ณ‘์„ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”. ๊ฝƒ๋ณ‘์˜ ์ƒ‰์ƒ์€ #02eb3c ์ƒ‰์ƒ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ #edfa3c๋กœ ๋๋‚˜๋Š” ๊ทธ๋ผ๋ฐ์ด์…˜์ž…๋‹ˆ๋‹ค. ๊ฝƒ๋ณ‘ ์•ˆ์˜ ๊ฝƒ๋“ค์€ #ff0088 ์ƒ‰์ƒ์ž…๋‹ˆ๋‹ค"],
["๋ฒ ๋ฅผ๋ฆฐ TV ํƒ€์›Œ(Fernsehturm)์˜ ์ „์ฒด ๊ตฌ์กฐ๋ฅผ ์ง€๋ฉด ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์•ˆํ…Œ๋‚˜ ๋๊นŒ์ง€ ๋ณด์—ฌ์ฃผ๋Š” ์‚ฌ์ง„์ฒ˜๋Ÿผ ์‚ฌ์‹ค์ ์ธ ์ธํฌ๊ทธ๋ž˜ํ”ฝ, ์ฝ˜ํฌ๋ฆฌํŠธ ์ถ•, ๊ธˆ์† ๊ตฌ์ฒด, ์•ˆํ…Œ๋‚˜ ์ฒจํƒ‘์„ ํฌํ•จํ•œ ์ „์ฒด ๊ตฌ์กฐ๊ฐ€ ๋ณด์ด๋Š” ์ˆ˜์ง ์ „์ฒด ๋ทฐ. ์ƒ์ง•์ ์ธ ๊ตฌ์ฒด๋ฅผ ์˜ฌ๋ ค๋‹ค๋ณด๋Š” ์•ฝ๊ฐ„์˜ ์œ„์ชฝ ์›๊ทผ๊ฐ ๊ฐ๋„, ๊นจ๋—ํ•œ ํฐ์ƒ‰ ๋ฐฐ๊ฒฝ์— ์™„๋ฒฝํ•˜๊ฒŒ ์ค‘์•™ ๋ฐฐ์น˜. ์–‡์€ ์ˆ˜ํ‰ ์—ฐ๊ฒฐ์„ ์ด ์žˆ๋Š” ์™ผ์ชฝ ๋ ˆ์ด๋ธ”: ๋งค์šฐ ํฐ ๊ตต์€ ์ง„ํ•œ ํšŒ์ƒ‰ ์ˆซ์ž(#2D3748)๋กœ ๋œ '368m' ํ…์ŠคํŠธ๊ฐ€ ์•ˆํ…Œ๋‚˜ ๋์— ์ •ํ™•ํžˆ ์œ„์น˜ํ•˜๊ณ  ๊ทธ ์•„๋ž˜์— ์ž‘์€ ๋Œ€๋ฌธ์ž๋กœ 'TOTAL HEIGHT'๊ฐ€ ์žˆ์Œ. ๋งค์šฐ ํฐ ๊ตต์€ ๊ธ€์”จ๋กœ ๋œ '207m' ํ…์ŠคํŠธ์™€ ๊ทธ ์•„๋ž˜ ์ž‘์€ ๋Œ€๋ฌธ์ž๋กœ 'TELECAFร‰'๊ฐ€ ์žˆ์œผ๋ฉฐ, ์—ฐ๊ฒฐ์„ ์ด ์ฐฝ๋ฌธ ๋†’์ด์˜ ๊ตฌ์ฒด์— ์ •ํ™•ํžˆ ๋‹ฟ์•„ ์žˆ์Œ. ๊ตฌ์ฒด์˜ ์ ๋„์— ๋‹ฟ๋Š” ์ˆ˜ํ‰ ์—ฐ๊ฒฐ์„ ์ด ์žˆ๋Š” ์˜ค๋ฅธ์ชฝ ๋ ˆ์ด๋ธ”: ๋งค์šฐ ํฐ ๊ตต์€ ์ง„ํ•œ ํšŒ์ƒ‰ ์ˆซ์ž๋กœ ๋œ '32m' ํ…์ŠคํŠธ์™€ ๊ทธ ์•„๋ž˜ ์ž‘์€ ๋Œ€๋ฌธ์ž๋กœ 'SPHERE DIAMETER'๊ฐ€ ์žˆ์Œ. ์„ธ ๊ฐœ์˜ ๊ท ํ˜• ์žกํžŒ ์—ด๋กœ ๋ฐฐ์—ด๋œ ํ•˜๋‹จ ์„น์…˜: ์™ผ์ชฝ - ๋งค์šฐ ๊ตต์€ ์ง„ํ•œ ํšŒ์ƒ‰์˜ ํฐ ํ…์ŠคํŠธ '986'๊ณผ ๊ทธ ์•„๋ž˜ ๋Œ€๋ฌธ์ž๋กœ 'STEPS'. ์ค‘์•™ - ๊ตต์€ ๋Œ€๋ฌธ์ž๋กœ 'BERLIN TV TOWER'์™€ ๊ทธ ์•„๋ž˜ ๊ฐ€๋ฒผ์šด ๋ฌด๊ฒŒ๋กœ 'FERNSEHTURM'. ์˜ค๋ฅธ์ชฝ - ๊ตต์€ ๋Œ€๋ฌธ์ž๋กœ 'INAUGURATED'์™€ ๊ทธ ์•„๋ž˜ 'OCTOBER 3, 1969'. ๋ชจ๋“  ํƒ€์ดํฌ๊ทธ๋ž˜ํ”ผ๋Š” ํ˜„๋Œ€์ ์ธ ์‚ฐ์„ธ๋ฆฌํ”„ ํฐํŠธ(Inter ๋˜๋Š” Helvetica ๊ฐ™์€), ์ƒ‰์ƒ #2D3748, ๊นจ๋—ํ•˜๊ณ  ๋ฏธ๋‹ˆ๋ฉ€ํ•œ ๊ธฐ์ˆ  ๋‹ค์ด์–ด๊ทธ๋žจ ์Šคํƒ€์ผ. ์ˆ˜ํ‰ ์—ฐ๊ฒฐ์„ ์€ ์–‡๊ณ  ์ •ํ™•ํ•˜๋ฉฐ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณด์ด๊ณ  ํƒ€์›Œ ๊ตฌ์กฐ์˜ ์ •ํ™•ํ•œ ํ•ด๋‹น ์ธก์ • ์ง€์ ์— ๋‹ฟ์•„ ์žˆ์Œ. ๋†’์ด์™€ ์›…์žฅํ•จ์„ ๋А๋‚„ ์ˆ˜ ์žˆ๋Š” ์—ญ๋™์ ์ธ ๋‚ฎ์€ ๊ฐ๋„ ์›๊ทผ๊ฐ์ด ์žˆ๋Š” ์ „๋ฌธ์ ์ธ ๊ฑด์ถ• ์ž…๋ฉด๋„ ๋ฏธํ•™, ์™„๋ฒฝํ•œ ์‹œ๊ฐ์  ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ํฌ์Šคํ„ฐ๊ธ‰ ์ธํฌ๊ทธ๋ž˜ํ”ฝ ๋””์ž์ธ."],
["๋น„์˜ค๋Š” ์ •๊ธ€์—์„œ ๋ฐ”๋‚˜๋‚˜ ์žŽ ์•„๋ž˜ ํ”ผ์‹ ํ•˜๊ณ  ์žˆ๋Š” ํ ๋ป‘ ์ –์€ ์นดํ”ผ๋ฐ”๋ผ, ํด๋กœ์ฆˆ์—… ์‚ฌ์ง„"],
["ํ†ตํ†ตํ•œ ์ฃผํ™ฉ์ƒ‰ ๊ณ ์–‘์ด์˜ ์นด์™€์ด ๋‹ค์ด์ปท ์Šคํ‹ฐ์ปค, ํฌ๊ณ  ๋ฐ˜์ง์ด๋Š” ๋ˆˆ๊ณผ ์ธ์‚ฌํ•˜๋ฉฐ ๋ฐœ์„ ์˜ฌ๋ฆฐ ํ–‰๋ณตํ•œ ๋ฏธ์†Œ์™€ ํ•˜ํŠธ ๋ชจ์–‘์˜ ๋ถ„ํ™ ์ฝ”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋””์ž์ธ์€ ๊ฒ€์€์ƒ‰ ์œค๊ณฝ์„ ๊ณผ ๋ถ„ํ™ ๋ณผ์ด ์žˆ๋Š” ๋ถ€๋“œ๋Ÿฌ์šด ๊ทธ๋ผ๋ฐ์ด์…˜ ์Œ์˜์ด ์žˆ๋Š” ๋ถ€๋“œ๋Ÿฌ์šด ๋‘ฅ๊ทผ ์„ ์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."],
]
examples_images = [
# ["Replace the top of the person from image 1 with the one from image 2", ["person1.webp", "woman2.webp"]],
["์ด๋ฏธ์ง€ 1์˜ ์‚ฌ๋žŒ์ด ์ด๋ฏธ์ง€ 2์˜ ๊ณ ์–‘์ด๋ฅผ ์“ฐ๋‹ค๋“ฌ๊ณ  ์žˆ๊ณ , ์ด๋ฏธ์ง€ 3์˜ ์ƒˆ๊ฐ€ ๊ทธ๋“ค ์˜†์— ์žˆ์Šต๋‹ˆ๋‹ค", ["woman1.webp", "cat_window.webp", "bird.webp"]]
]
css="""
#col-container {
margin: 0 auto;
max-width: 620px;
}
"""
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(f"""# FLUX.2 [dev]
FLUX.2 [dev]๋Š” ํ…์ŠคํŠธ ์ง€์‹œ์‚ฌํ•ญ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑ, ํŽธ์ง‘ ๋ฐ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” 32B ํŒŒ๋ผ๋ฏธํ„ฐ rectified flow ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค [[๋ชจ๋ธ](https://huggingface.co/black-forest-labs/FLUX.2-dev)], [[๋ธ”๋กœ๊ทธ](https://bfl.ai/blog/flux-2)]
""")
with gr.Accordion("์ž…๋ ฅ ์ด๋ฏธ์ง€ (์„ ํƒ์‚ฌํ•ญ)", open=False):
input_images = gr.Gallery(
label="์ž…๋ ฅ ์ด๋ฏธ์ง€",
type="pil",
columns=3,
rows=1,
)
prompt = gr.Text(
label="ํ”„๋กฌํ”„ํŠธ",
show_label=False,
lines=10,
max_lines=15,
placeholder="ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”",
container=False,
)
run_button = gr.Button("์‹คํ–‰")
result = gr.Image(label="๊ฒฐ๊ณผ", show_label=False)
with gr.Accordion("๊ณ ๊ธ‰ ์„ค์ •", open=False):
seed = gr.Slider(
label="์‹œ๋“œ",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="๋žœ๋ค ์‹œ๋“œ", value=True)
with gr.Row():
width = gr.Slider(
label="๋„ˆ๋น„",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
)
height = gr.Slider(
label="๋†’์ด",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=32,
value=1024,
)
with gr.Row():
num_inference_steps = gr.Slider(
label="์ถ”๋ก  ๋‹จ๊ณ„ ์ˆ˜",
minimum=1,
maximum=100,
step=1,
value=30,
)
guidance_scale = gr.Slider(
label="๊ฐ€์ด๋˜์Šค ์Šค์ผ€์ผ",
minimum=0.0,
maximum=10.0,
step=0.1,
value=4,
)
gr.Examples(
examples=examples,
fn=infer,
inputs=[prompt],
outputs=[result, seed],
cache_examples=True,
cache_mode="lazy"
)
gr.Examples(
examples=examples_images,
fn=infer,
inputs=[prompt, input_images],
outputs=[result, seed],
cache_examples=True,
cache_mode="lazy"
)
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[prompt, input_images, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
outputs=[result, seed]
)
demo.launch(css=css)