File size: 6,754 Bytes
4ba4a77
 
 
a199795
 
 
db9e998
 
4ba4a77
97d7261
 
a199795
 
 
 
 
 
 
97d7261
 
 
a199795
 
 
 
 
 
ea2cadc
 
a199795
ea2cadc
a199795
 
db9e998
 
a199795
 
 
 
ea2cadc
b507883
2443dec
8a63757
2443dec
b507883
a199795
 
 
 
ef27278
a199795
 
 
 
 
 
 
2443dec
b507883
2443dec
f10eece
ef27278
a199795
 
 
 
 
b07867d
a199795
 
 
 
 
 
 
 
 
 
 
2443dec
3fc83f7
 
 
 
 
 
 
2443dec
3fc83f7
2443dec
 
 
3fc83f7
2443dec
3fc83f7
2443dec
a199795
3fc83f7
2443dec
8a63757
86d1530
a199795
 
 
 
8a63757
a199795
 
3fc97a9
db9e998
a199795
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef27278
 
a199795
 
 
 
 
 
2443dec
b507883
2443dec
f10eece
a199795
2443dec
a199795
 
 
 
 
 
ef27278
a199795
ef27278
a199795
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
import requests
import random
import time
import pandas as pd
import gradio as gr
import numpy as np
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline

def read3(num_selected_former):
    fname = 'data3_convai2_inferred.txt'
    with open(fname, encoding='utf-8') as f:
        content = f.readlines()
        index_selected = random.randint(0,len(content)/2-1)
        while index_selected == num_selected_former:
            index_selected = random.randint(0,len(content)/2-1)
        text = eval(content[index_selected*2])
        interpretation = eval(content[int(index_selected*2+1)])
        
        min_len = 5

        tokens = [i[0] for i in interpretation]
        tokens = tokens[1:-1]
        while len(tokens) <= min_len or '\\' in text['text'] or '//' in text['text']:
            index_selected = random.randint(0,len(content)/2-1)
            text = eval(content[int(index_selected*2)])
        res_tmp = [(i, 0) for i in text['text'].split(' ')]
        res = {"original": text['text'], "interpretation": res_tmp}    
    return res, index_selected
    
def func3(num_selected, human_predict, num1, num2, user_important):
    chatbot = []
    # num1: Human score; num2: AI score
    fname = 'data3_convai2_inferred.txt'

    with open(fname) as f:
        content = f.readlines()
        text = eval(content[int(num_selected*2)])
        interpretation = eval(content[int(num_selected*2+1)])
        
        if text['binary_label'] == 1:
            golden_label = int(50 * (1 - text['binary_score']))
        else:
            golden_label = int(50 * (1 + text['binary_score']))
            
    # (START) off-the-shelf version -- slow at the beginning
    # Load model directly
    # Use a pipeline as a high-level helper

    classifier = pipeline("text-classification", model="padmajabfrl/Gender-Classification")
    output = classifier([text['text']])

    print(output)
    out = output[0]
    
    # (END) off-the-shelf version
    
    if out['label'] == 'Female':
        ai_predict = int(100 * out['score'])
    else:
        ai_predict = 100 - int(100 * out['score'])
    
    user_select = "You focused on "
    flag_select = False
    if user_important == "":
        user_select += "nothing. Interesting! "
    else:
        user_select += "'" + user_important + "'. "
    # for i in range(len(user_marks)):
    #     if user_marks[i][1] != None and h1[i][0] not in ["P", "N"]:
    #         flag_select = True
    #         user_select += "'" + h1[i][0] + "'"
    #         if i == len(h1) - 1:
    #             user_select += ". "
    #         else:
    #             user_select += ", "
    # if not flag_select:
    #     user_select += "nothing. Interesting! "
    user_select += "Wanna see how the AI made the guess? Click here. ⬅️"
    
    if golden_label > 60:
        gender = ' (female)'
    elif golden_label < 40:
        gender = ' (male)'    
    else:
        gender = ' (neutral)'
        
    if abs(golden_label - human_predict) <= 20  and abs(golden_label - ai_predict) <= 20:
        chatbot.append(("The correct answer is " + str(golden_label) + gender + ". Congratulations! 🎉 Both of you get the correct answer!", user_select))
        num1 += 1
        num2 += 1
    elif abs(golden_label - human_predict) > 20 and abs(golden_label - ai_predict) > 20:
        chatbot.append(("The correct answer is " + str(golden_label) + gender + ". Sorry.. No one gets the correct answer. But nice try! 😉", user_select))
    elif abs(golden_label - human_predict) <= 20 and abs(golden_label - ai_predict) > 20:
        chatbot.append(("The correct answer is " + str(golden_label) + gender + ". Great! 🎉 You are closer to the answer and better than AI!", user_select))
        num1 += 1
    else:
        chatbot.append(("The correct answer is " + str(golden_label) + gender + ". Sorry.. AI wins in this round.", user_select))
        num2 += 1
    
    tot_scores = ''' ### <p style="text-align: center;"> 🤖 Machine &ensp; ''' + str(int(num2)) + ''' &ensp; VS &ensp; ''' + str(int(num1)) + ''' &ensp; Human 👨👩 </p>'''

    
    num_tmp = max(num1, num2)
    y_lim_upper = (int((num_tmp + 3)/10)+1) * 10

    return ai_predict, chatbot, num1, num2, tot_scores

def interpre3(num_selected):
    fname = 'data3_convai2_inferred.txt'
    with open(fname) as f:
        content = f.readlines()
        text = eval(content[int(num_selected*2)])
        interpretation = eval(content[int(num_selected*2+1)])
    
    print(interpretation)

    res = {"original": text['text'], "interpretation": interpretation}
    # pos = []
    # neg = []
    # res = []
    # for i in interpretation:
    #     if i[1] > 0:
    #         pos.append(i[1])
    #     elif i[1] < 0:
    #         neg.append(i[1])
    #     else:
    #         continue
    # median_pos = np.median(pos)
    # median_neg = np.median(neg)


    # res.append(("P", "+"))
    # res.append(("/", None))
    # res.append(("N", "-"))
    # res.append(("Review:", None))
    # for i in interpretation:
    #     if i[1] > median_pos:
    #         res.append((i[0], "+"))
    #     elif i[1] < median_neg:
    #         res.append((i[0], "-"))
    #     else:
    #         res.append((i[0], None))
    return res

    
def func3_written(text_written, human_predict, lang_written):
    chatbot = []
    # num1: Human score; num2: AI score

    # (START) off-the-shelf version

    # tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
    # model = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")

    classifier = pipeline("text-classification", model="padmajabfrl/Gender-Classification")

    output = classifier([text_written])

    print(output)
    out = output[0]
    # (END) off-the-shelf version

    if out['label'] == 'Female':
        ai_predict = int(100 * out['score'])
    else:
        ai_predict = 100 - int(100 * out['score'])
    
    if abs(ai_predict - human_predict) <= 20:
        chatbot.append(("AI gives it a close score! 🎉", "⬅️ Feel free to try another one! ⬅️"))
    else:
        chatbot.append(("AI thinks in a different way from human. 😉", "⬅️ Feel free to try another one! ⬅️"))

    import shap

    gender_classifier = pipeline("text-classification", model="padmajabfrl/Gender-Classification", return_all_scores=True)

    explainer = shap.Explainer(gender_classifier)

    shap_values = explainer([text_written])
    interpretation = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
    
    res = {"original": text_written, "interpretation": interpretation}
    print(res)

    return res, ai_predict, chatbot