File size: 33,036 Bytes
96d3362 718d231 96d3362 fffe047 96d3362 bc2a174 96d3362 bc2a174 96d3362 fd0f496 bc2a174 96d3362 ba42f25 0f98ea0 d3f89de 0f98ea0 d3f89de 4557ff8 0f98ea0 fffe047 f0f4ea0 5a28255 fffe047 62e2909 8c5938d a5b1363 de3a3ea b6433bb aa31d18 b6433bb 8c5938d 4ba24c3 8c5938d de3a3ea 48ae6b7 de3a3ea 48ae6b7 8c5938d de3a3ea 00765ea 18edfec 96d3362 18edfec eeff5a7 aa31d18 02664bd b6433bb 4ba24c3 02664bd b1c0e1c 18edfec ad8e8ee 6a0831e 48ac9c9 a3eabb7 ad8e8ee 0d89b24 a3eabb7 0d89b24 eeff5a7 b77a280 33db681 ad8e8ee 6354e98 96d3362 718d231 96d3362 09bf063 96d3362 dc27637 aef128b 96d3362 ed05241 96d3362 ef00b69 718d231 96d3362 dc27637 aef128b dc27637 96d3362 dc27637 718d231 96d3362 dc27637 aef128b dc27637 96d3362 dc27637 96d3362 dc27637 aef128b dc27637 96d3362 dc27637 96d3362 dc27637 96d3362 dc27637 aef128b dc27637 96d3362 dc27637 96d3362 dc27637 96d3362 dc27637 aef128b dc27637 96d3362 dc27637 718d231 dc27637 96d3362 b77a280 718d231 96d3362 718d231 96d3362 b77a280 718d231 b77a280 718d231 30d861e 718d231 835c7af 718d231 c493c66 46bdc18 1ef5427 835c7af fd0f496 c493c66 46bdc18 c493c66 66afc93 c493c66 66afc93 c493c66 66afc93 c493c66 d9d5b25 718d231 66afc93 381ad46 96d3362 b77a280 718d231 30d861e 718d231 835c7af 718d231 589e750 718d231 46bdc18 718d231 835c7af fd0f496 718d231 46bdc18 718d231 66afc93 718d231 66afc93 718d231 66afc93 718d231 66afc93 381ad46 718d231 b77a280 718d231 30d861e 718d231 835c7af 718d231 589e750 718d231 46bdc18 718d231 835c7af fd0f496 718d231 46bdc18 718d231 66afc93 718d231 66afc93 718d231 66afc93 718d231 66afc93 381ad46 718d231 b77a280 718d231 30d861e 718d231 835c7af 718d231 589e750 718d231 46bdc18 718d231 835c7af fd0f496 718d231 46bdc18 718d231 66afc93 718d231 66afc93 718d231 66afc93 718d231 66afc93 381ad46 718d231 b77a280 718d231 30d861e 718d231 835c7af 718d231 589e750 2d1590e 718d231 46bdc18 718d231 835c7af fd0f496 718d231 46bdc18 718d231 66afc93 718d231 66afc93 718d231 66afc93 718d231 66afc93 381ad46 718d231 96d3362 2c3cf7b 606cf69 2c3cf7b 46bdc18 c08f173 834dbac b77a280 2c3cf7b 4f6b5e5 46bdc18 2c3cf7b 46bdc18 9d334f4 b0e98cc 2c3cf7b 9d334f4 46bdc18 |
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
import streamlit as st
from transformers import pipeline
import re
import time
import requests
from PIL import Image
HF_SPACES_API_KEY = st.secrets["HF_token"]
API_URL = "https://api-inference.huggingface.co/models/microsoft/prophetnet-large-uncased-squad-qg"
headers = {"Authorization": HF_SPACES_API_KEY}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
#-----------------------------------------------------------
API_URL_evidence = "https://api-inference.huggingface.co/models/google/flan-t5-xxl"
headers_evidence = {"Authorization": HF_SPACES_API_KEY}
def query_evidence(payload):
response = requests.post(API_URL_evidence, headers=headers_evidence, json=payload)
return response.json()
#-----------------------------------------------------------
st.title('Welcome to :blue[FACTIFY - 5WQA] ')
# st.set_page_config(
# page_title="Welcome to :blue[FACTIFY - 5WQA]",
# layout="wide"
# )
# st.markdown("<center> <h2> 5W Aspect-based Fact Verification through Question Answering :blue[Web Demo]", unsafe_allow_html=True)
#st.markdown("<center>Ask a question about the collapse of the Silicon Valley Bank (SVB).</center>", unsafe_allow_html=True)
st.header('5W Aspect-based Fact Verification through Question Answering :blue[Web Demo]')
image = Image.open('5W QA Illustration.jpg')
st.image(image, caption='5W QA Generation Pipeline')
st.subheader('Here are a few steps to begin exploring and interacting with this demo.')
st.caption('First you need to input your claim.')
st.caption('Then you need to input your evidence.')
st.caption('Upon completing these two steps, kindly wait for a minute to receive the results.')
st.caption('Start by inputting the following instance of a claim and corresponding evidence into the designated text fields.')
#-----------------------------------------------------------------------------------------------
st.caption('**Example 1**')
st.caption(''':green[Claim:] :point_right: Moderna's legal actions towards Pfizer-BioNTech indicate that the development of COVID-19 vaccines was underway prior to the commencement of the pandemic.''')
st.caption(''':green[Evidence:] :point_right: Moderna is suing Pfizer and BioNTech for patent infringement, alleging the rival companies used key parts of its mRNA technology to develop their COVID-19 vaccine. Modernaβs patents were filed between 2010 and 2016.
''')
# st.caption(''':green[Evidence:] :point_right: Due to the consumers increasingly relying on online retailers,
# Amazon planned to hire over 99,000 workers in the warehouse and delivery sector during the Pandemic in the USA.''')
#-----------------------------------------------------------------------------------------------
st.caption('**Example 2**')
st.caption(''':green[Claim:] :point_right: In China, Buddhist monks and nuns lived together in places such as the Yunnan monastery.''')
st.caption(''':green[Evidence:] :point_right: Monastics in Japan are particularly exceptional in the Buddhist tradition because the monks and nuns can marry after receiving their higher ordination . ''')
#-----------------------------------------------------------------------------------------------
st.caption('**Example 3**')
st.caption(''':green[Claim:] :point_right: In Batman, Penguin hydrates the henchmen with water contaminated with atomic waste.''')
st.caption(''':green[Evidence:] :point_right: And Penguin even schemes his way into the Batcave along with five dehydrated henchmen ;
this plan fails when the henchmen are unexpectedly killed
when he mistakenly rehydrates them with heavy water contaminated with atomic waste ,
regularly used to recharge the Batcave s atomic pile . ''')
#-----------------------------------------------------------
st.caption('**Example 4**')
st.caption(''':green[Claim:] :point_right: Amazon to hire 100K workers and until April Amazon will raise hourly wages by $2 due to pandemic demand.''')
st.caption(''':green[Evidence:] : Due to the consumers increasingly relying on online retailers, Amazon planned to hire over 99,000 workers in the warehouse and delivery sector during the Pandemic in the USA.''')
#-----------------------------------------------------------
def proc():
st.write(st.session_state.text_key)
claim_text=st.text_area("Enter your claim:", on_change=proc, key='text_key')
evidence_text=st.text_area("Enter your evidence:")
# form_evidence = st.form(key='my_evidence')
# form_evidence.text_input(label='Enter your evidence')
# evidence_text = form_evidence.form_submit_button(label='Submit')
# if evidence_text:
#st.caption(':green[Kindly hold on for a few minutes while the QA pairs are being generated]')
#st.caption(':blue[At times, you may encounter null/none outputs, which could be a result of a delay in loading the models through the API. If you experience this problem, kindly try again after a few minutes.]')
import pandas as pd
from rouge_score import rouge_scorer
import numpy as np
from allennlp.predictors.predictor import Predictor
import allennlp_models.tagging
predictor = Predictor.from_path("structured-prediction-srl-bert.tar.gz")
#---------------------------------------------------------------
list_of_pronouns = ["I", "you", "he", "she", "it", "we", "they", "me", "him", "her", "us", "them",
"mine", "yours", "his", "hers", "its", "ours", "theirs",
"this", "that", "these", "those",
"myself", "yourself", "himself", "herself", "itself", "ourselves", "yourselves", "themselves",
"who", "whom", "what", "which", "whose",
"all", "another", "any", "anybody", "anyone", "anything", "both", "each", "either",
"everybody", "everyone", "everything", "few", "many", "neither", "nobody", "none", "nothing",
"one", "other", "several", "some", "somebody", "someone", "something"]
#---------------------------------------------------------------
# @st.cache
def claim(text):
import re
def remove_special_chars(text):
# Remove special characters that are not in between numbers
text = re.sub(r'(?<!\d)[^\w\s]+(?!\d)', '', text)
return text
df = pd.DataFrame({'claim' : remove_special_chars(text)},index=[0])
def srl_allennlp(sent):
try:
#result = predictor.predict(sentence=sent)['verbs'][0]['description']
#result = predictor.predict(sentence=sent)['verbs'][0]['tags']
result = predictor.predict(sentence=sent)
return(result)
except IndexError:
pass
#return(predictor.predict(sentence=sent))
df['allennlp_srl'] = df['claim'].apply(lambda x: srl_allennlp(x))
df['number_of_verbs'] = ''
df['verbs_group'] = ''
df['words'] = ''
df['verbs'] = ''
df['modified'] =''
col1 = df['allennlp_srl']
for i in range(len(col1)):
num_verb = len(col1[i]['verbs'])
df['number_of_verbs'][i] = num_verb
df['verbs_group'][i] = col1[i]['verbs']
df['words'][i] = col1[i]['words']
x=[]
for verb in range(len(col1[i]['verbs'])):
x.append(col1[i]['verbs'][verb]['verb'])
df['verbs'][i] = x
verb_dict ={}
desc = []
for j in range(len(col1[i]['verbs'])):
string = (col1[i]['verbs'][j]['description'])
string = string.replace("ARG0", "who")
string = string.replace("ARG1", "what")
string = string.replace("ARGM-TMP", "when")
string = string.replace("ARGM-LOC", "where")
string = string.replace("ARGM-CAU", "why")
desc.append(string)
verb_dict[col1[i]['verbs'][j]['verb']]=string
df['modified'][i] = verb_dict
#----------FOR COLUMN "WHO"------------#
df['who'] = ''
for j in range(len(df['modified'])):
val_list = []
val_string = ''
for k,v in df['modified'][j].items():
val_list.append(v)
who = set() # use set to remove duplicates
for indx in range(len(val_list)):
val_string = val_list[indx]
pos = val_string.find("who: ")
substr = ''
if pos != -1:
for i in range(pos+5, len(val_string)):
if val_string[i] == "]":
break
else:
substr = substr + val_string[i]
substr = substr.strip() # remove leading/trailing white space
pronouns = list_of_pronouns
if substr.lower() not in pronouns and not substr.lower().endswith("'s"): # remove pronouns and possessive pronouns
who.add(substr)
else:
pass
df['who'][j] = "<sep>".join(who)
# else:
# continue
#----------FOR COLUMN "WHAT"------------#
df['what'] = ''
for j in range(len(df['modified'])):
val_list = []
val_string = ''
for k,v in df['modified'][j].items():
val_list.append(v)
what = set() # use set to remove duplicates
for indx in range(len(val_list)):
val_string = val_list[indx]
pos = val_string.find("what: ")
substr = ''
if pos != -1:
for i in range(pos+5, len(val_string)):
if val_string[i] == "]":
break
else:
substr = substr + val_string[i]
substr = substr.strip() # remove leading/trailing white space
pronouns = list_of_pronouns
if substr.lower() not in pronouns and not substr.lower().endswith("'s"): # remove pronouns and possessive pronouns
what.add(substr)
else:
pass
df['what'][j] = "<sep>".join(what)
#----------FOR COLUMN "WHY"------------#
df['why'] = ''
for j in range(len(df['modified'])):
val_list = []
val_string = ''
for k,v in df['modified'][j].items():
val_list.append(v)
why = set() # use set to remove duplicates
for indx in range(len(val_list)):
val_string = val_list[indx]
pos = val_string.find("why: ")
substr = ''
if pos != -1:
for i in range(pos+5, len(val_string)):
if val_string[i] == "]":
break
else:
substr = substr + val_string[i]
substr = substr.strip() # remove leading/trailing white space
pronouns = list_of_pronouns
if substr.lower() not in pronouns and not substr.lower().endswith("'s"): # remove pronouns and possessive pronouns
why.add(substr)
else:
pass
df['why'][j] = "<sep>".join(why)
#----------FOR COLUMN "WHEN"------------#
df['when'] = ''
for j in range(len(df['modified'])):
val_list = []
val_string = ''
for k,v in df['modified'][j].items():
val_list.append(v)
when = set() # use set to remove duplicates
for indx in range(len(val_list)):
val_string = val_list[indx]
pos = val_string.find("when: ")
substr = ''
if pos != -1:
for i in range(pos+5, len(val_string)):
if val_string[i] == "]":
break
else:
substr = substr + val_string[i]
substr = substr.strip() # remove leading/trailing white space
pronouns = list_of_pronouns
if substr.lower() not in pronouns and not substr.lower().endswith("'s"): # remove pronouns and possessive pronouns
when.add(substr)
else:
pass
df['when'][j] = "<sep>".join(when)
#----------FOR COLUMN "WHERE"------------#
df['where'] = ''
for j in range(len(df['modified'])):
val_list = []
val_string = ''
for k,v in df['modified'][j].items():
val_list.append(v)
where = set() # use set to remove duplicates
for indx in range(len(val_list)):
val_string = val_list[indx]
pos = val_string.find("where: ")
substr = ''
if pos != -1:
for i in range(pos+5, len(val_string)):
if val_string[i] == "]":
break
else:
substr = substr + val_string[i]
substr = substr.strip() # remove leading/trailing white space
pronouns = list_of_pronouns
if substr.lower() not in pronouns and not substr.lower().endswith("'s"): # remove pronouns and possessive pronouns
where.add(substr)
else:
pass
df['where'][j] = "<sep>".join(where)
data=df[["claim","who","what","why","when","where"]].copy()
return data
#-------------------------------------------------------------------------
# @st.cache
def split_ws(input_list, delimiter="<sep>"):
output_list = []
for item in input_list:
split_item = item.split(delimiter)
for sub_item in split_item:
sub_item = sub_item.strip()
if sub_item:
output_list.append(sub_item)
return output_list
#--------------------------------------------------------------------------
# @st.cache
def calc_rouge_l_score(list_of_evidence, list_of_ans):
scorer = rouge_scorer.RougeScorer(['rougeL'], use_stemmer=True)
scores = scorer.score(' '.join(list_of_evidence), ' '.join(list_of_ans))
return scores['rougeL'].fmeasure
#-------------------------------------------------------------------------
# @st.cache
def rephrase_question_who(question):
if not question.lower().startswith("who"):
words = question.split()
words[0] = "Who"
return " ".join(words)
else:
return question
#------------------------------------------------------------------------
# @st.cache
def gen_qa_who(df):
list_of_ques_who=[]
list_of_ans_who=[]
list_of_evidence_answer_who=[]
rouge_l_scores=[]
for i,row in df.iterrows():
srl=df["who"][i]
claim=df['claim'][i]
answer= split_ws(df["who"])
evidence=df["evidence"][i]
time.sleep(5)
if srl!="":
try:
for j in range(0,len(answer)):
FACT_TO_GENERATE_QUESTION_FROM = f"""{answer[j]} [SEP] {claim}"""
#FACT_TO_GENERATE_QUESTION_FROM = f"""generate_who_based_question_from_context_using_the_next_answer:{answer[j]} [SEP] context:{claim}"""
#time.sleep(10)
question_ids = query({"inputs":FACT_TO_GENERATE_QUESTION_FROM,
"num_beams":5,
"early_stopping":True,
"min_length": 100,"wait_for_model":True})[0]['generated_text'].capitalize()
question_ids = rephrase_question_who(question_ids)
list_of_ques_who.append(f"""Q{j+1} :\n {question_ids}""")
list_of_ans_who.append(f"""Claim :\n {answer[j]}""")
input_evidence = f"answer_the_next_question_from_context: {question_ids} context: {evidence}"
time.sleep(5)
answer_evidence = query_evidence({"inputs":input_evidence,"truncation":True,"wait_for_model":True})[0]['generated_text']
if answer_evidence.lower() in evidence.lower():
list_of_evidence_answer_who.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
else:
answer_evidence=""
list_of_evidence_answer_who.append(f"""No mention of 'who'in any related documents.""")
threshold = 0.2
list_of_pairs = [(answer_evidence, answer[j])]
rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
if rouge_l_score >= threshold:
verification_status = 'β
Verified Valid'
elif rouge_l_score == 0:
verification_status = 'β Not verifiable'
else:
verification_status = 'β Verified False'
rouge_l_scores.append(verification_status)
except:
pass
else:
list_of_ques_who="No claims"
list_of_ans_who=""
list_of_evidence_answer_who="No mention of 'who'in any related documents."
rouge_l_scores="β Not verifiable"
return list_of_ques_who,list_of_ans_who,list_of_evidence_answer_who,rouge_l_scores
#------------------------------------------------------------
# @st.cache
def rephrase_question_what(question):
if not question.lower().startswith("what"):
words = question.split()
words[0] = "What"
return " ".join(words)
else:
return question
#----------------------------------------------------------
# @st.cache
def gen_qa_what(df):
list_of_ques_what=[]
list_of_ans_what=[]
list_of_evidence_answer_what=[]
rouge_l_scores=[]
for i,row in df.iterrows():
srl=df["what"][i]
claim=df['claim'][i]
answer= split_ws(df["what"])
evidence=df["evidence"][i]
time.sleep(5)
if srl!="":
try:
for j in range(0,len(answer)):
FACT_TO_GENERATE_QUESTION_FROM = f"""{answer[j]} [SEP] context:{claim}"""
#time.sleep(10)
question_ids = query({"inputs":FACT_TO_GENERATE_QUESTION_FROM,
"num_beams":5,
"early_stopping":True,
"min_length": 100,"wait_for_model":True})[0]['generated_text'].capitalize()
question_ids = rephrase_question_what(question_ids)
list_of_ques_what.append(f"""Q{j+1}:{question_ids}""")
list_of_ans_what.append(f"""Claim :\n {answer[j]}""")
input_evidence = f"answer_the_next_question_from_context: {question_ids} context: {evidence}"
time.sleep(5)
answer_evidence = query_evidence({"inputs":input_evidence,"truncation":True,"wait_for_model":True})[0]['generated_text']
if answer_evidence.lower() in evidence.lower():
list_of_evidence_answer_what.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
else:
answer_evidence=""
list_of_evidence_answer_what.append(f"""No mention of 'what'in any related documents.""")
threshold = 0.2
list_of_pairs = [(answer_evidence, answer[j])]
rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
if rouge_l_score >= threshold:
verification_status = 'β
Verified Valid'
elif rouge_l_score == 0:
verification_status = 'β Not verifiable'
else:
verification_status = 'β Verified False'
rouge_l_scores.append(verification_status)
except:
pass
else:
list_of_ques_what="No claims"
list_of_ans_what=""
list_of_evidence_answer_what="No mention of 'what'in any related documents."
rouge_l_scores="β Not verifiable"
return list_of_ques_what,list_of_ans_what,list_of_evidence_answer_what,rouge_l_scores
#----------------------------------------------------------
# @st.cache
def rephrase_question_why(question):
if not question.lower().startswith("why"):
words = question.split()
words[0] = "Why"
return " ".join(words)
else:
return question
#---------------------------------------------------------
# @st.cache
def gen_qa_why(df):
list_of_ques_why=[]
list_of_ans_why=[]
list_of_evidence_answer_why=[]
rouge_l_scores=[]
for i,row in df.iterrows():
srl=df["why"][i]
claim=df['claim'][i]
answer= split_ws(df["why"])
evidence=df["evidence"][i]
time.sleep(5)
if srl!="":
try:
for j in range(0,len(answer)):
FACT_TO_GENERATE_QUESTION_FROM = f"""{answer[j]} [SEP] {claim}"""
#time.sleep(10)
question_ids = query({"inputs":FACT_TO_GENERATE_QUESTION_FROM,
"num_beams":5,
"early_stopping":True,
"min_length": 100,"wait_for_model":True})[0]['generated_text'].capitalize()
question_ids = rephrase_question_why(question_ids)
list_of_ques_why.append(f"""Q{j+1}:{question_ids}""")
list_of_ans_why.append(f"""Claim :\n {answer[j]}""")
input_evidence = f"answer_the_next_question_from_context: {question_ids} context: {evidence}"
time.sleep(5)
answer_evidence = query_evidence({"inputs":input_evidence,"truncation":True,"wait_for_model":True})[0]['generated_text']
if answer_evidence.lower() in evidence.lower():
list_of_evidence_answer_why.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
else:
answer_evidence=""
list_of_evidence_answer_why.append(f"""No mention of 'why'in any related documents.""")
threshold = 0.2
list_of_pairs = [(answer_evidence, answer[j])]
rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
if rouge_l_score >= threshold:
verification_status = 'β
Verified Valid'
elif rouge_l_score == 0:
verification_status = 'β Not verifiable'
else:
verification_status = 'β Verified False'
rouge_l_scores.append(verification_status)
except:
pass
else:
list_of_ques_why="No claims"
list_of_ans_why=""
list_of_evidence_answer_why="No mention of 'why'in any related documents."
rouge_l_scores="β Not verifiable"
return list_of_ques_why,list_of_ans_why,list_of_evidence_answer_why,rouge_l_scores
#---------------------------------------------------------
# @st.cache
def rephrase_question_when(question):
if not question.lower().startswith("when"):
words = question.split()
words[0] = "When"
return " ".join(words)
else:
return question
#---------------------------------------------------------
# @st.cache
def gen_qa_when(df):
list_of_ques_when=[]
list_of_ans_when=[]
list_of_evidence_answer_when=[]
rouge_l_scores=[]
for i,row in df.iterrows():
srl=df["when"][i]
claim=df['claim'][i]
answer= split_ws(df["when"])
evidence=df["evidence"][i]
time.sleep(5)
if srl!="":
try:
for j in range(0,len(answer)):
FACT_TO_GENERATE_QUESTION_FROM = f"""{answer[j]} [SEP] {claim}"""
#time.sleep(10)
question_ids = query({"inputs":FACT_TO_GENERATE_QUESTION_FROM,
"num_beams":5,
"early_stopping":True,
"min_length": 100,"wait_for_model":True})[0]['generated_text'].capitalize()
question_ids = rephrase_question_when(question_ids)
list_of_ques_when.append(f"""Q{j+1}:{question_ids}""")
list_of_ans_when.append(f"""Claim :\n {answer[j]}""")
input_evidence = f"answer_the_next_question_from_context: {question_ids} context: {evidence}"
time.sleep(5)
answer_evidence = query_evidence({"inputs":input_evidence,"truncation":True,"wait_for_model":True})[0]['generated_text']
if answer_evidence.lower() in evidence.lower():
list_of_evidence_answer_when.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
else:
answer_evidence=""
list_of_evidence_answer_when.append(f"""No mention of 'when'in any related documents.""")
threshold = 0.2
list_of_pairs = [(answer_evidence, answer[j])]
rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
if rouge_l_score >= threshold:
verification_status = 'β
Verified Valid'
elif rouge_l_score == 0:
verification_status = 'β Not verifiable'
else:
verification_status = 'β Verified False'
rouge_l_scores.append(verification_status)
except:
pass
else:
list_of_ques_when="No claims"
list_of_ans_when=""
list_of_evidence_answer_when="No mention of 'when'in any related documents."
rouge_l_scores="β Not verifiable"
return list_of_ques_when,list_of_ans_when,list_of_evidence_answer_when,rouge_l_scores
#------------------------------------------------------
# @st.cache
def rephrase_question_where(question):
if not question.lower().startswith("where"):
words = question.split()
words[0] = "Where"
return " ".join(words)
else:
return question
#------------------------------------------------------
# @st.cache
def gen_qa_where(df):
list_of_ques_where=[]
list_of_ans_where=[]
list_of_evidence_answer_where=[]
rouge_l_scores=[]
for i,row in df.iterrows():
srl=df["where"][i]
claim=df['claim'][i]
answer= split_ws(df["where"])
evidence=df["evidence"][i]
time.sleep(5)
if srl!="":
try:
for j in range(0,len(answer)):
FACT_TO_GENERATE_QUESTION_FROM = f"""{answer[j]} [SEP] {claim}"""
time.sleep(10)
question_ids = query({"inputs":FACT_TO_GENERATE_QUESTION_FROM,
"num_beams":5,
"early_stopping":True,
"min_length": 100,"wait_for_model":True})[0]['generated_text'].capitalize()
question_ids = rephrase_question_where(question_ids)
list_of_ques_where.append(f"""Q{j+1}:{question_ids}""")
list_of_ans_where.append(f"""Claim :\n {answer[j]}""")
input_evidence = f"answer_the_next_question_from_context: {question_ids} context: {evidence}"
time.sleep(5)
answer_evidence = query_evidence({"inputs":input_evidence,"truncation":True,"wait_for_model":True})[0]['generated_text']
if answer_evidence.lower() in evidence.lower():
list_of_evidence_answer_where.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
else:
answer_evidence=""
list_of_evidence_answer_where.append(f"""No mention of 'where'in any related documents.""")
threshold = 0.2
list_of_pairs = [(answer_evidence, answer[j])]
rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
if rouge_l_score >= threshold:
verification_status = 'β
Verified Valid'
elif rouge_l_score == 0:
verification_status = 'β Not verifiable'
else:
verification_status = 'β Verified False'
rouge_l_scores.append(verification_status)
except:
pass
else:
list_of_ques_where="No claims"
list_of_ans_where=""
list_of_evidence_answer_where="No mention of 'where'in any related documents."
rouge_l_scores="β Not verifiable"
return list_of_ques_where,list_of_ans_where,list_of_evidence_answer_where,rouge_l_scores
#------------------------------------------------------
#------------------------------------------------------------
# if claim_text:
# if evidence_text:
# df=claim(claim_text)
# df["evidence"]=evidence_text
# final_df = pd.DataFrame(columns=['Who Claims', 'What Claims', 'When Claims', 'Where Claims', 'Why Claims'])
# final_df["Who Claims"]=gen_qa_who(df)
# final_df["What Claims"]=gen_qa_what(df)
# final_df["When Claims"]=gen_qa_when(df)
# final_df["Where Claims"]=gen_qa_where(df)
# final_df["Why Claims"]=gen_qa_why(df)
# st.table(final_df)
# st.write(df["claim"])
# st.write(df["evidence"])
# # st.write(gen_qa_who(df))
# # st.table(final_df)
# if claim_text and evidence_text:
# st.write("You entered: ", claim_text)
# st.write("You entered: ", evidence_text)
# st.caption(':green[Kindly hold on for a few minutes while the QA pairs are being generated]')
# df=claim(claim_text)
# df["evidence"]=evidence_text
# final_df = pd.DataFrame(columns=['Who Claims', 'What Claims', 'When Claims', 'Where Claims', 'Why Claims'])
# final_df["Who Claims"]=gen_qa_who(df)
# final_df["What Claims"]=gen_qa_what(df)
# final_df["When Claims"]=gen_qa_when(df)
# final_df["Where Claims"]=gen_qa_where(df)
# final_df["Why Claims"]=gen_qa_why(df)
# st.table(final_df)
# st.write(df["claim"])
# st.write(df["evidence"])
if claim_text and evidence_text:
# st.write("You entered: ", claim_text)
# st.write("You entered: ", evidence_text)
st.caption(':green[Kindly hold on for a few minutes while the QA pairs are being generated]')
df=claim(claim_text)
df["evidence"]=evidence_text
lst1=gen_qa_who(df)
lst2=gen_qa_what(df)
lst3=gen_qa_when(df)
lst4=gen_qa_where(df)
lst5=gen_qa_why(df)
output1=[]
if 'No claims' in lst1[0]:
output1=[item for item in lst1]
else:
for i in range(len(lst1[0])):
output1.append(lst1[0][i])
output1.append(lst1[1][i])
output1.append(lst1[2][i])
output1.append(lst1[3][i])
output2=[]
if 'No claims' in lst2[0]:
output2=[item for item in lst2]
else:
for i in range(len(lst2[0])):
output2.append(lst2[0][i])
output2.append(lst2[1][i])
output2.append(lst2[2][i])
output2.append(lst2[3][i])
output3=[]
if 'No claims' in lst3[0]:
output3=[item for item in lst3]
else:
for i in range(len(lst3[0])):
output3.append(lst3[0][i])
output3.append(lst3[1][i])
output3.append(lst3[2][i])
output3.append(lst3[3][i])
output4=[]
if 'No claims' in lst4[0]:
output4=[item for item in lst4]
else:
for i in range(len(lst4[0])):
output4.append(lst4[0][i])
output4.append(lst4[1][i])
output4.append(lst4[2][i])
output4.append(lst4[3][i])
output5=[]
if 'No claims' in lst5[0]:
output5=[item for item in lst5]
else:
for i in range(len(lst5[0])):
output5.append(lst5[0][i])
output5.append(lst5[1][i])
output5.append(lst5[2][i])
output5.append(lst5[3][i])
max_rows = max(len(output1), len(output2), len(output3), len(output4), len(output5))
final_df = pd.DataFrame(columns=['Who Claims', 'What Claims', 'When Claims', 'Where Claims', 'Why Claims'])
# add the data to the dataframe
final_df['Who Claims'] = output1 + [''] * (max_rows - len(output1))
final_df['What Claims'] = output2 + [''] * (max_rows - len(output2))
final_df['When Claims'] = output3 + [''] * (max_rows - len(output3))
final_df['Where Claims'] = output4 + [''] * (max_rows - len(output4))
final_df['Why Claims'] = output5 + [''] * (max_rows - len(output5))
st.write(f"""Claim : {claim_text}""")
st.write(f"""Evidence : {evidence_text}""")
st.table(final_df)
# st.write(df["claim"])
# st.write(df["evidence"])
|