Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from transformers import AutoProcessor, Llama4ForConditionalGeneration
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
import torch
|
6 |
+
import gradio as gr
|
7 |
+
import re
|
8 |
+
|
9 |
+
model_id = "meta-llama/Llama-Guard-4-12B"
|
10 |
+
|
11 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
12 |
+
model = Llama4ForConditionalGeneration.from_pretrained(
|
13 |
+
model_id,
|
14 |
+
device_map="cuda:0",
|
15 |
+
torch_dtype=torch.bfloat16,
|
16 |
+
)
|
17 |
+
|
18 |
+
labels_dict = {
|
19 |
+
"S1": "Violent Crimes",
|
20 |
+
"S2": "Non-Violent Crimes",
|
21 |
+
"S3": "Sex Crimes",
|
22 |
+
"S4": "Child Exploitation",
|
23 |
+
"S5": "Defamation",
|
24 |
+
"S6": "Specialized Advice",
|
25 |
+
"S7": "Privacy",
|
26 |
+
"S8": "Intellectual Property",
|
27 |
+
"S9": "Indiscriminate Weapons",
|
28 |
+
"S10": "Hate",
|
29 |
+
"S11": "Self-Harm",
|
30 |
+
"S12": "Sexual Content",
|
31 |
+
"S13": "Elections",
|
32 |
+
}
|
33 |
+
|
34 |
+
def infer(image, text_input, model_output, exclude_categories):
|
35 |
+
if image is None and text_input:
|
36 |
+
messages = [
|
37 |
+
{
|
38 |
+
"role": "user",
|
39 |
+
"content": [{"type": "text", "text": text_input}]
|
40 |
+
},
|
41 |
+
]
|
42 |
+
elif image is not None and text_input:
|
43 |
+
messages = [
|
44 |
+
{
|
45 |
+
"role": "user",
|
46 |
+
"content": [{"type": "text", "text": text_input}]
|
47 |
+
}
|
48 |
+
]
|
49 |
+
messages[0]["content"].append({"type": "image", "url": image})
|
50 |
+
else:
|
51 |
+
return "Please provide at least text input."
|
52 |
+
|
53 |
+
if model_output:
|
54 |
+
messages.append(
|
55 |
+
{
|
56 |
+
"role": "assistant",
|
57 |
+
"content": [{"type": "text", "text": model_output}]
|
58 |
+
}
|
59 |
+
)
|
60 |
+
print("messages", messages )
|
61 |
+
inputs = processor.apply_chat_template(
|
62 |
+
messages,
|
63 |
+
add_generation_prompt=True,
|
64 |
+
tokenize=True,
|
65 |
+
return_dict=True,
|
66 |
+
return_tensors="pt",
|
67 |
+
exclude_category_keys=exclude_categories,
|
68 |
+
).to(model.device)
|
69 |
+
outputs = model.generate(
|
70 |
+
**inputs,
|
71 |
+
max_new_tokens=100,
|
72 |
+
do_sample=False,
|
73 |
+
)
|
74 |
+
|
75 |
+
response = processor.batch_decode(outputs[:, inputs["input_ids"].shape[-1]:])[0]
|
76 |
+
|
77 |
+
if "unsafe" in response:
|
78 |
+
match = re.search(r'S(\d+)', response)
|
79 |
+
if match:
|
80 |
+
s_number = f"S{match.group(1)}"
|
81 |
+
category = labels_dict.get(s_number, "Unknown Category")
|
82 |
+
response = f"This content is unsafe under category {category}"
|
83 |
+
if "safe<|eot|>" in response:
|
84 |
+
response = "This conversation is safe."
|
85 |
+
return messages, response
|
86 |
+
|
87 |
+
with gr.Blocks() as demo:
|
88 |
+
gr.Markdown("## Llama Guard 4 for Multimodal Safety")
|
89 |
+
gr.Markdown(
|
90 |
+
"Llama Guard 4 is a safety moderation model for both large language and vision language models. "
|
91 |
+
"It can detect unsafe images and text inputs. To use it, simply input text or images along with text."
|
92 |
+
"You can also provide hypothetical LLM outputs separately, as this model can take in entire conversation."
|
93 |
+
)
|
94 |
+
|
95 |
+
with gr.Column():
|
96 |
+
image = gr.Image(label="Image Input (Optional)", type="filepath")
|
97 |
+
text_input = gr.Textbox(label="Text Input")
|
98 |
+
model_output = gr.Textbox(label="Model Output")
|
99 |
+
|
100 |
+
with gr.Accordion("Exclude Safety Categories", open=False):
|
101 |
+
exclude_categories = gr.CheckboxGroup(
|
102 |
+
choices=[k for k in labels_dict.keys()],
|
103 |
+
label="Select categories to EXCLUDE from moderation",
|
104 |
+
info="Selected categories will NOT be flagged."
|
105 |
+
)
|
106 |
+
|
107 |
+
btn = gr.Button("Submit")
|
108 |
+
complete_conversation = gr.Textbox(label="Complete Conversation Provided by User")
|
109 |
+
outputs = [complete_conversation, gr.Textbox(label="Safety Category")]
|
110 |
+
|
111 |
+
btn.click(
|
112 |
+
fn=infer,
|
113 |
+
inputs=[image, text_input, model_output, exclude_categories],
|
114 |
+
outputs=outputs
|
115 |
+
)
|
116 |
+
|
117 |
+
gr.Examples(
|
118 |
+
examples=[[None, "How to make a bomb?", "", []], ["./fruit_knife.png", "How to use this?", "", []], [None, "How to make a bomb?", "Sorry I can't respond to this.", ["S1"]]],
|
119 |
+
inputs=[image, text_input, model_output, exclude_categories],
|
120 |
+
outputs=outputs
|
121 |
+
)
|
122 |
+
|
123 |
+
demo.launch()
|