Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,72 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import transformers
|
| 4 |
from transformers import BertTokenizer, BertForSequenceClassification
|
| 5 |
import torch
|
| 6 |
|
| 7 |
# Load pre-trained BERT model and tokenizer
|
| 8 |
-
tokenizer = BertTokenizer.from_pretrained(
|
| 9 |
-
model = BertForSequenceClassification.from_pretrained(
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
# Create a persistent DataFrame to store classification results
|
| 13 |
-
|
| 14 |
|
| 15 |
def classify_text(text):
|
| 16 |
# Tokenize input text
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Perform inference with BERT model
|
| 22 |
with torch.no_grad():
|
| 23 |
-
outputs = model(input_ids
|
| 24 |
logits = outputs.logits
|
| 25 |
-
probabilities = torch.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return toxicity_score
|
| 29 |
-
|
| 30 |
-
def add_to_results(text, toxicity):
|
| 31 |
-
global results_df
|
| 32 |
-
results_df = results_df.append({'Text': text, 'Toxicity': toxicity}, ignore_index=True)
|
| 33 |
|
| 34 |
# Streamlit app
|
| 35 |
def main():
|
| 36 |
-
st.title(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# Display classification results
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
if __name__ ==
|
| 53 |
main()
|
|
|
|
| 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 and tokenizer
|
| 7 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 8 |
+
model = BertForSequenceClassification.from_pretrained("bert-base-uncased")
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
# Create a persistent DataFrame to store classification results
|
| 12 |
+
df_results = pd.DataFrame(columns=['Text', 'Toxic', 'Obscene', 'Insult', 'Identity Hate', 'Threat', 'Severe Toxic'])
|
| 13 |
|
| 14 |
def classify_text(text):
|
| 15 |
# Tokenize input text
|
| 16 |
+
input_ids = tokenizer.encode(text, add_special_tokens=True)
|
| 17 |
+
# Convert tokenized input to tensors
|
| 18 |
+
input_ids = torch.tensor([input_ids])
|
| 19 |
+
# Feed input to the BERT model
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
+
outputs = model(input_ids)
|
| 22 |
logits = outputs.logits
|
| 23 |
+
probabilities = torch.sigmoid(logits)
|
| 24 |
+
return probabilities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Streamlit app
|
| 27 |
def main():
|
| 28 |
+
st.title("Toxicity Classification App")
|
| 29 |
+
|
| 30 |
+
# Get user input
|
| 31 |
+
text = st.text_area("Enter text", "")
|
| 32 |
+
if text:
|
| 33 |
+
# Classify text using BERT model
|
| 34 |
+
probabilities = classify_text(text)
|
| 35 |
+
toxic = probabilities[0][0].item()
|
| 36 |
+
obscene = probabilities[0][1].item()
|
| 37 |
+
insult = probabilities[0][2].item()
|
| 38 |
+
identity_hate = probabilities[0][3].item()
|
| 39 |
+
threat = probabilities[0][4].item()
|
| 40 |
+
severe_toxic = probabilities[0][5].item()
|
| 41 |
|
| 42 |
+
# Display classification results
|
| 43 |
+
st.write("### Classification Results")
|
| 44 |
+
st.write(f"Toxic: {toxic:.4f}")
|
| 45 |
+
st.write(f"Obscene: {obscene:.4f}")
|
| 46 |
+
st.write(f"Insult: {insult:.4f}")
|
| 47 |
+
st.write(f"Identity Hate: {identity_hate:.4f}")
|
| 48 |
+
st.write(f"Threat: {threat:.4f}")
|
| 49 |
+
st.write(f"Severe Toxic: {severe_toxic:.4f}")
|
| 50 |
|
| 51 |
+
# Add classification results to DataFrame
|
| 52 |
+
if st.button("Add to Results"):
|
| 53 |
+
global df_results
|
| 54 |
+
row = {
|
| 55 |
+
'Text': text,
|
| 56 |
+
'Toxic': toxic,
|
| 57 |
+
'Obscene': obscene,
|
| 58 |
+
'Insult': insult,
|
| 59 |
+
'Identity Hate': identity_hate,
|
| 60 |
+
'Threat': threat,
|
| 61 |
+
'Severe Toxic': severe_toxic
|
| 62 |
+
}
|
| 63 |
+
df_results = df_results.append(row, ignore_index=True)
|
| 64 |
+
st.success("Classification results added to DataFrame.")
|
| 65 |
|
| 66 |
+
# Display persistent DataFrame with classification results
|
| 67 |
+
if not df_results.empty:
|
| 68 |
+
st.write("### Classification Results DataFrame")
|
| 69 |
+
st.write(df_results)
|
| 70 |
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
main()
|