File size: 5,519 Bytes
aa7f5a4
 
ef71cfe
 
1bf7e66
 
bd8d813
aa7f5a4
e4f9c2a
aa7f5a4
ef71cfe
4b8f05e
b02687c
 
 
 
ef71cfe
 
 
 
 
 
 
9ab46c1
 
 
 
 
 
 
 
 
 
 
 
ef71cfe
 
 
9ab46c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef71cfe
 
 
 
 
 
 
 
 
 
 
 
b02687c
e4f9c2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c5dac1
e4f9c2a
 
 
 
bd8d813
aa7f5a4
 
13c3404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import streamlit as st  #Web App
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import numpy as np 
import pandas as pd

#title
st.title("Toxic Tweets")

# model = st.selectbox("Which pretrained model would you like to use?",("roberta-large-mnli","twitter-XLM-roBERTa-base","bertweet-sentiment-analysis"))

#d = {'col1':[1,2],'col2':[3,4]}
#data = pd.DataFrame(data=d)
#st.table(data)

# data = []
# text = st.text_input("Enter text here:","Artificial Intelligence is useful")
# data.append(text)

tokenizer = AutoTokenizer.from_pretrained("Ariel8/toxic-tweets-classification")
model = AutoModelForSequenceClassification.from_pretrained("Ariel8/toxic-tweets-classification")

tweets = ["BlackKite being a faggot",
"Yo bitch Ja Rule is more succesful then you'll ever be whats up with you and hating you sad mofuckas...i should bitch slap ur pethedic white faces and get you to kiss my ass you guys sicken me. Ja rule is about pride in da music man. dont diss that shit on him. and nothin is wrong bein like tupac he was a brother too...fuckin white boys get things right next time.,",
":Dear god this site is horrible.",
"I think its crap that the link to roggenbier is to this article. Somebody that knows how to do things should change it.",
":::::I have added more wikilinks to my sections and included more secondary sources as you suggested. For all the citations under human disease section, I went through them and exchanged review articles for the primary sources. Thanks again for all your input!",
"DJ Robinson is gay as hell! he sucks his dick so much!!!!!",
"I WILL BURN YOU TO HELL IF YOU REVOKE MY TALK PAGE ACCESS!!!!!!!!!!!!!",
"um, taking a shot here... Um lets say Three separate people whos wills are to each other.",
"How dare you vandalize that page about the HMS Beagle! Don't vandalize again, demon!",
":Thanks for the comment about Wiki-defenderness. I like that one. I usually wikiling Wiki-defender. I agree that at first he was somewhat innocent but now have my doubts as he is being really agressive about the whole matter."]


batch = tokenizer(X_train, truncation=True, padding='max_length', return_tensors="pt")
labels = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]

results = []
for b in batch:  
    with torch.no_grad():
        outputs = model(**b)
        predictions = torch.sigmoid(outputs.logits)*100
        probs = predictions[0].tolist()
        # for i in range(len(probs)):
        #     st.write(f"{labels[i]}: {round(probs[i], 3)}%")
        results.append(probs)

# main_class = []
# toxic_type = []
for i in range(len(tweets)): 
    first_max = max(results[i])
    fm_index = results[i].index(first_max)
    second_max = max(results[i][2:])
    sm_index = results[i].index(second_max)
    #main_class.append((labels[fm_index],first_max))
    #toxic_type.append((labels[sm_index],second_max))
    d = {'tweet':[tweets[i]],'Main Classification':[labels[fm_index]],'Score':[first_max],
    'Toxicity Type':[labels[sm_index]],'Toxicity Score':[second_max]}
    dataframe = pd.DataFrame(data=d)
    st.table(dataframe)

# if model == "roberta-large-mnli":
#     #1
#     if st.button("Run Sentiment Analysis of Text"): 
#         model_path = "roberta-large-mnli"
#         sentiment_pipeline = pipeline(model=model_path)
#         result = sentiment_pipeline(data)
#         label = result[0]["label"]
#         score = result[0]["score"]
#         d = {'tweet':[model_path],'classification':[label],'score':[score]}
#         dataframe = pd.DataFrame(data=d)
#         st.table(dataframe)
        #st.write("The classification of the given text is " + label + " with a score of " + str(score))


# data = []
# text = st.text_input("Enter text here:","Artificial Intelligence is useful")
# data.append(text)
# if model == "roberta-large-mnli":
#     #1
#     if st.button("Run Sentiment Analysis of Text"): 
#         model_path = "roberta-large-mnli"
#         sentiment_pipeline = pipeline(model=model_path)
#         result = sentiment_pipeline(data)
#         label = result[0]["label"]
#         score = result[0]["score"]
#         st.write("The classification of the given text is " + label + " with a score of " + str(score))
# elif model == "twitter-XLM-roBERTa-base":
#     #2
#     if st.button("Run Sentiment Analysis of Text"): 
#         model_path = "cardiffnlp/twitter-xlm-roberta-base-sentiment"
#         sentiment_task = pipeline("sentiment-analysis", model=model_path, tokenizer=model_path)
#         result = sentiment_task(text)
#         label = result[0]["label"].capitalize()
#         score = result[0]["score"]
#         st.write("The classification of the given text is " + label + " with a score of " + str(score))

# elif model == "bertweet-sentiment-analysis": 
#     #3 
#     if st.button("Run Sentiment Analysis of Text"): 
#         analyzer = create_analyzer(task="sentiment", lang="en")
#         result = analyzer.predict(text)
#         if result.output == "POS": 
#             label = "POSITIVE"
#         elif result.output == "NEU": 
#             label = "NEUTRAL"
#         else: 
#             label = "NEGATIVE"
        
#         neg = result.probas["NEG"]
#         pos = result.probas["POS"]
#         neu = result.probas["NEU"]
#         st.write("The classification of the given text is " + label + " with the scores broken down as: Positive - " + str(pos) + ", Neutral - " + str(neu) + ", Negative - " + str(neg))