Spaces:
Runtime error
Runtime error
nicoaspra commited on
Commit ·
fb18846
1
Parent(s): babc4c6
initial commit
Browse files- .gitignore +1 -0
- app.py +178 -0
- questions.tex +27 -0
- requirements.txt +0 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.DS_Store
|
app.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import re
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# -------------------- PARSER -------------------- #
|
| 7 |
+
def parse_latex_questions(tex_path: str):
|
| 8 |
+
text = Path(tex_path).read_text(encoding="utf-8")
|
| 9 |
+
pattern = re.compile(
|
| 10 |
+
r"\\question\s*(.*?)\s*\\choice\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}\s*\{(.*?)\}",
|
| 11 |
+
re.DOTALL
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
letter_map = {"a": 0, "b": 1, "c": 2, "d": 3}
|
| 15 |
+
questions = []
|
| 16 |
+
|
| 17 |
+
for m in pattern.finditer(text):
|
| 18 |
+
q_text = m.group(1).strip()
|
| 19 |
+
options = [m.group(i).strip() for i in range(2, 6)]
|
| 20 |
+
answer_letter = m.group(6).strip().lower()
|
| 21 |
+
correct_answer = options[letter_map.get(answer_letter, 0)]
|
| 22 |
+
questions.append(
|
| 23 |
+
{
|
| 24 |
+
"question": q_text,
|
| 25 |
+
"options": options,
|
| 26 |
+
"answer": correct_answer,
|
| 27 |
+
}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return questions
|
| 31 |
+
|
| 32 |
+
# -------------------- LOAD QUESTIONS -------------------- #
|
| 33 |
+
all_questions = parse_latex_questions("questions.tex")
|
| 34 |
+
|
| 35 |
+
# -------------------- GAME LOGIC -------------------- #
|
| 36 |
+
def start_game():
|
| 37 |
+
questions = all_questions.copy()
|
| 38 |
+
random.shuffle(questions)
|
| 39 |
+
return {"index": 0, "score": 0, "finished": False, "questions": questions}
|
| 40 |
+
|
| 41 |
+
def get_current_question(state):
|
| 42 |
+
if state["index"] < len(state["questions"]):
|
| 43 |
+
current_q = state["questions"][state["index"]]
|
| 44 |
+
return current_q["question"], current_q["options"]
|
| 45 |
+
else:
|
| 46 |
+
state["finished"] = True
|
| 47 |
+
return "Game Over! Please press 'Restart' to play again.", []
|
| 48 |
+
|
| 49 |
+
def process_answer(user_answer, state):
|
| 50 |
+
# If game already finished
|
| 51 |
+
if state["finished"]:
|
| 52 |
+
return (
|
| 53 |
+
"Game Over! Please press 'Restart' to play again.",
|
| 54 |
+
state,
|
| 55 |
+
gr.update(choices=[], value=None),
|
| 56 |
+
"### Game Over!",
|
| 57 |
+
str(state["score"]),
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
current_q = state["questions"][state["index"]]
|
| 61 |
+
correct_answer = current_q["answer"]
|
| 62 |
+
|
| 63 |
+
if user_answer == correct_answer:
|
| 64 |
+
state["score"] += 1
|
| 65 |
+
feedback = "✅ Correct!"
|
| 66 |
+
else:
|
| 67 |
+
feedback = f"❌ Incorrect. The correct answer was {correct_answer}."
|
| 68 |
+
|
| 69 |
+
# Move to next question
|
| 70 |
+
state["index"] += 1
|
| 71 |
+
|
| 72 |
+
# If no more questions
|
| 73 |
+
if state["index"] == len(state["questions"]):
|
| 74 |
+
state["finished"] = True
|
| 75 |
+
feedback += f" 🎉 Game Over! Your final score is {state['score']}/{len(state['questions'])}."
|
| 76 |
+
return (
|
| 77 |
+
feedback,
|
| 78 |
+
state,
|
| 79 |
+
gr.update(choices=[], value=None),
|
| 80 |
+
"### Game Over!",
|
| 81 |
+
str(state["score"]),
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Otherwise show next question
|
| 85 |
+
new_question, new_options = get_current_question(state)
|
| 86 |
+
# wrap question in markdown header so it looks nice
|
| 87 |
+
return (
|
| 88 |
+
feedback,
|
| 89 |
+
state,
|
| 90 |
+
gr.update(choices=new_options, value=None),
|
| 91 |
+
f"### {new_question}",
|
| 92 |
+
str(state["score"]),
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
def reset_game():
|
| 96 |
+
new_state = start_game()
|
| 97 |
+
question, options = get_current_question(new_state)
|
| 98 |
+
return (
|
| 99 |
+
new_state,
|
| 100 |
+
f"### {question}",
|
| 101 |
+
gr.update(choices=options, value=None),
|
| 102 |
+
"",
|
| 103 |
+
"0",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
def load_game():
|
| 107 |
+
state = start_game()
|
| 108 |
+
question, options = get_current_question(state)
|
| 109 |
+
return (
|
| 110 |
+
state,
|
| 111 |
+
f"### {question}",
|
| 112 |
+
gr.update(choices=options, value=None),
|
| 113 |
+
"0",
|
| 114 |
+
"",
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# -------------------- UI -------------------- #
|
| 118 |
+
with gr.Blocks() as demo:
|
| 119 |
+
gr.Markdown("# CNC G-Codes and M-Codes Review")
|
| 120 |
+
|
| 121 |
+
# make radio vertical
|
| 122 |
+
gr.HTML("""
|
| 123 |
+
<style>
|
| 124 |
+
.vertical-radio .wrap {
|
| 125 |
+
display: flex;
|
| 126 |
+
flex-direction: column;
|
| 127 |
+
gap: 6px;
|
| 128 |
+
}
|
| 129 |
+
</style>
|
| 130 |
+
""")
|
| 131 |
+
|
| 132 |
+
state = gr.State()
|
| 133 |
+
|
| 134 |
+
# use Markdown so question auto-sizes
|
| 135 |
+
question_md = gr.Markdown("## Question will appear here")
|
| 136 |
+
|
| 137 |
+
options_buttons = gr.Radio(
|
| 138 |
+
choices=[],
|
| 139 |
+
label=" ",
|
| 140 |
+
interactive=True,
|
| 141 |
+
elem_classes=["vertical-radio"],
|
| 142 |
+
show_label=False,
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
with gr.Row():
|
| 146 |
+
submit_btn = gr.Button("Submit")
|
| 147 |
+
reset_btn = gr.Button("Restart")
|
| 148 |
+
|
| 149 |
+
feedback_box = gr.Textbox(label="Feedback", interactive=False)
|
| 150 |
+
score_box = gr.Textbox(label="Score", value="0", interactive=False)
|
| 151 |
+
|
| 152 |
+
# Submit button logic
|
| 153 |
+
submit_btn.click(
|
| 154 |
+
process_answer,
|
| 155 |
+
inputs=[options_buttons, state],
|
| 156 |
+
outputs=[feedback_box, state, options_buttons, question_md, score_box],
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Restart button logic
|
| 160 |
+
reset_btn.click(
|
| 161 |
+
reset_game,
|
| 162 |
+
inputs=[],
|
| 163 |
+
outputs=[state, question_md, options_buttons, feedback_box, score_box],
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
# On load, populate first question
|
| 167 |
+
demo.load(
|
| 168 |
+
load_game,
|
| 169 |
+
inputs=None,
|
| 170 |
+
outputs=[state, question_md, options_buttons, score_box, feedback_box],
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
demo.launch()
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
# cd 251104_trivia_game/
|
| 178 |
+
# python app.py
|
questions.tex
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
\begin{questions}
|
| 2 |
+
|
| 3 |
+
\question
|
| 4 |
+
What is claimed to be the overall advantage of criterion-referred interpretation?
|
| 5 |
+
\choice {An individual's score is compared with the set mastery level}
|
| 6 |
+
{An individual's score is compared with that of his peers.}
|
| 7 |
+
{An individual's score is compared with the average scores.}
|
| 8 |
+
{An individual's score does not to be compared with any measure.}
|
| 9 |
+
{a}
|
| 10 |
+
|
| 11 |
+
\question
|
| 12 |
+
Which is an element of norm-referenced grading?
|
| 13 |
+
\choice {The student's past performance.}
|
| 14 |
+
{An absolute standard}
|
| 15 |
+
{The performance of the group}
|
| 16 |
+
{What constitutes a perfect score}
|
| 17 |
+
{c}
|
| 18 |
+
|
| 19 |
+
\question
|
| 20 |
+
Which characteristic of good test will pupils be assured of when a teacher constructs a table of specifications for test construction purposes?
|
| 21 |
+
\choice {Authenticity}
|
| 22 |
+
{Reliability}
|
| 23 |
+
{Construct Validity}
|
| 24 |
+
{Content Validity}
|
| 25 |
+
{d}
|
| 26 |
+
|
| 27 |
+
\end{questions}
|
requirements.txt
ADDED
|
File without changes
|