import gradio as gr import random from backend.utils import ( generate_quiz, get_closed_book_answers, get_web_rag_answers_and_snippets, ) def goto_llm_tab(): return gr.Tabs(selected="llm_tab") def populate_quiz(quiz, url): quiz = None max_tries = 2 tries = 0 while quiz is None and tries < max_tries: try: quiz = generate_quiz(url) except: tries += 1 quiz_header = gr.Markdown("## đ Quiz") options, md_blocks = [], [] for i in range(5): option = gr.Radio( value=None, choices=quiz["questions"][i]["options"], interactive=True, label=quiz["questions"][i]["question"], visible=True, ) options.append(option) md_blocks.append(gr.Markdown(visible=False)) submit_btn = gr.Button(value="Submit", variant="primary", visible=True) let_llm_play_btn = gr.Button(visible=True, value="đ¤ Let the LLM play đŽ") score_label = gr.Label(visible=False) llm_tab = gr.TabItem("đ¤ Let the LLM play đŽ", id="llm_tab", visible=True) closed_book_btn = gr.Button(value="Try", visible=True, variant="primary") closed_score_label = gr.Label("Results") closed_book_accordion = gr.Accordion(visible=False) web_rag_btn = gr.Button(value="Try", visible=True, variant="primary") web_score_label = gr.Label("Results") web_rag_accordion = gr.Accordion(visible=False) return ( quiz, quiz_header, *options, *md_blocks, submit_btn, let_llm_play_btn, score_label, llm_tab, closed_book_btn, closed_score_label, closed_book_accordion, web_rag_btn, web_score_label, web_rag_accordion, ) def compute_display_results(quiz, answer0, answer1, answer2, answer3, answer4): md_blocks = [] score = 0 for i, answer in enumerate([answer0, answer1, answer2, answer3, answer4]): right_option = quiz["questions"][i]["right_option"] if isinstance(answer, str) and answer[0] == right_option: score += 1 md_blocks.append(gr.Markdown("Correct", visible=True)) else: md_blocks.append( gr.Markdown("Wrong. Correct answer: " + right_option, visible=True) ) score = score / 5 score_label = gr.Label( f"Your score is {score*100}%", visible=True, show_label=False ) options = [] for i in range(5): option = gr.Radio( choices=quiz["questions"][i]["options"], interactive=False, label=quiz["questions"][i]["question"], visible=True, ) options.append(option) submit_btn = gr.Button(visible=False) return score_label, *md_blocks, *options, submit_btn def compute_display_closed_book(quiz): answers = get_closed_book_answers(quiz) details = "" score = 0 for i, answer in enumerate(answers): details += f"**Question**: {quiz['questions'][i]['question']}\n\n" details += f"**Answer from LLM**: {quiz['questions'][i]['options'][ord(answer) - ord('a')]}\n\n" details += f"**Correct answer**: {quiz['questions'][i]['options'][ord(quiz['questions'][i]['right_option']) - ord('a')]}\n\n" details += "---\n\n" if answer == quiz["questions"][i]["right_option"]: score += 1 score = score / 5 closed_book_label = gr.Label( f"LLM closed book score is {score*100}%", visible=True, show_label=False ) closed_book_btn = gr.Button(visible=False) closed_book_accordion = gr.Accordion("Details", visible=True, open=False) closed_book_details = gr.Markdown(details, visible=True) return ( closed_book_label, closed_book_btn, closed_book_accordion, closed_book_details, ) def compute_display_web_rag(quiz): answers, snippets = get_web_rag_answers_and_snippets(quiz) details = "" score = 0 for i, answer in enumerate(answers): details += f"**Question**: {quiz['questions'][i]['question']}\n\n" details += f"**Answer from LLM**: {quiz['questions'][i]['options'][ord(answer) - ord('a')]}\n\n" details += f"**Correct answer**: {quiz['questions'][i]['options'][ord(quiz['questions'][i]['right_option']) - ord('a')]}\n\n" details += f"**Top 3 snippets from Google search**:\n\n" for snippet in snippets[i]: details += f"- {snippet}\n" details += "---\n\n" if answer == quiz["questions"][i]["right_option"]: score += 1 score = score / 5 web_rag_label = gr.Label( f"LLM Web RAG score is {score*100}%", visible=True, show_label=False ) web_rag_btn = gr.Button(visible=False) web_rag_accordion = gr.Accordion("Details", visible=True, open=False) web_rag_details = gr.Markdown(details, visible=True) return web_rag_label, web_rag_btn, web_rag_accordion, web_rag_details # Gradio app HEADER = """
đ§âđĢ AutoQuizzer
AutoQuizzer generates a quiz from a URL. You can play the quiz, or let the LLM play it.
đŦ Project walkthrough video - Built using: đī¸ Haystack âĸ đĻ Llama 3 8B Instruct âĸ ⥠Groq