mariarivaille/Diffusion_Models_Course_Space
0
1import gradio as gr2import numpy as np3from infer import infer, CONTROLNET_MODE, MAX_SEED4 5MAX_IMAGE_SIZE = 10246 7examples = [8 "The image of a cartoonish mouse with clown red round nose and white curly hair. The mouse is gray with big pink ears and a black pointed nose. It has a simple design, the background color is white. The style of the image is reminiscent of a sticker or a digital illustration.",9 "The image of a cartoonish mouse eating from a red bowl of yellow triangle chips, her cheeks are full. The mouse is gray with big pink ears, small white eyes and a black pointed nose. It has a simple design, the background color is white. The style of the image is reminiscent of a sticker or a digital illustration.",10 "The image of a cartoonish mouse with red hearts instead of eyes meaning that the mouse is in love with something. The mouse is gray with big pink ears and a black pointed nose. It has a simple design, the background color is white. The style of the image is reminiscent of a sticker or a digital illustration.",11 "The image of a cartoonish mouse with sunglasses and smiling. The mouse is gray with big pink ears and a black pointed nose. It has a simple design, the background color is white. The style of the image is reminiscent of a sticker or a digital illustration.",12]13 14css = """15#col-container {16 margin: 0 auto;17 max-width: 640px;18}19"""20 21def on_checkbox_change(use_advanced):22 visible = use_advanced23 return (gr.update(visible=visible, interactive=visible),24 gr.update(visible=visible, interactive=visible),25 gr.update(visible=visible, interactive=visible))26 27with gr.Blocks(css=css) as demo:28 with gr.Column(elem_id="col-container"):29 gr.Markdown(" # Maria Lashina T2I Rat Stickers Generation App")30 31 MODEL_LIST = [32 "CompVis/stable-diffusion-v1-4",33 "stable-diffusion-v1-5/stable-diffusion-v1-5",34 "Maria_Lashina_LoRA"35 ]36 with gr.Row():37 model_id = gr.Dropdown(38 label="Model",39 choices=MODEL_LIST40 )41 42 with gr.Row():43 prompt = gr.Text(44 label="Prompt",45 show_label=False,46 max_lines=1,47 placeholder="Enter your prompt",48 container=False,49 )50 51 run_button = gr.Button("Run", scale=0, variant="primary")52 53 result = gr.Image(label="Result", show_label=False)54 55 with gr.Accordion("Advanced Settings", open=False):56 negative_prompt = gr.Text(57 label="Negative prompt",58 max_lines=1,59 # placeholder="Enter a negative prompt",60 value="bad anatomy, disfigured, poorly drawn face, ugly, low quality, blurry, distortion",61 visible=True,62 )63 64 with gr.Row():65 delete_background = gr.Checkbox(label="Delete background?")66 67 use_controlnet = gr.Checkbox(label="Use ControlNet")68 control_strength = gr.Slider(69 label="ControlNet strength",70 minimum=0,71 maximum=1,72 step=0.1,73 value=0.8,74 visible=False75 )76 controlnet_mode = gr.Dropdown(CONTROLNET_MODE.keys(), label="ControlNet mode", visible=False)77 controlnet_image = gr.Image(label="ControlNet image", visible=False)78 use_controlnet.change(on_checkbox_change, use_controlnet, [control_strength, controlnet_mode, controlnet_image])79 80 use_ip_adapter = gr.Checkbox(label="Use IPAdapter")81 ip_adapter_scale = gr.Slider(82 label="IPAdapter scale",83 minimum=0,84 maximum=1,85 step=0.1,86 value=0.8,87 visible=False88 )89 ip_adapter_image = gr.Image(label="IPAdapter image", visible=False)90 use_ip_adapter.change(on_checkbox_change, use_ip_adapter, [ip_adapter_scale, ip_adapter_image])91 92 seed = gr.Slider(93 label="Seed",94 minimum=0,95 maximum=MAX_SEED,96 step=1,97 value=42,98 )99 100 randomize_seed = gr.Checkbox(label="Randomize seed", value=False)101 102 with gr.Row():103 width = gr.Slider(104 label="Width",105 minimum=256,106 maximum=MAX_IMAGE_SIZE,107 step=32,108 value=512, # Replace with defaults that work for your model109 )110 111 height = gr.Slider(112 label="Height",113 minimum=256,114 maximum=MAX_IMAGE_SIZE,115 step=32,116 value=512, # Replace with defaults that work for your model117 )118 119 with gr.Row():120 guidance_scale = gr.Slider(121 label="Guidance scale",122 minimum=0.0,123 maximum=15.0,124 step=1.0,125 value=8.0, # Replace with defaults that work for your model126 )127 128 lora_scale = gr.Slider(129 label="LoRA scale",130 minimum=0.0,131 maximum=1.0,132 step=0.1,133 value=0.8, # Replace with defaults that work for your model134 )135 136 num_inference_steps = gr.Slider(137 label="Number of inference steps",138 minimum=1,139 maximum=50,140 step=1,141 value=30, # Replace with defaults that work for your model142 )143 144 gr.Examples(examples=examples, inputs=[prompt])145 gr.on(146 triggers=[run_button.click, prompt.submit],147 fn=infer,148 inputs=[149 model_id,150 prompt,151 negative_prompt,152 seed,153 randomize_seed,154 width,155 height,156 guidance_scale,157 lora_scale,158 num_inference_steps,159 use_controlnet,160 control_strength,161 controlnet_mode,162 controlnet_image,163 use_ip_adapter,164 ip_adapter_scale,165 ip_adapter_image,166 delete_background167 ],168 outputs=[result, seed],169 )170 171if __name__ == "__main__":172 demo.launch(share=True, debug=True)173 