Spaces:
Runtime error
Runtime error
Rob Caamano
commited on
Update app.py
Browse files
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
|
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 =
|
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]
|
49 |
-
"Toxicity Class":
|
50 |
-
"Probability":
|
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)
|