Spaces:
Sleeping
Sleeping
import torch | |
import gradio as gr | |
from diffusers import StableDiffusionPipeline | |
from diffusers import ControlNetModel, DDIMScheduler,EulerDiscreteScheduler,EulerAncestralDiscreteScheduler,UniPCMultistepScheduler | |
from diffusers import KDPM2DiscreteScheduler,KDPM2AncestralDiscreteScheduler,PNDMScheduler,StableDiffusionPipeline | |
from diffusers import DPMSolverMultistepScheduler | |
import random | |
# pipe = StableDiffusionPipeline.from_pretrained( | |
# "SG161222/Realistic_Vision_V5.1_noVAE", | |
# torch_dtype=torch.float16, | |
# use_safetensors=True, | |
# ).to("cpu") | |
def set_pipeline(model_id_repo,scheduler): | |
# pipe = StableDiffusionPipeline.from_single_file( | |
# "/home/ubuntu/stable-diffusion-webui/models/Stable-diffusion/realisticVisionV51_v51VAE.safetensors", | |
# # torch_dtype=torch.float16, | |
# use_safetensors=True, | |
# ).to("cpu") | |
model_ids_dict = { | |
"dreamshaper": "Lykon/DreamShaper", | |
"deliberate": "soren127/Deliberate", | |
"runwayml": "runwayml/stable-diffusion-v1-5", | |
"Realistic_Vision_V5_1_noVAE":"SG161222/Realistic_Vision_V5.1_noVAE" | |
} | |
model_id = model_id_repo | |
model_repo = model_ids_dict.get(model_id) | |
print("model_repo :",model_repo) | |
# pipe = StableDiffusionPipeline.from_pretrained( | |
# model_repo, | |
# # torch_dtype=torch.float16, # to run on cpu | |
# use_safetensors=True, | |
# ).to("cpu") | |
pipe = StableDiffusionPipeline.from_pretrained( | |
model_repo, | |
torch_dtype=torch.float16, # to run on cpu | |
use_safetensors=True, | |
).to("cuda") | |
scheduler_classes = { | |
"DDIM": DDIMScheduler, | |
"Euler": EulerDiscreteScheduler, | |
"Euler a": EulerAncestralDiscreteScheduler, | |
"UniPC": UniPCMultistepScheduler, | |
"DPM2 Karras": KDPM2DiscreteScheduler, | |
"DPM2 a Karras": KDPM2AncestralDiscreteScheduler, | |
"PNDM": PNDMScheduler, | |
"DPM++ 2M Karras": DPMSolverMultistepScheduler, | |
"DPM++ 2M SDE Karras": DPMSolverMultistepScheduler, | |
} | |
sampler_name = scheduler # Example sampler name, replace with the actual value | |
scheduler_class = scheduler_classes.get(sampler_name) | |
if scheduler_class is not None: | |
print("sampler_name:",sampler_name) | |
pipe.scheduler = scheduler_class.from_config(pipe.scheduler.config) | |
else: | |
pass | |
# # prompt = "a photo of an astronaut riding a horse on mars" | |
# # pipe.enable_attention_slicing() | |
# image = pipe(prompt).images[0] | |
# image.save("1.png") | |
return pipe | |
def img_args( | |
prompt, | |
negative_prompt, | |
model_id_repo = "Realistic_Vision_V5_1_noVAE", | |
scheduler= "Euler a", | |
height=896, | |
width=896, | |
num_inference_steps = 30, | |
guidance_scale = 7.5, | |
num_images_per_prompt = 1, | |
seed = 0 | |
): | |
print(model_id_repo) | |
print(scheduler) | |
print(prompt,"&&&&&&&&&&&&&&&&") | |
pipe = set_pipeline(model_id_repo,scheduler) | |
if seed == 0: | |
seed = random.randint(0,25647981548564) | |
print(f"random seed :{seed}") | |
generator = torch.manual_seed(seed) | |
else: | |
generator = torch.manual_seed(seed) | |
print(f"manual seed :{seed}") | |
image = pipe(prompt=prompt, | |
negative_prompt = negative_prompt, | |
height = height, | |
width = width, | |
num_inference_steps = num_inference_steps, | |
guidance_scale = guidance_scale, | |
num_images_per_prompt = num_images_per_prompt, # default 1 | |
generator = generator, | |
).images | |
print(image,"#############") | |
# image.save("1.png") | |
return image | |
block = gr.Blocks().queue() | |
block.title = "Inpaint Anything" | |
with block as image_gen: | |
with gr.Column(): | |
with gr.Row(): | |
gr.Markdown("## Image Generation") | |
with gr.Row(): | |
with gr.Column(): | |
# with gr.Row(): | |
prompt = gr.Textbox(placeholder="what you want to generate",label="Positive Prompt") | |
negative_prompt = gr.Textbox(placeholder="what you don't want to generate",label="Negative prompt") | |
run_btn = gr.Button("image generation", elem_id="select_btn", variant="primary") | |
with gr.Accordion(label="Advance Options",open=False): | |
model_selection = gr.Dropdown(choices=["dreamshaper","deliberate","runwayml","Realistic_Vision_V5_1_noVAE"],value="Realistic_Vision_V5_1_noVAE",label="Models") | |
schduler_selection = gr.Dropdown(choices=["DDIM","Euler","Euler a","UniPC","DPM2 Karras","DPM2 a Karras","PNDM","DPM++ 2M Karras","DPM++ 2M SDE Karras"],value="Euler a",label="Scheduler") | |
guidance_scale_slider = gr.Slider(label="guidance_scale", minimum=0, maximum=15, value=7.5, step=0.5) | |
num_images_per_prompt_slider = gr.Slider(label="num_images_per_prompt", minimum=0, maximum=5, value=1, step=1) | |
height_slider = gr.Slider(label="height", minimum=0, maximum=2048, value=896, step=1) | |
width_slider = gr.Slider(label="width", minimum=0, maximum=2048, value=896, step=1) | |
num_inference_steps_slider = gr.Slider(label="num_inference_steps", minimum=0, maximum=150, value=30, step=1) | |
seed_slider = gr.Slider(label="Seed Slider", minimum=0, maximum=256479815, value=0, step=1) | |
with gr.Column(): | |
# out_img = gr.Image(type="pil",label="Output",height=480) | |
out_img = gr.Gallery(label='Output', show_label=False, elem_id="gallery", preview=True) | |
run_btn.click(fn=img_args,inputs=[prompt,negative_prompt,model_selection,schduler_selection,height_slider,width_slider,num_inference_steps_slider,guidance_scale_slider,num_images_per_prompt_slider,seed_slider],outputs=[out_img]) | |
image_gen.launch() | |
# block = gr.Blocks().queue() | |
# block.title = "Inpaint Anything" | |
# with block as inpaint_anything_interface: | |
# with gr.Column(): | |
# with gr.Row(): | |
# gr.Markdown("## Inpainting with Segment Anything (Multi Controlnet)") | |
# with gr.Row(): | |
# with gr.Column(): | |
# # with gr.Row(): | |
# model_selection = gr.Dropdown(choices=["dreamshaper","deliberate","realisticVisionV51_v51VAE","revAnimated_v121Inp","runwayml","Realistic_Vision_V5_1_noVAE"],value = "Realistic_Vision_V5_1_noVAE",label="Models") | |
# # scheduler = gr.Dropdown(choices=["DDIM","Euler","Euler a","UniPC","DPM2 Karras","DPM2 a Karras","PNDM","DPM++ 2M Karras","DPM++ 2M SDE Karras"],value = "Euler a",label="Sampler") | |
# input_image = gr.Image(type="numpy",label="input",height=400) | |
# run_btn = gr.Button("Run Segment", elem_id="select_btn", variant="primary") | |
# prompt = gr.Textbox(placeholder="what you want to generate") | |
# guidance_scale_slider = gr.Slider(label="Guidance Scale", minimum=0, maximum=20.0, value=7.5, step=0.5) | |
# inference_slider = gr.Slider(label="Guidance Scale", minimum=0, maximum=150, value=50, step=1) | |
# with gr.Row(): | |
# canny_slider = gr.Slider(label="Canny Slider", minimum=0, maximum=1.0, value=0.5, step=0.1) | |
# depth_slider = gr.Slider(label="Depth Slider", minimum=0, maximum=1.0, value=0.5, step=0.1) | |
# seg_slider = gr.Slider(label="Segment Slider", minimum=0, maximum=1.0, value=0.5, step=0.1) | |
# out_img = gr.Image(type="pil",label="output") | |
# seed_slider = gr.Slider(label="Seed Slider",elem_id="expand_mask_iteration_count", minimum=0, maximum=25647981548564, value=0, step=1) | |
# grn_btn = gr.Button("image generation", elem_id="select_btn", variant="primary") | |
# # bru_btn = gr.Button("Brush generation", elem_id="select_btn", variant="primary") | |
# with gr.Column(): | |
# scheduler = gr.Dropdown(choices=["DDIM","Euler","Euler a","UniPC","DPM2 Karras","DPM2 a Karras","PNDM","DPM++ 2M Karras","DPM++ 2M SDE Karras"],value = "Euler a",label="Sampler") | |
# # lora_chk = gr.Checkbox(label="Use Lora", elem_id="invert_chk", show_label=True, value=False, interactive=True) | |
# # image_out = gr.Image(type="pil",label="Output") | |
# sam_image = gr.Image(label="Segment Anything image", elem_id="ia_sam_image", type="numpy", tool="sketch", brush_radius=8, | |
# show_label=False, interactive=True,height=400) | |
# mask_btn = gr.Button("Create Mask", elem_id="select_btn", variant="primary") | |
# with gr.Column(): | |
# with gr.Row(): | |
# invert_chk = gr.Checkbox(label="Invert mask", elem_id="invert_chk", show_label=True, value=True, interactive=True) | |
# ignore_black_chk = gr.Checkbox(label="Ignore black area", elem_id="ignore_black_chk", value=True, show_label=True, interactive=True) | |
# lora_chk = gr.Checkbox(label="Use Lora", elem_id="invert_chk", show_label=True, value=False, interactive=True) | |
# with gr.Column(): | |
# sel_mask = gr.Image(label="Selected mask image", elem_id="ia_sel_mask", type="numpy", tool="sketch", brush_radius=12, | |
# show_label=False, interactive=True, height=480) | |
# with gr.Column(): | |
# with gr.Row(): | |
# expand_mask_btn = gr.Button("Expand mask region", elem_id="expand_mask_btn") | |
# # with gr.Column(): | |
# expand_mask_iteration_count = gr.Slider(label="Expand Mask Iterations", | |
# elem_id="expand_mask_iteration_count", minimum=1, maximum=100, value=1, step=1) | |
# with gr.Row(): | |
# add_mask_btn = gr.Button("Add mask by sketch", elem_id="add_mask_btn") | |
# apply_mask_btn = gr.Button("Trim mask by sketch", elem_id="apply_mask_btn") | |
# run_btn.click(fn=run_seg,inputs=[input_image],outputs=[sam_image]) | |
# mask_btn.click(fn=select_mask,inputs=[input_image, sam_image, invert_chk, ignore_black_chk,sel_mask], outputs=[sel_mask]) | |
# expand_mask_btn.click(expand_mask, inputs=[input_image, sel_mask, expand_mask_iteration_count], outputs=[sel_mask]) | |
# apply_mask_btn.click(apply_mask, inputs=[input_image, sel_mask], outputs=[sel_mask]) | |
# add_mask_btn.click(add_mask, inputs=[input_image, sel_mask], outputs=[sel_mask]) | |
# grn_btn.click(fn=generate_image,inputs=[input_image,sam_image,prompt,seed_slider,canny_slider,depth_slider,seg_slider,model_selection,scheduler,guidance_scale_slider,inference_slider,lora_chk],outputs=[out_img]) | |
# bru_btn.click(fn=brush_geeration,inputs=[input_image,prompt],outputs=[out_img]) | |
# inpaint_anything_interface.launch() |