Spaces:
Running
on
L4
Running
on
L4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
|
4 |
+
from peft import PeftConfig, PeftModel
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
+
|
7 |
+
base_model_name = "google/gemma-7b"
|
8 |
+
#adapter_model_name = "samidh/cope-g2b-2c-hs-skr-s1.5.9-sx-sk-s5.d25"
|
9 |
+
#adapter_model_name = "samidh/cope-g2b-2c-hs-skr-s1.5.9-sx-sk-s1.5.l1e4-e10-d25"
|
10 |
+
#adapter_model_name = "samidh/cope-g2b-2c-hs-s1.f5.9.l5e5-e10-d25-r8"
|
11 |
+
#adapter_model_name = "samidh/cope-g2b-2c-hs.s1.5.9-sx.s1.5.9o-hr.s5-sh.s5.l1e4-e10-d25-r8"
|
12 |
+
#adapter_model_name = "samidh/cope-ap-g2b-2c-hs.s1.5.9-sx.s1.5.9o-vl.s1.5.9-hr.s5-sh.s5.l1e4-e5-d25-r8"
|
13 |
+
#adapter_model_name = "samidh/cope-g2b-2c-hs.s1.5pcf.9.l5e5-e10-d25-r8"
|
14 |
+
#adapter_model_name = "samidh/cope-g7bq-2c-hs.s1.5fpc.9-sx.s1.5.9o-VL.s1.5.9-HR.s5-SH.s5-l1e4-e5-d25-r8"
|
15 |
+
adapter_model_name = "samidh/cope-g7bq-2c-hs.s1.5fpc.9-sx.s1.5.9o-VL.s1.5.9-HR.s5-SH.s5-l5e5-e3-d25-r8"
|
16 |
+
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(base_model_name, token=os.environ['HF_TOKEN'])
|
18 |
+
model = PeftModel.from_pretrained(model, adapter_model_name, token=os.environ['HF_TOKEN'])
|
19 |
+
model.merge_and_unload()
|
20 |
+
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
22 |
+
|
23 |
+
#inputs = tokenizer.encode("This movie was really", return_tensors="pt")
|
24 |
+
|
25 |
+
#outputs = model.generate(inputs, max_new_tokens=1)
|
26 |
+
#print(tokenizer.decode(outputs[0], skip_special_tokens=True)[-1])
|
27 |
+
|
28 |
+
#gr.load("models/samidh/cope-gemma2b-hs-2c-skr-s1.5.9.d25", hf_token=os.environ['HF_TOKEN']).launch()
|
29 |
+
|
30 |
+
PROMPT = """
|
31 |
+
INSTRUCTIONS
|
32 |
+
============
|
33 |
+
Examine the given POLICY and determine if the given CONTENT meets the criteria for ANY of the LABELS. Answer "1" if yes, and "0" if no.
|
34 |
+
|
35 |
+
POLICY
|
36 |
+
======
|
37 |
+
|
38 |
+
{policy}
|
39 |
+
|
40 |
+
|
41 |
+
CONTENT
|
42 |
+
=======
|
43 |
+
|
44 |
+
{content}
|
45 |
+
|
46 |
+
|
47 |
+
ANSWER
|
48 |
+
======
|
49 |
+
|
50 |
+
"""
|
51 |
+
|
52 |
+
DEFAULT_POLICY = """# Policy
|
53 |
+
## Overview
|
54 |
+
This policy guides the classification of content related to expressions that may be sensitive or controversial, aiming to foster a community where open dialogue is encouraged while maintaining respect for all individuals.
|
55 |
+
## Definition of Terms
|
56 |
+
- Protected Classes: Individuals or groups of individuals defined by sharing a race, ethnicity, religion, disability, sex, gender identity, or sexual orientation. The scope is narrowed to focus on characteristics commonly recognized in anti-discrimination laws. Religions themselves, as concepts ARE NOT protected - only their members are.
|
57 |
+
- Non-Protected Classes: Groups defined by other characteristics, such as age, socioeconomic status, and education, which are typically excluded partially or entirely from anti-discrimination laws.
|
58 |
+
## Interpretation of Language
|
59 |
+
## Definition of Labels
|
60 |
+
### (HS): Hate Speech
|
61 |
+
#### Includes
|
62 |
+
- Explicit Violence Advocacy: Direct and clear threats of or praise for violence or harm against individuals or groups based on their membership in a particular protected class. Stating an intention to defend against aggression DOES NOT qualify
|
63 |
+
- Intentional Dehumanization: Statements that clearly depict individuals or groups as inherently ""other"", alien, animalistic, unintelligent, immoral, unclean, or less-than-fully-human based on their membership in a particular protected class in a way that justifies harm or discrimination.
|
64 |
+
- Targeted Use of Derogatory Slurs: Targeting another person or group of people using a one-word name for a particular protected class that has an inherent negative connotation (e.g. Nigger, Kike, Cunt, Retard). Multi-word terms are never slurs.
|
65 |
+
- Explicit Discrimination Advocacy: Direct and clear calls for exclusion, segregation, or discrimination against individuals or groups based on their membership in a particular protected class, with a clear intent to promote inequality.
|
66 |
+
- Direct Hateful Insults: Content that directly addresses another person or group of people the second person (e.g. ""You over there"") and insults them based on their membership in a particular protected class
|
67 |
+
#### Excludes
|
68 |
+
- Artistic and Educational Content: Expressions intended for artistic, educational, or documentary purposes that discuss sensitive topics but do not advocate for violence or discrimination against individuals or groups based on their membership in a particular protected class.
|
69 |
+
- Political and Social Commentary: Commentary on political issues, social issues, and political ideologies that does not directly incite violence or discrimination against individuals or groups based on their membership in a particular protected class.
|
70 |
+
- Rebutting Hateful Language: Content that rebuts, condemns, questions, criticizes, or mocks a different person's hateful language or ideas OR that insults the person advocating those hateful
|
71 |
+
- Quoting Hateful Language: Content in which the author quotes someone else's hateful language or ideas while discussing, explaining, or neutrally factually presenting those ideas.
|
72 |
+
- Describing Sectarian Violence: Content that describes, but does not endorse or praise, violent physical injury against a specifically named race, ethnicity, nationality, sexual orientation, or religious community by another specifically named race, ethnicity, nationality, sexual orientation, or religious community
|
73 |
+
"""
|
74 |
+
|
75 |
+
DEFAULT_CONTENT = "LLMs steal our jobs."
|
76 |
+
|
77 |
+
# Function to make predictions
|
78 |
+
def predict(content, policy):
|
79 |
+
input_text = PROMPT.format(policy=policy, content=content)
|
80 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
81 |
+
outputs = model.generate(inputs, max_new_tokens=1)
|
82 |
+
decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
83 |
+
if int(decoded_output[-1]) == 0:
|
84 |
+
return f'NON-Violating ({decoded_output[-1]})'
|
85 |
+
else:
|
86 |
+
return f'VIOLATING ({decoded_output[-1]})'
|
87 |
+
|
88 |
+
# Create Gradio interface
|
89 |
+
iface = gr.Interface(
|
90 |
+
fn=predict,
|
91 |
+
inputs=[gr.Textbox(label="Content", lines=2, value=DEFAULT_CONTENT),
|
92 |
+
gr.Textbox(label="Policy", lines=10, value=DEFAULT_POLICY)],
|
93 |
+
outputs="label",
|
94 |
+
title="CoPE Alpha Preview",
|
95 |
+
description="See if the given content violates your given policy."
|
96 |
+
)
|
97 |
+
|
98 |
+
# Launch the app
|
99 |
+
iface.launch()
|