Spaces:
Running
on
Zero
Running
on
Zero
tianleliphoebe
commited on
Commit
•
45c2aa5
1
Parent(s):
f2e4ce4
add NSFW gaurd
Browse files- model/model_manager.py +32 -6
- serve/gradio_web.py +12 -0
- serve/gradio_web_image_editing.py +12 -0
- serve/gradio_web_video_generation.py +12 -0
- serve/vote_utils.py +18 -0
model/model_manager.py
CHANGED
@@ -8,6 +8,9 @@ from PIL import Image
|
|
8 |
from .models import IMAGE_GENERATION_MODELS, IMAGE_EDITION_MODELS, VIDEO_GENERATION_MODELS, MUSEUM_UNSUPPORTED_MODELS, DESIRED_APPEAR_MODEL, load_pipeline
|
9 |
from .fetch_museum_results import draw_from_imagen_museum, draw2_from_imagen_museum, draw_from_videogen_museum, draw2_from_videogen_museum
|
10 |
|
|
|
|
|
|
|
11 |
class ModelManager:
|
12 |
def __init__(self):
|
13 |
self.model_ig_list = IMAGE_GENERATION_MODELS
|
@@ -24,11 +27,28 @@ class ModelManager:
|
|
24 |
else:
|
25 |
pipe = self.loaded_models[model_name]
|
26 |
return pipe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
@spaces.GPU(duration=120)
|
29 |
def generate_image_ig(self, prompt, model_name):
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
32 |
return result
|
33 |
|
34 |
def generate_image_ig_api(self, prompt, model_name):
|
@@ -96,8 +116,11 @@ class ModelManager:
|
|
96 |
|
97 |
@spaces.GPU(duration=200)
|
98 |
def generate_image_ie(self, textbox_source, textbox_target, textbox_instruct, source_image, model_name):
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
101 |
return result
|
102 |
|
103 |
def generate_image_ie_museum(self, model_name):
|
@@ -161,8 +184,11 @@ class ModelManager:
|
|
161 |
|
162 |
@spaces.GPU(duration=150)
|
163 |
def generate_video_vg(self, prompt, model_name):
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
166 |
return result
|
167 |
|
168 |
def generate_video_vg_api(self, prompt, model_name):
|
|
|
8 |
from .models import IMAGE_GENERATION_MODELS, IMAGE_EDITION_MODELS, VIDEO_GENERATION_MODELS, MUSEUM_UNSUPPORTED_MODELS, DESIRED_APPEAR_MODEL, load_pipeline
|
9 |
from .fetch_museum_results import draw_from_imagen_museum, draw2_from_imagen_museum, draw_from_videogen_museum, draw2_from_videogen_museum
|
10 |
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
12 |
+
import torch
|
13 |
+
|
14 |
class ModelManager:
|
15 |
def __init__(self):
|
16 |
self.model_ig_list = IMAGE_GENERATION_MODELS
|
|
|
27 |
else:
|
28 |
pipe = self.loaded_models[model_name]
|
29 |
return pipe
|
30 |
+
|
31 |
+
@spaces.GPU(duration=20)
|
32 |
+
def NSFW_filter(self, prompt):
|
33 |
+
model_id = "meta-llama/Meta-Llama-Guard-2-8B"
|
34 |
+
device = "cuda"
|
35 |
+
dtype = torch.bfloat16
|
36 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
37 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=dtype, device_map=device)
|
38 |
+
chat = [{"role": "user", "content": prompt}]
|
39 |
+
input_ids = tokenizer.apply_chat_template(chat, return_tensors="pt").to(device)
|
40 |
+
output = model.generate(input_ids=input_ids, max_new_tokens=100, pad_token_id=0)
|
41 |
+
prompt_len = input_ids.shape[-1]
|
42 |
+
result = tokenizer.decode(output[0][prompt_len:], skip_special_tokens=True)
|
43 |
+
return result
|
44 |
|
45 |
@spaces.GPU(duration=120)
|
46 |
def generate_image_ig(self, prompt, model_name):
|
47 |
+
if self.NSFW_filter(prompt) == 'safe':
|
48 |
+
pipe = self.load_model_pipe(model_name)
|
49 |
+
result = pipe(prompt=prompt)
|
50 |
+
else:
|
51 |
+
result = ''
|
52 |
return result
|
53 |
|
54 |
def generate_image_ig_api(self, prompt, model_name):
|
|
|
116 |
|
117 |
@spaces.GPU(duration=200)
|
118 |
def generate_image_ie(self, textbox_source, textbox_target, textbox_instruct, source_image, model_name):
|
119 |
+
if self.NSFW_filter(" ".join([textbox_source, textbox_target, textbox_instruct])) == 'safe':
|
120 |
+
pipe = self.load_model_pipe(model_name)
|
121 |
+
result = pipe(src_image = source_image, src_prompt = textbox_source, target_prompt = textbox_target, instruct_prompt = textbox_instruct)
|
122 |
+
else:
|
123 |
+
result = ''
|
124 |
return result
|
125 |
|
126 |
def generate_image_ie_museum(self, model_name):
|
|
|
184 |
|
185 |
@spaces.GPU(duration=150)
|
186 |
def generate_video_vg(self, prompt, model_name):
|
187 |
+
if self.NSFW_filter(prompt) == 'safe':
|
188 |
+
pipe = self.load_model_pipe(model_name)
|
189 |
+
result = pipe(prompt=prompt)
|
190 |
+
else:
|
191 |
+
result = ''
|
192 |
return result
|
193 |
|
194 |
def generate_video_vg_api(self, prompt, model_name):
|
serve/gradio_web.py
CHANGED
@@ -33,6 +33,12 @@ def build_side_by_side_ui_anony(models):
|
|
33 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
34 |
- Your data will be anonymized and will not be used for commercial purposes.
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
## 🏆 Arena Elo
|
37 |
Find out who is the 🥇conditional image generation models! More models are going to be supported.
|
38 |
|
@@ -206,6 +212,12 @@ def build_side_by_side_ui_named(models):
|
|
206 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
207 |
- Your data will be anonymized and will not be used for commercial purposes.
|
208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
## 🤖 Choose two models to compare
|
210 |
"""
|
211 |
model_list = models.model_ig_list
|
|
|
33 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
34 |
- Your data will be anonymized and will not be used for commercial purposes.
|
35 |
|
36 |
+
## 📑 User Guidelines for AI Content Generation
|
37 |
+
- No NSFW Content: All user inputs must adhere to our content guidelines. Prompts containing NSFW (Not Safe For Work) content are strictly prohibited.
|
38 |
+
- Automatic Filtering: Any input deemed inappropriate or NSFW will be automatically filtered out. Users will be prompted to re-enter a suitable request.
|
39 |
+
- Content Guidelines: Please avoid using language or descriptions that could be considered offensive, explicit, or inappropriate. This includes, but is not limited to, sexual content, violence, and hate speech.
|
40 |
+
- Respectful Use: We encourage all users to create content that is respectful and suitable for all audiences.
|
41 |
+
|
42 |
## 🏆 Arena Elo
|
43 |
Find out who is the 🥇conditional image generation models! More models are going to be supported.
|
44 |
|
|
|
212 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
213 |
- Your data will be anonymized and will not be used for commercial purposes.
|
214 |
|
215 |
+
## 📑 User Guidelines for AI Content Generation
|
216 |
+
- No NSFW Content: All user inputs must adhere to our content guidelines. Prompts containing NSFW (Not Safe For Work) content are strictly prohibited.
|
217 |
+
- Automatic Filtering: Any input deemed inappropriate or NSFW will be automatically filtered out. Users will be prompted to re-enter a suitable request.
|
218 |
+
- Content Guidelines: Please avoid using language or descriptions that could be considered offensive, explicit, or inappropriate. This includes, but is not limited to, sexual content, violence, and hate speech.
|
219 |
+
- Respectful Use: We encourage all users to create content that is respectful and suitable for all audiences.
|
220 |
+
|
221 |
## 🤖 Choose two models to compare
|
222 |
"""
|
223 |
model_list = models.model_ig_list
|
serve/gradio_web_image_editing.py
CHANGED
@@ -37,6 +37,12 @@ def build_side_by_side_ui_anony_ie(models):
|
|
37 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
38 |
- Your data will be anonymized and will not be used for commercial purposes.
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
## 🏆 Arena Elo
|
41 |
Find out who is the 🥇conditional image edition models!
|
42 |
|
@@ -244,6 +250,12 @@ def build_side_by_side_ui_named_ie(models):
|
|
244 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
245 |
- Your data will be anonymized and will not be used for commercial purposes.
|
246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
## 🤖 Choose two models to compare
|
248 |
"""
|
249 |
|
|
|
37 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
38 |
- Your data will be anonymized and will not be used for commercial purposes.
|
39 |
|
40 |
+
## 📑 User Guidelines for AI Content Generation
|
41 |
+
- No NSFW Content: All user inputs must adhere to our content guidelines. Prompts containing NSFW (Not Safe For Work) content are strictly prohibited.
|
42 |
+
- Automatic Filtering: Any input deemed inappropriate or NSFW will be automatically filtered out. Users will be prompted to re-enter a suitable request.
|
43 |
+
- Content Guidelines: Please avoid using language or descriptions that could be considered offensive, explicit, or inappropriate. This includes, but is not limited to, sexual content, violence, and hate speech.
|
44 |
+
- Respectful Use: We encourage all users to create content that is respectful and suitable for all audiences.
|
45 |
+
|
46 |
## 🏆 Arena Elo
|
47 |
Find out who is the 🥇conditional image edition models!
|
48 |
|
|
|
250 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
251 |
- Your data will be anonymized and will not be used for commercial purposes.
|
252 |
|
253 |
+
## 📑 User Guidelines for AI Content Generation
|
254 |
+
- No NSFW Content: All user inputs must adhere to our content guidelines. Prompts containing NSFW (Not Safe For Work) content are strictly prohibited.
|
255 |
+
- Automatic Filtering: Any input deemed inappropriate or NSFW will be automatically filtered out. Users will be prompted to re-enter a suitable request.
|
256 |
+
- Content Guidelines: Please avoid using language or descriptions that could be considered offensive, explicit, or inappropriate. This includes, but is not limited to, sexual content, violence, and hate speech.
|
257 |
+
- Respectful Use: We encourage all users to create content that is respectful and suitable for all audiences.
|
258 |
+
|
259 |
## 🤖 Choose two models to compare
|
260 |
"""
|
261 |
|
serve/gradio_web_video_generation.py
CHANGED
@@ -34,6 +34,12 @@ def build_side_by_side_ui_anony_vg(models):
|
|
34 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
35 |
- Your data will be anonymized and will not be used for commercial purposes.
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
## 🏆 Arena Elo
|
38 |
Find out who is the 🥇conditional video generation models! More models are going to be supported.
|
39 |
|
@@ -210,6 +216,12 @@ def build_side_by_side_ui_named_vg(models):
|
|
210 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
211 |
- Your data will be anonymized and will not be used for commercial purposes.
|
212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
## 🤖 Choose two models to compare
|
214 |
"""
|
215 |
model_list = models.model_vg_list
|
|
|
34 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
35 |
- Your data will be anonymized and will not be used for commercial purposes.
|
36 |
|
37 |
+
## 📑 User Guidelines for AI Content Generation
|
38 |
+
- No NSFW Content: All user inputs must adhere to our content guidelines. Prompts containing NSFW (Not Safe For Work) content are strictly prohibited.
|
39 |
+
- Automatic Filtering: Any input deemed inappropriate or NSFW will be automatically filtered out. Users will be prompted to re-enter a suitable request.
|
40 |
+
- Content Guidelines: Please avoid using language or descriptions that could be considered offensive, explicit, or inappropriate. This includes, but is not limited to, sexual content, violence, and hate speech.
|
41 |
+
- Respectful Use: We encourage all users to create content that is respectful and suitable for all audiences.
|
42 |
+
|
43 |
## 🏆 Arena Elo
|
44 |
Find out who is the 🥇conditional video generation models! More models are going to be supported.
|
45 |
|
|
|
216 |
- By using this service, you agree to the collection of your input and votes for research purposes.
|
217 |
- Your data will be anonymized and will not be used for commercial purposes.
|
218 |
|
219 |
+
## 📑 User Guidelines for AI Content Generation
|
220 |
+
- No NSFW Content: All user inputs must adhere to our content guidelines. Prompts containing NSFW (Not Safe For Work) content are strictly prohibited.
|
221 |
+
- Automatic Filtering: Any input deemed inappropriate or NSFW will be automatically filtered out. Users will be prompted to re-enter a suitable request.
|
222 |
+
- Content Guidelines: Please avoid using language or descriptions that could be considered offensive, explicit, or inappropriate. This includes, but is not limited to, sexual content, violence, and hate speech.
|
223 |
+
- Respectful Use: We encourage all users to create content that is respectful and suitable for all audiences.
|
224 |
+
|
225 |
## 🤖 Choose two models to compare
|
226 |
"""
|
227 |
model_list = models.model_vg_list
|
serve/vote_utils.py
CHANGED
@@ -514,6 +514,8 @@ def generate_ig(gen_func, state, text, model_name, request: gr.Request):
|
|
514 |
ig_logger.info(f"generate. ip: {ip}")
|
515 |
start_tstamp = time.time()
|
516 |
generated_image = gen_func(text, model_name)
|
|
|
|
|
517 |
state.prompt = text
|
518 |
state.output = generated_image
|
519 |
state.model_name = model_name
|
@@ -596,6 +598,8 @@ def generate_igm(gen_func, state0, state1, text, model_name0, model_name1, reque
|
|
596 |
model_name0 = re.sub(r"### Model A: ", "", model_name0)
|
597 |
model_name1 = re.sub(r"### Model B: ", "", model_name1)
|
598 |
generated_image0, generated_image1 = gen_func(text, model_name0, model_name1)
|
|
|
|
|
599 |
state0.prompt = text
|
600 |
state1.prompt = text
|
601 |
state0.output = generated_image0
|
@@ -712,6 +716,8 @@ def generate_igm_annoy(gen_func, state0, state1, text, model_name0, model_name1,
|
|
712 |
model_name0 = ""
|
713 |
model_name1 = ""
|
714 |
generated_image0, generated_image1, model_name0, model_name1 = gen_func(text, model_name0, model_name1)
|
|
|
|
|
715 |
state0.prompt = text
|
716 |
state1.prompt = text
|
717 |
state0.output = generated_image0
|
@@ -831,6 +837,8 @@ def generate_ie(gen_func, state, source_text, target_text, instruct_text, source
|
|
831 |
ig_logger.info(f"generate. ip: {ip}")
|
832 |
start_tstamp = time.time()
|
833 |
generated_image = gen_func(source_text, target_text, instruct_text, source_image, model_name)
|
|
|
|
|
834 |
state.source_prompt = source_text
|
835 |
state.target_prompt = target_text
|
836 |
state.instruct_prompt = instruct_text
|
@@ -933,6 +941,8 @@ def generate_iem(gen_func, state0, state1, source_text, target_text, instruct_te
|
|
933 |
model_name0 = re.sub(r"### Model A: ", "", model_name0)
|
934 |
model_name1 = re.sub(r"### Model B: ", "", model_name1)
|
935 |
generated_image0, generated_image1 = gen_func(source_text, target_text, instruct_text, source_image, model_name0, model_name1)
|
|
|
|
|
936 |
state0.source_prompt = source_text
|
937 |
state0.target_prompt = target_text
|
938 |
state0.instruct_prompt = instruct_text
|
@@ -1074,6 +1084,8 @@ def generate_iem_annoy(gen_func, state0, state1, source_text, target_text, instr
|
|
1074 |
model_name0 = ""
|
1075 |
model_name1 = ""
|
1076 |
generated_image0, generated_image1, model_name0, model_name1 = gen_func(source_text, target_text, instruct_text, source_image, model_name0, model_name1)
|
|
|
|
|
1077 |
state0.source_prompt = source_text
|
1078 |
state0.target_prompt = target_text
|
1079 |
state0.instruct_prompt = instruct_text
|
@@ -1205,6 +1217,8 @@ def generate_vg(gen_func, state, text, model_name, request: gr.Request):
|
|
1205 |
vg_logger.info(f"generate. ip: {ip}")
|
1206 |
start_tstamp = time.time()
|
1207 |
generated_video = gen_func(text, model_name)
|
|
|
|
|
1208 |
state.prompt = text
|
1209 |
state.output = generated_video
|
1210 |
state.model_name = model_name
|
@@ -1299,6 +1313,8 @@ def generate_vgm(gen_func, state0, state1, text, model_name0, model_name1, reque
|
|
1299 |
model_name0 = re.sub(r"### Model A: ", "", model_name0)
|
1300 |
model_name1 = re.sub(r"### Model B: ", "", model_name1)
|
1301 |
generated_video0, generated_video1 = gen_func(text, model_name0, model_name1)
|
|
|
|
|
1302 |
state0.prompt = text
|
1303 |
state1.prompt = text
|
1304 |
state0.output = generated_video0
|
@@ -1437,6 +1453,8 @@ def generate_vgm_annoy(gen_func, state0, state1, text, model_name0, model_name1,
|
|
1437 |
model_name0 = ""
|
1438 |
model_name1 = ""
|
1439 |
generated_video0, generated_video1, model_name0, model_name1 = gen_func(text, model_name0, model_name1)
|
|
|
|
|
1440 |
state0.prompt = text
|
1441 |
state1.prompt = text
|
1442 |
state0.output = generated_video0
|
|
|
514 |
ig_logger.info(f"generate. ip: {ip}")
|
515 |
start_tstamp = time.time()
|
516 |
generated_image = gen_func(text, model_name)
|
517 |
+
if generated_image == '':
|
518 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
519 |
state.prompt = text
|
520 |
state.output = generated_image
|
521 |
state.model_name = model_name
|
|
|
598 |
model_name0 = re.sub(r"### Model A: ", "", model_name0)
|
599 |
model_name1 = re.sub(r"### Model B: ", "", model_name1)
|
600 |
generated_image0, generated_image1 = gen_func(text, model_name0, model_name1)
|
601 |
+
if generated_image0 == '' and generated_image1 == '':
|
602 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
603 |
state0.prompt = text
|
604 |
state1.prompt = text
|
605 |
state0.output = generated_image0
|
|
|
716 |
model_name0 = ""
|
717 |
model_name1 = ""
|
718 |
generated_image0, generated_image1, model_name0, model_name1 = gen_func(text, model_name0, model_name1)
|
719 |
+
if generated_image0 == '' and generated_image1 == '':
|
720 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
721 |
state0.prompt = text
|
722 |
state1.prompt = text
|
723 |
state0.output = generated_image0
|
|
|
837 |
ig_logger.info(f"generate. ip: {ip}")
|
838 |
start_tstamp = time.time()
|
839 |
generated_image = gen_func(source_text, target_text, instruct_text, source_image, model_name)
|
840 |
+
if generated_image == '':
|
841 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
842 |
state.source_prompt = source_text
|
843 |
state.target_prompt = target_text
|
844 |
state.instruct_prompt = instruct_text
|
|
|
941 |
model_name0 = re.sub(r"### Model A: ", "", model_name0)
|
942 |
model_name1 = re.sub(r"### Model B: ", "", model_name1)
|
943 |
generated_image0, generated_image1 = gen_func(source_text, target_text, instruct_text, source_image, model_name0, model_name1)
|
944 |
+
if generated_image0 == '' and generated_image1 == '':
|
945 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
946 |
state0.source_prompt = source_text
|
947 |
state0.target_prompt = target_text
|
948 |
state0.instruct_prompt = instruct_text
|
|
|
1084 |
model_name0 = ""
|
1085 |
model_name1 = ""
|
1086 |
generated_image0, generated_image1, model_name0, model_name1 = gen_func(source_text, target_text, instruct_text, source_image, model_name0, model_name1)
|
1087 |
+
if generated_image0 == '' and generated_image1 == '':
|
1088 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
1089 |
state0.source_prompt = source_text
|
1090 |
state0.target_prompt = target_text
|
1091 |
state0.instruct_prompt = instruct_text
|
|
|
1217 |
vg_logger.info(f"generate. ip: {ip}")
|
1218 |
start_tstamp = time.time()
|
1219 |
generated_video = gen_func(text, model_name)
|
1220 |
+
if generated_video == '':
|
1221 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
1222 |
state.prompt = text
|
1223 |
state.output = generated_video
|
1224 |
state.model_name = model_name
|
|
|
1313 |
model_name0 = re.sub(r"### Model A: ", "", model_name0)
|
1314 |
model_name1 = re.sub(r"### Model B: ", "", model_name1)
|
1315 |
generated_video0, generated_video1 = gen_func(text, model_name0, model_name1)
|
1316 |
+
if generated_video0 == '' and generated_video1 == '':
|
1317 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
1318 |
state0.prompt = text
|
1319 |
state1.prompt = text
|
1320 |
state0.output = generated_video0
|
|
|
1453 |
model_name0 = ""
|
1454 |
model_name1 = ""
|
1455 |
generated_video0, generated_video1, model_name0, model_name1 = gen_func(text, model_name0, model_name1)
|
1456 |
+
if generated_video0 == '' and generated_video1 == '':
|
1457 |
+
raise gr.Warning("Input prompt is blocked by the NSFW filter, please input safe content and try again!")
|
1458 |
state0.prompt = text
|
1459 |
state1.prompt = text
|
1460 |
state0.output = generated_video0
|