John Alexander
commited on
Upload 8 files
Browse files- .gitattributes +2 -35
- LICENSE +21 -0
- README.md +4 -5
- app.py +101 -0
- gitattributes +35 -0
- requirements.txt +5 -0
- safety_checker.py +137 -0
- style.css +12 -0
.gitattributes
CHANGED
@@ -1,35 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
1 |
+
# Auto detect text files and perform LF normalization
|
2 |
+
* text=auto
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 John Alexander
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
|
5 |
-
colorTo: blue
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.19.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
1 |
---
|
2 |
+
title: Lightning Text to Image Generative AI
|
3 |
+
colorFrom: yellow
|
4 |
+
colorTo: gray
|
|
|
5 |
sdk: gradio
|
6 |
+
sdk_version: 4.19.1
|
7 |
app_file: app.py
|
8 |
pinned: false
|
9 |
license: mit
|
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
from safetensors.torch import load_file
|
6 |
+
import spaces
|
7 |
+
import os
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", "0") == "1"
|
11 |
+
|
12 |
+
# Constants
|
13 |
+
base = "stabilityai/stable-diffusion-xl-base-1.0"
|
14 |
+
repo = "ByteDance/SDXL-Lightning"
|
15 |
+
checkpoints = {
|
16 |
+
"1-Step" : ["sdxl_lightning_1step_unet_x0.safetensors", 1],
|
17 |
+
"2-Step" : ["sdxl_lightning_2step_unet.safetensors", 2],
|
18 |
+
"4-Step" : ["sdxl_lightning_4step_unet.safetensors", 4],
|
19 |
+
"8-Step" : ["sdxl_lightning_8step_unet.safetensors", 8],
|
20 |
+
}
|
21 |
+
|
22 |
+
|
23 |
+
# Ensure model and scheduler are initialized in GPU-enabled function
|
24 |
+
if torch.cuda.is_available():
|
25 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(base, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
26 |
+
|
27 |
+
if SAFETY_CHECKER:
|
28 |
+
from safety_checker import StableDiffusionSafetyChecker
|
29 |
+
from transformers import CLIPFeatureExtractor
|
30 |
+
|
31 |
+
safety_checker = StableDiffusionSafetyChecker.from_pretrained(
|
32 |
+
"CompVis/stable-diffusion-safety-checker"
|
33 |
+
).to("cuda")
|
34 |
+
feature_extractor = CLIPFeatureExtractor.from_pretrained(
|
35 |
+
"openai/clip-vit-base-patch32"
|
36 |
+
)
|
37 |
+
|
38 |
+
def check_nsfw_images(
|
39 |
+
images: list[Image.Image],
|
40 |
+
) -> tuple[list[Image.Image], list[bool]]:
|
41 |
+
safety_checker_input = feature_extractor(images, return_tensors="pt").to("cuda")
|
42 |
+
has_nsfw_concepts = safety_checker(
|
43 |
+
images=[images],
|
44 |
+
clip_input=safety_checker_input.pixel_values.to("cuda")
|
45 |
+
)
|
46 |
+
|
47 |
+
return images, has_nsfw_concepts
|
48 |
+
|
49 |
+
# Function
|
50 |
+
@spaces.GPU(enable_queue=True)
|
51 |
+
def generate_image(prompt, ckpt):
|
52 |
+
|
53 |
+
checkpoint = checkpoints[ckpt][0]
|
54 |
+
num_inference_steps = checkpoints[ckpt][1]
|
55 |
+
|
56 |
+
if num_inference_steps==1:
|
57 |
+
# Ensure sampler uses "trailing" timesteps and "sample" prediction type for 1-step inference.
|
58 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample")
|
59 |
+
else:
|
60 |
+
# Ensure sampler uses "trailing" timesteps.
|
61 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
|
62 |
+
|
63 |
+
pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device="cuda"))
|
64 |
+
results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0)
|
65 |
+
|
66 |
+
if SAFETY_CHECKER:
|
67 |
+
images, has_nsfw_concepts = check_nsfw_images(results.images)
|
68 |
+
if any(has_nsfw_concepts):
|
69 |
+
gr.Warning("NSFW content detected.")
|
70 |
+
return Image.new("RGB", (512, 512))
|
71 |
+
return images[0]
|
72 |
+
return results.images[0]
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
# Gradio Interface
|
77 |
+
description = """
|
78 |
+
This demo utilizes the SDXL-Lightning model by ByteDance, which is a lightning-fast text-to-image generative model capable of producing high-quality images in 4 steps.
|
79 |
+
As a community effort, this demo was put together by AngryPenguin. Link to model: https://huggingface.co/ByteDance/SDXL-Lightning
|
80 |
+
"""
|
81 |
+
|
82 |
+
with gr.Blocks(css="style.css") as demo:
|
83 |
+
gr.HTML("<h1><center>Text-to-Image with SDXL-Lightning ⚡</center></h1>")
|
84 |
+
gr.Markdown(description)
|
85 |
+
with gr.Group():
|
86 |
+
with gr.Row():
|
87 |
+
prompt = gr.Textbox(label='Enter you image prompt:', scale=8)
|
88 |
+
ckpt = gr.Dropdown(label='Select inference steps',choices=['1-Step', '2-Step', '4-Step', '8-Step'], value='4-Step', interactive=True)
|
89 |
+
submit = gr.Button(scale=1, variant='primary')
|
90 |
+
img = gr.Image(label='SDXL-Lightning Generated Image')
|
91 |
+
|
92 |
+
prompt.submit(fn=generate_image,
|
93 |
+
inputs=[prompt, ckpt],
|
94 |
+
outputs=img,
|
95 |
+
)
|
96 |
+
submit.click(fn=generate_image,
|
97 |
+
inputs=[prompt, ckpt],
|
98 |
+
outputs=img,
|
99 |
+
)
|
100 |
+
|
101 |
+
demo.queue().launch()
|
gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
diffusers
|
3 |
+
torch
|
4 |
+
accelerate
|
5 |
+
gradio
|
safety_checker.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2023 The HuggingFace Team. All rights reserved.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
+
import numpy as np
|
16 |
+
import torch
|
17 |
+
import torch.nn as nn
|
18 |
+
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel
|
19 |
+
|
20 |
+
|
21 |
+
def cosine_distance(image_embeds, text_embeds):
|
22 |
+
normalized_image_embeds = nn.functional.normalize(image_embeds)
|
23 |
+
normalized_text_embeds = nn.functional.normalize(text_embeds)
|
24 |
+
return torch.mm(normalized_image_embeds, normalized_text_embeds.t())
|
25 |
+
|
26 |
+
|
27 |
+
class StableDiffusionSafetyChecker(PreTrainedModel):
|
28 |
+
config_class = CLIPConfig
|
29 |
+
|
30 |
+
_no_split_modules = ["CLIPEncoderLayer"]
|
31 |
+
|
32 |
+
def __init__(self, config: CLIPConfig):
|
33 |
+
super().__init__(config)
|
34 |
+
|
35 |
+
self.vision_model = CLIPVisionModel(config.vision_config)
|
36 |
+
self.visual_projection = nn.Linear(
|
37 |
+
config.vision_config.hidden_size, config.projection_dim, bias=False
|
38 |
+
)
|
39 |
+
|
40 |
+
self.concept_embeds = nn.Parameter(
|
41 |
+
torch.ones(17, config.projection_dim), requires_grad=False
|
42 |
+
)
|
43 |
+
self.special_care_embeds = nn.Parameter(
|
44 |
+
torch.ones(3, config.projection_dim), requires_grad=False
|
45 |
+
)
|
46 |
+
|
47 |
+
self.concept_embeds_weights = nn.Parameter(torch.ones(17), requires_grad=False)
|
48 |
+
self.special_care_embeds_weights = nn.Parameter(
|
49 |
+
torch.ones(3), requires_grad=False
|
50 |
+
)
|
51 |
+
|
52 |
+
@torch.no_grad()
|
53 |
+
def forward(self, clip_input, images):
|
54 |
+
pooled_output = self.vision_model(clip_input)[1] # pooled_output
|
55 |
+
image_embeds = self.visual_projection(pooled_output)
|
56 |
+
|
57 |
+
# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
|
58 |
+
special_cos_dist = (
|
59 |
+
cosine_distance(image_embeds, self.special_care_embeds)
|
60 |
+
.cpu()
|
61 |
+
.float()
|
62 |
+
.numpy()
|
63 |
+
)
|
64 |
+
cos_dist = (
|
65 |
+
cosine_distance(image_embeds, self.concept_embeds).cpu().float().numpy()
|
66 |
+
)
|
67 |
+
|
68 |
+
result = []
|
69 |
+
batch_size = image_embeds.shape[0]
|
70 |
+
for i in range(batch_size):
|
71 |
+
result_img = {
|
72 |
+
"special_scores": {},
|
73 |
+
"special_care": [],
|
74 |
+
"concept_scores": {},
|
75 |
+
"bad_concepts": [],
|
76 |
+
}
|
77 |
+
|
78 |
+
# increase this value to create a stronger `nfsw` filter
|
79 |
+
# at the cost of increasing the possibility of filtering benign images
|
80 |
+
adjustment = 0.0
|
81 |
+
|
82 |
+
for concept_idx in range(len(special_cos_dist[0])):
|
83 |
+
concept_cos = special_cos_dist[i][concept_idx]
|
84 |
+
concept_threshold = self.special_care_embeds_weights[concept_idx].item()
|
85 |
+
result_img["special_scores"][concept_idx] = round(
|
86 |
+
concept_cos - concept_threshold + adjustment, 3
|
87 |
+
)
|
88 |
+
if result_img["special_scores"][concept_idx] > 0:
|
89 |
+
result_img["special_care"].append(
|
90 |
+
{concept_idx, result_img["special_scores"][concept_idx]}
|
91 |
+
)
|
92 |
+
adjustment = 0.01
|
93 |
+
|
94 |
+
for concept_idx in range(len(cos_dist[0])):
|
95 |
+
concept_cos = cos_dist[i][concept_idx]
|
96 |
+
concept_threshold = self.concept_embeds_weights[concept_idx].item()
|
97 |
+
result_img["concept_scores"][concept_idx] = round(
|
98 |
+
concept_cos - concept_threshold + adjustment, 3
|
99 |
+
)
|
100 |
+
if result_img["concept_scores"][concept_idx] > 0:
|
101 |
+
result_img["bad_concepts"].append(concept_idx)
|
102 |
+
|
103 |
+
result.append(result_img)
|
104 |
+
|
105 |
+
has_nsfw_concepts = [len(res["bad_concepts"]) > 0 for res in result]
|
106 |
+
|
107 |
+
return has_nsfw_concepts
|
108 |
+
|
109 |
+
@torch.no_grad()
|
110 |
+
def forward_onnx(self, clip_input: torch.FloatTensor, images: torch.FloatTensor):
|
111 |
+
pooled_output = self.vision_model(clip_input)[1] # pooled_output
|
112 |
+
image_embeds = self.visual_projection(pooled_output)
|
113 |
+
|
114 |
+
special_cos_dist = cosine_distance(image_embeds, self.special_care_embeds)
|
115 |
+
cos_dist = cosine_distance(image_embeds, self.concept_embeds)
|
116 |
+
|
117 |
+
# increase this value to create a stronger `nsfw` filter
|
118 |
+
# at the cost of increasing the possibility of filtering benign images
|
119 |
+
adjustment = 0.0
|
120 |
+
|
121 |
+
special_scores = (
|
122 |
+
special_cos_dist - self.special_care_embeds_weights + adjustment
|
123 |
+
)
|
124 |
+
# special_scores = special_scores.round(decimals=3)
|
125 |
+
special_care = torch.any(special_scores > 0, dim=1)
|
126 |
+
special_adjustment = special_care * 0.01
|
127 |
+
special_adjustment = special_adjustment.unsqueeze(1).expand(
|
128 |
+
-1, cos_dist.shape[1]
|
129 |
+
)
|
130 |
+
|
131 |
+
concept_scores = (cos_dist - self.concept_embeds_weights) + special_adjustment
|
132 |
+
# concept_scores = concept_scores.round(decimals=3)
|
133 |
+
has_nsfw_concepts = torch.any(concept_scores > 0, dim=1)
|
134 |
+
|
135 |
+
images[has_nsfw_concepts] = 0.0 # black image
|
136 |
+
|
137 |
+
return images, has_nsfw_concepts
|
style.css
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.gradio-container {
|
2 |
+
max-width: 690px! important;
|
3 |
+
}
|
4 |
+
|
5 |
+
#share-btn-container{padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;margin-top: 0.35em;}
|
6 |
+
div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
|
7 |
+
#share-btn-container:hover {background-color: #060606}
|
8 |
+
#share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;font-size: 15px;}
|
9 |
+
#share-btn * {all: unset}
|
10 |
+
#share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
|
11 |
+
#share-btn-container .wrap {display: none !important}
|
12 |
+
#share-btn-container.hidden {display: none!important}
|