Walter de Back
reduce n
23ffb2e
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)