Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,56 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, BertForSequenceClassification, DistilBertModel
|
| 5 |
import torch
|
| 6 |
-
from torch import cuda
|
| 7 |
-
from torch.utils.data import Dataset, DataLoader
|
| 8 |
-
import finetuning
|
| 9 |
-
from finetuning import CustomDistilBertClass
|
| 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 |
-
def
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
st.
|
| 56 |
-
|
| 57 |
-
st.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
st.subheader('Enter comment below:')
|
| 63 |
-
text_input = st.text_area(label='', height=100, max_chars=500)
|
| 64 |
-
|
| 65 |
-
# Make prediction when user clicks 'Classify' button
|
| 66 |
-
if st.button('Classify Toxicity'):
|
| 67 |
-
if not text_input:
|
| 68 |
-
st.write('Please enter comment')
|
| 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 |
-
}, ignore_index=True)
|
| 94 |
-
st.write(st.session_state.classification_results)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
|
|
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load pre-trained BERT model
|
| 7 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
# Create a DataFrame to store classification results
|
| 12 |
+
classification_results_df = pd.DataFrame(columns=['Text', 'Toxic', 'Severe Toxic', 'Obscene', 'Threat', 'Insult', 'Identity Hate'])
|
| 13 |
+
|
| 14 |
+
def classify_text(text):
|
| 15 |
+
# Tokenize text
|
| 16 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
|
| 17 |
+
# Forward pass through the BERT model
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
# Get predicted probabilities for each class
|
| 20 |
+
probs = torch.sigmoid(outputs.logits)
|
| 21 |
+
# Round probabilities to 0 or 1 to get binary predictions
|
| 22 |
+
preds = (probs > 0.5).int().tolist()[0]
|
| 23 |
+
return preds
|
| 24 |
+
|
| 25 |
+
def add_classification_to_df(text, preds):
|
| 26 |
+
# Add classification results to the DataFrame
|
| 27 |
+
classification_results_df.loc[len(classification_results_df)] = [text] + preds
|
| 28 |
+
|
| 29 |
+
# Streamlit app
|
| 30 |
+
def main():
|
| 31 |
+
st.title("Toxicity Classification with BERT")
|
| 32 |
+
# Input text from user
|
| 33 |
+
text = st.text_area("Enter text for classification", "")
|
| 34 |
+
if st.button("Classify"):
|
| 35 |
+
if text.strip() == "":
|
| 36 |
+
st.warning("Please enter some text for classification.")
|
| 37 |
+
else:
|
| 38 |
+
# Perform classification
|
| 39 |
+
preds = classify_text(text)
|
| 40 |
+
# Display classification results
|
| 41 |
+
st.subheader("Classification Results:")
|
| 42 |
+
st.write("Toxic: ", preds[0])
|
| 43 |
+
st.write("Severe Toxic: ", preds[1])
|
| 44 |
+
st.write("Obscene: ", preds[2])
|
| 45 |
+
st.write("Threat: ", preds[3])
|
| 46 |
+
st.write("Insult: ", preds[4])
|
| 47 |
+
st.write("Identity Hate: ", preds[5])
|
| 48 |
+
# Add classification results to DataFrame
|
| 49 |
+
add_classification_to_df(text, preds)
|
| 50 |
+
if st.button("View Classification Results"):
|
| 51 |
+
# Display classification results DataFrame
|
| 52 |
+
st.subheader("All Classification Results:")
|
| 53 |
+
st.write(classification_results_df)
|
| 54 |
+
|
| 55 |
+
if __name__ == '__main__':
|
| 56 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|