Spaces:
Runtime error
Runtime error
File size: 5,065 Bytes
fb18846 c842089 fb18846 c842089 fb18846 c842089 fb18846 c842089 fb18846 c842089 fb18846 c842089 fb18846 c842089 fb18846 e1dcb9e fb18846 c842089 fb18846 |
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 |
import gradio as gr
import random
import re
from pathlib import Path
# -------------------- PARSER -------------------- #
def parse_latex_questions(tex_path: str):
text = Path(tex_path).read_text(encoding="utf-8")
pattern = re.compile(
r"\\question\s*(.*?)\s*\\choice\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}",
re.DOTALL
)
letter_map = {"a": 0, "b": 1, "c": 2, "d": 3}
questions = []
for m in pattern.finditer(text):
q_text = m.group(1).strip()
options = [m.group(i).strip() for i in range(2, 6)]
answer_letter = m.group(6).strip().lower()
correct_answer = options[letter_map.get(answer_letter, 0)]
questions.append(
{
"question": q_text,
"options": options,
"answer": correct_answer,
}
)
return questions
# -------------------- LOAD QUESTIONS -------------------- #
all_questions = parse_latex_questions("questions.tex")
# -------------------- GAME LOGIC -------------------- #
def start_game():
questions = all_questions.copy()
random.shuffle(questions)
return {"index": 0, "score": 0, "finished": False, "questions": questions}
def get_current_question(state):
if state["index"] < len(state["questions"]):
current_q = state["questions"][state["index"]]
return current_q["question"], current_q["options"]
else:
state["finished"] = True
return "Game Over! Please press 'Restart' to play again.", []
def process_answer(user_answer, state):
total = len(state["questions"])
# If game already finished and user clicks again
if state["finished"]:
return (
"Game Over! Please press 'Restart' to play again.",
state,
gr.update(choices=["Quiz finished"], value="Quiz finished"),
f"### π Game Over!\n**Final Score:** {state['score']}/{total}",
str(state["score"]),
)
current_q = state["questions"][state["index"]]
correct_answer = current_q["answer"]
if user_answer == correct_answer:
state["score"] += 1
feedback = "β
Correct!"
else:
feedback = f"β Incorrect. The correct answer was {correct_answer}."
# Move to next question
state["index"] += 1
# If no more questions
if state["index"] == total:
state["finished"] = True
feedback += f" π Game Over! Your final score is {state['score']}/{total}."
return (
feedback,
state,
gr.update(choices=["Quiz finished"], value="Quiz finished"),
f"### π Game Over!\n**Final Score:** {state['score']}/{total}",
str(state["score"]),
)
# Otherwise show next question
new_question, new_options = get_current_question(state)
return (
feedback,
state,
gr.update(choices=new_options, value=None),
f"### {new_question}",
str(state["score"]),
)
def reset_game():
new_state = start_game()
question, options = get_current_question(new_state)
return (
new_state,
f"### {question}",
gr.update(choices=options, value=None),
"",
"0",
)
def load_game():
state = start_game()
question, options = get_current_question(state)
return (
state,
f"### {question}",
gr.update(choices=options, value=None),
"0",
"",
)
# -------------------- UI -------------------- #
with gr.Blocks() as demo:
gr.Markdown("# CNC G-Codes and M-Codes Review")
# make radio vertical
gr.HTML("""
<style>
.vertical-radio .wrap {
display: flex;
flex-direction: column;
gap: 6px;
}
</style>
""")
state = gr.State()
# question shown as markdown so it can be multiline
question_md = gr.Markdown("### Question will appear here")
options_buttons = gr.Radio(
choices=[],
label=" ",
show_label=False,
interactive=True,
elem_classes=["vertical-radio"]
)
with gr.Row():
submit_btn = gr.Button("Submit")
reset_btn = gr.Button("Restart")
feedback_box = gr.Textbox(label="Feedback", interactive=False)
score_box = gr.Textbox(label="Score", value="0", interactive=False)
submit_btn.click(
process_answer,
inputs=[options_buttons, state],
outputs=[feedback_box, state, options_buttons, question_md, score_box],
)
reset_btn.click(
reset_game,
inputs=[],
outputs=[state, question_md, options_buttons, feedback_box, score_box],
)
demo.load(
load_game,
inputs=None,
outputs=[state, question_md, options_buttons, score_box, feedback_box],
)
gr.Markdown("---")
gr.Markdown("""
<div style="text-align: center;">
Developed by <strong>Aspra, N.</strong> | Β© 2025 All Rights Reserved
</div>
""")
demo.launch()
# cd 251104_CNC_code_review/
# python app.py |