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
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
import streamlit as st
from transformers import pipeline
import re

import requests

API_URL = "https://api-inference.huggingface.co/models/microsoft/prophetnet-large-uncased-squad-qg"
headers = {"Authorization": "Bearer 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"}

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": "Bearer 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"}

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.header('5W Aspect-based Fact Verification through Question Answering :blue[Web Demo]')
st.subheader('Here are a few steps to begin exploring and interacting with this demo.')
st.caption('First you need to input your claim and press :green[ctrl+enter].')
st.caption('Then you need to input your evidence and press :green[ctrl+enter].')
st.caption('Upon completing these two steps, kindly wait for a minute to receive the results.')
st.caption(':red[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.]')

st.caption('Start by inputting the following instance of a claim and corresponding evidence into the designated text fields.')

st.caption('**:Green[Claim]** :point_down:')
st.caption('''Amazon announced on March 16 it would hire 100,000 new warehouse and delivery workers and 
raise wages $ 2 per hour through April in response to increased demand for its services because of the coronavirus pandemic .''')

st.caption('**:Green[Evidence]** :point_down:')
st.caption('''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.''')


#-----------------------------------------------------------
# claim_text=st.text_area("Enter your claim:",'''
#     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.
#     ''')

# evidence_text=st.text_area("Enter your evidence:",'''
# Amazon announced on March 16 it would hire 100,000 new warehouse and delivery workers and raise wages $ 2 per hour through April in response to increased demand for its services because of the coronavirus pandemic .
# ''')


# import streamlit as st

claim_text = st.text_input('Enter your claim:')
# st.write('The claim is', claim_text)

evidence_text = st.text_input('Enter your evidence:')

import pandas as pd
import numpy as np
from allennlp.predictors.predictor import Predictor
import allennlp_models.tagging
predictor = Predictor.from_path("structured-prediction-srl-bert.tar.gz")

#---------------------------------------------------------------
def claim(text):
    import re
    df = pd.DataFrame({'claim' : [text]})
    pattern = r'(\d+),(\d+)'
    def remove_number_commas(match):
        return match.group(1) + match.group(2)
    df['claim'] = df['claim'].apply(lambda x: re.sub(pattern, remove_number_commas, x))
    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():
        # print(type(v))
        val_list.append(v)

      who = []
      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]
        else:
          substr = None
        who.append(substr)

      df['who'][j] = who

    #----------FOR COLUMN "WHAT"------------#
    df['what'] = ''
    for j in range(len(df['modified'])):
      val_list = []
      val_string = ''
      for k,v in df['modified'][j].items():
        # print(type(v))
        val_list.append(v)

      what = []
      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+6, len(val_string)):
            if val_string[i] == "]":
              break
            else:
              substr = substr + val_string[i]
        else:
          substr = None
        what.append(substr)

      df['what'][j] = 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():
        # print(type(v))
        val_list.append(v)

      why = []
      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]
        else:
          substr = None
        why.append(substr)

      df['why'][j] = 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():
        # print(type(v))
        val_list.append(v)

      when = []
      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+6, len(val_string)):
            if val_string[i] == "]":
              break
            else:
              substr = substr + val_string[i]
        else:
          substr = None
        when.append(substr)

      df['when'][j] = 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():
        # print(type(v))
        val_list.append(v)

      where = []
      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+7, len(val_string)):
            if val_string[i] == "]":
              break
            else:
              substr = substr + val_string[i]
        else:
          substr = None
        where.append(substr)

      df['where'][j] = where

    data=df[["claim","who","what","why","when","where"]].copy()    
    import re
    def remove_trail_comma(text):
        x = re.sub(",\s*$", "", text)
        return x


    data['claim']=data['claim'].apply(lambda x: str(x).replace('\'','').replace('\'',''))
    data['claim']=data['claim'].apply(lambda x: str(x).replace('[','').replace(']','')) 



    data['who']=data['who'].apply(lambda x: str(x).replace(" 's","'s"))
    data['who']=data['who'].apply(lambda x: str(x).replace("s ’","s’"))
    data['who']=data['who'].apply(lambda x: str(x).replace(" - ","-"))
    data['who']=data['who'].apply(lambda x: str(x).replace('\'','').replace('\'',''))
#     data['who']=data['who'].apply(lambda x: str(x).replace('"','').replace('"',''))
    data['who']=data['who'].apply(lambda x: str(x).replace('[','').replace(']','')) 
    data['who']=data['who'].apply(lambda x: str(x).rstrip(','))
    data['who']=data['who'].apply(lambda x: str(x).lstrip(','))
    data['who']=data['who'].apply(lambda x: str(x).replace('None,','').replace('None','')) 
    data['who']=data['who'].apply(remove_trail_comma)



    data['what']=data['what'].apply(lambda x: str(x).replace(" 's","'s"))
    data['what']=data['what'].apply(lambda x: str(x).replace("s ’","s’"))
    data['what']=data['what'].apply(lambda x: str(x).replace(" - ","-"))
    data['what']=data['what'].apply(lambda x: str(x).replace('\'','').replace('\'',''))
#     data['what']=data['what'].apply(lambda x: str(x).replace('"','').replace('"',''))
    data['what']=data['what'].apply(lambda x: str(x).replace('[','').replace(']','')) 
    data['what']=data['what'].apply(lambda x: str(x).rstrip(','))
    data['what']=data['what'].apply(lambda x: str(x).lstrip(','))
    data['what']=data['what'].apply(lambda x: str(x).replace('None,','').replace('None','')) 
    data['what']=data['what'].apply(remove_trail_comma)

    data['why']=data['why'].apply(lambda x: str(x).replace(" 's","'s"))
    data['why']=data['why'].apply(lambda x: str(x).replace("s ’","s’"))
    data['why']=data['why'].apply(lambda x: str(x).replace(" - ","-"))
    data['why']=data['why'].apply(lambda x: str(x).replace('\'','').replace('\'',''))
#     data['why']=data['why'].apply(lambda x: str(x).replace('"','').replace('"',''))
    data['why']=data['why'].apply(lambda x: str(x).replace('[','').replace(']',''))
    data['why']=data['why'].apply(lambda x: str(x).rstrip(','))
    data['why']=data['why'].apply(lambda x: str(x).lstrip(','))
    data['why']=data['why'].apply(lambda x: str(x).replace('None,','').replace('None','')) 
    data['why']=data['why'].apply(remove_trail_comma)

    data['when']=data['when'].apply(lambda x: str(x).replace(" 's","'s"))
    data['when']=data['when'].apply(lambda x: str(x).replace("s ’","s’"))
    data['when']=data['when'].apply(lambda x: str(x).replace(" - ","-"))
    data['when']=data['when'].apply(lambda x: str(x).replace('\'','').replace('\'',''))
#     data['when']=data['when'].apply(lambda x: str(x).replace('"','').replace('"',''))
    data['when']=data['when'].apply(lambda x: str(x).replace('[','').replace(']',''))
    data['when']=data['when'].apply(lambda x: str(x).rstrip(','))
    data['when']=data['when'].apply(lambda x: str(x).lstrip(','))
    data['when']=data['when'].apply(lambda x: str(x).replace('None,','').replace('None','')) 
    data['when']=data['when'].apply(remove_trail_comma)

    data['where']=data['where'].apply(lambda x: str(x).replace(" 's","'s"))
    data['where']=data['where'].apply(lambda x: str(x).replace("s ’","s’"))
    data['where']=data['where'].apply(lambda x: str(x).replace(" - ","-"))
    data['where']=data['where'].apply(lambda x: str(x).replace('\'','').replace('\'',''))
#     data['where']=data['where'].apply(lambda x: str(x).replace('"','').replace('"',''))
    data['where']=data['where'].apply(lambda x: str(x).replace('[','').replace(']','')) 
    data['where']=data['where'].apply(lambda x: str(x).rstrip(','))
    data['where']=data['where'].apply(lambda x: str(x).lstrip(','))
    data['where']=data['where'].apply(lambda x: str(x).replace('None,','').replace('None','')) 
    data['where']=data['where'].apply(remove_trail_comma)
    return data
#-------------------------------------------------------------------------
def split_ws(input_list):
    import re
    output_list = []
    for item in input_list:
        split_item = re.findall(r'[^",]+|"[^"]*"', item)
        output_list += split_item
    result = [x.strip() for x in output_list]
    return result

#--------------------------------------------------------------------------    
def gen_qq(df):
    w_list=["who","when","where","what","why"]
    ans=[]
    cl=[]
    ind=[]
    ques=[]
    evid=[]
    for index,value in enumerate(w_list):
        for i,row in df.iterrows():
            srl=df[value][i]
            claim=df['claim'][i]
            evidence_text=df['evidence'][i]
            answer= split_ws(df[value])
            try:
                if len(srl.split())>0 and len(srl.split(","))>0:
                    for j in range(0,len(answer)):
                        FACT_TO_GENERATE_QUESTION_FROM = f"""{answer[j]} [SEP] {claim}"""
                        question_ids = query({"inputs":FACT_TO_GENERATE_QUESTION_FROM, 
                                  "num_beams":5, 
                                  "early_stopping":True})
                        #print("claim : {}".format(claim))
                        #print("answer : {}".format(answer[j]))
                        #print("question : {}".format(question_ids[0]['generated_text']))
                        ind.append(i)
                        cl.append(claim)
                        ans.append(answer[j])
                        ques.append(question_ids[0]['generated_text'].capitalize())
                        evid.append(evidence_text)
                        #print("-----------------------------------------")
            except:
                pass
    return cl,ques,ans,evid
#------------------------------------------------------------    
def qa_evidence(final_data):
    ans=[]
    cl=[]
    #ind=[]
    ques=[]
    evi=[]
    srl_ans=[]


    for i,row in final_data.iterrows():
        question=final_data['gen_question'][i]
        evidence=final_data['evidence'][i]
        claim=final_data['actual_claim'][i]
        srl_answer=final_data['actual_answer'][i]
        #index=df["index"][i]

        input_evidence = f"question: {question} context: {evidence}"

        answer = query_evidence({
            "inputs":input_evidence,
            "truncation":True})

        #ind.append(index)
        cl.append(claim)
        ans.append(answer[0]["generated_text"])
        ques.append(question)
        evi.append(evidence)
        srl_ans.append(srl_answer)

        #print(f"""index: {index}""")
#         print(f"""evidence: {evidence}""")
#         print(f"""claim: {claim}""")
#         print(f"""Question: {question}""")
#         print(f"""Answer: {answer}""")
#         print(f"""SRL Answer: {srl_answer}""")
#         print("------------------------------------")
#     return list(zip(cl,ques,srl_ans)),list(zip(evi,ques,ans))
#     return cl,ques
    return list(zip(ques,srl_ans)),list(zip(ques,ans))

#------------------------------------------------------------  

if claim_text:
    if evidence_text:
        df=claim(claim_text)
        df["evidence"]=evidence_text
        actual_claim,gen_question,actual_answer,evidence=gen_qq(df)
        final_data=pd.DataFrame([actual_claim,gen_question,actual_answer,evidence]).T
        final_data.columns=["actual_claim","gen_question","actual_answer","evidence"]
        a,b=qa_evidence(final_data)
#         qa_evidence(final_data)
#         st.json(qa_evidence(final_data))
        st.json({'QA pair from claim':[{"Question": qu, "Answer": an} for qu, an in a],
                'QA pair from evidence':[{"Question": qu, "Answer": an} for qu, an in b]})