Views
No views yet
1from transformers import pipeline
2from transformers import AutoTokenizer ,AutoModelForCausalLM
3
4text="n Arkansas police officer has been fired after telling a group of African-American men that you don’t belong in my city."
5prompt='''Classify the text into 0, 1, and return the answer as the corresponding label.
6text: {}
7label: '''.format(text)
8
9tokenizer = AutoTokenizer.from_pretrained("NYUAD-ComNets/Racial_Bias_Detection_LLaMa")
10tokenizer.pad_token_id = tokenizer.eos_token_id
11
12model = AutoModelForCausalLM.from_pretrained(
13 "NYUAD-ComNets/Racial_Bias_Detection_LLaMa",
14 device_map="auto",
15 torch_dtype="float16",
16)
17
18pipe = pipeline(task="text-generation",
19 model=model,
20 tokenizer=tokenizer,
21 max_new_tokens=2,
22 temperature=0.1)
23
24result = pipe(prompt)
25answer = result[0]['generated_text'].split("label:")[-1].strip()
26print(answer)
27if('1' in answer):
28 print('This text has racial bias')
29else:
30 print('no racial bias')
31