Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# Import necessary libraries
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
|
@@ -9,22 +8,22 @@ from torch.utils.data import Dataset, DataLoader
|
|
| 9 |
import finetuning
|
| 10 |
from finetuning import CustomDistilBertClass
|
| 11 |
|
| 12 |
-
#
|
|
|
|
| 13 |
model_map = {
|
| 14 |
'BERT': 'bert-base-uncased',
|
| 15 |
'RoBERTa': 'roberta-base',
|
| 16 |
'DistilBERT': 'distilbert-base-uncased'
|
| 17 |
}
|
| 18 |
|
| 19 |
-
# Load dropdown options
|
| 20 |
model_options = list(model_map.keys())
|
| 21 |
|
| 22 |
-
# Load
|
| 23 |
train_df = pd.read_csv('train.csv')
|
| 24 |
train_df = train_df.sample(n=256)
|
| 25 |
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
|
| 26 |
|
| 27 |
-
# Define a function to load the pretrained BERT model
|
| 28 |
@st.cache_resource
|
| 29 |
def load_model(model_name):
|
| 30 |
"""Load pretrained BERT model."""
|
|
@@ -33,7 +32,6 @@ def load_model(model_name):
|
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained(model_map[model_name])
|
| 34 |
return model, tokenizer
|
| 35 |
|
| 36 |
-
# Define a function to classify text using the pretrained BERT model
|
| 37 |
def classify_text(model, tokenizer, text):
|
| 38 |
"""Classify text using pretrained BERT model."""
|
| 39 |
inputs = tokenizer.encode_plus(
|
|
@@ -49,18 +47,18 @@ def classify_text(model, tokenizer, text):
|
|
| 49 |
logits = model(inputs['input_ids'],inputs['attention_mask'])[0]
|
| 50 |
probabilities = torch.softmax(logits, dim=1)[0]
|
| 51 |
pred_class = torch.argmax(probabilities, dim=0)
|
| 52 |
-
print(f"
|
| 53 |
print(probabilities[0].tolist())
|
| 54 |
return label_cols[pred_class], round(probabilities[0].tolist(),2)
|
| 55 |
|
| 56 |
-
# Set up
|
| 57 |
st.title('Toxic Comment Classifier')
|
| 58 |
model_name = st.sidebar.selectbox('Select a model', model_options)
|
| 59 |
st.sidebar.write('Selected:', model_name)
|
| 60 |
model, tokenizer = load_model(model_name)
|
| 61 |
print(type(model))
|
| 62 |
|
| 63 |
-
# Define input text area
|
| 64 |
st.subheader('Enter comment below:')
|
| 65 |
text_input = st.text_area(label='', height=100, max_chars=500)
|
| 66 |
|
|
@@ -71,21 +69,25 @@ if st.button('Classify Toxicity'):
|
|
| 71 |
else:
|
| 72 |
class_label, class_prob = classify_text(model, tokenizer, text_input)
|
| 73 |
st.subheader('Results')
|
| 74 |
-
st.write('
|
| 75 |
st.write('Highest Toxicity Class:', class_label)
|
| 76 |
st.write('Probability:', class_prob)
|
| 77 |
|
| 78 |
# Display table of results
|
| 79 |
st.subheader('Toxic Classification Results')
|
| 80 |
if 'classification_results' not in st.session_state:
|
| 81 |
-
st.session_state.classification_results = pd.DataFrame(columns=['
|
| 82 |
if st.button('Add to Results'):
|
| 83 |
if not text_input:
|
| 84 |
st.write('Please enter comment')
|
| 85 |
else:
|
| 86 |
class_label, class_prob = classify_text(model, tokenizer, text_input)
|
| 87 |
st.subheader('Results')
|
| 88 |
-
st.write('
|
| 89 |
st.write('Highest Toxicity Class:', class_label)
|
| 90 |
st.write('Probability:', class_prob)
|
| 91 |
-
st.session_state.classification_results = st.session_state.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 8 |
import finetuning
|
| 9 |
from finetuning import CustomDistilBertClass
|
| 10 |
|
| 11 |
+
# device = 'cuda' if cuda.is_available() else 'cpu'
|
| 12 |
+
# Load pretrained models
|
| 13 |
model_map = {
|
| 14 |
'BERT': 'bert-base-uncased',
|
| 15 |
'RoBERTa': 'roberta-base',
|
| 16 |
'DistilBERT': 'distilbert-base-uncased'
|
| 17 |
}
|
| 18 |
|
| 19 |
+
# Load dropdown options
|
| 20 |
model_options = list(model_map.keys())
|
| 21 |
|
| 22 |
+
# Load dataset
|
| 23 |
train_df = pd.read_csv('train.csv')
|
| 24 |
train_df = train_df.sample(n=256)
|
| 25 |
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
|
| 26 |
|
|
|
|
| 27 |
@st.cache_resource
|
| 28 |
def load_model(model_name):
|
| 29 |
"""Load pretrained BERT model."""
|
|
|
|
| 32 |
tokenizer = AutoTokenizer.from_pretrained(model_map[model_name])
|
| 33 |
return model, tokenizer
|
| 34 |
|
|
|
|
| 35 |
def classify_text(model, tokenizer, text):
|
| 36 |
"""Classify text using pretrained BERT model."""
|
| 37 |
inputs = tokenizer.encode_plus(
|
|
|
|
| 47 |
logits = model(inputs['input_ids'],inputs['attention_mask'])[0]
|
| 48 |
probabilities = torch.softmax(logits, dim=1)[0]
|
| 49 |
pred_class = torch.argmax(probabilities, dim=0)
|
| 50 |
+
print(f"pred class: {pred_class}")
|
| 51 |
print(probabilities[0].tolist())
|
| 52 |
return label_cols[pred_class], round(probabilities[0].tolist(),2)
|
| 53 |
|
| 54 |
+
# Set up streamlit app
|
| 55 |
st.title('Toxic Comment Classifier')
|
| 56 |
model_name = st.sidebar.selectbox('Select a model', model_options)
|
| 57 |
st.sidebar.write('Selected:', model_name)
|
| 58 |
model, tokenizer = load_model(model_name)
|
| 59 |
print(type(model))
|
| 60 |
|
| 61 |
+
# Define input text area
|
| 62 |
st.subheader('Enter comment below:')
|
| 63 |
text_input = st.text_area(label='', height=100, max_chars=500)
|
| 64 |
|
|
|
|
| 69 |
else:
|
| 70 |
class_label, class_prob = classify_text(model, tokenizer, text_input)
|
| 71 |
st.subheader('Results')
|
| 72 |
+
st.write('Tweet:', text_input)
|
| 73 |
st.write('Highest Toxicity Class:', class_label)
|
| 74 |
st.write('Probability:', class_prob)
|
| 75 |
|
| 76 |
# Display table of results
|
| 77 |
st.subheader('Toxic Classification Results')
|
| 78 |
if 'classification_results' not in st.session_state:
|
| 79 |
+
st.session_state.classification_results = pd.DataFrame(columns=['tweet', 'toxicity_class', 'probability'])
|
| 80 |
if st.button('Add to Results'):
|
| 81 |
if not text_input:
|
| 82 |
st.write('Please enter comment')
|
| 83 |
else:
|
| 84 |
class_label, class_prob = classify_text(model, tokenizer, text_input)
|
| 85 |
st.subheader('Results')
|
| 86 |
+
st.write('Tweet:', text_input)
|
| 87 |
st.write('Highest Toxicity Class:', class_label)
|
| 88 |
st.write('Probability:', class_prob)
|
| 89 |
+
st.session_state.classification_results = st.session_state.classification_results.append({
|
| 90 |
+
'tweet': text_input,
|
| 91 |
+
'toxicity_class': class_label,
|
| 92 |
+
'probability': class_prob
|
| 93 |
+
},
|