Rob Caamano commited on
Commit
04fd2b6
·
unverified ·
1 Parent(s): 39e1615

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -29,25 +29,28 @@ if selected_model in ["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
- def get_toxicity_class(predictions, threshold=0.3):
33
- return {model.config.id2label[i]: pred for i, pred in enumerate(predictions) if pred >= threshold}
34
 
35
  input = tokenizer(text, return_tensors="tf")
36
 
37
  if st.button("Submit", type="primary"):
38
  results = dict(d.values() for d in clf(text)[0])
39
- toxic_labels = {k: results[k] for k in results.keys() if not k == "toxic"}
40
 
41
  tweet_portion = text[:50] + "..." if len(text) > 50 else text
42
 
43
  if len(toxic_labels) == 0:
44
  st.write("This text is not toxic.")
45
  else:
 
 
 
46
  df = pd.DataFrame(
47
  {
48
- "Text (portion)": [tweet_portion] * len(toxic_labels),
49
- "Toxicity Class": list(toxic_labels.keys()),
50
- "Probability": list(toxic_labels.values()),
51
  }
52
  )
53
  st.table(df)
 
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
+ def get_most_toxic_class(predictions):
33
+ return {model.config.id2label[i]: pred for i, pred in enumerate(predictions)}
34
 
35
  input = tokenizer(text, return_tensors="tf")
36
 
37
  if st.button("Submit", type="primary"):
38
  results = dict(d.values() for d in clf(text)[0])
39
+ toxic_labels = get_most_toxic_class(results)
40
 
41
  tweet_portion = text[:50] + "..." if len(text) > 50 else text
42
 
43
  if len(toxic_labels) == 0:
44
  st.write("This text is not toxic.")
45
  else:
46
+ max_toxic_class = max(toxic_labels, key=toxic_labels.get)
47
+ max_probability = toxic_labels[max_toxic_class]
48
+
49
  df = pd.DataFrame(
50
  {
51
+ "Text (portion)": [tweet_portion],
52
+ "Toxicity Class": [max_toxic_class],
53
+ "Probability": [max_probability],
54
  }
55
  )
56
  st.table(df)