File size: 20,097 Bytes
7ead7d1
 
 
49079cf
 
7b757e9
49079cf
 
 
 
 
7b757e9
b6a1d8d
49079cf
 
 
 
 
 
 
 
 
 
 
 
d7455e8
49079cf
d7455e8
2c8454a
49079cf
 
 
b6a1d8d
 
 
49079cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b757e9
49079cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b45733a
49079cf
 
 
 
 
 
 
 
 
9a81836
49079cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6a1d8d
5808cd0
49079cf
 
7ead7d1
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
import gradio as gr
import openai # For GPT-3 API ...
import re
import threading
import json
import os
from collections import Counter
from llm_utils import *
from utils import *
from retrieval_utils import *

openai.api_key = os.getenv("api_key")

COT_PROMPT = "Let's think step by step."
DIRECT_ANS_PROMPT = "The answer is"

#EXAMPLES = {
#    'arithmetic': ['Marco and his dad went strawberry picking. Together they collected strawberries that weighed 36 pounds. On the way back Marco \' dad lost 8 pounds of strawberries. Marco\'s strawberries now weighed 12 pounds. How much did his dad\'s strawberries weigh now?'],
#    'commonsense-verify': [['is the brain located in the torso?'], ['Is entire Common Era minuscule to lifespan of some trees?'], ['Did the Football War last at least a month?']],
#    'commonsens-mc': ['What would someone use a personal key for? Answer Choices: (A) car stand (B) at hotel (C) own home (D) front door (E) bus depot', ],
#    'symbolic-letter': ['Take the last letters of each words in \"Kristopher Deb Jake Tammy\" and concatenate them.'], 
#    'symbolic-coin': ['A coin is heads up. Isela flips the coin. Leslie flips the coin. Stacy flips the coin. Ingrid does not flip the coin. Is the coin still heads up? Note that \"flip\" here means \"reverse\".']
#}

EXAMPLES = ['Take the last letters of each words in \"Kristopher Deb Jake Tammy\" and concatenate them.', \
            'Do the telescopes at Goldstone Deep Space Communications Complex work the night shift?', \
            'What would someone use a personal key for? Answer Choices: (A) car stand (B) at hotel (C) own home (D) front door (E) bus depot', \
            'David watched some nesting birds using his binoculars while on vacation.  Where might David be? Answer Choices: (A) sky (B) vacation (C) forest (D) countryside (E) roof', \
            'Mary loves eating fruits. Mary paid $7.19 for berries, and $6.83 for peaches with a $20 bill. How much change did Mary receive?']



global lock #global lock, repo
lock = threading.Lock()

def answer_extraction_prompt(datatype):
    if datatype == "commonsense-mc":
        ans_prompt = "\nTherefore, among A through E, the answer is"
    elif datatype == "commonsense-verify":
        ans_prompt = "\nTherefore, the answer (Yes or No) is"
    elif datatype == "arithmetic":
        ans_prompt = "\nTherefore, the answer (arabic numerals) is"
    elif datatype == "symbolic-letter":
        ans_prompt = "\nTherefore, the answer is"
    elif datatype == "symbolic-coin":
        ans_prompt = "\nTherefore, the answer (Yes or No) is"
    else:   #if datatype == "Undefined"
        ans_prompt = "\nTherefore, the answer is"
    return ans_prompt


def zero_shot(datatype, question, engine):
    ANS_EXTRACTION_PROMPT = answer_extraction_prompt(datatype)
    ANS_EXTRACTION_PROMPT = ANS_EXTRACTION_PROMPT.replace("\nTherefore, ", "")
    ANS_EXTRACTION_PROMPT = ANS_EXTRACTION_PROMPT[0].upper() + ANS_EXTRACTION_PROMPT[1:]
    input = "Q: " + question + "\n" + "A: " + ANS_EXTRACTION_PROMPT
    ans_response = decoder_for_gpt3(input, max_length=32, engine=engine)
    ans_response = answer_cleansing_zero_shot(datatype, ans_response)
    if ans_response == "":
        ans_response = "VOID"
    return ans_response



def highlight_knowledge(entities, retrieved_knowledge):
    str_md = retrieved_knowledge
    for ent in entities:
        ent_md = {}
        m_pos = re.finditer(ent, retrieved_knowledge, re.IGNORECASE) #[(s,e),(s,e)]
        for m in m_pos:
            s, e = m.start(), m.end()
            if retrieved_knowledge[s:e] not in ent_md.keys():
                ent_ = retrieved_knowledge[s:e]
                ent_md[ent_] = '<span style="background-color: lightcoral"> **' + ent_ + '** </span>'
        for e_ori, e_md in ent_md.items():
            print(e_ori)
            print(e_md)
            str_md = str_md.replace(e_ori, e_md)
    return str_md

def zero_cot_consi(question, engine):
    input = "Q: " + question + "\n" + "A: " + COT_PROMPT
    cot_responses = decoder_for_gpt3_consistency(input,max_length=256, engine=engine) #list of cots
    return cot_responses

def auto_cot_consi(question, demo_text, engine):
    input = demo_text + "Q: " + question + "\n" + "A: " + COT_PROMPT
    cot_responses = decoder_for_gpt3_consistency(input,max_length=256, engine=engine) #list of cots
    return cot_responses


def cot_revision(datatype, question, ori_cots, knowledge, engine):
    ANS_EXTRACTION_PROMPT = answer_extraction_prompt(datatype)
    corrected_rationales = []
    corrected_answers = []
    correction_prompt = "Question: " + "[ " + question + "]\n"
    correction_prompt += "Knowledge: " + "[ " + knowledge + "]\n"
    for ori_r in ori_cots:
        cor_p = correction_prompt + "Original rationale: " + "[ " + ori_r + "]\n"
        cor_p += "With Knowledge given, output the revised rationale for Question in a precise and certain style by thinking step by step: "
        corrected_rationale = decoder_for_gpt3(cor_p,max_length=256, temperature=0.7, engine=engine)
        corrected_rationale = corrected_rationale.strip()
        corrected_rationales.append(corrected_rationale)
        input = "Q: " + question + "\n" + "A: " + corrected_rationale + ANS_EXTRACTION_PROMPT
        ans = decoder_for_gpt3(input, max_length=32, temperature=0.7, engine=engine)
        ans = answer_cleansing_zero_shot(datatype, ans)
        corrected_answers.append(ans)
    return corrected_rationales, corrected_answers


def consistency(arr):
    len_ans = len(arr)
    arr_acounts = Counter(arr)
    ans_freq_tuple = arr_acounts.most_common(len_ans)
    most_frequent_item, _ = ans_freq_tuple[0]
    ans_dict = {}
    for ans_freq in ans_freq_tuple:
        ans, times = ans_freq
        ans_dict[ans] = times/len_ans
    return most_frequent_item, ans_dict


## todo: git pull
def record_feedback(single_data, feedback, store_flag):
    global lock
    print(f"Logging feedback...")
    datatype = single_data['datatype']
    data_dir = './data_pool/{dataname}_feedback'.format(dataname=datatype)

    lock.acquire()
    if store_flag:
        single_data.update({'feedback':feedback})
        with open(data_dir, "a") as f:
            data_json = json.dumps(single_data)
            f.write(data_json + "\n")
    lock.release()
    print(f"Logging finished...")
    return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), \
            gr.update(value="πŸ˜ƒ Thank you for your valuable feedback!")


def record_feedback_agree(input_question, datatype, our_ans, zshot_ans, self_know, kb_know, refine_know, cor_ans, store_flag):
    single_data = {
        'question': input_question, 'datatype': datatype, 'zshot_ans': zshot_ans,
        'adapter_ans': our_ans, 'self_know': self_know, 'kb_know': kb_know,
        'refine_know': refine_know, 'cor_ans': cor_ans, 'feedback': ""}
    return record_feedback(single_data, 'agree', store_flag)
def record_feedback_disagree(input_question, datatype, our_ans, zshot_ans, self_know, kb_know, refine_know, cor_ans, store_flag):
    single_data = {
        'question': input_question, 'datatype': datatype, 'zshot_ans': zshot_ans,
        'adapter_ans': our_ans, 'self_know': self_know, 'kb_know': kb_know,
        'refine_know': refine_know, 'cor_ans': cor_ans, 'feedback': ""}
    return record_feedback(single_data, "disagree", store_flag)
def record_feedback_uncertain(input_question, datatype, our_ans, zshot_ans, self_know, kb_know, refine_know, cor_ans, store_flag):
    single_data = {
        'question': input_question, 'datatype': datatype, 'zshot_ans': zshot_ans,
        'adapter_ans': our_ans, 'self_know': self_know, 'kb_know': kb_know,
        'refine_know': refine_know, 'cor_ans': cor_ans, 'feedback': ""}
    return record_feedback(single_data, 'uncertain', store_flag)

def reset():
    return gr.update(value=""), gr.update(value=""), \
        gr.update(visible=False), gr.update(value="", label=""), gr.update(value="", label=""), gr.update(value="", label=""), \
        gr.update(value=""), gr.update(value=""), gr.update(value=""), gr.update(value="")


def identify_type(question, engine):
    with open('./demos/type', 'r') as f:
        typedemo = f.read()
    typedemo += "Question: " + question + "\nOutput the Type, choosing from <'arithmetic','commonsense-mc','commonsense-verify','symbolic-coin', 'symbolic-letter'>: "
    response  = decoder_for_gpt3(typedemo, 32, temperature=0, engine=engine)
    response = response.strip().lower()
    response = type_cleasing(response)
    return response

def load_examples(datatype):
    return gr.update(examples=EXAMPLES[datatype])


def self_construction(datatype):
    if datatype == "arithmetic":
        fig_adr = './figs/multiarith.png'
        demo_path = './demos/multiarith'
    elif datatype == "commonsense-mc":
        fig_adr = './figs/commonsensqa.png'
        demo_path = './demos/commonsensqa'
    elif datatype == "commonsense-verify":
        fig_adr = './figs/strategyqa.png'
        demo_path = './demos/strategyqa'
    elif datatype == "symbolic-coin":
        fig_adr = './figs/coin_flip.png'
        demo_path = './demos/coin_flip'
    elif datatype == "symbolic-letter":
        fig_adr = './figs/last_letters.png'
        demo_path = './demos/last_letters'
    else:
        pass    ##todo: datatype == 'UNDEFINED'
    
    ##read corresponding demo
    x, z, y =[], [], []
    with open(demo_path, encoding="utf-8") as f:
        json_data = json.load(f)
        json_data = json_data["demo"]
        for line in json_data:
            x.append(line["question"])
            z.append(line["rationale"])
            y.append(line["pred_ans"])
    index_list = list(range(len(x)))

    demo_md, demo_text = "", ""
    for i in index_list:
        demo_text += x[i] + " " + z[i] + " " + \
                    DIRECT_ANS_PROMPT + " " + y[i] + ".\n\n"
        demo_md += '<span style="background-color: #E0A182">' + "Q: "+ '</span>' + x[i][3:-3] + \
                        "<br>" + '<span style="background-color: #DD97AF">' + "A: "+ '</span>' + z[i] + " " + \
                        DIRECT_ANS_PROMPT + " " + y[i] + ".\n\n"
    

    return gr.update(value="## πŸ”­ Self construction..."), gr.update(visible=True, label="Visualization of clustering", value=fig_adr), \
        gr.update(visible=True, value=demo_md), gr.update(value=demo_text), \
        gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

def self_retrieval(input_question, engine):
    entities, self_retrieve_knowledge, kb_retrieve_knowledge = retrieve_for_question(input_question, engine)
    
    entities_string = ", ".join(entities)
    retr_md = "### ENTITIES:" + "<br>" + "> "+ entities_string + "\n\n"
    retr_md += "### LLM-KNOWLEDGE:" +  "<br>" + "> " + highlight_knowledge(entities,self_retrieve_knowledge) + "\n\n"
    retr_md += "### KB-KNOWLEDGE:" + "<br>" + "> " + highlight_knowledge(entities, kb_retrieve_knowledge) + "\n\n"

    return gr.update(value="## πŸ“š Self retrieval..."), gr.update(visible=True, label="", value='./figs/self-retrieval.png'), \
            gr.update(value=retr_md), \
            gr.update(value=entities_string), gr.update(value=self_retrieve_knowledge), gr.update(value=kb_retrieve_knowledge), \
            gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

def self_refinement(input_question, entities, self_retrieve_knowledge, kb_retrieve_knowledge, engine):
    refine_knowledge = refine_for_question(input_question, engine, self_retrieve_knowledge, kb_retrieve_knowledge)

    retr_md = "### ENTITIES:" + "<br>" + "> " + entities + "\n\n"
    entities = entities.strip().strip('<p>').strip('</p>').split(", ")
    retr_md += "### LLM-KNOWLEDGE:" +  "<br>" + "> " + highlight_knowledge(entities, self_retrieve_knowledge) + "\n\n"
    retr_md += "### KB-KNOWLEDGE:" + "<br>" + "> " + highlight_knowledge(entities, kb_retrieve_knowledge) + "\n\n"
    refine_md = retr_md + "### REFINED-KNOWLEDGE:" + "<br>" + "> "
    refine_md += highlight_knowledge(entities, refine_knowledge)


    return gr.update(value="## πŸͺ„ Self refinement..."), gr.update(visible=True, label="", value='./figs/self-refinement.png'), \
            gr.update(value=refine_md), gr.update(value=refine_knowledge), \
            gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

def self_revision(input_question, datatype, demo_text, refined_knowledge, engine):
    print(demo_text)
    print(refined_knowledge)
    ori_cots = auto_cot_consi(input_question, demo_text, engine)
    cor_cots, cor_ans = cot_revision(datatype, input_question, ori_cots, refined_knowledge, engine)
    cor_cots_md = "### Revised Rationales:" + "\n\n"
    for cor_cot in cor_cots:
        cor_cots_md += "> " + cor_cot + "\n\n"
    cor_ans = ", ".join(cor_ans)

    return gr.update(value="## πŸ”§ Self revision..."), gr.update(visible=True, label="", value='./figs/self-revision.png'), \
            gr.update(value=cor_cots_md), gr.update(value=cor_ans), \
            gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

def self_consistency(cor_ans, datatype, question, engine):
    cor_ans = cor_ans.strip().split(", ")
    our_ans, ans_dict = consistency(cor_ans)
    zeroshot_ans = zero_shot(datatype, question, engine)

    return gr.update(value="## πŸ—³ Self consistency..."), gr.update(visible=True, label="", value='./figs/self-consistency.png'), \
            gr.update(value=""), gr.update(value=ans_dict, visible=True), \
            gr.update(visible=True, value=our_ans), gr.update(visible=True, value=zeroshot_ans), \
            gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), \
            gr.update(visible=True, value='We would appreciate it very much if you could share your feedback. ')


def reset():
    return gr.update(value=""), gr.update(value=""), gr.update(value=""), \
        gr.update(visible=False), gr.update(value=""), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),\
        gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(value="")

#theme from: https://huggingface.co/spaces/gradio/theme-gallery
#EveryPizza/Cartoony-Gradio-Theme
#JohnSmith9982/small_and_pretty
#bethecloud/storj_theme
#gradio/soft
with gr.Blocks(theme="bethecloud/storj_theme", css="#process_btn {background-color:#8BA3C5}") as demo:
    gr.Markdown("# 🌟 ι€šη”¨θ‡ͺι€‚εΊ”ηš„ζŽ¨η†ε’žεΌΊη³»η»Ÿ (AuRoRA) 🌟")
    with gr.Row():
        with gr.Column(scale=4):
            input_question = gr.Textbox(placeholder="Input question here, or select an example from below.", label="Input Question",lines=2)
            store_flag = gr.Checkbox(label="Store data",value=True, interactive=True, info="If you agree to store data for research and development use:")
            single_data = gr.JSON(visible=False)
        with gr.Column(scale=3):
            engine = gr.Dropdown(choices=['gpt-3.5-turbo','text-davinci-003', 'text-davinci-002', 'text-curie-001', 'text-babbage-001', 'text-ada-001'], 
                                    label="Engine",  value="text-davinci-003", interactive=True, info="Choose the engine and have a try!")
            reset_btn = gr.Button(value='RESET')
    examples = gr.Examples(examples=EXAMPLES, inputs=[input_question])

    with gr.Row():
        with gr.Column(scale=1):
            type_btn = gr.Button(value="Self-identification", variant='primary', scale=1, elem_id="process_btn")
        with gr.Column(scale=3):
            datatype = gr.Dropdown(choices=['arithmetic','commonsense-mc','commonsense-verify','symbolic-letter','symbolic-coin','UNDEFINED'], 
                                    label="Input Type", info="If you disagree with our output, please select manually.", scale=3)

    demo_text = gr.Textbox(visible=False)
    entities = gr.Textbox(visible=False)
    self_know = gr.Textbox(visible=False)
    kb_know = gr.Textbox(visible=False)
    refine_know = gr.Textbox(visible=False)
    cor_ans = gr.Textbox(visible=False)
    with gr.Row():
        const_btn = gr.Button(value='Self-construction', variant='primary', elem_id="process_btn")
        retr_btn = gr.Button(value='Self-retrieval', variant='primary', elem_id="process_btn")
        refine_btn = gr.Button(value='Self-refinement', variant='primary', elem_id="process_btn")
        revis_btn = gr.Button(value='Self-revision', variant='primary', elem_id="process_btn")
        consis_btn = gr.Button(value='Self-consistency', variant='primary', elem_id="process_btn")
    
    sub_title = gr.Markdown()
    with gr.Row():
        with gr.Column(scale=2):
            plot = gr.Image(label="Visualization of clustering", visible=False)
        with gr.Column(scale=3):
            md = gr.Markdown()
            label = gr.Label(visible=False, label="Consistency Predictions")
            ans_ours = gr.Textbox(label="Unified-Adapter Answer",visible=False)
            ans_zeroshot = gr.Textbox(label="Zero-shot Answer", visible=False)
            with gr.Row():
                feedback_agree = gr.Button(value='😊 Agree', variant='secondary', visible=False)
                feedback_disagree = gr.Button(value='πŸ™ Disagree', variant='secondary', visible=False)
                feedback_uncertain = gr.Button(value='πŸ€” Uncertain', variant='secondary', visible=False)
            feedback_ack = gr.Markdown(value='', visible=True, interactive=False)

    
    type_btn.click(identify_type, inputs=[input_question, engine], outputs=[datatype])
    const_btn.click(self_construction, inputs=[datatype], outputs=[sub_title, plot, md, demo_text, label, ans_ours, ans_zeroshot, feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
    retr_btn.click(self_retrieval, inputs=[input_question, engine], outputs=[sub_title, plot, md, entities, self_know, kb_know, label, ans_ours, ans_zeroshot, feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
    refine_btn.click(self_refinement, inputs=[input_question, entities, self_know, kb_know, engine], outputs=[sub_title, plot, md, refine_know, label, ans_ours, ans_zeroshot, feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
    revis_btn.click(self_revision, inputs=[input_question, datatype, demo_text, refine_know, engine], outputs=[sub_title, plot, md, cor_ans, label, ans_ours, ans_zeroshot, feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
    consis_btn.click(self_consistency, inputs=[cor_ans, datatype, input_question, engine], outputs=[sub_title, plot, md, label, ans_ours, ans_zeroshot, feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
    reset_btn.click(reset, inputs=[], outputs=[input_question, datatype, sub_title, plot, md, label, ans_ours, ans_zeroshot, feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])

    feedback_agree.click(record_feedback_agree, inputs=[input_question, datatype, ans_ours, ans_zeroshot, self_know, kb_know, refine_know, cor_ans ,store_flag], outputs=[feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
    feedback_disagree.click(record_feedback_disagree, inputs=[input_question, datatype, ans_ours, ans_zeroshot, self_know, kb_know, refine_know, cor_ans ,store_flag], outputs=[feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
    feedback_uncertain.click(record_feedback_uncertain, inputs=[input_question, datatype, ans_ours, ans_zeroshot, self_know, kb_know, refine_know, cor_ans ,store_flag], outputs=[feedback_agree, feedback_disagree, feedback_uncertain, feedback_ack])
   

demo.launch()