Rob Caamano commited on
Commit
2d942ee
1 Parent(s): 4a0592e
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -8,7 +8,7 @@ from transformers import pipeline
8
 
9
  st.title("Detecting Toxic Tweets")
10
 
11
- demo = """I'm so proud of myself for accomplishing my goals today. #motivation #success"""
12
 
13
  text = st.text_area("Input text", demo, height=250)
14
 
@@ -26,7 +26,8 @@ model = AutoModelForSequenceClassification.from_pretrained(mod_name)
26
 
27
  # Update the id2label mapping for the fine-tuned model
28
  if selected_model == "Fine-tuned Toxicity Model":
29
- model.config.id2label = {i: f"LABEL_{i}" for i in range(model.config.num_labels)}
 
30
 
31
  clf = pipeline(
32
  "text-classification", model=model, tokenizer=tokenizer, return_all_scores=True
@@ -36,20 +37,20 @@ input = tokenizer(text, return_tensors="tf")
36
 
37
  if st.button("Submit", type="primary"):
38
  results = clf(text)[0]
 
39
 
 
 
 
40
  if selected_model == "Fine-tuned Toxicity Model":
41
- max_class = max(results, key=lambda x: x["score"])
42
- max_class["label"] = max_class["label"].split("_")[-1] # Extract the toxicity class from the label
43
  else:
44
- max_class = max(results, key=lambda x: x["score"])
45
-
46
- tweet_portion = text[:50] + "..." if len(text) > 50 else text
47
 
48
- # Create and display the table
49
  df = pd.DataFrame(
50
  {
51
  "Tweet (portion)": [tweet_portion],
52
- "Highest Toxicity Class": [max_class["label"]],
53
  "Probability": [max_class["score"]],
54
  }
55
  )
 
8
 
9
  st.title("Detecting Toxic Tweets")
10
 
11
+ demo = """Your words are like poison. They seep into my mind and make me feel worthless."""
12
 
13
  text = st.text_area("Input text", demo, height=250)
14
 
 
26
 
27
  # Update the id2label mapping for the fine-tuned model
28
  if selected_model == "Fine-tuned Toxicity Model":
29
+ toxicity_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
30
+ model.config.id2label = {i: toxicity_classes[i] for i in range(model.config.num_labels)}
31
 
32
  clf = pipeline(
33
  "text-classification", model=model, tokenizer=tokenizer, return_all_scores=True
 
37
 
38
  if st.button("Submit", type="primary"):
39
  results = clf(text)[0]
40
+ max_class = max(results, key=lambda x: x["score"])
41
 
42
+ tweet_portion = text[:50] + "..." if len(text) > 50 else text
43
+
44
+ # Create and display the table
45
  if selected_model == "Fine-tuned Toxicity Model":
46
+ column_name = "Highest Toxicity Class"
 
47
  else:
48
+ column_name = "Prediction"
 
 
49
 
 
50
  df = pd.DataFrame(
51
  {
52
  "Tweet (portion)": [tweet_portion],
53
+ column_name: [max_class["label"]],
54
  "Probability": [max_class["score"]],
55
  }
56
  )