Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,71 @@
|
|
| 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 |
-
|
| 8 |
-
|
|
|
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
-
# Create
|
| 12 |
-
|
| 13 |
|
| 14 |
def classify_text(text):
|
| 15 |
# Tokenize text
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Streamlit app
|
| 30 |
def main():
|
| 31 |
-
st.title(
|
| 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 |
-
st.write(classification_results_df)
|
| 61 |
|
| 62 |
if __name__ == '__main__':
|
| 63 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import torch
|
| 4 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 5 |
|
| 6 |
+
# Load pre-trained BERT model and tokenizer
|
| 7 |
+
MODEL_NAME = 'bert-base-uncased'
|
| 8 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=6)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
+
# Create DataFrame to store classification results
|
| 13 |
+
df_results = pd.DataFrame(columns=['Text', 'Toxic', 'Severe Toxic', 'Obscene', 'Threat', 'Insult', 'Identity Hate'])
|
| 14 |
|
| 15 |
def classify_text(text):
|
| 16 |
# Tokenize text
|
| 17 |
+
tokens = tokenizer.encode_plus(
|
| 18 |
+
text,
|
| 19 |
+
max_length=512,
|
| 20 |
+
truncation=True,
|
| 21 |
+
padding=True,
|
| 22 |
+
return_attention_mask=True,
|
| 23 |
+
return_tensors='pt'
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Get model's predictions
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model(**tokens)
|
| 29 |
+
logits = outputs.logits
|
| 30 |
+
probabilities = torch.softmax(logits, dim=1).tolist()[0]
|
| 31 |
+
|
| 32 |
+
# Extract predicted labels
|
| 33 |
+
labels = ['Toxic', 'Severe Toxic', 'Obscene', 'Threat', 'Insult', 'Identity Hate']
|
| 34 |
+
predicted_labels = [labels[i] for i, prob in enumerate(probabilities) if prob > 0.5]
|
| 35 |
+
|
| 36 |
+
return predicted_labels
|
| 37 |
|
| 38 |
# Streamlit app
|
| 39 |
def main():
|
| 40 |
+
st.title('Toxicity Classification')
|
| 41 |
+
|
| 42 |
+
# User input
|
| 43 |
+
text = st.text_area('Enter text:', max_chars=512)
|
| 44 |
+
|
| 45 |
+
# Perform classification
|
| 46 |
+
if st.button('Classify'):
|
| 47 |
+
predicted_labels = classify_text(text)
|
| 48 |
+
st.write('Predicted Labels:', predicted_labels)
|
| 49 |
+
|
| 50 |
+
# Allow user to add classification results to DataFrame
|
| 51 |
+
if st.button('Add to Results'):
|
| 52 |
+
global df_results
|
| 53 |
+
df_results = df_results.append({
|
| 54 |
+
'Text': text,
|
| 55 |
+
'Toxic': 'Toxic' in predicted_labels,
|
| 56 |
+
'Severe Toxic': 'Severe Toxic' in predicted_labels,
|
| 57 |
+
'Obscene': 'Obscene' in predicted_labels,
|
| 58 |
+
'Threat': 'Threat' in predicted_labels,
|
| 59 |
+
'Insult': 'Insult' in predicted_labels,
|
| 60 |
+
'Identity Hate': 'Identity Hate' in predicted_labels
|
| 61 |
+
}, ignore_index=True)
|
| 62 |
+
st.success('Classification results added to DataFrame.')
|
| 63 |
+
|
| 64 |
+
# Show DataFrame with classification results
|
| 65 |
+
if not df_results.empty:
|
| 66 |
+
st.subheader('Classification Results')
|
| 67 |
+
st.dataframe(df_results)
|
| 68 |
+
|
|
|
|
| 69 |
|
| 70 |
if __name__ == '__main__':
|
| 71 |
main()
|