Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,69 +4,65 @@ from transformers import BertTokenizer, BertForSequenceClassification
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
# Load pre-trained BERT model and tokenizer
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
model.eval()
|
| 10 |
|
| 11 |
-
# Create
|
| 12 |
-
|
| 13 |
|
| 14 |
def classify_text(text):
|
| 15 |
# Tokenize input text
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
-
outputs = model(input_ids)
|
| 22 |
logits = outputs.logits
|
| 23 |
-
probabilities = torch.
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Streamlit app
|
| 27 |
def main():
|
| 28 |
st.title("Toxicity Classification App")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
st.write(df_results)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
main()
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
# Load pre-trained BERT model and tokenizer
|
| 7 |
+
MODEL_PATH = "bert-base-uncased"
|
| 8 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
|
| 9 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
+
# Create DataFrame to store classification results
|
| 13 |
+
df = pd.DataFrame(columns=["Text", "Toxic", "Obscene", "Insult", "Identity Hate", "Threat", "Severe Toxic"])
|
| 14 |
|
| 15 |
def classify_text(text):
|
| 16 |
# Tokenize input text
|
| 17 |
+
encoded_input = tokenizer.encode_plus(
|
| 18 |
+
text,
|
| 19 |
+
max_length=512,
|
| 20 |
+
padding='max_length',
|
| 21 |
+
truncation=True,
|
| 22 |
+
return_tensors='pt'
|
| 23 |
+
)
|
| 24 |
+
input_ids = encoded_input['input_ids']
|
| 25 |
+
attention_mask = encoded_input['attention_mask']
|
| 26 |
+
|
| 27 |
+
# Predict toxicity using pre-trained BERT model
|
| 28 |
with torch.no_grad():
|
| 29 |
+
outputs = model(input_ids, attention_mask)
|
| 30 |
logits = outputs.logits
|
| 31 |
+
probabilities = torch.softmax(logits, dim=1)
|
| 32 |
+
toxic_prob = probabilities[0][1].item()
|
| 33 |
+
obscene_prob = probabilities[0][2].item()
|
| 34 |
+
insult_prob = probabilities[0][3].item()
|
| 35 |
+
identity_hate_prob = probabilities[0][4].item()
|
| 36 |
+
threat_prob = probabilities[0][5].item()
|
| 37 |
+
severe_toxic_prob = probabilities[0][6].item()
|
| 38 |
+
|
| 39 |
+
# Return predicted probabilities
|
| 40 |
+
return toxic_prob, obscene_prob, insult_prob, identity_hate_prob, threat_prob, severe_toxic_prob
|
| 41 |
|
| 42 |
# Streamlit app
|
| 43 |
def main():
|
| 44 |
st.title("Toxicity Classification App")
|
| 45 |
+
text = st.text_area("Enter text to classify", height=200)
|
| 46 |
+
if st.button("Classify"):
|
| 47 |
+
if text.strip() == "":
|
| 48 |
+
st.warning("Please enter some text.")
|
| 49 |
+
else:
|
| 50 |
+
toxic_prob, obscene_prob, insult_prob, identity_hate_prob, threat_prob, severe_toxic_prob = classify_text(text)
|
| 51 |
+
st.write("Toxicity probabilities:")
|
| 52 |
+
st.write("Toxic:", toxic_prob)
|
| 53 |
+
st.write("Obscene:", obscene_prob)
|
| 54 |
+
st.write("Insult:", insult_prob)
|
| 55 |
+
st.write("Identity Hate:", identity_hate_prob)
|
| 56 |
+
st.write("Threat:", threat_prob)
|
| 57 |
+
st.write("Severe Toxic:", severe_toxic_prob)
|
| 58 |
|
| 59 |
+
if st.button("Add to DataFrame"):
|
| 60 |
+
df.loc[len(df)] = [text, toxic_prob, obscene_prob, insult_prob, identity_hate_prob, threat_prob, severe_toxic_prob]
|
| 61 |
+
st.success("Classification results added to DataFrame.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Display DataFrame with classification results
|
| 64 |
+
st.subheader("Classification Results")
|
| 65 |
+
st.dataframe(df)
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
main()
|