|
import random |
|
from typing import AnyStr |
|
|
|
import streamlit as st |
|
from bs4 import BeautifulSoup |
|
|
|
import spacy |
|
from spacy import displacy |
|
|
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
from transformers import pipeline |
|
import os |
|
from transformers_interpret import SequenceClassificationExplainer |
|
|
|
|
|
model_names_to_URLs = { |
|
'ml6team/distilbert-base-dutch-cased-toxic-comments': |
|
'https://huggingface.co/ml6team/distilbert-base-dutch-cased-toxic-comments', |
|
'ml6team/robbert-dutch-base-toxic-comments': |
|
'https://huggingface.co/ml6team/robbert-dutch-base-toxic-comments', |
|
} |
|
|
|
about_page_markdown = f"""# π€¬ Dutch Toxic Comment Detection Space |
|
|
|
Made by [ML6](https://ml6.eu/). |
|
|
|
Token attribution is performed using [transformers-interpret](https://github.com/cdpierse/transformers-interpret). |
|
""" |
|
|
|
regular_emojis = [ |
|
'π', 'π', 'πΆ', 'π', |
|
] |
|
undecided_emojis = [ |
|
'π€¨', 'π§', 'π₯Έ', 'π₯΄', 'π€·', |
|
] |
|
potty_mouth_emojis = [ |
|
'π€', 'πΏ', 'π‘', 'π€¬', 'β οΈ', 'β£οΈ', 'β’οΈ', |
|
] |
|
|
|
|
|
st.set_page_config( |
|
page_title="Toxic Comment Detection Space", |
|
page_icon="π€¬", |
|
layout="centered", |
|
initial_sidebar_state="auto", |
|
menu_items={ |
|
'Get help': None, |
|
'Report a bug': None, |
|
'About': about_page_markdown, |
|
} |
|
) |
|
|
|
|
|
@st.cache(allow_output_mutation=True, |
|
suppress_st_warning=True, |
|
show_spinner=False) |
|
def load_pipeline(model_name): |
|
with st.spinner('Loading model (this might take a while)...'): |
|
toxicity_pipeline = pipeline( |
|
'text-classification', |
|
model=model_name, |
|
tokenizer=model_name) |
|
cls_explainer = SequenceClassificationExplainer( |
|
toxicity_pipeline.model, |
|
toxicity_pipeline.tokenizer) |
|
return toxicity_pipeline, cls_explainer |
|
|
|
|
|
|
|
def format_explainer_html(html_string): |
|
"""Extract tokens with attribution-based background color.""" |
|
inside_token_prefix = '##' |
|
soup = BeautifulSoup(html_string, 'html.parser') |
|
p = soup.new_tag('p', |
|
attrs={'style': 'color: black; background-color: white;'}) |
|
|
|
current_word = None |
|
for token in soup.find_all('td')[-1].find_all('mark')[1:-1]: |
|
text = token.font.text.strip() |
|
if text.startswith(inside_token_prefix): |
|
text = text[len(inside_token_prefix):] |
|
else: |
|
|
|
if current_word is not None: |
|
p.append(current_word) |
|
p.append(' ') |
|
current_word = soup.new_tag('span') |
|
token.string = text |
|
token.attrs['style'] = f"{token.attrs['style']}; padding: 0.2em 0em;" |
|
current_word.append(token) |
|
|
|
|
|
p.append(current_word) |
|
|
|
|
|
for span in p.find_all('span'): |
|
span.find_all('mark')[0].attrs['style'] = ( |
|
f"{span.find_all('mark')[0].attrs['style']}; padding-left: 0.2em;") |
|
span.find_all('mark')[-1].attrs['style'] = ( |
|
f"{span.find_all('mark')[-1].attrs['style']}; padding-right: 0.2em;") |
|
|
|
return p |
|
|
|
def list_all_article_names() -> list: |
|
filenames = [] |
|
for file in os.listdir('./sample-articles/'): |
|
if file.endswith('.txt'): |
|
filenames.append(file.replace('.txt', '')) |
|
return filenames |
|
|
|
def fetch_article_contents(filename: str) -> AnyStr: |
|
with open(f'./sample-articles/{filename.lower()}.txt', 'r') as f: |
|
data = f.read() |
|
return data |
|
|
|
def fetch_summary_contents(filename: str) -> AnyStr: |
|
with open(f'./sample-summaries/{filename.lower()}.txt', 'r') as f: |
|
data = f.read() |
|
return data |
|
|
|
def classify_comment(comment, selected_model): |
|
"""Classify the given comment and augment with additional information.""" |
|
toxicity_pipeline, cls_explainer = load_pipeline(selected_model) |
|
result = toxicity_pipeline(comment)[0] |
|
result['model_name'] = selected_model |
|
|
|
|
|
result['word_attribution'] = cls_explainer(comment, class_name="non-toxic") |
|
result['visualitsation_html'] = cls_explainer.visualize()._repr_html_() |
|
result['tokens_with_background'] = format_explainer_html( |
|
result['visualitsation_html']) |
|
|
|
|
|
label, score = result['label'], result['score'] |
|
if label == 'toxic' and score > 0.1: |
|
emoji = random.choice(potty_mouth_emojis) |
|
elif label in ['non_toxic', 'non-toxic'] and score > 0.1: |
|
emoji = random.choice(regular_emojis) |
|
else: |
|
emoji = random.choice(undecided_emojis) |
|
result.update({'text': comment, 'emoji': emoji}) |
|
|
|
|
|
st.session_state.results.append(result) |
|
|
|
|
|
|
|
if 'results' not in st.session_state: |
|
st.session_state.results = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with st.form("article-input"): |
|
|
|
selected_article = st.selectbox('Select an article or provide your own:', list_all_article_names(), |
|
) |
|
st.session_state.article_text = fetch_article_contents(selected_article) |
|
article_text = st.text_area( |
|
label='Enter the comment you want to classify below (in Dutch):', |
|
value = st.session_state.article_text) |
|
_, rightmost_col = st.columns([6,1]) |
|
get_summary = rightmost_col.form_submit_button("Generate summary", |
|
help="Generate summary for the given article text") |
|
|
|
|
|
def display_summary(article_name: str): |
|
st.subheader("GENERATED SUMMARY") |
|
st.markdown("######") |
|
summary_content = fetch_summary_contents(article_name) |
|
nlp = spacy.load('en_core_web_sm') |
|
doc = nlp(summary_content) |
|
html = displacy.render(doc, style="ent") |
|
html = html.replace("\n", " ") |
|
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>""" |
|
st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True) |
|
st.markdown(summary_content) |
|
|
|
|
|
if get_summary: |
|
if article_text: |
|
with st.spinner('Generating summary...'): |
|
|
|
display_summary(selected_article) |
|
else: |
|
st.error('**Error**: No comment to classify. Please provide a comment.') |
|
|
|
|
|
if 'results' in st.session_state and st.session_state.results: |
|
first = True |
|
for result in st.session_state.results[::-1]: |
|
if not first: |
|
st.markdown("---") |
|
st.markdown(f"Text:\n> {result['text']}") |
|
col_1, col_2, col_3 = st.columns([1,2,2]) |
|
col_1.metric(label='', value=f"{result['emoji']}") |
|
col_2.metric(label='Label', value=f"{result['label']}") |
|
col_3.metric(label='Score', value=f"{result['score']:.3f}") |
|
st.markdown(f"Token Attribution:\n{result['tokens_with_background']}", |
|
unsafe_allow_html=True) |
|
st.caption(f"Model: {result['model_name']}") |
|
first = False |
|
|
|
|