Text_to_Image / app.py
jpjp9292's picture
Update app.py
226281a verified
import gradio as gr
from diffusers import StableDiffusionPipeline
import torch
import transformers
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
transformers.logging.set_verbosity_error()
# λͺ¨λΈ 경둜 λͺ©λ‘
model_paths = {
"CompVis/stable-diffusion-v1-4": "CompVis/stable-diffusion-v1-4",
"runwayml/stable-diffusion-v1-5": "runwayml/stable-diffusion-v1-5",
"stabilityai/stable-diffusion-2-1": "stabilityai/stable-diffusion-2-1"
}
# λͺ¨λΈμ„ λ‘œλ“œν•©λ‹ˆλ‹€.
pipes = {}
for name, path in model_paths.items():
pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16, low_cpu_mem_usage=True)
pipes[name] = pipe
def generate_image(prompt, negative_prompt, model_name, steps, sampler, cfg_scale, width, height, seed):
pipe = pipes[model_name]
if seed == -1:
seed = None
generator = torch.manual_seed(seed) if seed is not None else None
output = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=steps,
guidance_scale=cfg_scale,
width=width,
height=height,
generator=generator,
)
image = output.images[0]
return image
css = """
#generate {
height: 100%;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Row():
model_name = gr.Dropdown(
label="Model (λͺ¨λΈ 선택)",
choices=list(model_paths.keys()),
value="runwayml/stable-diffusion-v1-5",
interactive=True
)
with gr.Tab("Text-to-Image (ν…μŠ€νŠΈμ—μ„œ 이미지 생성)"):
with gr.Row():
with gr.Column(scale=6, min_width=600):
prompt = gr.Textbox(
label="Prompt (ν”„λ‘¬ν”„νŠΈ)",
value="a high-resolution photograph of a samoyed cub wearing a shiny silver astronaut suit",
lines=3
)
negative_prompt = gr.Textbox(
label="Negative Prompt (λ„€κ±°ν‹°λΈŒ ν”„λ‘¬ν”„νŠΈ)",
value="low resolution, blurry, cartoon, deformed, ugly, disfigured, malformed, bad anatomy, unrealistic",
lines=3
)
with gr.Column():
generate_button = gr.Button("Generate (생성)", variant='primary')
with gr.Row():
with gr.Column(scale=3):
with gr.Tab("Generation Settings (생성 μ„€μ •)"):
with gr.Row():
sampler = gr.Dropdown(
label="Sampler (μƒ˜ν”ŒλŸ¬)",
choices=["ddim", "ddpm", "pndm"],
value="ddpm"
)
with gr.Row():
steps = gr.Slider(
label="Steps (μƒ˜ν”Œλ§ 단계 수)",
minimum=1,
maximum=50,
value=20,
step=1
)
with gr.Row():
width = gr.Slider(
label="Width (λ„ˆλΉ„, ν”½μ…€)",
minimum=512,
maximum=1024,
value=512,
step=64
)
height = gr.Slider(
label="Height (높이, ν”½μ…€)",
minimum=512,
maximum=1024,
value=512,
step=64
)
cfg_scale = gr.Slider(
label="CFG Scale (CFG μŠ€μΌ€μΌ, μ œμ–΄ 강도)",
minimum=1,
maximum=20,
value=7,
step=1
)
seed = gr.Number(
label="Seed (μ‹œλ“œ, 고유 번호)",
value=-1
)
with gr.Column(scale=2):
output_image = gr.Image(
label="Output Image (μƒμ„±λœ 이미지)"
)
generate_button.click(
fn=generate_image,
inputs=[
prompt,
negative_prompt,
model_name,
steps,
sampler,
cfg_scale,
width,
height,
seed
],
outputs=output_image
)
demo.launch()