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