experiment / app.py
MRamzan's picture
Correct syntax mistake
c24138e
# import random
# import gradio as gr
# from PIL import Image
# import torch
# import uuid
# import numpy as np
# from diffusers import AutoPipelineForText2Image, AutoPipelineForInpainting, StableDiffusionXLInpaintPipeline, StableDiffusionXLPipeline
# model_id = "stabilityai/stable-diffusion-xl-base-1.0"
# # we have give lora weights here
# adapter_id = "ohwx_v4_sdxl_lora.safetensors"
# pipe = StableDiffusionXLPipeline.from_pretrained(model_id,
# torch_dtype=torch.float16,
# variant="fp16")
# # pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.2, b2=1.4)
# pipe.to("cuda")
# pipe.load_lora_weights('lora_weights',
# weight_name=adapter_id,
# adapter_name="qwe")
# pipe.fuse_lora() #lora_scale=0.7
# def set_lora_weight(lora_scale):
# pipe.unfuse_lora(True)
# pipe.load_lora_weights('lora_weights',
# weight_name=adapter_id,
# adapter_name="qwe")
# pipe.fuse_lora(lora_scale=lora_scale) #lora_scale=0.7
# print('DONE')
# def generate(text, guidance_scale, num_images_per_prompt, height, width, generator_seed):
# generator = torch.Generator("cuda").manual_seed(generator_seed)
# prompt = text
# image = pipe(prompt=prompt,
# negative_prompt='worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting',
# guidance_scale=guidance_scale,
# num_images_per_prompt=num_images_per_prompt,
# height=height,
# width=width,
# num_inference_steps=20,
# generator=generator).images
# return image
# with gr.Blocks() as demo:
# with gr.Row():
# with gr.Column():
# gallery = gr.Gallery(
# label="Generate",
# object_fit="contain", height="512")
# text = gr.Textbox(
# label="Enter Prompt...")
# btn = gr.Button("Generate", scale=0)
# guidance_scale = gr.Slider(minimum=0, maximum=15, value=7.5, label='guidance scale')
# num_images_per_prompt = gr.Slider(minimum=1, maximum=4, value=2, step=1, label = 'number of images per prompt')
# height = gr.Slider(minimum=512, maximum=2048, value=1024, label = 'Image height')
# width = gr.Slider(minimum=512, maximum=2048, value=1024,step=8,label = 'Image width')
# lora_scale = gr.Slider(minimum=0.1, maximum=1, value=1,step=0.01,label = 'Lora scale')
# generator_seed = gr.Slider(minimum=-1, maximum=100, value=1,step=1,label = 'generator_seed')
# # with gr.Column():
# # gallery = gr.Gallery(
# # label="Generate",
# # object_fit="contain", height="2048")
# btn.click(generate,
# inputs=[text,guidance_scale,num_images_per_prompt, height, width, generator_seed],
# outputs=gallery)
# lora_scale.change(set_lora_weight,
# inputs=lora_scale)
# if __name__ == "__main__":
# demo.launch(debug=True)
import random
import gradio as gr
from PIL import Image
import torch
import uuid
import numpy as np
from diffusers import AutoPipelineForText2Image, AutoPipelineForInpainting, StableDiffusionXLInpaintPipeline, StableDiffusionXLPipeline
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
# we have give lora weights here
adapter_id = "qwe_cat_long.safetensors"
pipe = StableDiffusionXLPipeline.from_pretrained(model_id,
torch_dtype=torch.float16,
variant="fp16")
pipe.to("cuda")
pipe.load_lora_weights('lora_weights',
weight_name='qwe_cat_long.safetensors',
adapter_name="qwe")
pipe.fuse_lora() #lora_scale=0.7
lora_models = {
'garfield':'garfield_01.safetensors',
'ohwx_photoshop':'ohwx_cat_64_5.safetensors',
'qwe long':'qwe_cat_long.safetensors',
'qwe old':'qwe_cat.safetensors',
'ohwx new': 'ohwx_v4_sdxl_lora.safetensors'
}
trigger_word = {
'garfield':'garfield cat',
'ohwx_photoshop':'ohwx cat',
'qwe long':'qwe cat',
'qwe old':'qwe cat',
'ohwx new':'ohwx'
}
def set_lora_weight(lora_scale):
pipe.unfuse_lora(True)
pipe.load_lora_weights('lora_weights',
weight_name='qwe_cat_long.safetensors',
adapter_name="qwe")
pipe.fuse_lora(lora_scale=lora_scale) #lora_scale=0.7
print('LoRA Scale Changed')
def set_lora_model(lora_name, lora_scale):
pipe.unfuse_lora(True)
pipe.load_lora_weights('lora_weights',
weight_name=lora_models[lora_name],
adapter_name="qwe")
pipe.fuse_lora(lora_scale=lora_scale) #lora_scale=0.7
print('Model swapped')
return trigger_word[lora_name]
def toggle_freeU(freeU_toggle):
if freeU_toggle:
print('freeU enabled')
pipe.enable_freeu(s1=0.9, s2=0.2, b1=1.2, b2=1.4)
else:
print('freeU disabled')
pipe.disable_freeu()
def generate(prompt,
guidance_scale,
num_images_per_prompt,
height,
width,
generator_seed,
negative_prompt
):
generator = torch.Generator("cuda").manual_seed(generator_seed)
# prompt = text
image = pipe(prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
num_images_per_prompt=num_images_per_prompt,
height=height,
width=width,
num_inference_steps=20,
generator=generator).images
return image
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
gallery = gr.Gallery(
label="Generate",
object_fit="contain", height="512")
positive_prompt = gr.Textbox(
label="Enter Positive Prompt...",
value='qwe cat'
)
negative_prompt = gr.Textbox(
label="Enter Negative Prompt...",
value='worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting'
)
with gr.Row():
lora_model_dropdown = gr.Dropdown(list(lora_models.keys()), label='Select LoRA model',value='qwe long')
with gr.Row():
guidance_scale = gr.Slider(minimum=0, maximum=15, value=9.5, label='guidance scale')
lora_scale = gr.Slider(minimum=0.1, maximum=1, value=1,step=0.01,label = 'Lora scale')
with gr.Row():
num_images_per_prompt = gr.Slider(minimum=1, maximum=4, value=2, step=1, label = 'number of images per prompt')
generator_seed = gr.Slider(minimum=-1, maximum=100, value=1,step=1,label = 'generator_seed')
with gr.Row():
height = gr.Slider(minimum=512, maximum=2048, value=1024, label = 'Image height')
width = gr.Slider(minimum=512, maximum=2048, value=1024,step=8,label = 'Image width')
freeu = gr.Checkbox(value=True, label='Toggle FreeU')
with gr.Column():
btn = gr.Button("Generate")
# with gr.Column():
# gallery = gr.Gallery(
# label="Generate",
# object_fit="contain", height="2048")
btn.click(generate,
inputs=[positive_prompt,
guidance_scale,
num_images_per_prompt,
height,
width,
generator_seed,
negative_prompt],
outputs=gallery)
lora_scale.change(set_lora_weight,
inputs=lora_scale)
freeu.select(toggle_freeU, freeu)
lora_model_dropdown.select(set_lora_model,
[lora_model_dropdown, lora_scale],
positive_prompt)
if __name__ == "__main__":
demo.launch()