|
|
import gradio as gr |
|
|
import numpy as np |
|
|
from methods import infer |
|
|
|
|
|
model_id_default = "sd-legacy/stable-diffusion-v1-5" |
|
|
model_dropdown = ['stabilityai/sdxl-turbo', 'CompVis/stable-diffusion-v1-4', 'sd-legacy/stable-diffusion-v1-5'] |
|
|
|
|
|
MAX_SEED = np.iinfo(np.int32).max |
|
|
MAX_IMAGE_SIZE = 1024 |
|
|
|
|
|
|
|
|
examples = [ |
|
|
"Cartoon sticker of sad Elon Musk", |
|
|
"A cartoon-style sticker of Elon Musk shaking hands with Donald Trump. Both figures have exaggerated facial expressions, with Musk grinning confidently and Trump giving a signature thumbs-up. The background features a patriotic red, white, and blue color scheme with fireworks exploding behind them.", |
|
|
"A cyberpunk-themed cartoon sticker of Elon Musk standing atop a futuristic Tesla spaceship. He wears a sleek, neon-lit jacket with glowing circuits, while the city skyline behind him is filled with holographic billboards displaying SpaceX and Neuralink logos. His sunglasses reflect the distant stars, adding to the sci-fi aesthetic.", |
|
|
"A medieval fantasy sticker of Elon Musk depicted as a wizard. He holds a glowing blue orb in one hand and a spellbook in the other, wearing a long, starry robe with intricate golden details. His expression is both wise and mischievous, as if he's about to reveal the secrets of the universe. The background features a mystical castle and a dragon flying in the sky.", |
|
|
"A sticker of Elon Musk dressed as a cowboy in the Wild West. He wears a wide-brimmed hat, leather boots, and a long trench coat, standing in front of a saloon with a SpaceX rocket docked nearby instead of a horse. A wanted poster on the wall reads 'Wanted: Mars Pioneer', adding to the playful western theme.", |
|
|
"A parody cartoon sticker of Elon Musk arm-wrestling a robotic version of himself. The robot Musk has glowing red eyes and mechanical arms, while the real Musk smirks confidently. Sparks fly from the table as the intense match unfolds, and the background features a neon sign that reads 'Tesla vs. AI: Ultimate Showdown'." |
|
|
] |
|
|
|
|
|
|
|
|
def on_checkbox_change(use_advanced): |
|
|
visible = use_advanced |
|
|
return (gr.update(visible=visible, interactive=visible), |
|
|
gr.update(visible=visible, interactive=visible), |
|
|
gr.update(visible=visible, interactive=visible)) |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
with gr.Row(): |
|
|
|
|
|
with gr.Column(): |
|
|
|
|
|
gr.Markdown("## ControlNet") |
|
|
use_advanced_controlnet = gr.Checkbox(label="ControlNet Settings") |
|
|
control_strength = gr.Slider( |
|
|
label="control_strength", |
|
|
minimum=0, |
|
|
maximum=1, |
|
|
step=0.01, |
|
|
value=0.8, |
|
|
visible=False) |
|
|
mode = gr.Dropdown(["edge_detection", "pose_estimation"], label="Выбор режима", visible=False) |
|
|
image_upload_cn = gr.Image(label="Загрузите изображение", visible=False) |
|
|
use_advanced_controlnet.change(on_checkbox_change, use_advanced_controlnet, [control_strength, mode, image_upload_cn]) |
|
|
|
|
|
gr.Markdown("## IPAdapter") |
|
|
use_advanced_ip = gr.Checkbox(label="IPAdapter Settings") |
|
|
ip_adapter_scale = gr.Slider( |
|
|
label="ip_adapter_scale", |
|
|
minimum=0, |
|
|
maximum=1, |
|
|
step=0.01, |
|
|
value=0.8, |
|
|
visible=False) |
|
|
image_upload_ip = gr.Image(label="Загрузите изображение", visible=False) |
|
|
use_advanced_ip.change(on_checkbox_change, use_advanced_ip, [ip_adapter_scale, image_upload_ip]) |
|
|
|
|
|
|
|
|
with gr.Column(): |
|
|
gr.Markdown("## Generate") |
|
|
|
|
|
with gr.Row(): |
|
|
prompt = gr.Text( |
|
|
label="Prompt", |
|
|
show_label=False, |
|
|
max_lines=1, |
|
|
placeholder="Enter your prompt", |
|
|
container=False, |
|
|
) |
|
|
run_button = gr.Button("Run", scale=0, variant="primary") |
|
|
|
|
|
result = gr.Image(label="Result", show_label=False) |
|
|
|
|
|
with gr.Accordion("Advanced Settings", open=False): |
|
|
|
|
|
model_repo_id = gr.Dropdown( |
|
|
label="Model Id", |
|
|
choices=model_dropdown, |
|
|
info="Choose model", |
|
|
visible=True, |
|
|
allow_custom_value=True, |
|
|
value=model_id_default, |
|
|
) |
|
|
|
|
|
negative_prompt = gr.Text( |
|
|
label="Negative prompt", |
|
|
max_lines=1, |
|
|
placeholder="Enter a negative prompt", |
|
|
visible=True, |
|
|
value="monochrome, lowres, bad anatomy, worst quality, low quality, medical mask" |
|
|
) |
|
|
|
|
|
seed = gr.Slider( |
|
|
label="Seed", |
|
|
minimum=0, |
|
|
maximum=MAX_SEED, |
|
|
step=1, |
|
|
value=22, |
|
|
) |
|
|
|
|
|
guidance_scale = gr.Slider( |
|
|
label="guidance_scale", |
|
|
minimum=0, |
|
|
maximum=100, |
|
|
step=1, |
|
|
value=7, |
|
|
) |
|
|
|
|
|
lora_scale = gr.Slider( |
|
|
label="lora_scale", |
|
|
minimum=0, |
|
|
maximum=1, |
|
|
step=0.01, |
|
|
value=0.95, |
|
|
visible=False) |
|
|
|
|
|
num_inference_steps = gr.Slider( |
|
|
label="num_inference_steps", |
|
|
minimum=0, |
|
|
maximum=100, |
|
|
step=1, |
|
|
value=50, |
|
|
) |
|
|
|
|
|
randomize_seed = gr.Checkbox(label="Randomize seed", value=True) |
|
|
|
|
|
with gr.Row(): |
|
|
width = gr.Slider( |
|
|
label="Width", |
|
|
minimum=256, |
|
|
maximum=MAX_IMAGE_SIZE, |
|
|
step=32, |
|
|
value=512, |
|
|
) |
|
|
|
|
|
height = gr.Slider( |
|
|
label="Height", |
|
|
minimum=256, |
|
|
maximum=MAX_IMAGE_SIZE, |
|
|
step=32, |
|
|
value=512, |
|
|
) |
|
|
|
|
|
with gr.Accordion("Prompt examples", open=False): |
|
|
gr.Examples(examples=examples, inputs=[prompt]) |
|
|
|
|
|
gr.on( |
|
|
triggers=[run_button.click, prompt.submit], |
|
|
fn=infer, |
|
|
inputs=[ |
|
|
prompt, |
|
|
negative_prompt, |
|
|
randomize_seed, |
|
|
width, |
|
|
height, |
|
|
model_repo_id, |
|
|
seed, |
|
|
guidance_scale, |
|
|
num_inference_steps, |
|
|
|
|
|
use_advanced_controlnet, |
|
|
control_strength, |
|
|
image_upload_cn, |
|
|
|
|
|
use_advanced_ip, |
|
|
ip_adapter_scale, |
|
|
image_upload_ip |
|
|
], |
|
|
outputs=[result, seed], |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(share=False, debug=True) |