File size: 7,670 Bytes
44663cf
 
 
 
 
 
 
 
 
 
 
0f38a56
44663cf
0f38a56
 
44663cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2423be8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44663cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177651b
44663cf
 
 
 
 
 
 
 
8029781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44663cf
8029781
44663cf
8029781
44663cf
df8105a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import streamlit as st
import pandas as pd
import numpy as np
import re
import string
import textwrap
from transformers import BertTokenizer, BertForSequenceClassification, AutoModelForCausalLM, AutoTokenizer, pipeline, AdamW
from happytransformer import HappyTextToText, TTSettings
import torch
from torch.nn import BCEWithLogitsLoss
from torch.utils.data import DataLoader, TensorDataset, random_split
from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("saurabhg2083/model_bert")
model = AutoModelForSequenceClassification.from_pretrained("saurabhg2083/model_bert")
happy_tt = HappyTextToText("T5", "vennify/t5-base-grammar-correction")
args = TTSettings(num_beams=5, min_length=1)

gendered_pronouns = [
          'ambition', 'driven', 'lead', 'persist', 'principle', 'decision', 'superior', 'individual', 'assertive',
          'strong', 'hierarchical', 'rigid', 'silicon valley', 'stock options', 'takes risk', 'workforce', 'autonomous',
          'ping pong', 'pool table', 'must', 'competitive', 'he', 'his', 'himself', 'confident', 'active', 'aggressive',
          'ambitious', 'fearless', 'headstrong', 'defensive', 'independent', 'dominant', 'outspoken', 'leader', 'fast paced',
          'adventurous', 'analytical', 'decisive', 'determined', 'ninja', 'objective', 'rock star', 'boast', 'challenging', 'courage',
          'thoughtful', 'creative', 'adaptable', 'choose', 'curious', 'excellent', 'flexible', 'multitasking', 'health',
          'imaginative', 'intuitive', 'leans in', 'plans for the future', 'resilient', 'self-aware', 'socially responsible',
          'trustworthy', 'shup-to-date', 'wellness program', 'nurture', 'teach', 'dependable', 'community', 'serving', 'loyal',
          'enthusiasm', 'interpersonal', 'connect', 'commit', 'she', 'agree', 'empathy', 'sensitive', 'affectionate', 'feel',
          'support', 'collaborate', 'honest', 'trust', 'understand', 'compassion', 'share', 'polite', 'kind', 'caring', 'her',
          'hers', 'herself', 'feminine', 'cheer', 'communal', 'emotional', 'flatterable', 'gentle', 'interdependent', 'kinship',
          'modesty', 'pleasant', 'polite', 'quiet', 'sympathy', 'warm', 'dominant', 'yield',
          'native english speaker', 'professionally groomed hair', 'native', 'culture fit', 'non-white', 'clean-shaven',
          'neat hairstyle', 'master', 'slave', 'a cakewalk', 'brownbag session', 'spirit animal', 'digital native',
          'servant leadership', 'tribe', 'oriental', 'spic', 'english fluency', 'level native', 'illegals', 'eskimo',
          'latino', 'latina', 'migrant', 'blacklist', 'whitelist'
      ]

# List of neutral words
neutral_words = [
    "drive",
    "motivated",
    "guide",
    "continue",
    "ethic",
    "choice",
    "excellent",
    "person",
    "confident",
    "resilient",
    "structured",
    "inflexible",
    "tech industry",
    "equity options",
    "is adventurous",
    "employees",
    "independent",
    "table tennis",
    "billiards table",
    "should",
    "challenging",
    "they",
    "their",
    "themselves",
    "self-assured",
    "energetic",
    "assertive",
    "aspiring",
    "courageous",
    "determined",
    "protective",
    "self-reliant",
    "influential",
    "expressive",
    "guiding force",
    "high-speed",
    "daring",
    "logical",
    "resolute",
    "committed",
    "expert",
    "impartial",
    "outstanding performer",
    "brag",
    "demanding",
    "bravery",
    "considerate",
    "innovative",
    "flexible",
    "select",
    "inquisitive",
    "outstanding",
    "adaptable",
    "handling multiple tasks",
    "well-being",
    "creative",
    "instinctive",
    "long-term planning",
    "tough",
    "aware of oneself",
    "ethical",
    "reliable",
    "current",
    "health program",
    "foster",
    "instruct",
    "reliable",
    "society",
    "assisting",
    "devoted",
    "passion",
    "relational",
    "link",
    "dedicate",
    "they",
    "concur",
    "understanding",
    "responsive",
    "loving",
    "experience",
    "assist",
    "work together",
    "truthful",
    "confidence",
    "comprehend",
    "sympathy",
    "contribute",
    "courteous",
    "considerate",
    "supportive",
    "their",
    "theirs",
    "themselves",
    "androgynous",
    "encourage",
    "collective",
    "expressive",
    "complimentable",
    "tender",
    "mutual",
    "connection",
    "humility",
    "agreeable",
    "silent",
    "empathy",
    "friendly",
    "leading",
    "produce",
    "fluent English speaker",
    "well-groomed appearance",
    "indigenous",
    "cultural alignment",
    "diverse",
    "clean-cut",
    "tidy hair",
    "expert",
    "subordinate",
    "easy task",
    "informal meeting",
    "personal inspiration",
    "tech-savvy",
    "supportive leadership",
    "community",
    "eastern",
    "avoid using",
    "english proficiency",
    "fluent",
    "unauthorized individuals",
    "indigenous Northern people",
    "hispanic",
    "latinx",
    "mobile worker",
    "inclusion list",
]



def replace_gendered_pronouns(text):
    # Define a dictionary of gendered pronouns and their gender-neutral replacements
    word_dict = dict(zip(gendered_pronouns, neutral_words))

    # Use regular expressions to find and replace gendered pronouns in the text
    for pronoun, replacement in word_dict.items():
        # Use word boundaries to match whole words only
        pattern = r'\b' + re.escape(pronoun) + r'\b'
        text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)

    return text

def model_eval(text):
    # Put the model in evaluation mode
    model.eval()

    # Input text
    input_text = text

    # Tokenize the input text
    inputs = tokenizer(input_text, padding='max_length', truncation=True, max_length=512, return_tensors="pt")

    # Make the prediction
    with torch.no_grad():
        outputs = model(**inputs)

    logits = outputs.logits
    predicted_label = (logits > 0).int().item()
    
    return predicted_label
    

st.title("Job Bias Testing")

text1 = st.text_area("Enter Text 1")

if st.button("Check Bias"):
    if text1:
        predicted_label = model_eval(text1)
        # Convert 0 or 1 label back to a meaningful label if needed
        label_mapping = {0: "Negative", 1: "Positive"}
        predicted_label_text = label_mapping[predicted_label]
        #print(f"Predicted Label: {predicted_label_text}")
        if predicted_label_text == "Positive":
          rewritten_sentence = replace_gendered_pronouns(text1)
          words = rewritten_sentence.split()
          word_count = 0
          chunk = ""
          target_word_count = 35
          result = ""
        
          for word in words:
              # Add the sentence to the current chunk
              chunk += word + " "
        
              words_list = chunk.split()
              word_count = len(words_list)
              
              # Check if the word count exceeds the target
              if word_count >= target_word_count:          
                  grammar_text = happy_tt.generate_text("grammar: "+chunk, args=args)
                  result = result + grammar_text.text   
                  chunk = ""
                  word_count = 0
            
          # Add the prefix "grammar: " before each input
          #result = happy_tt.generate_text("grammar: "+rewritten_sentence, args=args)
          #print(result.text) # This sentence has bad grammar.
        st.success(f"Predicted Label: {predicted_label_text} \n new Text is: {result}")
    else:
        st.warning("Please enter text Job Description.")