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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import random
|
| 3 |
import time
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
|
| 8 |
+
def read3(lang, num_selected_former):
|
| 9 |
+
if lang in ['en']:
|
| 10 |
+
fname = 'data1_en.txt'
|
| 11 |
+
else:
|
| 12 |
+
fname = 'data1_nl_10.txt'
|
| 13 |
+
with open(fname, encoding='utf-8') as f:
|
| 14 |
+
content = f.readlines()
|
| 15 |
+
index_selected = random.randint(0,len(content)/2-1)
|
| 16 |
+
while index_selected == num_selected_former:
|
| 17 |
+
index_selected = random.randint(0,len(content)/2-1)
|
| 18 |
+
text = eval(content[index_selected*2])
|
| 19 |
+
interpretation = eval(content[int(index_selected*2+1)])
|
| 20 |
+
if lang == 'en':
|
| 21 |
+
min_len = 4
|
| 22 |
+
else:
|
| 23 |
+
min_len = 2
|
| 24 |
+
tokens = [i[0] for i in interpretation]
|
| 25 |
+
tokens = tokens[1:-1]
|
| 26 |
+
while len(tokens) <= min_len or '\\' in text['text'] or '//' in text['text']:
|
| 27 |
+
index_selected = random.randint(0,len(content)/2-1)
|
| 28 |
+
text = eval(content[int(index_selected*2)])
|
| 29 |
+
res_tmp = [(i, 0) for i in text['text'].split(' ')]
|
| 30 |
+
res = {"original": text['text'], "interpretation": res_tmp}
|
| 31 |
+
# res_empty = {"original": "", "interpretation": []}
|
| 32 |
+
|
| 33 |
+
# res = []
|
| 34 |
+
# res.append(("P", "+"))
|
| 35 |
+
# res.append(("/", None))
|
| 36 |
+
# res.append(("N", "-"))
|
| 37 |
+
# res.append(("Review:", None))
|
| 38 |
+
# for i in text['text'].split(' '):
|
| 39 |
+
# res.append((i, None))
|
| 40 |
+
# res_empty = None
|
| 41 |
+
# checkbox_update = gr.CheckboxGroup.update(choices=tokens, value=None)
|
| 42 |
+
|
| 43 |
+
return res, lang, index_selected
|
| 44 |
+
|
| 45 |
+
def func3(lang_selected, num_selected, human_predict, num1, num2, user_important):
|
| 46 |
+
chatbot = []
|
| 47 |
+
# num1: Human score; num2: AI score
|
| 48 |
+
if lang_selected in ['en']:
|
| 49 |
+
fname = 'data1_en.txt'
|
| 50 |
+
else:
|
| 51 |
+
fname = 'data1_nl_10.txt'
|
| 52 |
+
with open(fname) as f:
|
| 53 |
+
content = f.readlines()
|
| 54 |
+
text = eval(content[int(num_selected*2)])
|
| 55 |
+
interpretation = eval(content[int(num_selected*2+1)])
|
| 56 |
+
if lang_selected in ['en']:
|
| 57 |
+
golden_label = text['label'] * 25
|
| 58 |
+
else:
|
| 59 |
+
golden_label = text['label'] * 100
|
| 60 |
+
|
| 61 |
+
'''
|
| 62 |
+
# (START) API version -- quick
|
| 63 |
+
|
| 64 |
+
API_URL = "https://api-inference.huggingface.co/models/nlptown/bert-base-multilingual-uncased-sentiment"
|
| 65 |
+
# API_URL = "https://api-inference.huggingface.co/models/cmarkea/distilcamembert-base-sentiment"
|
| 66 |
headers = {"Authorization": "Bearer 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"}
|
| 67 |
+
|
| 68 |
+
response = requests.post(API_URL, headers=headers, json=text['text'])
|
| 69 |
+
output = response.json()
|
| 70 |
+
|
| 71 |
+
# result = dict()
|
| 72 |
+
star2num = {
|
| 73 |
+
"5 stars": 100,
|
| 74 |
+
"4 stars": 75,
|
| 75 |
+
"3 stars": 50,
|
| 76 |
+
"2 stars": 25,
|
| 77 |
+
"1 star": 0,
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
print(output)
|
| 81 |
+
out = output[0][0]
|
| 82 |
+
# (END) API version
|
| 83 |
+
'''
|
| 84 |
+
|
| 85 |
+
# (START) off-the-shelf version -- slow at the beginning
|
| 86 |
+
# Load model directly
|
| 87 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 88 |
+
|
| 89 |
+
tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
| 90 |
+
model = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
| 91 |
+
|
| 92 |
+
# Use a pipeline as a high-level helper
|
| 93 |
+
from transformers import pipeline
|
| 94 |
+
|
| 95 |
+
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 96 |
+
output = classifier([text['text']])
|
| 97 |
+
|
| 98 |
+
star2num = {
|
| 99 |
+
"5 stars": 100,
|
| 100 |
+
"4 stars": 75,
|
| 101 |
+
"3 stars": 50,
|
| 102 |
+
"2 stars": 25,
|
| 103 |
+
"1 star": 0,
|
| 104 |
+
}
|
| 105 |
+
print(output)
|
| 106 |
+
out = output[0]
|
| 107 |
+
|
| 108 |
+
# (END) off-the-shelf version
|
| 109 |
+
|
| 110 |
+
ai_predict = star2num[out['label']]
|
| 111 |
+
# result[label] = out['score']
|
| 112 |
+
|
| 113 |
+
user_select = "You focused on "
|
| 114 |
+
flag_select = False
|
| 115 |
+
if user_important == "":
|
| 116 |
+
user_select += "nothing. Interesting! "
|
| 117 |
+
else:
|
| 118 |
+
user_select += user_important
|
| 119 |
+
user_select += ". "
|
| 120 |
+
# for i in range(len(user_marks)):
|
| 121 |
+
# if user_marks[i][1] != None and h1[i][0] not in ["P", "N"]:
|
| 122 |
+
# flag_select = True
|
| 123 |
+
# user_select += "'" + h1[i][0] + "'"
|
| 124 |
+
# if i == len(h1) - 1:
|
| 125 |
+
# user_select += ". "
|
| 126 |
+
# else:
|
| 127 |
+
# user_select += ", "
|
| 128 |
+
# if not flag_select:
|
| 129 |
+
# user_select += "nothing. Interesting! "
|
| 130 |
+
user_select += "Wanna see how the AI made the guess? Click here. β¬
οΈ"
|
| 131 |
+
if lang_selected in ['en']:
|
| 132 |
+
if ai_predict == golden_label:
|
| 133 |
+
if abs(human_predict - golden_label) < 12.5: # Both correct
|
| 134 |
+
golden_label = int((human_predict + ai_predict) / 2)
|
| 135 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Congratulations! π Both of you get the correct answer!", user_select))
|
| 136 |
+
num1 += 1
|
| 137 |
+
num2 += 1
|
| 138 |
+
else:
|
| 139 |
+
golden_label += random.randint(-2, 2)
|
| 140 |
+
while golden_label > 100 or golden_label < 0 or golden_label % 25 == 0:
|
| 141 |
+
golden_label += random.randint(-2, 2)
|
| 142 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Sorry.. AI wins in this round.", user_select))
|
| 143 |
+
num2 += 1
|
| 144 |
+
else:
|
| 145 |
+
if abs(human_predict - golden_label) < abs(ai_predict - golden_label):
|
| 146 |
+
if abs(human_predict - golden_label) < 12.5:
|
| 147 |
+
golden_label = int((golden_label + human_predict) / 2)
|
| 148 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Great! π You are closer to the answer and better than AI!", user_select))
|
| 149 |
+
num1 += 1
|
| 150 |
+
else:
|
| 151 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Both wrong... Maybe next time you'll win!", user_select))
|
| 152 |
+
else:
|
| 153 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Sorry.. No one gets the correct answer. But nice try! π", user_select))
|
| 154 |
+
else:
|
| 155 |
+
if golden_label == 100:
|
| 156 |
+
if ai_predict > 50 and human_predict > 50:
|
| 157 |
+
golden_label = int((human_predict + ai_predict)/2) + random.randint(-10, 10)
|
| 158 |
+
while golden_label > 100:
|
| 159 |
+
golden_label = int((human_predict + ai_predict)/2) + random.randint(-10, 10)
|
| 160 |
+
ai_predict = int((golden_label + ai_predict) / 2)
|
| 161 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Congratulations! π Both of you get the correct answer!", user_select))
|
| 162 |
+
num1 += 1
|
| 163 |
+
num2 += 1
|
| 164 |
+
elif ai_predict > 50 and human_predict <= 50:
|
| 165 |
+
golden_label -= random.randint(0, 10)
|
| 166 |
+
ai_predict = 90 + random.randint(-5, 5)
|
| 167 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Sorry.. AI wins in this round.", user_select))
|
| 168 |
+
num2 += 1
|
| 169 |
+
elif ai_predict <= 50 and human_predict > 50:
|
| 170 |
+
golden_label = human_predict + random.randint(-4, 4)
|
| 171 |
+
while golden_label > 100:
|
| 172 |
+
golden_label = human_predict + random.randint(-4, 4)
|
| 173 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Great! π You are close to the answer and better than AI!", user_select))
|
| 174 |
+
num1 += 1
|
| 175 |
+
else:
|
| 176 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Sorry... No one gets the correct answer. But nice try! π", user_select))
|
| 177 |
+
else:
|
| 178 |
+
if ai_predict < 50 and human_predict < 50:
|
| 179 |
+
golden_label = int((human_predict + ai_predict)/2) + random.randint(-10, 10)
|
| 180 |
+
while golden_label < 0:
|
| 181 |
+
golden_label = int((human_predict + ai_predict)/2) + random.randint(-10, 10)
|
| 182 |
+
ai_predict = int((golden_label + ai_predict) / 2)
|
| 183 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Congratulations! π Both of you get the correct answer!", user_select))
|
| 184 |
+
num1 += 1
|
| 185 |
+
num2 += 1
|
| 186 |
+
elif ai_predict < 50 and human_predict >= 50:
|
| 187 |
+
golden_label += random.randint(0, 10)
|
| 188 |
+
ai_predict = 10 + random.randint(-5, 5)
|
| 189 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Sorry.. AI wins in this round.", user_select))
|
| 190 |
+
num2 += 1
|
| 191 |
+
elif ai_predict >= 50 and human_predict < 50:
|
| 192 |
+
golden_label = human_predict + random.randint(-4, 4)
|
| 193 |
+
while golden_label < 0:
|
| 194 |
+
golden_label = human_predict + random.randint(-4, 4)
|
| 195 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Great! π You are close to the answer and better than AI!", user_select))
|
| 196 |
+
num1 += 1
|
| 197 |
+
else:
|
| 198 |
+
chatbot.append(("The correct answer is " + str(golden_label) + ". Sorry... No one gets the correct answer. But nice try! π", user_select))
|
| 199 |
+
|
| 200 |
+
# data = pd.DataFrame(
|
| 201 |
+
# {
|
| 202 |
+
# "Role": ["AI π€", "HUMAN π¨π©"],
|
| 203 |
+
# "Scores": [num2, num1],
|
| 204 |
+
# }
|
| 205 |
+
# )
|
| 206 |
+
# scroe_human = ''' # Human: ''' + str(int(num1))
|
| 207 |
+
# scroe_robot = ''' # Robot: ''' + str(int(num2))
|
| 208 |
+
tot_scores = ''' ### <p style="text-align: center;"> Machine   ''' + str(int(num2)) + '''   VS   ''' + str(int(num1)) + '''   Human </p>'''
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
num_tmp = max(num1, num2)
|
| 212 |
+
y_lim_upper = (int((num_tmp + 3)/10)+1) * 10
|
| 213 |
+
# figure = gr.BarPlot.update(
|
| 214 |
+
# data,
|
| 215 |
+
# x="Role",
|
| 216 |
+
# y="Scores",
|
| 217 |
+
# color="Role",
|
| 218 |
+
# vertical=False,
|
| 219 |
+
# y_lim=[0,y_lim_upper],
|
| 220 |
+
# color_legend_position='none',
|
| 221 |
+
# height=250,
|
| 222 |
+
# width=500,
|
| 223 |
+
# show_label=False,
|
| 224 |
+
# container=False,
|
| 225 |
+
# )
|
| 226 |
+
# tooltip=["Role", "Scores"],
|
| 227 |
+
return ai_predict, chatbot, num1, num2, tot_scores
|
| 228 |
+
|
| 229 |
+
def interpre3(lang_selected, num_selected):
|
| 230 |
+
if lang_selected in ['en']:
|
| 231 |
+
fname = 'data1_en.txt'
|
| 232 |
+
else:
|
| 233 |
+
fname = 'data1_nl_10.txt'
|
| 234 |
+
with open(fname) as f:
|
| 235 |
+
content = f.readlines()
|
| 236 |
+
text = eval(content[int(num_selected*2)])
|
| 237 |
+
interpretation = eval(content[int(num_selected*2+1)])
|
| 238 |
+
|
| 239 |
+
print(interpretation)
|
| 240 |
+
|
| 241 |
+
res = {"original": text['text'], "interpretation": interpretation}
|
| 242 |
+
# pos = []
|
| 243 |
+
# neg = []
|
| 244 |
+
# res = []
|
| 245 |
+
# for i in interpretation:
|
| 246 |
+
# if i[1] > 0:
|
| 247 |
+
# pos.append(i[1])
|
| 248 |
+
# elif i[1] < 0:
|
| 249 |
+
# neg.append(i[1])
|
| 250 |
+
# else:
|
| 251 |
+
# continue
|
| 252 |
+
# median_pos = np.median(pos)
|
| 253 |
+
# median_neg = np.median(neg)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
# res.append(("P", "+"))
|
| 257 |
+
# res.append(("/", None))
|
| 258 |
+
# res.append(("N", "-"))
|
| 259 |
+
# res.append(("Review:", None))
|
| 260 |
+
# for i in interpretation:
|
| 261 |
+
# if i[1] > median_pos:
|
| 262 |
+
# res.append((i[0], "+"))
|
| 263 |
+
# elif i[1] < median_neg:
|
| 264 |
+
# res.append((i[0], "-"))
|
| 265 |
+
# else:
|
| 266 |
+
# res.append((i[0], None))
|
| 267 |
+
return res
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def func3_written(text_written, human_predict, lang_written):
|
| 271 |
+
chatbot = []
|
| 272 |
+
# num1: Human score; num2: AI score
|
| 273 |
+
|
| 274 |
+
'''
|
| 275 |
+
# (START) API version
|
| 276 |
|
| 277 |
+
API_URL = "https://api-inference.huggingface.co/models/nlptown/bert-base-multilingual-uncased-sentiment"
|
| 278 |
+
# API_URL = "https://api-inference.huggingface.co/models/cmarkea/distilcamembert-base-sentiment"
|
| 279 |
+
headers = {"Authorization": "Bearer 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"}
|
| 280 |
+
|
| 281 |
+
response = requests.post(API_URL, headers=headers, json=text_written)
|
| 282 |
output = response.json()
|
| 283 |
+
|
| 284 |
+
# result = dict()
|
| 285 |
+
star2num = {
|
| 286 |
+
"5 stars": 100,
|
| 287 |
+
"4 stars": 75,
|
| 288 |
+
"3 stars": 50,
|
| 289 |
+
"2 stars": 25,
|
| 290 |
+
"1 star": 0,
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
out = output[0][0]
|
| 294 |
+
# (END) API version
|
| 295 |
+
'''
|
| 296 |
+
|
| 297 |
+
# (START) off-the-shelf version
|
| 298 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 299 |
+
from transformers import pipeline
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
# tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
| 303 |
+
# model = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
|
| 304 |
+
|
| 305 |
+
classifier = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
| 306 |
+
|
| 307 |
+
output = classifier([text_written])
|
| 308 |
+
|
| 309 |
+
star2num = {
|
| 310 |
+
"5 stars": 100,
|
| 311 |
+
"4 stars": 75,
|
| 312 |
+
"3 stars": 50,
|
| 313 |
+
"2 stars": 25,
|
| 314 |
+
"1 star": 0,
|
| 315 |
+
}
|
| 316 |
+
print(output)
|
| 317 |
+
out = output[0]
|
| 318 |
+
# (END) off-the-shelf version
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
ai_predict = star2num[out['label']]
|
| 322 |
+
# result[label] = out['score']
|
| 323 |
+
|
| 324 |
+
if abs(ai_predict - human_predict) <= 12.5:
|
| 325 |
+
chatbot.append(("AI gives it a close score! π", "β¬
οΈ Feel free to try another one! β¬
οΈ"))
|
| 326 |
+
else:
|
| 327 |
+
ai_predict += random.randint(-2, 2)
|
| 328 |
+
while ai_predict > 100 or ai_predict < 0 or ai_predict % 25 == 0:
|
| 329 |
+
ai_predict += random.randint(-2, 2)
|
| 330 |
+
chatbot.append(("AI thinks in a different way from human. π", "β¬
οΈ Feel free to try another one! β¬
οΈ"))
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
import shap
|
| 334 |
+
|
| 335 |
+
# sentiment_classifier = pipeline("text-classification", return_all_scores=True)
|
| 336 |
+
if lang_written == "Dutch":
|
| 337 |
+
sentiment_classifier = pipeline("text-classification", model='DTAI-KULeuven/robbert-v2-dutch-sentiment', return_all_scores=True)
|
| 338 |
+
else:
|
| 339 |
+
sentiment_classifier = pipeline("text-classification", model='distilbert-base-uncased-finetuned-sst-2-english', return_all_scores=True)
|
| 340 |
+
|
| 341 |
+
explainer = shap.Explainer(sentiment_classifier)
|
| 342 |
+
|
| 343 |
+
shap_values = explainer([text_written])
|
| 344 |
+
interpretation = list(zip(shap_values.data[0], shap_values.values[0, :, 1]))
|
| 345 |
+
|
| 346 |
+
res = {"original": text_written, "interpretation": interpretation}
|
| 347 |
+
print(res)
|
| 348 |
+
|
| 349 |
+
return res, ai_predict, chatbot
|