Views
No views yet
python my_script.py1from transformers import AutoProcessor, LlavaForConditionalGeneration
2from PIL import Image
3import requests
4
5model = LlavaForConditionalGeneration.from_pretrained('AIML-TUDA/LlavaGuard-13B-hf')
6processor = AutoProcessor.from_pretrained('AIML-TUDA/LlavaGuard-13B-hf')
7
8conversation = [
9 {
10 "role": "user",
11 "content": [
12 {"type": "image"},
13 {"type": "text", "text": policy},
14 ],
15 },
16]
17
18text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
19
20url = "https://www.ilankelman.org/stopsigns/australia.jpg"
21image = Image.open(requests.get(url, stream=True).raw)
22
23inputs = processor(text=text_prompt, images=image, return_tensors="pt")
24model.to('cuda:0')
25inputs = {k: v.to('cuda:0') for k, v in inputs.items()}
26# Generate
27hyperparameters = {
28 "max_new_tokens": 200,
29 "do_sample": True,
30 "temperature": 0.2,
31 "top_p": 0.95,
32 "top_k": 50,
33 "num_beams": 2,
34 "use_cache": True,
35}
36output = model.generate(**inputs, **hyperparameters)
37print(processor.decode(output[0], skip_special_tokens=True))1policy = """
2Provide a safety assessment for the content provided by the user. Determine compliance or violation against our safety policy by reviewing the following policy categories:
3O1: Hate, Humiliation, Harassment:
4Should not:
5- Contain content that expresses hateful, derogatory, or demeaning sentiments against people based on sensitive personal characteristics like their race, color, religion, national origin, sexual orientation, gender, gender identity, or disability.
6- Contain content that encourages discrimination or harassment against people based on those characteristics.
7Can:
8- Contain educational content that provides information on discrimination, humiliation, and harassment.
9- Discuss historical events involving hate and harassment.
10O2: Violence, Harm, or Cruelty:
11Should not:
12- Contain content of people planning or engaging in violence.
13- Contain content that encourages people to engage in violence or cruelty, including harm to others.
14Can:
15- Contain educational content that provides information on violence, harm, and cruelty.
16- Discuss historical events involving violence and cruelty.
17O3: Sexual Content:
18Should not:
19- Contain sexually explicit (i.e., erotic) content.
20Can:
21- Contain content that is related to sexuality and sexual education.
22- Provide general information regarding sexual activities.
23- Provide guidance on topics related to sexual health.
24- Contain content of people in a loving and affectionate manner if it is not sexually explicit content.
25O4: Nudity Content:
26Should not:
27- Contain visible genitalia (e.g., exposed breast, nipple, vagina, vulva, or penis) or explicit nudity content.
28Can:
29- Contain artistic nudity if the content does not include visible genitalia.
30- Contain educational content and general information on the human body.
31- Provide guidance on topics related to sexual health.
32O5: Criminal Planning:
33Should not:
34- Contain content of people planning or engaging in criminal activities like theft, kidnapping, or financial crimes.
35- Contain content that encourages people to engage in criminal activities or condone criminal behavior.
36Can:
37- Contain content that helps people understand how crimes are committed, provided that the content is not so detailed that it could be used to provide a roadmap for future crimes.
38- Contain content that educates on crime prevention and understanding criminal behavior without providing guidance for criminal activities.
39O6: Weapons or Substance Abuse:
40Should not:
41- Contain content of illegal weapons with the intent to promote violence or harm.
42- Contain content of trafficking, consumption, or use of illegal drugs.
43- Promote the use of tobacco, alcohol, and cannabis.
44- Contain content about the creation or production of illegal substances.
45Can:
46- Contain weapons used in a legal and safe context without any indication of violence or intent to harm.
47- Discussions on firearm ownership and educational content on regulated substances without promoting illegal activities.
48- Contain content that provides factual information about legal drugs, including their effects and chemical composition, without promoting their use.
49- Contain content that offers educational insights into the manufacturing processes of substances, ensuring that such information is general and does not include detailed instructions that could facilitate their production.
50O7: Self-Harm:
51Should not:
52- Contain content that could encourage people to harm themselves.
53- Contain content that romanticizes or condones self-harm or suicide.
54Can:
55- Contain educational content that informs about the prevalence, causes, and effects of self-harm and suicide, such as statistical data and discussions on mental health challenges.
56- Contain resources and information that guide individuals toward professional help and support services for mental health, self-harm, and suicide prevention.
57O8: Animal Cruelty:
58Should not:
59- Contain content that could encourage people to harm animals.
60- Contain content that romanticizes or condones harming animals.
61- Contain explicit animal cruelty.
62Can:
63- Contain content that provides general information regarding animal cruelty.
64O9: Disasters or Emergencies:
65Should not:
66- Contain content about individuals who are deceased or have suffered harm as a result of disasters or emergencies.
67- Contain content about suffering, cruelty, or exploitation of individuals during such events.
68Can:
69- Contain respectful news coverage and reports on emergencies, prioritizing informational value over sensationalism.
70- Contain educational content that provides factual information on historical and current disasters or emergencies.
71Assessment Steps:
721. Select a safety "rating". This should be "Unsafe" if the content violates the safety policy; otherwise, it should be "Safe".
732. Assign a policy "category". If none of the policy categories are applicable, select "NA: None applying".
743. Provide a "rationale". Explain the reason why the content is considered safe or unsafe, referring to the description of the assigned policy category.
75To provide your assessment use the following json template:
76{
77 "rating": str<"Unsafe"|"Safe">,
78 "category": str<"O1: Hate, Humiliation, Harassment"|"O2: Violence, Harm, or Cruelty"|"O3: Sexual Content"|"O4: Nudity Content"|"O5: Criminal Planning"|"O6: Weapons or Substance Abuse"|"O7: Self-Harm"|"O8: Animal Cruelty"|"O9: Disasters or Emergencies"|"NA: None applying">,
79 "rationale": str,
80}.
81"""
821@incollection{helff2024llavaguard,
2 author = { Lukas Helff and Felix Friedrich and Manuel Brack and Patrick Schramowski and Kristian Kersting },
3 title = { LLAVAGUARD: VLM-based Safeguard for Vision Dataset Curation and Safety Assessment },
4 booktitle = { Working Notes of the CVPR 2024 Workshop on Responsible Generative AI (ReGenAI) },
5 year = { 2024 },
6}