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
|
4 |
+
from src.SecondModule.module2 import SimilarQuestionGenerator
|
5 |
+
from src.ThirdModule.module3 import AnswerVerifier
|
6 |
+
import logging
|
7 |
+
from typing import Optional, Tuple
|
8 |
+
#from latex_formatter import LatexFormatter # LaTeX ν¬λ§·ν° import
|
9 |
+
from pylatexenc.latex2text import LatexNodes2Text
|
10 |
+
|
11 |
+
|
12 |
+
logging.basicConfig(level=logging.DEBUG)
|
13 |
+
|
14 |
+
|
15 |
+
# Streamlit νμ΄μ§ κΈ°λ³Έ μ€μ
|
16 |
+
st.set_page_config(
|
17 |
+
page_title="MisconcepTutor",
|
18 |
+
layout="wide",
|
19 |
+
initial_sidebar_state="expanded"
|
20 |
+
)
|
21 |
+
|
22 |
+
@st.cache_resource
|
23 |
+
def load_answer_verifier():
|
24 |
+
"""λ΅μ κ²μ¦ λͺ¨λΈ λ‘λ"""
|
25 |
+
from src.ThirdModule.module3 import AnswerVerifier
|
26 |
+
return AnswerVerifier()
|
27 |
+
|
28 |
+
# κ²½λ‘ μ€μ
|
29 |
+
base_path = os.path.dirname(os.path.abspath(__file__))
|
30 |
+
data_path = os.path.join(base_path, 'Data')
|
31 |
+
misconception_csv_path = os.path.join(data_path, 'misconception_mapping.csv')
|
32 |
+
|
33 |
+
# λ‘κΉ
μ€μ
|
34 |
+
logging.basicConfig(level=logging.INFO)
|
35 |
+
logger = logging.getLogger(__name__)
|
36 |
+
|
37 |
+
# μΈμ
μν μ΄κΈ°ν - κ°μ₯ λ¨Όμ μ€νλλλ‘ μ΅μλ¨μ λ°°μΉ
|
38 |
+
if 'initialized' not in st.session_state:
|
39 |
+
st.session_state.initialized = True
|
40 |
+
st.session_state.wrong_questions = []
|
41 |
+
st.session_state.misconceptions = []
|
42 |
+
st.session_state.current_question_index = 0
|
43 |
+
st.session_state.generated_questions = []
|
44 |
+
st.session_state.current_step = 'initial'
|
45 |
+
st.session_state.selected_wrong_answer = None
|
46 |
+
st.session_state.questions = []
|
47 |
+
logger.info("Session state initialized")
|
48 |
+
|
49 |
+
# λ¬Έμ μμ±κΈ° μ΄κΈ°ν
|
50 |
+
@st.cache_resource
|
51 |
+
def load_question_generator():
|
52 |
+
"""λ¬Έμ μμ± λͺ¨λΈ λ‘λ"""
|
53 |
+
if not os.path.exists(misconception_csv_path):
|
54 |
+
st.error(f"CSV νμΌμ΄ μ‘΄μ¬νμ§ μμ΅λλ€: {misconception_csv_path}")
|
55 |
+
raise FileNotFoundError(f"CSV νμΌμ΄ μ‘΄μ¬νμ§ μμ΅λλ€: {misconception_csv_path}")
|
56 |
+
return SimilarQuestionGenerator(misconception_csv_path=misconception_csv_path)
|
57 |
+
|
58 |
+
# CSV λ°μ΄ν° λ‘λ ν¨μ
|
59 |
+
@st.cache_data
|
60 |
+
def load_data(data_file = '/train.csv'):
|
61 |
+
try:
|
62 |
+
file_path = os.path.join(data_path, data_file.lstrip('/'))
|
63 |
+
df = pd.read_csv(file_path)
|
64 |
+
logger.info(f"Data loaded successfully from {file_path}")
|
65 |
+
return df
|
66 |
+
except FileNotFoundError:
|
67 |
+
st.error(f"νμΌμ μ°Ύμ μ μμ΅λλ€: {data_file}")
|
68 |
+
logger.error(f"File not found: {data_file}")
|
69 |
+
return None
|
70 |
+
|
71 |
+
def start_quiz():
|
72 |
+
"""ν΄μ¦ μμ λ° μ΄κΈ°ν"""
|
73 |
+
df = load_data()
|
74 |
+
if df is None or df.empty:
|
75 |
+
st.error("λ°μ΄ν°λ₯Ό λΆλ¬μ¬ μ μμ΅λλ€. λ°μ΄ν°μ
μ νμΈν΄μ£ΌμΈμ.")
|
76 |
+
return
|
77 |
+
|
78 |
+
st.session_state.questions = df.sample(n=10, random_state=42)
|
79 |
+
st.session_state.current_step = 'quiz'
|
80 |
+
st.session_state.current_question_index = 0
|
81 |
+
st.session_state.wrong_questions = []
|
82 |
+
st.session_state.misconceptions = []
|
83 |
+
st.session_state.generated_questions = []
|
84 |
+
logger.info("Quiz started")
|
85 |
+
|
86 |
+
|
87 |
+
def generate_similar_question(wrong_q, misconception_id, generator):
|
88 |
+
"""μ μ¬ λ¬Έμ μμ±"""
|
89 |
+
logger.info(f"Generating similar question for misconception_id: {misconception_id}")
|
90 |
+
|
91 |
+
# μ
λ ₯ λ°μ΄ν° μ ν¨μ± κ²μ¬
|
92 |
+
if not isinstance(wrong_q, dict):
|
93 |
+
logger.error(f"Invalid wrong_q type: {type(wrong_q)}")
|
94 |
+
st.error("μ μ¬ λ¬Έμ μμ±μ νμν λ°μ΄ν° νμμ΄ μλͺ»λμμ΅λλ€.")
|
95 |
+
return None
|
96 |
+
|
97 |
+
try:
|
98 |
+
# misconception_idκ° μκ±°λ NaNμΈ κ²½μ° λ€λ₯Έ misconception μ¬μ©
|
99 |
+
if pd.isna(misconception_id):
|
100 |
+
logger.info("Original misconception_id is NaN, trying to find alternative")
|
101 |
+
# νμ¬κΉμ§ λμ¨ misconceptionλ€ μ€μμ μ ν
|
102 |
+
available_misconceptions = [m for m in st.session_state.misconceptions if not pd.isna(m)]
|
103 |
+
|
104 |
+
if available_misconceptions:
|
105 |
+
# κ°μ₯ μ΅κ·Όμ λμ¨ misconception μ ν
|
106 |
+
misconception_id = available_misconceptions[-1]
|
107 |
+
logger.info(f"Using alternative misconception_id: {misconception_id}")
|
108 |
+
else:
|
109 |
+
# κΈ°λ³Έ misconception ID μ¬μ© (μ: κ°μ₯ κΈ°λ³Έμ μΈ misconception)
|
110 |
+
misconception_id = 2001 # μ μ ν κΈ°λ³Έκ°μΌλ‘ μμ νμ
|
111 |
+
logger.info(f"Using default misconception_id: {misconception_id}")
|
112 |
+
|
113 |
+
# λ°μ΄ν° μ€λΉ (νν λ³ν λ°©μ§)
|
114 |
+
input_data = {
|
115 |
+
'construct_name': str(wrong_q.get('ConstructName', '')),
|
116 |
+
'subject_name': str(wrong_q.get('SubjectName', '')),
|
117 |
+
'question_text': str(wrong_q.get('QuestionText', '')),
|
118 |
+
'correct_answer_text': str(wrong_q.get(f'Answer{wrong_q["CorrectAnswer"]}Text', '')),
|
119 |
+
'wrong_answer_text': str(wrong_q.get(f'Answer{st.session_state.selected_wrong_answer}Text', '')),
|
120 |
+
'misconception_id': int(misconception_id)
|
121 |
+
}
|
122 |
+
|
123 |
+
logger.info(f"Prepared input data: {input_data}")
|
124 |
+
|
125 |
+
with st.spinner("π μ μ¬ λ¬Έμ λ₯Ό μμ±νκ³ μμ΅λλ€..."):
|
126 |
+
# μ μ¬ λ¬Έμ μμ± νΈμΆ
|
127 |
+
generated_q, _ = generator.generate_similar_question_with_text(
|
128 |
+
construct_name=input_data['construct_name'],
|
129 |
+
subject_name=input_data['subject_name'],
|
130 |
+
question_text=input_data['question_text'],
|
131 |
+
correct_answer_text=input_data['correct_answer_text'],
|
132 |
+
wrong_answer_text=input_data['wrong_answer_text'],
|
133 |
+
misconception_id=input_data['misconception_id']
|
134 |
+
)
|
135 |
+
|
136 |
+
if generated_q:
|
137 |
+
verifier = load_answer_verifier()
|
138 |
+
with st.status("π€ AIκ° λ¬Έμ λ₯Ό κ²ν νκ³ μμ΅λλ€..."):
|
139 |
+
st.write("λ΅μμ μ νμ±μ κ²μ¦νκ³ μμ΅λλ€...")
|
140 |
+
verified_answer = verifier.verify_answer(
|
141 |
+
question=generated_q.question,
|
142 |
+
choices=generated_q.choices
|
143 |
+
)
|
144 |
+
|
145 |
+
if verified_answer:
|
146 |
+
logger.info(f"Answer verified: {verified_answer}")
|
147 |
+
st.write("β
κ²μ¦ μλ£!")
|
148 |
+
result = {
|
149 |
+
'question': generated_q.question,
|
150 |
+
'choices': generated_q.choices,
|
151 |
+
'correct': verified_answer,
|
152 |
+
'explanation': generated_q.explanation
|
153 |
+
}
|
154 |
+
st.session_state['current_similar_question_answer'] = verified_answer
|
155 |
+
return result
|
156 |
+
else:
|
157 |
+
logger.warning("Answer verification failed, using original answer")
|
158 |
+
st.write("β οΈ κ²μ¦μ μ€ν¨νμ΅λλ€. μλ³Έ λ΅μμ μ¬μ©ν©λλ€.")
|
159 |
+
result = {
|
160 |
+
'question': generated_q.question,
|
161 |
+
'choices': generated_q.choices,
|
162 |
+
'correct': generated_q.correct_answer,
|
163 |
+
'explanation': generated_q.explanation
|
164 |
+
}
|
165 |
+
st.session_state['current_similar_question_answer'] = generated_q.correct_answer
|
166 |
+
return result
|
167 |
+
|
168 |
+
except Exception as e:
|
169 |
+
logger.error(f"Error in generate_similar_question: {str(e)}")
|
170 |
+
st.error(f"λ¬Έμ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}")
|
171 |
+
return None
|
172 |
+
|
173 |
+
return None
|
174 |
+
|
175 |
+
def handle_answer(answer, current_q):
|
176 |
+
"""λ΅λ³ μ²λ¦¬"""
|
177 |
+
if answer != current_q['CorrectAnswer']:
|
178 |
+
wrong_q_dict = current_q.to_dict()
|
179 |
+
st.session_state.wrong_questions.append(wrong_q_dict)
|
180 |
+
st.session_state.selected_wrong_answer = answer
|
181 |
+
|
182 |
+
misconception_key = f'Misconception{answer}Id'
|
183 |
+
misconception_id = current_q.get(misconception_key)
|
184 |
+
st.session_state.misconceptions.append(misconception_id)
|
185 |
+
|
186 |
+
st.session_state.current_question_index += 1
|
187 |
+
if st.session_state.current_question_index >= 10:
|
188 |
+
st.session_state.current_step = 'review'
|
189 |
+
|
190 |
+
# μ μ LaTeX ν¬λ§·ν° μΈμ€ν΄μ€ μμ±
|
191 |
+
latex_formatter = LatexFormatter()
|
192 |
+
|
193 |
+
def display_math_content(content: str):
|
194 |
+
"""μν λ΄μ©μ νλ©΄μ νμ"""
|
195 |
+
formatted_content = latex_formatter.format_expression(content)
|
196 |
+
st.latex(formatted_content)
|
197 |
+
st.markdown(formatted_content, unsafe_allow_html=True)
|
198 |
+
|
199 |
+
def format_answer_choice(choice: str) -> str:
|
200 |
+
"""μ νμ§ LaTeX ν¬λ§·ν
"""
|
201 |
+
return latex_formatter.format_expression(choice)
|
202 |
+
|
203 |
+
def display_question_and_answers(question_text: str, answers: dict):
|
204 |
+
"""
|
205 |
+
Display the question and its answer choices in a formatted layout.
|
206 |
+
Args:
|
207 |
+
question_text (str): LaTeX-formatted question text.
|
208 |
+
answers (dict): A dictionary of answer choices (keys: 'A', 'B', 'C', 'D').
|
209 |
+
"""
|
210 |
+
# Display the question
|
211 |
+
st.write("### λ¬Έμ ")
|
212 |
+
st.latex(question_text)
|
213 |
+
|
214 |
+
# Arrange the answers in two columns
|
215 |
+
col1, col2 = st.columns(2)
|
216 |
+
with col1:
|
217 |
+
if st.button(f"A) {answers['A']}", key="A"):
|
218 |
+
return 'A'
|
219 |
+
if st.button(f"C) {answers['C']}", key="C"):
|
220 |
+
return 'C'
|
221 |
+
with col2:
|
222 |
+
if st.button(f"B) {answers['B']}", key="B"):
|
223 |
+
return 'B'
|
224 |
+
if st.button(f"D) {answers['D']}", key="D"):
|
225 |
+
return 'D'
|
226 |
+
|
227 |
+
return None
|
228 |
+
|
229 |
+
def main():
|
230 |
+
"""λ©μΈ μ ν리μΌμ΄μ
λ‘μ§"""
|
231 |
+
st.title("MisconcepTutor")
|
232 |
+
|
233 |
+
# Generator μ΄κΈ°ν
|
234 |
+
generator = load_question_generator()
|
235 |
+
|
236 |
+
# μ΄κΈ° νλ©΄
|
237 |
+
if st.session_state.current_step == 'initial':
|
238 |
+
st.write("#### νμ΅μ μμνκ² μ΅λλ€. 10κ°μ λ¬Έμ λ₯Ό νμ΄λ³ΌκΉμ?")
|
239 |
+
if st.button("νμ΅ μμ", key="start_quiz"):
|
240 |
+
start_quiz()
|
241 |
+
st.rerun()
|
242 |
+
|
243 |
+
# ν΄μ¦ νλ©΄
|
244 |
+
# elif st.session_state.current_step == 'quiz':
|
245 |
+
# current_q = st.session_state.questions.iloc[st.session_state.current_question_index]
|
246 |
+
|
247 |
+
# # μ§ν μν© νμ
|
248 |
+
# progress = st.session_state.current_question_index / 10
|
249 |
+
# st.progress(progress)
|
250 |
+
# st.write(f"### λ¬Έμ {st.session_state.current_question_index + 1}/10")
|
251 |
+
|
252 |
+
# # λ¬Έμ νμ
|
253 |
+
# st.markdown("---")
|
254 |
+
# #display_math_question(current_q['QuestionText'])
|
255 |
+
# display_math_content(current_q['QuestionText']) # display_math_question λμ display_math_content μ¬μ©
|
256 |
+
|
257 |
+
# # 보기 νμ
|
258 |
+
# col1, col2 = st.columns(2)
|
259 |
+
# with col1:
|
260 |
+
# if st.button(f"A) {latex_formatter.format_expression(current_q['AnswerAText'])}", key="A"):
|
261 |
+
# handle_answer('A', current_q)
|
262 |
+
# st.rerun()
|
263 |
+
# if st.button(f"C) {latex_formatter.format_expression(current_q['AnswerCText'])}", key="C"):
|
264 |
+
# handle_answer('C', current_q)
|
265 |
+
# st.rerun()
|
266 |
+
# with col2:
|
267 |
+
# if st.button(f"B) {latex_formatter.format_expression(current_q['AnswerBText'])}", key="B"):
|
268 |
+
# handle_answer('B', current_q)
|
269 |
+
# st.rerun()
|
270 |
+
# if st.button(f"D) {latex_formatter.format_expression(current_q['AnswerDText'])}", key="D"):
|
271 |
+
# handle_answer('D', current_q)
|
272 |
+
# st.rerun()
|
273 |
+
# ν΄μ¦ νλ©΄
|
274 |
+
elif st.session_state.current_step == 'quiz':
|
275 |
+
current_q = st.session_state.questions.iloc[st.session_state.current_question_index]
|
276 |
+
|
277 |
+
# μ§ν μν© νμ
|
278 |
+
progress = st.session_state.current_question_index / 10
|
279 |
+
st.progress(progress)
|
280 |
+
st.write(f"### λ¬Έμ {st.session_state.current_question_index + 1}/10")
|
281 |
+
|
282 |
+
# Extract question and answers
|
283 |
+
question_text = current_q['QuestionText'] # LaTeX content for the question
|
284 |
+
answers = {
|
285 |
+
'A': current_q['AnswerAText'],
|
286 |
+
'B': current_q['AnswerBText'],
|
287 |
+
'C': current_q['AnswerCText'],
|
288 |
+
'D': current_q['AnswerDText']
|
289 |
+
}
|
290 |
+
|
291 |
+
# Display the question and answers
|
292 |
+
selected_answer = display_question_and_answers(question_text, answers)
|
293 |
+
|
294 |
+
# Handle answer selection
|
295 |
+
if selected_answer:
|
296 |
+
handle_answer(selected_answer, current_q)
|
297 |
+
st.rerun()
|
298 |
+
# λ³΅μ΅ νλ©΄
|
299 |
+
elif st.session_state.current_step == 'review':
|
300 |
+
st.write("### νμ΅ κ²°κ³Ό")
|
301 |
+
|
302 |
+
# κ²°κ³Ό ν΅κ³
|
303 |
+
col1, col2, col3 = st.columns(3)
|
304 |
+
col1.metric("μ΄ λ¬Έμ μ", 10)
|
305 |
+
col2.metric("λ§μ λ¬Έμ ", 10 - len(st.session_state.wrong_questions))
|
306 |
+
col3.metric("νλ¦° λ¬Έμ ", len(st.session_state.wrong_questions))
|
307 |
+
|
308 |
+
# κ²°κ³Όμ λ°λ₯Έ λ©μμ§ νμ
|
309 |
+
if len(st.session_state.wrong_questions) == 0:
|
310 |
+
st.balloons() # μΆν ν¨κ³Ό
|
311 |
+
st.success("π μΆνν©λλ€! λͺ¨λ λ¬Έμ λ₯Ό λ§μΆμ
¨μ΄μ!")
|
312 |
+
st.markdown("""
|
313 |
+
### π μνμμ΄μλλ€!
|
314 |
+
μλ²½ν μ μλ₯Ό λ°μΌμ
¨λ€μ! μνμ κ°λ
μ μ ννκ² μ΄ν΄νκ³ κ³μ κ² κ°μ΅λλ€.
|
315 |
+
""")
|
316 |
+
elif len(st.session_state.wrong_questions) <= 3:
|
317 |
+
st.success("μ νμ
¨μ΄μ! μ‘°κΈλ§ λ μ°μ΅νλ©΄ μλ²½ν κ±°μμ!")
|
318 |
+
else:
|
319 |
+
st.info("μ²μ²ν κ°λ
μ 볡μ΅ν΄λ³΄μμ. μ°μ΅νλ€ λ³΄λ©΄ λμ΄λ κ±°μμ!")
|
320 |
+
|
321 |
+
# λ€λΉκ²μ΄μ
λ²νΌ
|
322 |
+
col1, col2 = st.columns(2)
|
323 |
+
with col1:
|
324 |
+
if st.button("π μλ‘μ΄ λ¬Έμ μΈνΈ μμνκΈ°", use_container_width=True):
|
325 |
+
start_quiz()
|
326 |
+
st.rerun()
|
327 |
+
with col2:
|
328 |
+
if st.button("π μ²μμΌλ‘ λμκ°κΈ°", use_container_width=True):
|
329 |
+
st.session_state.clear()
|
330 |
+
st.rerun()
|
331 |
+
|
332 |
+
# νλ¦° λ¬Έμ λΆμ λΆλΆ
|
333 |
+
if st.session_state.wrong_questions:
|
334 |
+
st.write("### βοΈ νλ¦° λ¬Έμ λΆμ")
|
335 |
+
tabs = st.tabs([f"π νλ¦° λ¬Έμ #{i + 1}" for i in range(len(st.session_state.wrong_questions))])
|
336 |
+
|
337 |
+
for i, (tab, (wrong_q, misconception_id)) in enumerate(zip(
|
338 |
+
tabs,
|
339 |
+
zip(st.session_state.wrong_questions, st.session_state.misconceptions)
|
340 |
+
)):
|
341 |
+
with tab:
|
342 |
+
st.write("**π λ¬Έμ :**")
|
343 |
+
st.write(wrong_q['QuestionText'])
|
344 |
+
st.write("**β
μ λ΅:**", wrong_q['CorrectAnswer'])
|
345 |
+
|
346 |
+
st.write("---")
|
347 |
+
st.write("**π κ΄λ ¨λ Misconception:**")
|
348 |
+
if misconception_id and not pd.isna(misconception_id):
|
349 |
+
misconception_text = generator.get_misconception_text(misconception_id)
|
350 |
+
st.info(f"Misconception ID: {int(misconception_id)}\n\n{misconception_text}")
|
351 |
+
else:
|
352 |
+
st.info("Misconception μ λ³΄κ° μμ΅λλ€.")
|
353 |
+
|
354 |
+
if st.button(f"π μ μ¬ λ¬Έμ νκΈ°", key=f"retry_{i}"):
|
355 |
+
st.session_state[f"show_similar_question_{i}"] = True
|
356 |
+
st.session_state[f"similar_question_answered_{i}"] = False
|
357 |
+
st.rerun()
|
358 |
+
|
359 |
+
if st.session_state.get(f"show_similar_question_{i}", False):
|
360 |
+
st.divider()
|
361 |
+
new_question = generate_similar_question(wrong_q, misconception_id, generator)
|
362 |
+
if new_question:
|
363 |
+
st.write("### π― μ μ¬ λ¬Έμ ")
|
364 |
+
#st.write(new_question['question'])
|
365 |
+
display_math_question(new_question['question'])
|
366 |
+
|
367 |
+
|
368 |
+
# λ΅λ³ μν νμΈ
|
369 |
+
answered = st.session_state.get(f"similar_question_answered_{i}", False)
|
370 |
+
|
371 |
+
# 보기 νμ
|
372 |
+
st.write("**보기:**")
|
373 |
+
col1, col2 = st.columns(2)
|
374 |
+
|
375 |
+
# # λ΅λ³νμ§ μμ κ²½μ°μλ§ λ²νΌ νμ±ν
|
376 |
+
# if not answered:
|
377 |
+
# with col1:
|
378 |
+
# for option in ['A', 'C']:
|
379 |
+
# if st.button(
|
380 |
+
# f"{option}) {new_question['choices'][option]}",
|
381 |
+
# key=f"similar_{option}_{i}"
|
382 |
+
# ):
|
383 |
+
# st.session_state[f"similar_question_answered_{i}"] = True
|
384 |
+
# st.session_state[f"selected_answer_{i}"] = option
|
385 |
+
# correct_answer = st.session_state.get('current_similar_question_answer')
|
386 |
+
# if option == correct_answer:
|
387 |
+
# st.session_state[f"is_correct_{i}"] = True
|
388 |
+
# else:
|
389 |
+
# st.session_state[f"is_correct_{i}"] = False
|
390 |
+
# st.rerun()
|
391 |
+
|
392 |
+
# with col2:
|
393 |
+
# for option in ['B', 'D']:
|
394 |
+
# if st.button(
|
395 |
+
# f"{option}) {new_question['choices'][option]}",
|
396 |
+
# key=f"similar_{option}_{i}"
|
397 |
+
# ):
|
398 |
+
# st.session_state[f"similar_question_answered_{i}"] = True
|
399 |
+
# st.session_state[f"selected_answer_{i}"] = option
|
400 |
+
# correct_answer = st.session_state.get('current_similar_question_answer')
|
401 |
+
# if option == correct_answer:
|
402 |
+
# st.session_state[f"is_correct_{i}"] = True
|
403 |
+
# else:
|
404 |
+
# st.session_state[f"is_correct_{i}"] = False
|
405 |
+
# st.rerun()
|
406 |
+
# λ΅λ³νμ§ μμ κ²½μ°μλ§ λ²νΌ νμ±ν
|
407 |
+
if not answered:
|
408 |
+
with col1:
|
409 |
+
for option in ['A', 'C']:
|
410 |
+
if st.button(
|
411 |
+
f"{option}) {latex_formatter.format_expression(new_question['choices'][option])}",
|
412 |
+
key=f"similar_{option}_{i}"
|
413 |
+
):
|
414 |
+
st.session_state[f"similar_question_answered_{i}"] = True
|
415 |
+
st.session_state[f"selected_answer_{i}"] = option
|
416 |
+
correct_answer = st.session_state.get('current_similar_question_answer')
|
417 |
+
if option == correct_answer:
|
418 |
+
st.session_state[f"is_correct_{i}"] = True
|
419 |
+
else:
|
420 |
+
st.session_state[f"is_correct_{i}"] = False
|
421 |
+
st.rerun()
|
422 |
+
|
423 |
+
with col2:
|
424 |
+
for option in ['B', 'D']:
|
425 |
+
if st.button(
|
426 |
+
f"{option}) {format_math_expression(new_question['choices'][option])}",
|
427 |
+
key=f"similar_{option}_{i}"
|
428 |
+
):
|
429 |
+
st.session_state[f"similar_question_answered_{i}"] = True
|
430 |
+
st.session_state[f"selected_answer_{i}"] = option
|
431 |
+
correct_answer = st.session_state.get('current_similar_question_answer')
|
432 |
+
if option == correct_answer:
|
433 |
+
st.session_state[f"is_correct_{i}"] = True
|
434 |
+
else:
|
435 |
+
st.session_state[f"is_correct_{i}"] = False
|
436 |
+
st.rerun()
|
437 |
+
# λ΅λ³ν κ²½μ° κ²°κ³Ό νμ
|
438 |
+
if answered:
|
439 |
+
is_correct = st.session_state.get(f"is_correct_{i}", False)
|
440 |
+
correct_answer = st.session_state.get('current_similar_question_answer')
|
441 |
+
if is_correct:
|
442 |
+
st.success("β
μ λ΅μ
λλ€!")
|
443 |
+
else:
|
444 |
+
st.error(f"β νλ Έμ΅λλ€. μ λ΅μ {correct_answer}μ
λλ€.")
|
445 |
+
|
446 |
+
# ν΄μ€ νμ
|
447 |
+
st.write("---")
|
448 |
+
st.write("**π ν΄μ€:**", new_question['explanation'])
|
449 |
+
|
450 |
+
# λ€μ νκΈ° λ²νΌ
|
451 |
+
if st.button("π λ€μ νκΈ°", key=f"reset_{i}"):
|
452 |
+
st.session_state[f"similar_question_answered_{i}"] = False
|
453 |
+
st.session_state[f"selected_answer_{i}"] = None
|
454 |
+
st.session_state[f"is_correct_{i}"] = None
|
455 |
+
st.rerun()
|
456 |
+
|
457 |
+
# λ¬Έμ λ«κΈ° λ²νΌ
|
458 |
+
if st.button("β λ¬Έμ λ«κΈ°", key=f"close_{i}"):
|
459 |
+
st.session_state[f"show_similar_question_{i}"] = False
|
460 |
+
st.session_state[f"similar_question_answered_{i}"] = False
|
461 |
+
st.session_state[f"selected_answer_{i}"] = None
|
462 |
+
st.session_state[f"is_correct_{i}"] = None
|
463 |
+
st.rerun()
|
464 |
+
|
465 |
+
# νλ©΄ μλ μ¬λ°± μΆκ°
|
466 |
+
st.markdown("<br>" * 5, unsafe_allow_html=True) # 5μ€μ λΉ μ€ μΆκ°
|
467 |
+
st.markdown("""
|
468 |
+
<div style="height: 100px;">
|
469 |
+
</div>
|
470 |
+
""", unsafe_allow_html=True) # μΆκ° μ¬λ°±
|
471 |
+
else:
|
472 |
+
st.error("μ μ¬ λ¬Έμ λ₯Ό μμ±ν μ μμ΅λλ€.")
|
473 |
+
if st.button("β λ«κΈ°", key=f"close_error_{i}"):
|
474 |
+
st.session_state[f"show_similar_question_{i}"] = False
|
475 |
+
st.rerun()
|
476 |
+
# νλ©΄ μλ μ¬λ°± μΆκ°
|
477 |
+
st.markdown("<br>" * 5, unsafe_allow_html=True) # 5μ€μ λΉ μ€ μΆκ°
|
478 |
+
st.markdown("""
|
479 |
+
<div style="height: 100px;">
|
480 |
+
</div>
|
481 |
+
""", unsafe_allow_html=True) # μΆκ° μ¬λ°±
|
482 |
+
if __name__ == "__main__":
|
483 |
+
main()
|
484 |
+
|
485 |
+
# random_state 42μμ μ λ΅
|
486 |
+
# D C A A C
|
487 |
+
# A B B B B
|