import gradio as gr from PIL import Image import diffusers from diffusers.models import AutoencoderKL vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse") 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,model, scheduler, lora, lora_weight): pipeline = diffusers.DiffusionPipeline.from_pretrained("SG161222/RealVisXL_V4.0").to("cuda") if model == 'Realistic_V6.0': pipeline = diffusers.DiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V6.0_B1_noVAE", vae=vae).to("cuda") pipeline.safety_checker = lambda images, **kwargs: (images, [False] * len(images)) if model == "Realistic_V5.1": pipeline = diffusers.DiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V5.1_noVAE", vae=vae).to("cuda") if model == "Realistic_V5.0": pipeline = diffusers.DiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V5.0_noVAE", vae=vae).to("cuda") if model == "EpicRealism": pipeline = diffusers.DiffusionPipeline.from_pretrained("emilianJR/epiCRealism", vae=vae).to("cuda") 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) if model != "RealVisXL_V4.0": pipeline.scheduler = scheduler.from_pretrained("emilianJR/epiCRealism", subfolder="scheduler", **add_kwargs) if lora == "nayanthara": lora = "profaker/Naya_lora" if lora == "saipallavi": lora = "profaker/saipallavi_lora" if lora == "shobita": lora = "profaker/Shobita_lora" if lora == "surya": lora = "profaker/Surya_lora" if lora == "vijay": lora = "profaker/Vijay_lora" if lora == "None": images = pipeline( prompt=prompt, negative_prompt=negative_prompt, num_inference_steps=int(num_inference_steps), guidance_scale=guidance_scale, clip_skip=1 ).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): models = ['RealVisXL_V4.0','Realistic_V6.0','Realistic_V5.1','Realistic_V5.0','EpicRealism'] model = gr.Dropdown(label="Models",choices=models,value="RealVisXL_V4.0") 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): loras = ['None','add_detail','nayanthara','shobita','surya','vijay','saipallavi'] lora = gr.Dropdown(label='Lora', choices=loras, value="None") lora_weight = gr.Number(value=0, minimum=0, maximum=1, step=0.01, label="Lora Weights") 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=1024, width=512) btn.click(fn=predict, inputs=[prompt, negative_prompt, guidance_scale, steps, model,scheduler, lora, lora_weight], outputs=[image_out], api_name='run') prompt.submit(fn=predict, inputs=[prompt, negative_prompt, guidance_scale, steps, model,scheduler, lora, lora_weight], outputs=[image_out]) image_blocks.queue(max_size=25, api_open=True).launch(show_api=True)