File size: 6,143 Bytes
3c77d98
cb58c8d
9a3ff71
cb58c8d
3c77d98
cb58c8d
 
 
 
 
 
 
 
 
 
 
 
 
7ba3a06
9a3ff71
7ba3a06
9a3ff71
 
 
3c77d98
9a3ff71
 
cb58c8d
9a3ff71
3c77d98
9a3ff71
cb58c8d
3c77d98
 
cb58c8d
9a3ff71
cb58c8d
9a3ff71
cb58c8d
 
 
9a3ff71
cb58c8d
 
9a3ff71
cb58c8d
9a3ff71
cb58c8d
 
 
 
 
 
9a3ff71
cb58c8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ba3a06
cb58c8d
 
 
 
 
7ba3a06
cb58c8d
 
 
 
 
 
7ba3a06
cb58c8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#using pipeline to predict the input text
import pandas as pd
from transformers import pipeline, AutoTokenizer
import pysbd

#-----------------Outcome Prediction-----------------
def outcome(text):
    label_mapping = {
        'delete': [0, 'LABEL_0'],
        'keep': [1, 'LABEL_1'],
        'merge': [2, 'LABEL_2'],
        'no consensus': [3, 'LABEL_3'],
        'speedy keep': [4, 'LABEL_4'],
        'speedy delete': [5, 'LABEL_5'],
        'redirect': [6, 'LABEL_6'],
        'withdrawn': [7, 'LABEL_7']
    }
    model_name = "research-dump/roberta-large_deletion_multiclass_complete_final"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = pipeline("text-classification", model=model_name, return_all_scores=True)
    
    # Tokenize and truncate the text
    tokens = tokenizer(text, truncation=True, max_length=512)
    truncated_text = tokenizer.decode(tokens['input_ids'], skip_special_tokens=True)
    
    results = model(truncated_text)
    
    res_list = []
    for result in results[0]:
        for key, value in label_mapping.items():
            if result['label'] == value[1]:
                res_list.append({'sentence': truncated_text, 'outcome': key, 'score': result['score']})
                break
    
    return res_list

#-----------------Stance Prediction-----------------

def extract_response(text, model_name, label_mapping):
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    pipe = pipeline("text-classification", model=model_name, tokenizer=tokenizer, top_k=None)

    tokens = tokenizer(text, truncation=True, max_length=512)
    truncated_text = tokenizer.decode(tokens['input_ids'], skip_special_tokens=True)
    
    results = pipe(truncated_text)
    
    final_scores = {key: 0.0 for key in label_mapping}
    for result in results[0]:
        for key, value in label_mapping.items():
            if result['label'] == f'LABEL_{value}':
                final_scores[key] = result['score']
                break
    
    return final_scores


def get_stance(text):
    label_mapping = {
            'delete': 0,
            'keep': 1,
            'merge': 2,
            'comment': 3
        }
    seg = pysbd.Segmenter(language="en", clean=False)
    text_list = seg.segment(text)
    model = 'research-dump/bert-large-uncased_wikistance_v1'
    res_list = []
    for t in text_list:
        res = extract_response(t, model,label_mapping) #, access_token)
        highest_key = max(res, key=res.get)
        highest_score = res[highest_key]
        result = {'sentence':t,'stance': highest_key, 'score': highest_score}
        res_list.append(result)
    
    return res_list


#-----------------Policy Prediction-----------------
def get_policy(text):
    label_mapping = {'Wikipedia:Notability': 0,
            'Wikipedia:What Wikipedia is not': 1,
            'Wikipedia:Neutral point of view': 2,
            'Wikipedia:Verifiability': 3,
            'Wikipedia:Wikipedia is not a dictionary': 4,
            'Wikipedia:Wikipedia is not for things made up one day': 5,
            'Wikipedia:Criteria for speedy deletion': 6,
            'Wikipedia:Deletion policy': 7,
            'Wikipedia:No original research': 8,
            'Wikipedia:Biographies of living persons': 9,
            'Wikipedia:Arguments to avoid in deletion discussions': 10,
            'Wikipedia:Conflict of interest': 11,
            'Wikipedia:Articles for deletion': 12
            }
    

    seg = pysbd.Segmenter(language="en", clean=False)
    text_list = seg.segment(text)
    model = 'research-dump/bert-large-uncased_wikistance_policy_v1'
    res_list = []
    
    for t in text_list:
        res = extract_response(t, model,label_mapping)
        highest_key = max(res, key=res.get)
        highest_score = res[highest_key]
        result = {'sentence': t, 'policy': highest_key, 'score': highest_score}
        res_list.append(result)
    
    return res_list



#-----------------Sentiment Analysis-----------------

def extract_highest_score_label(res):
    flat_res = [item for sublist in res for item in sublist]
    highest_score_item = max(flat_res, key=lambda x: x['score'])
    highest_score_label = highest_score_item['label']
    highest_score_value = highest_score_item['score']    
    return highest_score_label, highest_score_value


def get_sentiment(text):
    #sentiment analysis
    model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = pipeline("text-classification", model=model_name, top_k= None)

    #sentence tokenize the text using pysbd
    seg = pysbd.Segmenter(language="en", clean=False)
    text_list = seg.segment(text)

    res = []
    for t in text_list:
        results = model(t)
        highest_label, highest_score = extract_highest_score_label(results)
        result = {'sentence': t,'sentiment': highest_label, 'score': highest_score}
        res.append(result)
    return res


#-----------------Toxicity Prediction-----------------

def get_offensive_label(text):
    #offensive language detection model
    model_name = "cardiffnlp/twitter-roberta-base-offensive"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = pipeline("text-classification", model=model_name, top_k= None)

    #sentence tokenize the text using pysbd
    seg = pysbd.Segmenter(language="en", clean=False)
    text_list = seg.segment(text)

    res = []
    for t in text_list:
        results = model(t)
        highest_label, highest_score = extract_highest_score_label(results)
        result = {'sentence': t,'offensive_label': highest_label, 'score': highest_score}
        res.append(result)
    return res


#create the anchor function
def predict_text(text, model_name):
    if model_name == 'outcome':
        return outcome(text)
    elif model_name == 'stance':
        return get_stance(text)
    elif model_name == 'policy':
        return get_policy(text)
    elif model_name == 'sentiment':
        return get_sentiment(text)
    elif model_name == 'offensive':
        return get_offensive_label(text)
    else:
        return "Invalid Task name"