File size: 3,400 Bytes
ea93266
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23ffb2e
ea93266
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import emoji
from random import shuffle
from . import llm
from loguru import logger

languages = llm.get_languages()
levels = llm.get_level()

global input_language
input_language = "en"
global target_language
target_language = "nl"
global level
level = "medium"
global debug
debug = False
global share
share = True

qas = llm.get_QAs(n=5, input_country=input_language, target_country=target_language, level=level, debug=debug)

def get_qa(n:int=1, input_country:str=input_language, target_country:str=target_language, level:str=level, debug:bool=debug):
    logger.info(f"QAs left: {len(qas)}")
    qa = qas.pop(0)

    if len(qas) < 2:
        logger.info(f"Generating new QAs...")
        logger.debug(f"{debug = }")
        qas.extend(llm.get_QAs(n=n,
                            input_country=input_country,
                            target_country=target_country,
                            level=level,
                            debug=debug))

    question_md = f"## {qa['original']}"
    options = [qa["true"], qa["false"], qa["funny"]]
    true = qa["true"]
    shuffle(options)
    options_md = f"""### A: {options[0]}\n\n### B: {options[1]}\n\n### C: {options[2]}"""

    global correct_answer
    correct_answer = [letter for letter, option in zip(["A", "B", "C"], options) if option == true][0] # set global

    logger.info(f"get_qa()")
    logger.info(f"Question: {question_md}")
    logger.info(f"Options: {options}")
    logger.info(f"Correct answer: {correct_answer}")
    return question_md, options_md
    
def update(answer):

    # check answer
    logger.info(f"Checking answer \"{answer}\" against \"{correct_answer}\"")
    result = answer == correct_answer
    # display output
    gr.Info(emoji.emojize(":thumbs_up:"), ) if result else gr.Info(emoji.emojize(":thumbs_down:"))
    #output = emoji.emojize("# :thumbs_up:") if result else emoji.emojize("# :thumbs_down:")

    # update question
    question, options = get_qa()

    logger.info(f"update()")
    logger.info(f"Question: {question}")
    logger.info(f"Options: {options}")

    return question, options
    

def get_interface(question, options):

    with gr.Blocks() as blocks:
        with gr.Column():
            # show question
            question_md = gr.Markdown(question)

            # show answers
            answers_md = gr.Markdown(options)

            # add radio buttons
            radio = gr.Radio(choices=["A", "B", "C"], label=None, show_label=False, info=None)

            # add submit button
            button = gr.Button("Submit")

            # # show output box
            # output = gr.Markdown()

            # event listener
            button.click(fn=update, inputs=radio, outputs=[question_md, answers_md], scroll_to_output=True)


        with gr.Accordion("See Details", open=False):
            # Advanced options
            input_language = gr.Radio(languages, label="Input language", value="se", interactive=True)
            target_language = gr.Radio(languages, label="Output language", value="nl", interactive=True)
            level = gr.Radio(levels, label="Level", value="medium", interactive=True)
            
    return blocks.queue().launch(share=share, debug=debug)


question, options = get_qa(n=1, input_country=input_language, target_country=target_language, level=level, debug=debug)
interface = get_interface(question, options)