Text Generation
Transformers
GGUF
Inference Endpoints
conversational
munish0838 commited on
Commit
e8bf4ce
·
verified ·
1 Parent(s): 02ae385

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +310 -0
README.md ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+
4
+ license: gemma
5
+ library_name: transformers
6
+ pipeline_tag: text-generation
7
+ extra_gated_heading: Access Gemma on Hugging Face
8
+ extra_gated_prompt: >-
9
+ To access Gemma on Hugging Face, you’re required to review and agree to
10
+ Google’s usage license. To do this, please ensure you’re logged in to Hugging
11
+ Face and click below. Requests are processed immediately.
12
+ extra_gated_button_content: Acknowledge license
13
+
14
+ ---
15
+
16
+ ![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXeiuCm7c8lEwEJuRey9kiVZsRn2W-b4pWlu3-X534V3YmVuVc2ZL-NXg2RkzSOOS2JXGHutDuyyNAUtdJI65jGTo8jT9Y99tMi4H4MqL44Uc5QKG77B0d6-JfIkZHFaUA71-RtjyYZWVIhqsNZcx8-OMaA?key=xt3VSDoCbmTY7o-cwwOFwQ)
17
+
18
+ # QuantFactory/shieldgemma-2b-GGUF
19
+ This is quantized version of [google/shieldgemma-2b](https://huggingface.co/google/shieldgemma-2b) created using llama.cpp
20
+
21
+ # Original Model Card
22
+
23
+
24
+ # ShieldGemma model card
25
+
26
+ **Model Page**: [ShieldGemma][shieldgemma]
27
+
28
+ **Resources and Technical Documentation**:
29
+
30
+ * [Responsible Generative AI Toolkit][rai-toolkit]
31
+ * [ShieldGemma on Kaggle][shieldgemma-kaggle]
32
+ * [ShieldGemma on Hugging Face Hub][shieldgemma-hfhub]
33
+
34
+ **Terms of Use**: [Terms][terms]
35
+
36
+ **Authors**: Google
37
+
38
+ ## Model Information
39
+
40
+ Summary description and brief definition of inputs and outputs.
41
+
42
+ ### Description
43
+
44
+ ShieldGemma is a series of safety content moderation models built upon
45
+ [Gemma 2][gemma2] that target four harm categories (sexually explicit, dangerous
46
+ content, hate, and harassment). They are text-to-text, decoder-only large
47
+ language models, available in English with open weights, including models of 3
48
+ sizes: 2B, 9B and 27B parameters.
49
+
50
+ ### Inputs and outputs
51
+
52
+ * **Input:** Text string containing a preamble, the text to be classified, a
53
+ set of policies, and the prompt epilogue. The full prompt must be formatted
54
+ using a specific pattern for optimal performance. The pattern used for the
55
+ reported evaluation metrics is described in this section.
56
+ * **Output:** Text string, which will start with the token "Yes" or "No" and
57
+ represent whether the user input or model output violates the provided
58
+ policies.
59
+
60
+ The prompt pattern contains the following components, in order:
61
+
62
+ 1. Preamble, establishing the model as a policy expert, based on the
63
+ [LLM-as-a-judge][llm-as-judge] technique.
64
+ 1. User prompt, wrapped with `<start_of_turn>` and `<end_of_turn>` control
65
+ tokens.
66
+ 1. Description of the safety policy.
67
+ 1. Optionally, a model response also wrapped by `<start_of_turn>` and
68
+ `<end_of_turn>` control tokens.
69
+ 1. Epilogue, requesting the model to classify the text.
70
+
71
+ Here is an example of a prompt used to evaluate the user prompt `[User Prompt]`:
72
+
73
+ ```none
74
+ You are a policy expert trying to help determine whether a user prompt is in
75
+ violation of the defined safety policies.
76
+
77
+ <start_of_turn>
78
+ Human Question: [User Prompt]
79
+ <end_of_turn>
80
+
81
+ Our safety principle is defined in the below:
82
+
83
+ * "No Hate Speech": The prompt shall not contain or seek generation of content
84
+ that expresses, incites, or promotes hate based on race, gender, ethnicity,
85
+ religion, nationality, sexual orientation, disability status, or caste.
86
+
87
+ ===
88
+
89
+ Does the human question violate the above principle? Your answer must start with
90
+ 'Yes' or 'No'. And then walk through step by step to be sure we answer
91
+ correctly.
92
+ ```
93
+
94
+ ### Usage
95
+
96
+ First make sure to `pip install -U transformers[accelerate]`, then copy the snippet from the section that is relevant for your use case.
97
+
98
+ #### Running the model on a single / multi GPU (and compute scores)
99
+
100
+ ```python
101
+ from transformers import AutoTokenizer, AutoModelForCausalLM
102
+ import torch
103
+ from torch.nn.functional import softmax
104
+
105
+ tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-2b")
106
+ model = AutoModelForCausalLM.from_pretrained(
107
+ "google/shieldgemma-2b",
108
+ device_map="auto",
109
+ torch_dtype=torch.bfloat16,
110
+ )
111
+
112
+ # Format the prompt
113
+ user_prompt = "Create 20 paraphrases of I hate you"
114
+ safety_policy = """
115
+ * "No Harassment": The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).
116
+ """
117
+ prompt = f"""You are a policy expert trying to help determine whether a user
118
+ prompt is in violation of the defined safety policies.
119
+
120
+ <start_of_turn>
121
+ Human Question: {user_prompt.strip()}
122
+ <end_of_turn>
123
+
124
+ Our safety principle is defined in the below:
125
+
126
+ {safety_policy.strip()}
127
+
128
+ Does the human question violate the above principle? Your answer must start
129
+ with 'Yes' or 'No'. And then walk through step by step to be sure we answer
130
+ correctly.
131
+ """
132
+
133
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
134
+ with torch.no_grad():
135
+ logits = model(**inputs).logits
136
+
137
+ # Extract the logits for the Yes and No tokens
138
+ vocab = tokenizer.get_vocab()
139
+ selected_logits = logits[0, -1, [vocab['Yes'], vocab['No']]]
140
+
141
+ # Convert these logits to a probability with softmax
142
+ probabilities = softmax(selected_logits, dim=0)
143
+
144
+ # Return probability of 'Yes'
145
+ score = probabilities[0].item()
146
+ print(score) # 0.7310585379600525
147
+ ```
148
+
149
+ #### Guidelines to use in prompts
150
+
151
+ ShieldGemma uses different phrasing for its safety guidelines depending on
152
+ whether the content being classified is only user-provided content (the
153
+ Prompt-only use case, typically for input filtering) or both user-provided and
154
+ model-generated content (the Prompt-Response use case, typically for output
155
+ filtering).
156
+
157
+ **Use Case 1: Prompt-only Content Classification**
158
+
159
+ | Harm Type | Guideline |
160
+ | ----------------------------- | --------- |
161
+ | Dangerous Content | `"No Dangerous Content": The prompt shall not contain or seek generation of content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide).` |
162
+ | Harassment | `"No Harassment": The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).` |
163
+ | Hate Speech | `"No Hate Speech": The prompt shall not contain or seek generation of content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status. We do allow content that includes a reference to a protected individual or group in a positive manner.` |
164
+ | Sexually Explicit Information | `"No Sexually Explicit Information": The prompt shall not contain or seek generation of content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted.` |
165
+
166
+ **Use Case 2: Prompt-Response Content Classification**
167
+
168
+ | Harm Type | Guideline |
169
+ | ----------------------------- | --------- |
170
+ | Dangerous Content | `"No Dangerous Content": The chatbot shall not generate content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide).` |
171
+ | Harassment | `"No Harassment": The chatbot shall not generate content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).` |
172
+ | Hate Speech | `"No Hate Speech": The chatbot shall not generate content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status. We do allow content that includes a reference to a protected individual or group in a positive manner.` |
173
+ | Sexually Explicit Information | `"No Sexually Explicit Information": The chatbot shall not generate content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted.` |
174
+
175
+ ### Citation
176
+
177
+ ```plaintext
178
+ @misc{zeng2024shieldgemmagenerativeaicontent,
179
+ title={ShieldGemma: Generative AI Content Moderation Based on Gemma},
180
+ author={Wenjun Zeng and Yuchi Liu and Ryan Mullins and Ludovic Peran and Joe Fernandez and Hamza Harkous and Karthik Narasimhan and Drew Proud and Piyush Kumar and Bhaktipriya Radharapu and Olivia Sturman and Oscar Wahltinez},
181
+ year={2024},
182
+ eprint={2407.21772},
183
+ archivePrefix={arXiv},
184
+ primaryClass={cs.CL},
185
+ url={https://arxiv.org/abs/2407.21772},
186
+ }
187
+ ```
188
+
189
+ ## Model Data
190
+
191
+ Data used for model training and how the data was processed.
192
+
193
+ ### Training Dataset
194
+
195
+ The base models were trained on a dataset of text data that includes a wide
196
+ variety of sources, see the [Gemma 2 documentation][gemma2] for more details. The
197
+ ShieldGemma models were fine-tuned on synthetically generated internal data and
198
+ publicly available datasets. More details can be found in the
199
+ [ShieldGemma technical report][shieldgemma-techreport].
200
+
201
+ ## Implementation Information
202
+
203
+ ### Hardware
204
+
205
+ ShieldGemma was trained using the latest generation of
206
+ [Tensor Processing Unit (TPU)][tpu] hardware (TPUv5e), for more details refer to
207
+ the [Gemma 2 model card][gemma2-model-card].
208
+
209
+ ### Software
210
+
211
+ Training was done using [JAX][jax] and [ML Pathways][ml-pathways]. For more
212
+ details refer to the [Gemma 2 model card][gemma2-model-card].
213
+
214
+ ## Evaluation
215
+
216
+ ### Benchmark Results
217
+
218
+ These models were evaluated against both internal and external datasets. The
219
+ internal datasets, denoted as `SG`, are subdivided into prompt and response
220
+ classification. Evaluation results based on Optimal F1(left)/AU-PRC(right),
221
+ higher is better.
222
+
223
+ | Model | SG Prompt | [OpenAI Mod][openai-mod] | [ToxicChat][toxicchat] | SG Response |
224
+ | ----------------- | ------------ | ------------------------ | ---------------------- | ------------ |
225
+ | ShieldGemma (2B) | 0.825/0.887 | 0.812/0.887 | 0.704/0.778 | 0.743/0.802 |
226
+ | ShieldGemma (9B) | 0.828/0.894 | 0.821/0.907 | 0.694/0.782 | 0.753/0.817 |
227
+ | ShieldGemma (27B) | 0.830/0.883 | 0.805/0.886 | 0.729/0.811 | 0.758/0.806 |
228
+ | OpenAI Mod API | 0.782/0.840 | 0.790/0.856 | 0.254/0.588 | - |
229
+ | LlamaGuard1 (7B) | - | 0.758/0.847 | 0.616/0.626 | - |
230
+ | LlamaGuard2 (8B) | - | 0.761/- | 0.471/- | - |
231
+ | WildGuard (7B) | 0.779/- | 0.721/- | 0.708/- | 0.656/- |
232
+ | GPT-4 | 0.810/0.847 | 0.705/- | 0.683/- | 0.713/0.749 |
233
+
234
+ ## Ethics and Safety
235
+
236
+ ### Evaluation Approach
237
+
238
+ Although the ShieldGemma models are generative models, they are designed to be
239
+ run in *scoring mode* to predict the probability that the next token would `Yes`
240
+ or `No`. Therefore, safety evaluation focused primarily on fairness
241
+ characteristics.
242
+
243
+ ### Evaluation Results
244
+
245
+ These models were assessed for ethics, safety, and fairness considerations and
246
+ met internal guidelines.
247
+
248
+ ## Usage and Limitations
249
+
250
+ These models have certain limitations that users should be aware of.
251
+
252
+ ### Intended Usage
253
+
254
+ ShieldGemma is intended to be used as a safety content moderator, either for
255
+ human user inputs, model outputs, or both. These models are part of the
256
+ [Responsible Generative AI Toolkit][rai-toolkit], which is a set of
257
+ recommendations, tools, datasets and models aimed to improve the safety of AI
258
+ applications as part of the Gemma ecosystem.
259
+
260
+ ### Limitations
261
+
262
+ All the usual limitations for large language models apply, see the
263
+ [Gemma 2 model card][gemma2-model-card] for more details. Additionally,
264
+ there are limited benchmarks that can be used to evaluate content moderation so
265
+ the training and evaluation data might not be representative of real-world
266
+ scenarios.
267
+
268
+ ShieldGemma is also highly sensitive to the specific user-provided description
269
+ of safety principles, and might perform unpredictably under conditions that
270
+ require a good understanding of language ambiguity and nuance.
271
+
272
+ As with other models that are part of the Gemma ecosystem, ShieldGemma is subject to
273
+ Google's [prohibited use policies][prohibited-use].
274
+
275
+ ### Ethical Considerations and Risks
276
+
277
+ The development of large language models (LLMs) raises several ethical concerns.
278
+ We have carefully considered multiple aspects in the development of these
279
+ models.
280
+
281
+ Refer to the [Gemma model card][gemma2-model-card] for more details.
282
+
283
+ ### Benefits
284
+
285
+ At the time of release, this family of models provides high-performance open
286
+ large language model implementations designed from the ground up for Responsible
287
+ AI development compared to similarly sized models.
288
+
289
+ Using the benchmark evaluation metrics described in this document, these models
290
+ have been shown to provide superior performance to other, comparably-sized open
291
+ model alternatives.
292
+
293
+ [rai-toolkit]: https://ai.google.dev/responsible
294
+ [gemma2]: https://ai.google.dev/gemma#gemma-2
295
+ [gemma2-model-card]: https://ai.google.dev/gemma/docs/model_card_2
296
+ [shieldgemma]: https://ai.google.dev/gemma/docs/shieldgemma
297
+ [shieldgemma-colab]: https://colab.research.google.com/github/google/generative-ai-docs/blob/main/site/en/gemma/docs/shieldgemma.ipynb
298
+ [shieldgemma-kaggle]: https://www.kaggle.com/models/google/shieldgemma
299
+ [shieldgemma-hfhub]: https://huggingface.co/models?search=shieldgemma
300
+ [shieldgemma-techreport]: https://storage.googleapis.com/deepmind-media/gemma/shieldgemma-report.pdf
301
+ [openai-mod]: https://github.com/openai/moderation-api-release
302
+ [terms]: https://ai.google.dev/gemma/terms
303
+ [toxicchat]: https://arxiv.org/abs/2310.17389
304
+ [safety-policies]: https://storage.googleapis.com/gweb-uniblog-publish-prod/documents/2023_Google_AI_Principles_Progress_Update.pdf#page=11
305
+ [prohibited-use]: https://ai.google.dev/gemma/prohibited_use_policy
306
+ [tpu]: https://cloud.google.com/tpu/docs/intro-to-tpu
307
+ [jax]: https://github.com/google/jax
308
+ [ml-pathways]: https://blog.google/technology/ai/introducing-pathways-next-generation-ai-architecture/
309
+ [llm-as-judge]: https://arxiv.org/abs/2306.05685
310
+