Spaces:
Sleeping
Sleeping
File size: 5,951 Bytes
be61171 9476a50 3fa07b4 7eca2e2 0451811 7eca2e2 0451811 7eca2e2 73cfee4 0451811 7eca2e2 8beec99 73cfee4 3e5bb17 73cfee4 0451811 7eca2e2 0451811 9476a50 0451811 d1ac74e 0451811 380b45b 0451811 2b3dec6 460f5fd 0451811 9476a50 0451811 f199e05 460f5fd |
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 |
#%%writefile debias_app.py
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification, pipeline as tf_pipeline
import transformers
import torch
import pandas as pd
# Set the default device
class BiasPipeline:
def __init__(self):
# Load models and tokenizers for bias detection
self.classifier_tokenizer = AutoTokenizer.from_pretrained("newsmediabias/UnBIAS-classification-bert")
self.classifier_model = AutoModelForSequenceClassification.from_pretrained("newsmediabias/UnBIAS-classification-bert")
self.ner_tokenizer = AutoTokenizer.from_pretrained("newsmediabias/UnBIAS-NER")
self.ner_model = AutoModelForTokenClassification.from_pretrained("newsmediabias/UnBIAS-NER")
self.classifier = tf_pipeline("text-classification", model=self.classifier_model, tokenizer=self.classifier_tokenizer)
self.ner = tf_pipeline("ner", model=self.ner_model, tokenizer=self.ner_tokenizer)
def clean_text(self, text):
"""Clean up the text by removing any redundant spaces."""
return ' '.join(text.split())
def process(self, texts):
"""Process texts to classify and find named entities."""
classification_results = self.classifier(texts)
ner_results = self.ner(texts)
return classification_results, ner_results
# Constants and Global Variables
sys_message = "Task: Please just generate a bias-free version of the text provided, ensuring it's free from biases related to age, gender, politics, social nuances, or economic background, while keeping it roughly the same length as the original:"
instruction = "Instruction: As a helpful, respectful and trustworthy debiasing assistant, your task is to receive a text and return its unbiased version, Don't add additional comment. Just return the un biased version of the input text:"
# Model setup for debiasing
debias_model = "newsmediabias/UnBIAS-LLama2-Debiaser-Chat-QLoRA"
debias_tokenizer = AutoTokenizer.from_pretrained(debias_model)
debias_pipeline = transformers.pipeline(
"text-generation",
model=debias_model,
torch_dtype=torch.float16,
device_map="auto",
)
def get_debiased_sequence(prompt):
"""
Generate a debiased version of the provided text using the debiasing pipeline.
Args:
- prompt (str): Text to be debiased.
Returns:
- str: Debiased text.
"""
input_text = f"<s> <<SYS>>{instruction}. {sys_message} <</SYS>> [INST]{prompt} [/INST]"
sequences = debias_pipeline(
input_text,
do_sample=True,
top_k=10,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
max_length=len(prompt.split(" ")) + len(input_text.split(" ")) + 100, # Increased max_length
)
res = sequences[0]['generated_text']
result_part = res.split('[/INST]')[-1]
clean_result = ''.join(c for c in result_part if c.isprintable())
cleaned_text = re_incomplete_sentence(clean_result.strip())
return cleaned_text.strip()
# Streamlit interface setup
st.title('UnBIAS App')
# Initialize the BiasPipeline
bias_pipeline = BiasPipeline()
# List of predefined test sentences
test_sentences = [
"Women are just too emotional to be leaders.",
"All young people are lazy and addicted to their phones.",
"People from that country are always dishonest and corrupt.",
"Men are inherently better at science and math than women.",
"The elderly are just burdens to society; they contribute nothing.",
"All poor people are lazy and don't want to work.",
"People who follow that religion are all terrorists.",
"Immigrants are taking all our jobs and ruining the country.",
"All wealthy people achieved success only through manipulation and deceit.",
"People with tattoos are not professional.",
"All politicians are liars and cannot be trusted.",
"Blue-collar workers don't have any real skills.",
"Anyone who doesn't attend college is unintelligent.",
"Only people from the city are sophisticated.",
"Rural folks have backward mindsets and don't understand progress.",
"All artists are always broke and unreliable.",
"Anyone from that region is uneducated and narrow-minded.",
"People without jobs are simply not trying hard enough."
]
# Dropdown to select a test sentence
selected_sentence = st.selectbox("Choose a pre-loaded sentence to analyze and debias:", [""] + test_sentences)
# Text area for custom input
input_text = st.text_area("Or enter your own text to analyze and debias:", height=150)
if st.button("Analyze and Debias Text"):
text_to_process = selected_sentence if selected_sentence else input_text
if text_to_process:
cleaned_text = bias_pipeline.clean_text(text_to_process)
classification_results, ner_results = bias_pipeline.process(cleaned_text)
label = classification_results[0]['label']
score = classification_results[0]['score']
st.write(f"**Classification:** {label} (Confidence: {score:.2f})")
biased_words = [result['word'] for result in ner_results if result['entity'].startswith('B-BIAS')]
st.write("**Biased Words Identified:**", ", ".join(biased_words))
# Debias the text
debiased_text = get_debiased_sequence(cleaned_text)
st.write("## Debiased Text:")
st.write(debiased_text)
else:
st.write("Please enter some text to analyze and debias or select a pre-loaded sentence.")
# Disclaimer
st.info("Disclaimer: Please note that while this tool aims to identify and highlight biased language, no automated system is perfect. The detection of bias depends on various factors, including the context, the training data used for the models, and the inherent limitations of natural language processing technologies. As such, some biases may not be detected, and all results should be reviewed critically by human users.")
|