Spaces:
Runtime error
Runtime error
File size: 5,682 Bytes
cc79fc8 2ff1fc5 cc79fc8 2ff1fc5 cc79fc8 2ff1fc5 cc79fc8 0df2873 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import gradio as gr
from PIL import Image
import diffusers
from diffusers.models import AutoencoderKL
vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse")
pipeline = diffusers.DiffusionPipeline.from_pretrained("SG161222/RealVisXL_V4.0", vae=vae).to("cuda")
def read_content(file_path: str) -> str:
"""read the content of target file
"""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
return content
def predict(prompt, negative_prompt, guidance_scale, num_inference_steps, scheduler, lora, lora_weight):
pipeline.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
scheduler_class_name = scheduler.split("-")[0]
add_kwargs = {}
if len(scheduler.split("-")) > 1:
add_kwargs["use_karras_sigmas"] = True
if len(scheduler.split("-")) > 2:
add_kwargs["algorithm_type"] = "sde-dpmsolver++"
scheduler = getattr(diffusers, scheduler_class_name)
pipeline.scheduler = scheduler.from_pretrained("emilianJR/epiCRealism", subfolder="scheduler", **add_kwargs)
if lora == "add_detail":
lora = "profaker/add_detail_lora"
if lora == "nursing_job":
lora = "profaker/Nursing_job_lora"
if lora == "nsfw_POV":
lora = "profaker/NSFW_POV_lora"
if lora == "None":
images = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=int(num_inference_steps),
guidance_scale=guidance_scale
).images[0]
print("Prompt", prompt)
print("Negative", negative_prompt)
print("Steps", num_inference_steps)
print("Scale", guidance_scale)
print("Scheduler", scheduler)
return images
pipeline.load_lora_weights(lora)
images = pipeline(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=int(num_inference_steps),
guidance_scale=guidance_scale,
cross_attention_kwargs={"scale": lora_weight}
).images[0]
print("Prompt", prompt)
print("Negative", negative_prompt)
print("Steps", num_inference_steps)
print("Scale", guidance_scale)
print("Scheduler", scheduler)
return images
css = '''
.gradio-container{max-width: 1100px !important}
#image_upload{min-height:400px}
#image_upload [data-testid="image"], #image_upload [data-testid="image"] > div{min-height: 400px}
#mask_radio .gr-form{background:transparent; border: none}
#word_mask{margin-top: .75em !important}
#word_mask textarea:disabled{opacity: 0.3}
.footer {margin-bottom: 45px;margin-top: 35px;text-align: center;border-bottom: 1px solid #e5e5e5}
.footer>p {font-size: .8rem; display: inline-block; padding: 0 10px;transform: translateY(10px);background: white}
.dark .footer {border-color: #303030}
.dark .footer>p {background: #0b0f19}
.acknowledgments h4{margin: 1.25em 0 .25em 0;font-weight: bold;font-size: 115%}
#image_upload .touch-none{display: flex}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
#prompt-container{margin-top:-18px;}
#prompt-container .form{border-top-left-radius: 0;border-top-right-radius: 0}
'''
image_blocks = gr.Blocks(css=css, elem_id="total-container")
with image_blocks as demo:
gr.HTML(read_content("header.html"))
with gr.Row():
with gr.Column():
with gr.Row(elem_id="prompt-container", equal_height=True):
with gr.Row():
prompt = gr.Textbox(placeholder="Your prompt", show_label=False, elem_id="prompt", lines=5)
with gr.Accordion(label="Advanced Settings", open=False):
with gr.Row(equal_height=True):
guidance_scale = gr.Number(value=7.5, minimum=1.0, maximum=20.0, step=0.1, label="guidance_scale")
steps = gr.Number(value=40, minimum=0, maximum=100, step=1, label="steps")
with gr.Row(equal_height=True):
negative_prompt = gr.Textbox(label="negative_prompt", placeholder="Your negative prompt",
info="what you don't want to see in the image")
with gr.Row(equal_height=True):
schedulers = ["DEISMultistepScheduler", "HeunDiscreteScheduler", "EulerDiscreteScheduler",
"DPMSolverMultistepScheduler", "DPMSolverMultistepScheduler-Karras",
"DPMSolverMultistepScheduler-Karras-SDE"]
scheduler = gr.Dropdown(label="Schedulers", choices=schedulers,
value="DPMSolverMultistepScheduler-Karras")
with gr.Row(equal_height=True):
lora = ['None','add_detail', 'nursing_job', 'nsfw_POV']
lora = gr.Dropdown(label='Lora', choices=lora, value="None")
lora_weight = [-1, -0.5, 0, 0.5, 1]
lora_weight = gr.Dropdown(label="Lora Weights", choices=lora_weight, value=0.5)
with gr.Row(equal_height=True):
btn = gr.Button("Generate", elem_id="run_button")
with gr.Column():
image_out = gr.Image(label="Output", elem_id="output-img", height=512, width=512)
btn.click(fn=predict, inputs=[prompt, negative_prompt, guidance_scale, steps, scheduler, lora, lora_weight],
outputs=[image_out], api_name='run')
prompt.submit(fn=predict, inputs=[prompt, negative_prompt, guidance_scale, steps, scheduler, lora, lora_weight],
outputs=[image_out])
image_blocks.queue(max_size=25, api_open=True).launch(show_api=True)
|