Towhidul commited on
Commit
6512609
·
1 Parent(s): 12b7e8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -341
app.py CHANGED
@@ -6,6 +6,7 @@ import re
6
  import time
7
  import requests
8
  from PIL import Image
 
9
 
10
 
11
  HF_SPACES_API_KEY = st.secrets["HF_token"]
@@ -148,7 +149,7 @@ list_of_pronouns = ["I", "you", "he", "she", "it", "we", "they", "me", "him", "h
148
  "one", "other", "several", "some", "somebody", "someone", "something"]
149
  #---------------------------------------------------------------
150
  # @st.cache
151
- def claim(text):
152
  import re
153
  def remove_special_chars(text):
154
  # Remove special characters that are not in between numbers
@@ -349,21 +350,7 @@ def claim(text):
349
  pass
350
 
351
  df['where'][j] = "<sep>".join(where)
352
-
353
-
354
- data=df[["claim","who","what","why","when","where"]].copy()
355
- return data
356
- #-------------------------------------------------------------------------
357
- # @st.cache
358
- def split_ws(input_list, delimiter="<sep>"):
359
- output_list = []
360
- for item in input_list:
361
- split_item = item.split(delimiter)
362
- for sub_item in split_item:
363
- sub_item = sub_item.strip()
364
- if sub_item:
365
- output_list.append(sub_item)
366
- return output_list
367
 
368
  #--------------------------------------------------------------------------
369
  # @st.cache
@@ -372,336 +359,81 @@ def calc_rouge_l_score(list_of_evidence, list_of_ans):
372
  scores = scorer.score(' '.join(list_of_evidence), ' '.join(list_of_ans))
373
  return scores['rougeL'].fmeasure
374
  #-------------------------------------------------------------------------
375
-
376
- # @st.cache
377
- def rephrase_question_who(question):
378
- if not question.lower().startswith("who"):
379
- words = question.split()
380
- words[0] = "Who"
381
- return " ".join(words)
382
- else:
383
- return question
384
- #------------------------------------------------------------------------
385
- # @st.cache
386
- def gen_qa_who(df):
387
- list_of_ques_who=[]
388
- list_of_ans_who=[]
389
- list_of_evidence_answer_who=[]
390
- rouge_l_scores=[]
391
- for i,row in df.iterrows():
392
- srl=df["who"][i]
393
- claim=df['claim'][i]
394
- answer= split_ws(df["who"])
395
- evidence=df["evidence"][i]
396
- if srl!="":
397
- try:
398
- for j in range(0,len(answer)):
399
- question_ids = model_load_qg(answer[j],claim)
400
- question_ids = rephrase_question_who(question_ids)
401
- list_of_ques_who.append(f"""Q{j+1} :\n {question_ids}""")
402
- list_of_ans_who.append(f"""Claim :\n {answer[j]}""")
403
- answer_evidence = model_load_qa(question_ids,evidence)
404
- if answer_evidence.lower() in evidence.lower():
405
- list_of_evidence_answer_who.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
406
- else:
407
- answer_evidence=""
408
- list_of_evidence_answer_who.append(f"""No mention of 'who'in any related documents.""")
409
- threshold = 0.2
410
- list_of_pairs = [(answer_evidence, answer[j])]
411
- rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
412
- if rouge_l_score >= threshold:
413
- verification_status = '✅ Verified Valid'
414
- elif rouge_l_score == 0:
415
- verification_status = '❔ Not verifiable'
416
- else:
417
- verification_status = '❌ Verified False'
418
- rouge_l_scores.append(verification_status)
419
- except:
420
- pass
421
- else:
422
- list_of_ques_who="No claims"
423
- list_of_ans_who=""
424
- list_of_evidence_answer_who="No mention of 'who'in any related documents."
425
- rouge_l_scores="❔ Not verifiable"
426
- return list_of_ques_who,list_of_ans_who,list_of_evidence_answer_who,rouge_l_scores
427
- #------------------------------------------------------------
428
- # @st.cache
429
- def rephrase_question_what(question):
430
- if not question.lower().startswith("what"):
431
- words = question.split()
432
- words[0] = "What"
433
- return " ".join(words)
434
- else:
435
- return question
436
- #----------------------------------------------------------
437
- # @st.cache
438
- def gen_qa_what(df):
439
- list_of_ques_what=[]
440
- list_of_ans_what=[]
441
- list_of_evidence_answer_what=[]
442
- rouge_l_scores=[]
443
- for i,row in df.iterrows():
444
- srl=df["what"][i]
445
- claim=df['claim'][i]
446
- answer= split_ws(df["what"])
447
- evidence=df["evidence"][i]
448
- if srl!="":
449
- try:
450
- for j in range(0,len(answer)):
451
- question_ids = model_load_qg(answer[j],claim)
452
- question_ids = rephrase_question_what(question_ids)
453
- list_of_ques_what.append(f"""Q{j+1}:{question_ids}""")
454
- list_of_ans_what.append(f"""Claim :\n {answer[j]}""")
455
- answer_evidence = model_load_qa(question_ids,evidence)
456
- if answer_evidence.lower() in evidence.lower():
457
- list_of_evidence_answer_what.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
458
-
459
- else:
460
- answer_evidence=""
461
- list_of_evidence_answer_what.append(f"""No mention of 'what'in any related documents.""")
462
- threshold = 0.2
463
- list_of_pairs = [(answer_evidence, answer[j])]
464
- rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
465
- if rouge_l_score >= threshold:
466
- verification_status = '✅ Verified Valid'
467
- elif rouge_l_score == 0:
468
- verification_status = '❔ Not verifiable'
469
- else:
470
- verification_status = '❌ Verified False'
471
- rouge_l_scores.append(verification_status)
472
- except:
473
- pass
474
  else:
475
- list_of_ques_what="No claims"
476
- list_of_ans_what=""
477
- list_of_evidence_answer_what="No mention of 'what'in any related documents."
478
- rouge_l_scores="❔ Not verifiable"
479
- return list_of_ques_what,list_of_ans_what,list_of_evidence_answer_what,rouge_l_scores
480
- #----------------------------------------------------------
481
- # @st.cache
482
- def rephrase_question_why(question):
483
- if not question.lower().startswith("why"):
484
- words = question.split()
485
- words[0] = "Why"
486
- return " ".join(words)
487
- else:
488
- return question
489
-
490
- #---------------------------------------------------------
491
- # @st.cache
492
- def gen_qa_why(df):
493
- list_of_ques_why=[]
494
- list_of_ans_why=[]
495
- list_of_evidence_answer_why=[]
496
- rouge_l_scores=[]
497
- for i,row in df.iterrows():
498
- srl=df["why"][i]
499
- claim=df['claim'][i]
500
- answer= split_ws(df["why"])
501
- evidence=df["evidence"][i]
502
- if srl!="":
503
- try:
504
- for j in range(0,len(answer)):
505
- question_ids = model_load_qg(answer[j],claim)
506
- question_ids = rephrase_question_why(question_ids)
507
- list_of_ques_why.append(f"""Q{j+1}:{question_ids}""")
508
- list_of_ans_why.append(f"""Claim :\n {answer[j]}""")
509
- answer_evidence = model_load_qa(question_ids,evidence)
510
- if answer_evidence.lower() in evidence.lower():
511
- list_of_evidence_answer_why.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
512
- else:
513
- answer_evidence=""
514
- list_of_evidence_answer_why.append(f"""No mention of 'why'in any related documents.""")
515
- threshold = 0.2
516
- list_of_pairs = [(answer_evidence, answer[j])]
517
- rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
518
- if rouge_l_score >= threshold:
519
- verification_status = '✅ Verified Valid'
520
- elif rouge_l_score == 0:
521
- verification_status = '❔ Not verifiable'
522
- else:
523
- verification_status = '❌ Verified False'
524
- rouge_l_scores.append(verification_status)
525
- except:
526
- pass
527
  else:
528
- list_of_ques_why="No claims"
529
- list_of_ans_why=""
530
- list_of_evidence_answer_why="No mention of 'why'in any related documents."
531
- rouge_l_scores="❔ Not verifiable"
532
- return list_of_ques_why,list_of_ans_why,list_of_evidence_answer_why,rouge_l_scores
533
-
534
- #---------------------------------------------------------
535
- # @st.cache
536
- def rephrase_question_when(question):
537
- if not question.lower().startswith("when"):
538
- words = question.split()
539
- words[0] = "When"
540
- return " ".join(words)
541
- else:
542
- return question
543
- #---------------------------------------------------------
544
- # @st.cache
545
- def gen_qa_when(df):
546
- list_of_ques_when=[]
547
- list_of_ans_when=[]
548
- list_of_evidence_answer_when=[]
549
- rouge_l_scores=[]
550
- for i,row in df.iterrows():
551
- srl=df["when"][i]
552
- claim=df['claim'][i]
553
- answer= split_ws(df["when"])
554
- evidence=df["evidence"][i]
555
- if srl!="":
556
- try:
557
- for j in range(0,len(answer)):
558
- question_ids = model_load_qg(answer[j],claim)
559
- question_ids = rephrase_question_when(question_ids)
560
- list_of_ques_when.append(f"""Q{j+1}:{question_ids}""")
561
- list_of_ans_when.append(f"""Claim :\n {answer[j]}""")
562
- answer_evidence = model_load_qa(question_ids,evidence)
563
- if answer_evidence.lower() in evidence.lower():
564
- list_of_evidence_answer_when.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
565
- else:
566
- answer_evidence=""
567
- list_of_evidence_answer_when.append(f"""No mention of 'when'in any related documents.""")
568
- threshold = 0.2
569
- list_of_pairs = [(answer_evidence, answer[j])]
570
- rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
571
- if rouge_l_score >= threshold:
572
- verification_status = '✅ Verified Valid'
573
- elif rouge_l_score == 0:
574
- verification_status = '❔ Not verifiable'
575
- else:
576
- verification_status = '❌ Verified False'
577
- rouge_l_scores.append(verification_status)
578
- except:
579
- pass
580
- else:
581
- list_of_ques_when="No claims"
582
- list_of_ans_when=""
583
- list_of_evidence_answer_when="No mention of 'when'in any related documents."
584
- rouge_l_scores="❔ Not verifiable"
585
- return list_of_ques_when,list_of_ans_when,list_of_evidence_answer_when,rouge_l_scores
586
-
587
- #------------------------------------------------------
588
- # @st.cache
589
- def rephrase_question_where(question):
590
- if not question.lower().startswith("where"):
591
- words = question.split()
592
- words[0] = "Where"
593
- return " ".join(words)
594
- else:
595
- return question
596
- #------------------------------------------------------
597
- # @st.cache
598
- def gen_qa_where(df):
599
- list_of_ques_where=[]
600
- list_of_ans_where=[]
601
- list_of_evidence_answer_where=[]
602
- rouge_l_scores=[]
603
- for i,row in df.iterrows():
604
- srl=df["where"][i]
605
- claim=df['claim'][i]
606
- answer= split_ws(df["where"])
607
- evidence=df["evidence"][i]
608
- if srl!="":
609
- try:
610
- for j in range(0,len(answer)):
611
- question_ids = model_load_qg(answer[j],claim)
612
- question_ids = rephrase_question_where(question_ids)
613
- list_of_ques_where.append(f"""Q{j+1}:{question_ids}""")
614
- list_of_ans_where.append(f"""Claim :\n {answer[j]}""")
615
- answer_evidence = model_load_qa(question_ids,evidence)
616
- if answer_evidence.lower() in evidence.lower():
617
- list_of_evidence_answer_where.append(f"""Answer retrieved from evidence :\n {answer_evidence}""")
618
- else:
619
- answer_evidence=""
620
- list_of_evidence_answer_where.append(f"""No mention of 'where'in any related documents.""")
621
- threshold = 0.2
622
- list_of_pairs = [(answer_evidence, answer[j])]
623
- rouge_l_score = calc_rouge_l_score(answer_evidence, answer[j])
624
- if rouge_l_score >= threshold:
625
- verification_status = '✅ Verified Valid'
626
- elif rouge_l_score == 0:
627
- verification_status = '❔ Not verifiable'
628
- else:
629
- verification_status = '❌ Verified False'
630
- rouge_l_scores.append(verification_status)
631
- except:
632
- pass
633
- else:
634
- list_of_ques_where="No claims"
635
- list_of_ans_where=""
636
- list_of_evidence_answer_where="No mention of 'where'in any related documents."
637
- rouge_l_scores="❔ Not verifiable"
638
- return list_of_ques_where,list_of_ans_where,list_of_evidence_answer_where,rouge_l_scores
639
- #------------------------------------------------------
640
-
641
 
 
642
  if claim_text and evidence_text:
643
  st.caption(':green[Kindly hold on for a few minutes while the QA pairs are being generated]')
644
- df=claim(claim_text)
645
- df["evidence"]=evidence_text
646
- lst1=gen_qa_who(df)
647
- lst2=gen_qa_what(df)
648
- lst3=gen_qa_when(df)
649
- lst4=gen_qa_where(df)
650
- lst5=gen_qa_why(df)
651
-
652
- output1=[]
653
- if 'No claims' in lst1[0]:
654
- output1=[item for item in lst1]
655
- else:
656
- for i in range(len(lst1[0])):
657
- output1.append(lst1[0][i])
658
- output1.append(lst1[1][i])
659
- output1.append(lst1[2][i])
660
- output1.append(lst1[3][i])
661
- output2=[]
662
- if 'No claims' in lst2[0]:
663
- output2=[item for item in lst2]
664
- else:
665
- for i in range(len(lst2[0])):
666
- output2.append(lst2[0][i])
667
- output2.append(lst2[1][i])
668
- output2.append(lst2[2][i])
669
- output2.append(lst2[3][i])
670
- output3=[]
671
- if 'No claims' in lst3[0]:
672
- output3=[item for item in lst3]
673
- else:
674
- for i in range(len(lst3[0])):
675
- output3.append(lst3[0][i])
676
- output3.append(lst3[1][i])
677
- output3.append(lst3[2][i])
678
- output3.append(lst3[3][i])
679
- output4=[]
680
- if 'No claims' in lst4[0]:
681
- output4=[item for item in lst4]
682
- else:
683
- for i in range(len(lst4[0])):
684
- output4.append(lst4[0][i])
685
- output4.append(lst4[1][i])
686
- output4.append(lst4[2][i])
687
- output4.append(lst4[3][i])
688
- output5=[]
689
- if 'No claims' in lst5[0]:
690
- output5=[item for item in lst5]
691
- else:
692
- for i in range(len(lst5[0])):
693
- output5.append(lst5[0][i])
694
- output5.append(lst5[1][i])
695
- output5.append(lst5[2][i])
696
- output5.append(lst5[3][i])
697
- max_rows = max(len(output1), len(output2), len(output3), len(output4), len(output5))
698
- final_df = pd.DataFrame(columns=['Who Claims', 'What Claims', 'When Claims', 'Where Claims', 'Why Claims'])
699
- # add the data to the dataframe
700
- final_df['Who Claims'] = output1 + [''] * (max_rows - len(output1))
701
- final_df['What Claims'] = output2 + [''] * (max_rows - len(output2))
702
- final_df['When Claims'] = output3 + [''] * (max_rows - len(output3))
703
- final_df['Where Claims'] = output4 + [''] * (max_rows - len(output4))
704
- final_df['Why Claims'] = output5 + [''] * (max_rows - len(output5))
705
  st.write(f"""Claim : {claim_text}""")
706
  st.write(f"""Evidence : {evidence_text}""")
707
  st.table(final_df)
 
6
  import time
7
  import requests
8
  from PIL import Image
9
+ import itertools
10
 
11
 
12
  HF_SPACES_API_KEY = st.secrets["HF_token"]
 
149
  "one", "other", "several", "some", "somebody", "someone", "something"]
150
  #---------------------------------------------------------------
151
  # @st.cache
152
+ def srl(text):
153
  import re
154
  def remove_special_chars(text):
155
  # Remove special characters that are not in between numbers
 
350
  pass
351
 
352
  df['where'][j] = "<sep>".join(where)
353
+ return who,what,when,where,why
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
 
355
  #--------------------------------------------------------------------------
356
  # @st.cache
 
359
  scores = scorer.score(' '.join(list_of_evidence), ' '.join(list_of_ans))
360
  return scores['rougeL'].fmeasure
361
  #-------------------------------------------------------------------------
362
+ def qa_list_gen(claim,srl,evidence):
363
+ list_of_qa_pipeline=[]
364
+ for index,answer_claim in enumerate(combined_list):
365
+ question = model_load_qg(answer_claim,claim)
366
+ answer_evidence = model_load_qa(question,evidence)
367
+ if answer_evidence.lower() in evidence.lower():
368
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  else:
370
+ answer_evidence=""
371
+ threshold = 0.2
372
+ list_of_pairs = [(answer_evidence, answer_claim)]
373
+ rouge_l_score = calc_rouge_l_score(answer_evidence, answer_claim)
374
+ if rouge_l_score >= threshold:
375
+ verification_status = '✅ Verified Valid'
376
+ elif rouge_l_score == 0:
377
+ verification_status = '❔ Not verifiable'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  else:
379
+ verification_status = '❌ Verified False'
380
+ qa_pipeline=[question,answer_claim,answer_evidence,verification_status]
381
+ list_of_qa_pipeline.append(qa_pipeline)
382
+ return list_of_qa_pipeline
383
+
384
+ #-------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
 
386
+
387
  if claim_text and evidence_text:
388
  st.caption(':green[Kindly hold on for a few minutes while the QA pairs are being generated]')
389
+ srl_list = list(itertools.chain(*[list(s) for s in srl(claim_text)]))
390
+ qa_list=qa_list_gen(claim_text,srl_list,evidence)
391
+ list_who = []
392
+ list_what = []
393
+ list_when = []
394
+ list_where = []
395
+ list_why = []
396
+ list_misc = []
397
+
398
+ for item in qa_list:
399
+ question = item[0]
400
+ if any(x in question.lower() for x in ['who', 'what', 'when', 'where', 'why']):
401
+ if 'who' in question.lower():
402
+ list_who.append(item)
403
+ elif 'what' in question.lower():
404
+ list_what.append(item)
405
+ elif 'when' in question.lower():
406
+ list_when.append(item)
407
+ elif 'where' in question.lower():
408
+ list_where.append(item)
409
+ elif 'why' in question.lower():
410
+ list_why.append(item)
411
+ else:
412
+ list_misc.append(item)
413
+ lists = [list_who, list_when, list_why, list_where, list_what]
414
+
415
+ for i in range(len(lists)):
416
+ if not lists[i]:
417
+ lists[i].extend([["No claims", "", f"No mention of '{['who', 'when', 'why', 'where', 'what'][i]}' in any related documents.", "❔ Not verifiable"]])
418
+
419
+ final_df = pd.DataFrame(columns=['Who Claims', 'What Claims', 'When Claims', 'Where Claims', 'Why Claims', 'Misc Claims'])
420
+
421
+ all_items_who = [item for item_list in list_who for item in item_list]
422
+ all_items_what = [item for item_list in list_what for item in item_list]
423
+ all_items_when = [item for item_list in list_when for item in item_list]
424
+ all_items_where = [item for item_list in list_where for item in item_list]
425
+ all_items_why = [item for item_list in list_why for item in item_list]
426
+ all_items_misc = [item for item_list in list_misc for item in item_list]
427
+
428
+
429
+ max_rows = max(len(all_items_who), len(all_items_what), len(all_items_when), len(all_items_where), len(all_items_why), len(all_items_misc))
430
+
431
+ final_df['Who Claims'] = all_items_who + [''] * (max_rows - len(all_items_who))
432
+ final_df['What Claims'] = all_items_what + [''] * (max_rows - len(all_items_what))
433
+ final_df['When Claims'] = all_items_when + [''] * (max_rows - len(all_items_when))
434
+ final_df['Where Claims'] = all_items_where + [''] * (max_rows - len(all_items_where))
435
+ final_df['Why Claims'] = all_items_why + [''] * (max_rows - len(all_items_why))
436
+ final_df['Misc Claims'] = all_items_misc + [''] * (max_rows - len(all_items_misc))
 
 
 
 
 
 
 
 
 
 
 
 
 
437
  st.write(f"""Claim : {claim_text}""")
438
  st.write(f"""Evidence : {evidence_text}""")
439
  st.table(final_df)