jpruzcuen
commited on
Commit
·
a08aae2
1
Parent(s):
f8d9c9f
logic fixes
Browse files
app.py
CHANGED
|
@@ -44,7 +44,7 @@ with gr.Blocks(title="TAI: AI Teacher Assistant") as demo:
|
|
| 44 |
with gr.Row():
|
| 45 |
|
| 46 |
# Left column: chat
|
| 47 |
-
with gr.Column(scale=
|
| 48 |
chatbot = gr.ChatInterface(
|
| 49 |
fn=lambda message, history: chat(llm, message, history),
|
| 50 |
examples=[
|
|
@@ -56,7 +56,7 @@ with gr.Blocks(title="TAI: AI Teacher Assistant") as demo:
|
|
| 56 |
)
|
| 57 |
|
| 58 |
# Right column: quiz
|
| 59 |
-
with gr.Column(scale=
|
| 60 |
gr.Markdown("## Test Yourself")
|
| 61 |
|
| 62 |
start_btn = gr.Button("Start Quiz", variant="primary")
|
|
@@ -66,45 +66,63 @@ with gr.Blocks(title="TAI: AI Teacher Assistant") as demo:
|
|
| 66 |
progress_md = gr.Markdown("")
|
| 67 |
|
| 68 |
# Answer buttons
|
| 69 |
-
btn_A = gr.Button("A")
|
| 70 |
-
btn_B = gr.Button("B")
|
| 71 |
-
btn_C = gr.Button("C")
|
| 72 |
-
btn_D = gr.Button("D")
|
| 73 |
-
retry_btn = gr.Button("Retry")
|
| 74 |
|
| 75 |
# States
|
| 76 |
idx_state = gr.State(0)
|
| 77 |
score_state = gr.State(0)
|
| 78 |
|
| 79 |
# Start quiz
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
start_btn.click(
|
| 83 |
-
fn=
|
| 84 |
inputs=[],
|
| 85 |
-
outputs=[question_md,
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
# Answer button clicks
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
btn_B.click(fn=answer_fn_B, inputs=[idx_state, score_state],
|
| 97 |
-
outputs=[question_md, idx_state, score_state, feedback_md, progress_md])
|
| 98 |
-
btn_C.click(fn=answer_fn_C, inputs=[idx_state, score_state],
|
| 99 |
-
outputs=[question_md, idx_state, score_state, feedback_md, progress_md])
|
| 100 |
-
btn_D.click(fn=answer_fn_D, inputs=[idx_state, score_state],
|
| 101 |
-
outputs=[question_md, idx_state, score_state, feedback_md, progress_md])
|
| 102 |
|
| 103 |
# Retry button
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
retry_btn.click(
|
| 105 |
-
fn=
|
| 106 |
inputs=[],
|
| 107 |
-
outputs=[question_md,
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
|
|
|
|
| 44 |
with gr.Row():
|
| 45 |
|
| 46 |
# Left column: chat
|
| 47 |
+
with gr.Column(scale=1):
|
| 48 |
chatbot = gr.ChatInterface(
|
| 49 |
fn=lambda message, history: chat(llm, message, history),
|
| 50 |
examples=[
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
# Right column: quiz
|
| 59 |
+
with gr.Column(scale=2):
|
| 60 |
gr.Markdown("## Test Yourself")
|
| 61 |
|
| 62 |
start_btn = gr.Button("Start Quiz", variant="primary")
|
|
|
|
| 66 |
progress_md = gr.Markdown("")
|
| 67 |
|
| 68 |
# Answer buttons
|
| 69 |
+
btn_A = gr.Button("A", visible=False)
|
| 70 |
+
btn_B = gr.Button("B", visible=False)
|
| 71 |
+
btn_C = gr.Button("C", visible=False)
|
| 72 |
+
btn_D = gr.Button("D", visible=False)
|
| 73 |
+
retry_btn = gr.Button("Retry", visible=False)
|
| 74 |
|
| 75 |
# States
|
| 76 |
idx_state = gr.State(0)
|
| 77 |
score_state = gr.State(0)
|
| 78 |
|
| 79 |
# Start quiz
|
| 80 |
+
def start_quiz_ui():
|
| 81 |
+
q_text, feedback, progress, idx, score, _ = start_quiz(parsed_quiz)
|
| 82 |
+
return (
|
| 83 |
+
q_text, feedback, progress, idx, score,
|
| 84 |
+
gr.update(visible=False), # hide start button
|
| 85 |
+
gr.update(visible=True), # show A
|
| 86 |
+
gr.update(visible=True), # show B
|
| 87 |
+
gr.update(visible=True), # show C
|
| 88 |
+
gr.update(visible=True), # show D
|
| 89 |
+
gr.update(visible=True) # show retry
|
| 90 |
+
)
|
| 91 |
|
| 92 |
start_btn.click(
|
| 93 |
+
fn=start_quiz_ui,
|
| 94 |
inputs=[],
|
| 95 |
+
outputs=[question_md, feedback_md, progress_md, idx_state, score_state,
|
| 96 |
+
start_btn, btn_A, btn_B, btn_C, btn_D, retry_btn]
|
| 97 |
)
|
| 98 |
|
| 99 |
# Answer button clicks
|
| 100 |
+
for letter, btn in zip(["A", "B", "C", "D"], [btn_A, btn_B, btn_C, btn_D]):
|
| 101 |
+
btn_fn = partial(answer_question, parsed_quiz, letter)
|
| 102 |
+
btn.click(
|
| 103 |
+
fn=btn_fn,
|
| 104 |
+
inputs=[idx_state, score_state],
|
| 105 |
+
outputs=[question_md, idx_state, score_state, feedback_md, progress_md]
|
| 106 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Retry button
|
| 109 |
+
def retry_quiz_ui():
|
| 110 |
+
q_text, feedback, progress, idx, score, _ = start_quiz(parsed_quiz)
|
| 111 |
+
return (
|
| 112 |
+
q_text, feedback, progress, idx, score,
|
| 113 |
+
gr.update(visible=True), # show start
|
| 114 |
+
gr.update(visible=False), # hide A
|
| 115 |
+
gr.update(visible=False), # hide B
|
| 116 |
+
gr.update(visible=False), # hide C
|
| 117 |
+
gr.update(visible=False), # hide D
|
| 118 |
+
gr.update(visible=False) # hide retry
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
retry_btn.click(
|
| 122 |
+
fn=retry_quiz_ui,
|
| 123 |
inputs=[],
|
| 124 |
+
outputs=[question_md, feedback_md, progress_md, idx_state, score_state,
|
| 125 |
+
start_btn, btn_A, btn_B, btn_C, btn_D, retry_btn]
|
| 126 |
)
|
| 127 |
|
| 128 |
|
quiz.py
CHANGED
|
@@ -68,16 +68,11 @@ def parse_quiz(text):
|
|
| 68 |
|
| 69 |
return parsed
|
| 70 |
|
| 71 |
-
def start_quiz(
|
| 72 |
"""Initialize quiz state, return first question display."""
|
| 73 |
idx = 0
|
| 74 |
q = quiz_parsed[idx]
|
| 75 |
-
return {
|
| 76 |
-
"question_text": q["q"],
|
| 77 |
-
"feedback": "",
|
| 78 |
-
"progress": f"{idx+1}/{len(quiz_parsed)}",
|
| 79 |
-
"show_start": False
|
| 80 |
-
}, idx, 0 # idx, score
|
| 81 |
|
| 82 |
def answer_question(parsed_quiz, selected, idx, score):
|
| 83 |
current = parsed_quiz[idx]
|
|
|
|
| 68 |
|
| 69 |
return parsed
|
| 70 |
|
| 71 |
+
def start_quiz(quiz_parsed):
|
| 72 |
"""Initialize quiz state, return first question display."""
|
| 73 |
idx = 0
|
| 74 |
q = quiz_parsed[idx]
|
| 75 |
+
return q["q"], "", f"{idx+1}/{len(quiz_parsed)}", idx, 0, False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def answer_question(parsed_quiz, selected, idx, score):
|
| 78 |
current = parsed_quiz[idx]
|