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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
import random
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
|
| 9 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
| 10 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def query(prompt, is_negative=False, image_style="None style", steps=8, cfg_scale=7, seed=None, num_images=4):
|
| 14 |
+
images = []
|
| 15 |
+
for _ in range(num_images):
|
| 16 |
+
if image_style == "None style":
|
| 17 |
+
payload = {
|
| 18 |
+
"inputs": prompt + ", 8k",
|
| 19 |
+
"is_negative": is_negative,
|
| 20 |
+
"steps": steps,
|
| 21 |
+
"cfg_scale": cfg_scale,
|
| 22 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 23 |
+
}
|
| 24 |
+
elif image_style == "Cinematic":
|
| 25 |
+
payload = {
|
| 26 |
+
"inputs": prompt + ", realistic, detailed, textured, skin, hair, eyes, by Alex Huguet, Mike Hill, Ian Spriggs, JaeCheol Park, Marek Denko",
|
| 27 |
+
"is_negative": is_negative + ", abstract, cartoon, stylized",
|
| 28 |
+
"steps": steps,
|
| 29 |
+
"cfg_scale": cfg_scale,
|
| 30 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 31 |
+
}
|
| 32 |
+
elif image_style == "Digital Art":
|
| 33 |
+
payload = {
|
| 34 |
+
"inputs": prompt + ", faded , vintage , nostalgic , by Jose Villa , Elizabeth Messina , Ryan Brenizer , Jonas Peterson , Jasmine Star",
|
| 35 |
+
"is_negative": is_negative + ", sharp , modern , bright",
|
| 36 |
+
"steps": steps,
|
| 37 |
+
"cfg_scale": cfg_scale,
|
| 38 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 39 |
+
}
|
| 40 |
+
elif image_style == "Portrait":
|
| 41 |
+
payload = {
|
| 42 |
+
"inputs": prompt + ", soft light, sharp, exposure blend, medium shot, bokeh, (hdr:1.4), high contrast, (cinematic, teal and orange:0.85), (muted colors, dim colors, soothing tones:1.3), low saturation, (hyperdetailed:1.2), (noir:0.4), (natural skin texture, hyperrealism, soft light, sharp:1.2)",
|
| 43 |
+
"is_negative": is_negative,
|
| 44 |
+
"steps": steps,
|
| 45 |
+
"cfg_scale": cfg_scale,
|
| 46 |
+
"seed": seed if seed is not None else random.randint(-1, 2147483647)
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
image_bytes = requests.post(API_URL, headers=headers, json=payload).content
|
| 50 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 51 |
+
images.append(image)
|
| 52 |
+
return images
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
css = """
|
| 56 |
+
.gradio-container {
|
| 57 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
| 58 |
+
}
|
| 59 |
+
.gr-button {
|
| 60 |
+
color: white;
|
| 61 |
+
border-color: black;
|
| 62 |
+
background: black;
|
| 63 |
+
}
|
| 64 |
+
input[type='range'] {
|
| 65 |
+
accent-color: black;
|
| 66 |
+
}
|
| 67 |
+
.dark input[type='range'] {
|
| 68 |
+
accent-color: #dfdfdf;
|
| 69 |
+
}
|
| 70 |
+
.container {
|
| 71 |
+
max-width: 730px;
|
| 72 |
+
margin: auto;
|
| 73 |
+
padding-top: 1.5rem;
|
| 74 |
+
}
|
| 75 |
+
#gallery {
|
| 76 |
+
min-height: 22rem;
|
| 77 |
+
margin-bottom: 15px;
|
| 78 |
+
margin-left: auto;
|
| 79 |
+
margin-right: auto;
|
| 80 |
+
border-bottom-right-radius: .5rem !important;
|
| 81 |
+
border-bottom-left-radius: .5rem !important;
|
| 82 |
+
}
|
| 83 |
+
#gallery>div>.h-full {
|
| 84 |
+
min-height: 20rem;
|
| 85 |
+
}
|
| 86 |
+
.details:hover {
|
| 87 |
+
text-decoration: underline;
|
| 88 |
+
}
|
| 89 |
+
.gr-button {
|
| 90 |
+
white-space: nowrap;
|
| 91 |
+
}
|
| 92 |
+
.gr-button:focus {
|
| 93 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
| 94 |
+
outline: none;
|
| 95 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
| 96 |
+
--tw-border-opacity: 1;
|
| 97 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
| 98 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
| 99 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
| 100 |
+
--tw-ring-opacity: .5;
|
| 101 |
+
}
|
| 102 |
+
#advanced-btn {
|
| 103 |
+
font-size: .7rem !important;
|
| 104 |
+
line-height: 19px;
|
| 105 |
+
margin-top: 12px;
|
| 106 |
+
margin-bottom: 12px;
|
| 107 |
+
padding: 2px 8px;
|
| 108 |
+
border-radius: 14px !important;
|
| 109 |
+
}
|
| 110 |
+
#advanced-options {
|
| 111 |
+
display: none;
|
| 112 |
+
margin-bottom: 20px;
|
| 113 |
+
}
|
| 114 |
+
.footer {
|
| 115 |
+
margin-bottom: 45px;
|
| 116 |
+
margin-top: 35px;
|
| 117 |
+
text-align: center;
|
| 118 |
+
border-bottom: 1px solid #e5e5e5;
|
| 119 |
+
}
|
| 120 |
+
.footer>p {
|
| 121 |
+
font-size: .8rem;
|
| 122 |
+
display: inline-block;
|
| 123 |
+
padding: 0 10px;
|
| 124 |
+
transform: translateY(10px);
|
| 125 |
+
background: white;
|
| 126 |
+
}
|
| 127 |
+
.dark .footer {
|
| 128 |
+
border-color: #303030;
|
| 129 |
+
}
|
| 130 |
+
.dark .footer>p {
|
| 131 |
+
background: #0b0f19;
|
| 132 |
+
}
|
| 133 |
+
.acknowledgments h4{
|
| 134 |
+
margin: 1.25em 0 .25em 0;
|
| 135 |
+
font-weight: bold;
|
| 136 |
+
font-size: 115%;
|
| 137 |
+
}
|
| 138 |
+
.animate-spin {
|
| 139 |
+
animation: spin 1s linear infinite;
|
| 140 |
+
}
|
| 141 |
+
@keyframes spin {
|
| 142 |
+
from {
|
| 143 |
+
transform: rotate(0deg);
|
| 144 |
+
}
|
| 145 |
+
to {
|
| 146 |
+
transform: rotate(360deg);
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
#share-btn-container {
|
| 150 |
+
display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
| 151 |
+
margin-top: 10px;
|
| 152 |
+
margin-left: auto;
|
| 153 |
+
}
|
| 154 |
+
#share-btn {
|
| 155 |
+
all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
|
| 156 |
+
}
|
| 157 |
+
#share-btn * {
|
| 158 |
+
all: unset;
|
| 159 |
+
}
|
| 160 |
+
#share-btn-container div:nth-child(-n+2){
|
| 161 |
+
width: auto !important;
|
| 162 |
+
min-height: 0px !important;
|
| 163 |
+
}
|
| 164 |
+
#share-btn-container .wrap {
|
| 165 |
+
display: none !important;
|
| 166 |
+
}
|
| 167 |
+
.gr-form{
|
| 168 |
+
flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
|
| 169 |
+
}
|
| 170 |
+
#prompt-container{
|
| 171 |
+
gap: 0;
|
| 172 |
+
}
|
| 173 |
+
#prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
|
| 174 |
+
#component-16{border-top-width: 1px!important;margin-top: 1em}
|
| 175 |
+
.image_duplication{position: absolute; width: 100px; left: 50px}
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
with gr.Blocks(css=css) as demo:
|
| 179 |
+
favicon = '<img src="https://www.gstatic.com/android/keyboard/emojikitchen/20201001/u1f600/u1f600_u1f525.png" width="48px" style="display: inline">'
|
| 180 |
+
gr.Markdown(
|
| 181 |
+
f"""<h1><center>{favicon} Stable Diffusion V2.1</center></h1>
|
| 182 |
+
"""
|
| 183 |
+
)
|
| 184 |
+
with gr.Group():
|
| 185 |
+
with gr.Box():
|
| 186 |
+
with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
|
| 187 |
+
with gr.Column():
|
| 188 |
+
text_prompt = gr.Textbox(show_label=False, placeholder="Enter your prompt", max_lines=1, elem_id="prompt-text-input").style(border=(True, False, True, True), rounded=(True, False, False, True), container=False)
|
| 189 |
+
negative_prompt = gr.Textbox(show_label=False, placeholder="Enter a negative prompt", max_lines=1, elem_id="negative-prompt-text-input").style(border=(True, False, True, True), rounded=(True, False, False, True), container=False)
|
| 190 |
+
text_button = gr.Button("Generate").style(margin=False, rounded=(False, True, True, False), full_width=False)
|
| 191 |
+
|
| 192 |
+
gallery_output = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
|
| 193 |
+
with gr.Accordion("Advanced settings", open=False):
|
| 194 |
+
image_style = gr.Radio(label="Style", choices=["None style", "Cinematic", "Digital Art", "Portrait"], value="None style")
|
| 195 |
+
|
| 196 |
+
text_button.click(query, inputs=[text_prompt, negative_prompt, image_style], outputs=gallery_output)
|
| 197 |
+
|
| 198 |
+
demo.launch(show_api=False)
|