File size: 6,641 Bytes
c0aebfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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/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)
    
    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 = ['Realistic_V6.0','Realistic_V5.1','Realistic_V5.0','EpicRealism']
                    model = gr.Dropdown(label="Models",choices=models,value="Realistic_V6.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.5, 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)