Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,34 +14,40 @@ dtype = torch.bfloat16
|
|
14 |
|
15 |
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
|
16 |
|
17 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, token=huggingface_token)
|
18 |
-
model = AutoModelForCausalLM.from_pretrained(
|
19 |
-
model_id,
|
20 |
-
torch_dtype=dtype,
|
21 |
-
device_map="auto",
|
22 |
-
quantization_config=quantization_config,
|
23 |
-
token=huggingface_token,
|
24 |
-
low_cpu_mem_usage=True
|
25 |
-
)
|
26 |
-
|
27 |
def parse_llama_guard_output(result):
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
if not lines:
|
31 |
-
return "Error", "No valid output",
|
32 |
|
|
|
33 |
safety_status = next((line for line in lines if line in ['safe', 'unsafe']), None)
|
34 |
|
35 |
if safety_status == 'safe':
|
36 |
-
return "Safe", "None",
|
37 |
elif safety_status == 'unsafe':
|
|
|
38 |
violated_categories = next((lines[i+1] for i, line in enumerate(lines) if line == 'unsafe' and i+1 < len(lines)), "Unspecified")
|
39 |
-
return "Unsafe", violated_categories,
|
40 |
else:
|
41 |
-
return "Error", f"Invalid output: {safety_status}",
|
42 |
|
43 |
@spaces.GPU
|
44 |
def moderate(user_input, assistant_response):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
chat = [
|
46 |
{"role": "user", "content": user_input},
|
47 |
{"role": "assistant", "content": assistant_response},
|
@@ -51,12 +57,12 @@ def moderate(user_input, assistant_response):
|
|
51 |
with torch.no_grad():
|
52 |
output = model.generate(
|
53 |
input_ids=input_ids,
|
54 |
-
max_new_tokens=
|
55 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
56 |
)
|
57 |
|
58 |
-
|
59 |
-
result = tokenizer.decode(output[0][prompt_len:], skip_special_tokens=True)
|
60 |
|
61 |
return parse_llama_guard_output(result)
|
62 |
|
|
|
14 |
|
15 |
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def parse_llama_guard_output(result):
|
18 |
+
# "<END CONVERSATION>" 以降の部分を抽出
|
19 |
+
safety_assessment = result.split("<END CONVERSATION>")[-1].strip()
|
20 |
+
|
21 |
+
# 行ごとに分割して処理
|
22 |
+
lines = [line.strip().lower() for line in safety_assessment.split('\n') if line.strip()]
|
23 |
|
24 |
if not lines:
|
25 |
+
return "Error", "No valid output", safety_assessment
|
26 |
|
27 |
+
# "safe" または "unsafe" を探す
|
28 |
safety_status = next((line for line in lines if line in ['safe', 'unsafe']), None)
|
29 |
|
30 |
if safety_status == 'safe':
|
31 |
+
return "Safe", "None", safety_assessment
|
32 |
elif safety_status == 'unsafe':
|
33 |
+
# "unsafe" の次の行を違反カテゴリーとして扱う
|
34 |
violated_categories = next((lines[i+1] for i, line in enumerate(lines) if line == 'unsafe' and i+1 < len(lines)), "Unspecified")
|
35 |
+
return "Unsafe", violated_categories, safety_assessment
|
36 |
else:
|
37 |
+
return "Error", f"Invalid output: {safety_status}", safety_assessment
|
38 |
|
39 |
@spaces.GPU
|
40 |
def moderate(user_input, assistant_response):
|
41 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=huggingface_token)
|
42 |
+
model = AutoModelForCausalLM.from_pretrained(
|
43 |
+
model_id,
|
44 |
+
torch_dtype=dtype,
|
45 |
+
device_map="auto",
|
46 |
+
quantization_config=quantization_config,
|
47 |
+
token=huggingface_token,
|
48 |
+
low_cpu_mem_usage=True
|
49 |
+
)
|
50 |
+
|
51 |
chat = [
|
52 |
{"role": "user", "content": user_input},
|
53 |
{"role": "assistant", "content": assistant_response},
|
|
|
57 |
with torch.no_grad():
|
58 |
output = model.generate(
|
59 |
input_ids=input_ids,
|
60 |
+
max_new_tokens=200,
|
61 |
pad_token_id=tokenizer.eos_token_id,
|
62 |
+
do_sample=False
|
63 |
)
|
64 |
|
65 |
+
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
66 |
|
67 |
return parse_llama_guard_output(result)
|
68 |
|