DongfuJiang commited on
Commit
dfc5b34
1 Parent(s): d9230ec
Files changed (1) hide show
  1. model/model_manager.py +23 -11
model/model_manager.py CHANGED
@@ -12,13 +12,14 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
12
  import torch
13
 
14
  class ModelManager:
15
- def __init__(self):
16
  self.model_ig_list = IMAGE_GENERATION_MODELS
17
  self.model_ie_list = IMAGE_EDITION_MODELS
18
  self.model_vg_list = VIDEO_GENERATION_MODELS
19
  self.excluding_model_list = MUSEUM_UNSUPPORTED_MODELS
20
  self.desired_model_list = DESIRED_APPEAR_MODEL
21
- self.load_guard()
 
22
  self.loaded_models = {}
23
 
24
  def load_model_pipe(self, model_name):
@@ -29,22 +30,33 @@ class ModelManager:
29
  pipe = self.loaded_models[model_name]
30
  return pipe
31
 
32
- def load_guard(self):
33
  model_id = "meta-llama/Llama-Guard-3-8B"
34
  device = "cuda" if torch.cuda.is_available() else "cpu"
35
  dtype = torch.bfloat16
36
- self.tokenizer = AutoTokenizer.from_pretrained(model_id, token=os.environ['HF_GUARD'])
37
- self.guard = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=dtype, device_map=device, token=os.environ['HF_GUARD'])
 
 
 
 
38
 
39
- @spaces.GPU(duration=30)
40
  def NSFW_filter(self, prompt):
41
  chat = [{"role": "user", "content": prompt}]
42
- input_ids = self.tokenizer.apply_chat_template(chat, return_tensors="pt").to('cuda')
43
  self.guard.cuda()
44
- output = self.guard.generate(input_ids=input_ids, max_new_tokens=100, pad_token_id=0)
45
- prompt_len = input_ids.shape[-1]
46
- result = self.tokenizer.decode(output[0][prompt_len:], skip_special_tokens=True)
47
- return result
 
 
 
 
 
 
 
 
48
 
49
  @spaces.GPU(duration=120)
50
  def generate_image_ig(self, prompt, model_name):
 
12
  import torch
13
 
14
  class ModelManager:
15
+ def __init__(self, enable_nsfw=True):
16
  self.model_ig_list = IMAGE_GENERATION_MODELS
17
  self.model_ie_list = IMAGE_EDITION_MODELS
18
  self.model_vg_list = VIDEO_GENERATION_MODELS
19
  self.excluding_model_list = MUSEUM_UNSUPPORTED_MODELS
20
  self.desired_model_list = DESIRED_APPEAR_MODEL
21
+ self.enable_nsfw = enable_nsfw
22
+ self.load_guard(enable_nsfw)
23
  self.loaded_models = {}
24
 
25
  def load_model_pipe(self, model_name):
 
30
  pipe = self.loaded_models[model_name]
31
  return pipe
32
 
33
+ def load_guard(self, enable_nsfw=True):
34
  model_id = "meta-llama/Llama-Guard-3-8B"
35
  device = "cuda" if torch.cuda.is_available() else "cpu"
36
  dtype = torch.bfloat16
37
+ if enable_nsfw:
38
+ self.guard_tokenizer = AutoTokenizer.from_pretrained(model_id, token=os.environ['HF_GUARD'])
39
+ self.guard = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=dtype, device_map=device, token=os.environ['HF_GUARD'])
40
+ else:
41
+ self.guard_tokenizer = None
42
+ self.guard = None
43
 
 
44
  def NSFW_filter(self, prompt):
45
  chat = [{"role": "user", "content": prompt}]
46
+ input_ids = self.guard_tokenizer.apply_chat_template(chat, return_tensors="pt").to('cuda')
47
  self.guard.cuda()
48
+ if self.guard:
49
+ @spaces.GPU(duration=30)
50
+ def _generate():
51
+ return self.guard.generate(input_ids=input_ids, max_new_tokens=100, pad_token_id=0)
52
+ output = _generate()
53
+ output = self.guard.generate(input_ids=input_ids, max_new_tokens=100, pad_token_id=0)
54
+ prompt_len = input_ids.shape[-1]
55
+ result = self.guard_tokenizer.decode(output[0][prompt_len:], skip_special_tokens=True)
56
+ return result
57
+ else:
58
+ # guard is disabled
59
+ return "safe"
60
 
61
  @spaces.GPU(duration=120)
62
  def generate_image_ig(self, prompt, model_name):