File size: 23,630 Bytes
d9a31b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import streamlit as st
import pandas as pd
import os
from src.SecondModule.module2 import SimilarQuestionGenerator
from src.ThirdModule.module3 import AnswerVerifier
import logging
from typing import Optional, Tuple
#from latex_formatter import LatexFormatter  # LaTeX 포맷터 import
from pylatexenc.latex2text import LatexNodes2Text


logging.basicConfig(level=logging.DEBUG)


# Streamlit νŽ˜μ΄μ§€ κΈ°λ³Έ μ„€μ •
st.set_page_config(
    page_title="MisconcepTutor", 
    layout="wide", 
    initial_sidebar_state="expanded"
)

@st.cache_resource
def load_answer_verifier():
    """λ‹΅μ•ˆ 검증 λͺ¨λΈ λ‘œλ“œ"""
    from src.ThirdModule.module3 import AnswerVerifier
    return AnswerVerifier()
    
# 경둜 μ„€μ •
base_path = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(base_path, 'Data')
misconception_csv_path = os.path.join(data_path, 'misconception_mapping.csv')

# λ‘œκΉ… μ„€μ •
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# μ„Έμ…˜ μƒνƒœ μ΄ˆκΈ°ν™” - κ°€μž₯ λ¨Όμ € μ‹€ν–‰λ˜λ„λ‘ μ΅œμƒλ‹¨μ— 배치
if 'initialized' not in st.session_state:
    st.session_state.initialized = True
    st.session_state.wrong_questions = []
    st.session_state.misconceptions = []
    st.session_state.current_question_index = 0
    st.session_state.generated_questions = []
    st.session_state.current_step = 'initial'
    st.session_state.selected_wrong_answer = None
    st.session_state.questions = []
    logger.info("Session state initialized")

# 문제 생성기 μ΄ˆκΈ°ν™”
@st.cache_resource
def load_question_generator():
    """문제 생성 λͺ¨λΈ λ‘œλ“œ"""
    if not os.path.exists(misconception_csv_path):
        st.error(f"CSV 파일이 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€: {misconception_csv_path}")
        raise FileNotFoundError(f"CSV 파일이 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€: {misconception_csv_path}")
    return SimilarQuestionGenerator(misconception_csv_path=misconception_csv_path)

# CSV 데이터 λ‘œλ“œ ν•¨μˆ˜
@st.cache_data
def load_data(data_file = '/train.csv'):
    try:
        file_path = os.path.join(data_path, data_file.lstrip('/'))
        df = pd.read_csv(file_path)
        logger.info(f"Data loaded successfully from {file_path}")
        return df
    except FileNotFoundError:
        st.error(f"νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€: {data_file}")
        logger.error(f"File not found: {data_file}")
        return None

def start_quiz():
    """ν€΄μ¦ˆ μ‹œμž‘ 및 μ΄ˆκΈ°ν™”"""
    df = load_data()
    if df is None or df.empty:
        st.error("데이터λ₯Ό 뢈러올 수 μ—†μŠ΅λ‹ˆλ‹€. 데이터셋을 ν™•μΈν•΄μ£Όμ„Έμš”.")
        return

    st.session_state.questions = df.sample(n=10, random_state=42)
    st.session_state.current_step = 'quiz'
    st.session_state.current_question_index = 0
    st.session_state.wrong_questions = []
    st.session_state.misconceptions = []
    st.session_state.generated_questions = []
    logger.info("Quiz started")


def generate_similar_question(wrong_q, misconception_id, generator):
    """μœ μ‚¬ 문제 생성"""
    logger.info(f"Generating similar question for misconception_id: {misconception_id}")
    
    # μž…λ ₯ 데이터 μœ νš¨μ„± 검사
    if not isinstance(wrong_q, dict):
        logger.error(f"Invalid wrong_q type: {type(wrong_q)}")
        st.error("μœ μ‚¬ 문제 생성에 ν•„μš”ν•œ 데이터 ν˜•μ‹μ΄ 잘λͺ»λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
        return None
        
    try:
        # misconception_idκ°€ μ—†κ±°λ‚˜ NaN인 경우 λ‹€λ₯Έ misconception μ‚¬μš©
        if pd.isna(misconception_id):
            logger.info("Original misconception_id is NaN, trying to find alternative")
            # ν˜„μž¬κΉŒμ§€ λ‚˜μ˜¨ misconceptionλ“€ μ€‘μ—μ„œ 선택
            available_misconceptions = [m for m in st.session_state.misconceptions if not pd.isna(m)]
            
            if available_misconceptions:
                # κ°€μž₯ μ΅œκ·Όμ— λ‚˜μ˜¨ misconception 선택
                misconception_id = available_misconceptions[-1]
                logger.info(f"Using alternative misconception_id: {misconception_id}")
            else:
                # κΈ°λ³Έ misconception ID μ‚¬μš© (예: κ°€μž₯ 기본적인 misconception)
                misconception_id = 2001  # μ μ ˆν•œ κΈ°λ³Έκ°’μœΌλ‘œ μˆ˜μ • ν•„μš”
                logger.info(f"Using default misconception_id: {misconception_id}")

        # 데이터 μ€€λΉ„ (νŠœν”Œ λ³€ν™˜ λ°©μ§€)
        input_data = {
            'construct_name': str(wrong_q.get('ConstructName', '')),
            'subject_name': str(wrong_q.get('SubjectName', '')),
            'question_text': str(wrong_q.get('QuestionText', '')),
            'correct_answer_text': str(wrong_q.get(f'Answer{wrong_q["CorrectAnswer"]}Text', '')),
            'wrong_answer_text': str(wrong_q.get(f'Answer{st.session_state.selected_wrong_answer}Text', '')),
            'misconception_id': int(misconception_id)
        }
        
        logger.info(f"Prepared input data: {input_data}")

        with st.spinner("πŸ“ μœ μ‚¬ 문제λ₯Ό μƒμ„±ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€..."):
            # μœ μ‚¬ 문제 생성 호좜
            generated_q, _ = generator.generate_similar_question_with_text(
                construct_name=input_data['construct_name'],
                subject_name=input_data['subject_name'],
                question_text=input_data['question_text'],
                correct_answer_text=input_data['correct_answer_text'],
                wrong_answer_text=input_data['wrong_answer_text'],
                misconception_id=input_data['misconception_id']
            )

        if generated_q:
            verifier = load_answer_verifier()
            with st.status("πŸ€” AIκ°€ 문제λ₯Ό κ²€ν† ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€..."):
                st.write("λ‹΅μ•ˆμ˜ 정확성을 κ²€μ¦ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€...")
                verified_answer = verifier.verify_answer(
                    question=generated_q.question,
                    choices=generated_q.choices
                )
                
                if verified_answer:
                    logger.info(f"Answer verified: {verified_answer}")
                    st.write("βœ… 검증 μ™„λ£Œ!")
                    result = {
                        'question': generated_q.question,
                        'choices': generated_q.choices,
                        'correct': verified_answer,
                        'explanation': generated_q.explanation
                    }
                    st.session_state['current_similar_question_answer'] = verified_answer
                    return result
                else:
                    logger.warning("Answer verification failed, using original answer")
                    st.write("⚠️ 검증에 μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€. 원본 λ‹΅μ•ˆμ„ μ‚¬μš©ν•©λ‹ˆλ‹€.")
                    result = {
                        'question': generated_q.question,
                        'choices': generated_q.choices,
                        'correct': generated_q.correct_answer,
                        'explanation': generated_q.explanation
                    }
                    st.session_state['current_similar_question_answer'] = generated_q.correct_answer
                    return result
                    
    except Exception as e:
        logger.error(f"Error in generate_similar_question: {str(e)}")
        st.error(f"문제 생성 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}")
        return None

    return None

def handle_answer(answer, current_q):
    """λ‹΅λ³€ 처리"""
    if answer != current_q['CorrectAnswer']:
        wrong_q_dict = current_q.to_dict()
        st.session_state.wrong_questions.append(wrong_q_dict)
        st.session_state.selected_wrong_answer = answer
        
        misconception_key = f'Misconception{answer}Id'
        misconception_id = current_q.get(misconception_key)
        st.session_state.misconceptions.append(misconception_id)
    
    st.session_state.current_question_index += 1
    if st.session_state.current_question_index >= 10:
        st.session_state.current_step = 'review'

# μ „μ—­ LaTeX 포맷터 μΈμŠ€ν„΄μŠ€ 생성
latex_formatter = LatexFormatter()

def display_math_content(content: str):
    """μˆ˜ν•™ λ‚΄μš©μ„ 화면에 ν‘œμ‹œ"""
    formatted_content = latex_formatter.format_expression(content)
    st.latex(formatted_content)
    st.markdown(formatted_content, unsafe_allow_html=True)

def format_answer_choice(choice: str) -> str:
    """선택지 LaTeX ν¬λ§·νŒ…"""
    return latex_formatter.format_expression(choice)

def display_question_and_answers(question_text: str, answers: dict):
    """
    Display the question and its answer choices in a formatted layout.
    Args:
        question_text (str): LaTeX-formatted question text.
        answers (dict): A dictionary of answer choices (keys: 'A', 'B', 'C', 'D').
    """
    # Display the question
    st.write("### 문제")
    st.latex(question_text)

    # Arrange the answers in two columns
    col1, col2 = st.columns(2)
    with col1:
        if st.button(f"A) {answers['A']}", key="A"):
            return 'A'
        if st.button(f"C) {answers['C']}", key="C"):
            return 'C'
    with col2:
        if st.button(f"B) {answers['B']}", key="B"):
            return 'B'
        if st.button(f"D) {answers['D']}", key="D"):
            return 'D'

    return None

def main():
    """메인 μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 둜직"""
    st.title("MisconcepTutor")
    
    # Generator μ΄ˆκΈ°ν™”
    generator = load_question_generator()

    # 초기 ν™”λ©΄
    if st.session_state.current_step == 'initial':
        st.write("#### ν•™μŠ΅μ„ μ‹œμž‘ν•˜κ² μŠ΅λ‹ˆλ‹€. 10개의 문제λ₯Ό ν’€μ–΄λ³ΌκΉŒμš”?")
        if st.button("ν•™μŠ΅ μ‹œμž‘", key="start_quiz"):
            start_quiz()
            st.rerun()

    # ν€΄μ¦ˆ ν™”λ©΄
    # elif st.session_state.current_step == 'quiz':
    #     current_q = st.session_state.questions.iloc[st.session_state.current_question_index]
        
    #     # μ§„ν–‰ 상황 ν‘œμ‹œ
    #     progress = st.session_state.current_question_index / 10
    #     st.progress(progress)
    #     st.write(f"### 문제 {st.session_state.current_question_index + 1}/10")
        
    #     # 문제 ν‘œμ‹œ
    #     st.markdown("---")
    #     #display_math_question(current_q['QuestionText'])
    #     display_math_content(current_q['QuestionText'])  # display_math_question λŒ€μ‹  display_math_content μ‚¬μš©
        
    #     # 보기 ν‘œμ‹œ
    #     col1, col2 = st.columns(2)
    #     with col1:
    #         if st.button(f"A) {latex_formatter.format_expression(current_q['AnswerAText'])}", key="A"):
    #             handle_answer('A', current_q)
    #             st.rerun()
    #         if st.button(f"C) {latex_formatter.format_expression(current_q['AnswerCText'])}", key="C"):
    #             handle_answer('C', current_q)
    #             st.rerun()
    #     with col2:
    #         if st.button(f"B) {latex_formatter.format_expression(current_q['AnswerBText'])}", key="B"):
    #             handle_answer('B', current_q)
    #             st.rerun()
    #         if st.button(f"D) {latex_formatter.format_expression(current_q['AnswerDText'])}", key="D"):
    #             handle_answer('D', current_q)
    #             st.rerun()
    # ν€΄μ¦ˆ ν™”λ©΄
    elif st.session_state.current_step == 'quiz':
        current_q = st.session_state.questions.iloc[st.session_state.current_question_index]
    
        # μ§„ν–‰ 상황 ν‘œμ‹œ
        progress = st.session_state.current_question_index / 10
        st.progress(progress)
        st.write(f"### 문제 {st.session_state.current_question_index + 1}/10")
        
        # Extract question and answers
        question_text = current_q['QuestionText']  # LaTeX content for the question
        answers = {
            'A': current_q['AnswerAText'],
            'B': current_q['AnswerBText'],
            'C': current_q['AnswerCText'],
            'D': current_q['AnswerDText']
        }
        
        # Display the question and answers
        selected_answer = display_question_and_answers(question_text, answers)
        
        # Handle answer selection
        if selected_answer:
            handle_answer(selected_answer, current_q)
            st.rerun()
    # 볡슡 ν™”λ©΄
    elif st.session_state.current_step == 'review':
        st.write("### ν•™μŠ΅ κ²°κ³Ό")
        
        # κ²°κ³Ό 톡계
        col1, col2, col3 = st.columns(3)
        col1.metric("총 문제 수", 10)
        col2.metric("λ§žμ€ 문제", 10 - len(st.session_state.wrong_questions))
        col3.metric("ν‹€λ¦° 문제", len(st.session_state.wrong_questions))
        
        # 결과에 λ”°λ₯Έ λ©”μ‹œμ§€ ν‘œμ‹œ
        if len(st.session_state.wrong_questions) == 0:
            st.balloons()  # μΆ•ν•˜ 효과
            st.success("πŸŽ‰ μΆ•ν•˜ν•©λ‹ˆλ‹€! λͺ¨λ“  문제λ₯Ό λ§žμΆ”μ…¨μ–΄μš”!")
            st.markdown("""
            ### πŸ† μˆ˜ν•™μ™•μ΄μ‹­λ‹ˆλ‹€! 
            μ™„λ²½ν•œ 점수λ₯Ό λ°›μœΌμ…¨λ„€μš”! μˆ˜ν•™μ  κ°œλ…μ„ μ •ν™•ν•˜κ²Œ μ΄ν•΄ν•˜κ³  계신 것 κ°™μŠ΅λ‹ˆλ‹€.
            """)
        elif len(st.session_state.wrong_questions) <= 3:
            st.success("잘 ν•˜μ…¨μ–΄μš”! 쑰금만 더 μ—°μŠ΅ν•˜λ©΄ μ™„λ²½ν•  κ±°μ˜ˆμš”!")
        else:
            st.info("천천히 κ°œλ…μ„ λ³΅μŠ΅ν•΄λ³΄μ•„μš”. μ—°μŠ΅ν•˜λ‹€ 보면 λŠ˜μ–΄λ‚  κ±°μ˜ˆμš”!")
        
        # λ„€λΉ„κ²Œμ΄μ…˜ λ²„νŠΌ
        col1, col2 = st.columns(2)
        with col1:
            if st.button("πŸ”„ μƒˆλ‘œμš΄ 문제 μ„ΈνŠΈ μ‹œμž‘ν•˜κΈ°", use_container_width=True):
                start_quiz()
                st.rerun()
        with col2:
            if st.button("🏠 처음으둜 λŒμ•„κ°€κΈ°", use_container_width=True):
                st.session_state.clear()
                st.rerun()
        
        # ν‹€λ¦° 문제 뢄석 λΆ€λΆ„
        if st.session_state.wrong_questions:
            st.write("### ✍️ ν‹€λ¦° 문제 뢄석")
            tabs = st.tabs([f"πŸ“ ν‹€λ¦° 문제 #{i + 1}" for i in range(len(st.session_state.wrong_questions))])
            
            for i, (tab, (wrong_q, misconception_id)) in enumerate(zip(
                tabs,
                zip(st.session_state.wrong_questions, st.session_state.misconceptions)
            )):
                with tab:
                    st.write("**πŸ“‹ 문제:**")
                    st.write(wrong_q['QuestionText'])
                    st.write("**βœ… μ •λ‹΅:**", wrong_q['CorrectAnswer'])
                    
                    st.write("---")
                    st.write("**πŸ” κ΄€λ ¨λœ Misconception:**")
                    if misconception_id and not pd.isna(misconception_id):
                        misconception_text = generator.get_misconception_text(misconception_id)
                        st.info(f"Misconception ID: {int(misconception_id)}\n\n{misconception_text}")
                    else:
                        st.info("Misconception 정보가 μ—†μŠ΅λ‹ˆλ‹€.")
        
                    if st.button(f"πŸ“š μœ μ‚¬ 문제 ν’€κΈ°", key=f"retry_{i}"):
                        st.session_state[f"show_similar_question_{i}"] = True
                        st.session_state[f"similar_question_answered_{i}"] = False
                        st.rerun()
                    
                    if st.session_state.get(f"show_similar_question_{i}", False):
                        st.divider()
                        new_question = generate_similar_question(wrong_q, misconception_id, generator)
                        if new_question:
                            st.write("### 🎯 μœ μ‚¬ 문제")
                            #st.write(new_question['question'])
                            display_math_question(new_question['question'])

                            
                            # λ‹΅λ³€ μƒνƒœ 확인
                            answered = st.session_state.get(f"similar_question_answered_{i}", False)
                            
                            # 보기 ν‘œμ‹œ
                            st.write("**보기:**")
                            col1, col2 = st.columns(2)
                            
                            # # λ‹΅λ³€ν•˜μ§€ μ•Šμ€ κ²½μš°μ—λ§Œ λ²„νŠΌ ν™œμ„±ν™”
                            # if not answered:
                            #     with col1:
                            #         for option in ['A', 'C']:
                            #             if st.button(
                            #                 f"{option}) {new_question['choices'][option]}", 
                            #                 key=f"similar_{option}_{i}"
                            #             ):
                            #                 st.session_state[f"similar_question_answered_{i}"] = True
                            #                 st.session_state[f"selected_answer_{i}"] = option
                            #                 correct_answer = st.session_state.get('current_similar_question_answer')
                            #                 if option == correct_answer:
                            #                     st.session_state[f"is_correct_{i}"] = True
                            #                 else:
                            #                     st.session_state[f"is_correct_{i}"] = False
                            #                 st.rerun()
                                
                            #     with col2:
                            #         for option in ['B', 'D']:
                            #             if st.button(
                            #                 f"{option}) {new_question['choices'][option]}", 
                            #                 key=f"similar_{option}_{i}"
                            #             ):
                            #                 st.session_state[f"similar_question_answered_{i}"] = True
                            #                 st.session_state[f"selected_answer_{i}"] = option
                            #                 correct_answer = st.session_state.get('current_similar_question_answer')
                            #                 if option == correct_answer:
                            #                     st.session_state[f"is_correct_{i}"] = True
                            #                 else:
                            #                     st.session_state[f"is_correct_{i}"] = False
                            #                 st.rerun()
                            # λ‹΅λ³€ν•˜μ§€ μ•Šμ€ κ²½μš°μ—λ§Œ λ²„νŠΌ ν™œμ„±ν™”
                            if not answered:
                                with col1:
                                    for option in ['A', 'C']:
                                        if st.button(
                                            f"{option}) {latex_formatter.format_expression(new_question['choices'][option])}", 
                                            key=f"similar_{option}_{i}"
                                        ):
                                            st.session_state[f"similar_question_answered_{i}"] = True
                                            st.session_state[f"selected_answer_{i}"] = option
                                            correct_answer = st.session_state.get('current_similar_question_answer')
                                            if option == correct_answer:
                                                st.session_state[f"is_correct_{i}"] = True
                                            else:
                                                st.session_state[f"is_correct_{i}"] = False
                                            st.rerun()
                                
                                with col2:
                                    for option in ['B', 'D']:
                                        if st.button(
                                            f"{option}) {format_math_expression(new_question['choices'][option])}", 
                                            key=f"similar_{option}_{i}"
                                        ):
                                            st.session_state[f"similar_question_answered_{i}"] = True
                                            st.session_state[f"selected_answer_{i}"] = option
                                            correct_answer = st.session_state.get('current_similar_question_answer')
                                            if option == correct_answer:
                                                st.session_state[f"is_correct_{i}"] = True
                                            else:
                                                st.session_state[f"is_correct_{i}"] = False
                                            st.rerun()
                            # λ‹΅λ³€ν•œ 경우 κ²°κ³Ό ν‘œμ‹œ
                            if answered:
                                is_correct = st.session_state.get(f"is_correct_{i}", False)
                                correct_answer = st.session_state.get('current_similar_question_answer')
                                if is_correct:
                                    st.success("βœ… μ •λ‹΅μž…λ‹ˆλ‹€!")
                                else:
                                    st.error(f"❌ ν‹€λ ΈμŠ΅λ‹ˆλ‹€. 정닡은 {correct_answer}μž…λ‹ˆλ‹€.")
                                
                                # ν•΄μ„€ ν‘œμ‹œ
                                st.write("---")
                                st.write("**πŸ“ ν•΄μ„€:**", new_question['explanation'])
                                
                                # λ‹€μ‹œ ν’€κΈ° λ²„νŠΌ
                                if st.button("πŸ”„ λ‹€μ‹œ ν’€κΈ°", key=f"reset_{i}"):
                                    st.session_state[f"similar_question_answered_{i}"] = False
                                    st.session_state[f"selected_answer_{i}"] = None
                                    st.session_state[f"is_correct_{i}"] = None
                                    st.rerun()
                            
                            # 문제 λ‹«κΈ° λ²„νŠΌ
                            if st.button("❌ 문제 λ‹«κΈ°", key=f"close_{i}"):
                                st.session_state[f"show_similar_question_{i}"] = False
                                st.session_state[f"similar_question_answered_{i}"] = False
                                st.session_state[f"selected_answer_{i}"] = None
                                st.session_state[f"is_correct_{i}"] = None
                                st.rerun()

                            # ν™”λ©΄ μ•„λž˜ μ—¬λ°± μΆ”κ°€
                            st.markdown("<br>" * 5, unsafe_allow_html=True)  # 5μ€„μ˜ 빈 쀄 μΆ”κ°€
                            st.markdown("""
                                <div style="height: 100px;">
                                </div>
                                """, unsafe_allow_html=True)  # μΆ”κ°€ μ—¬λ°±
                        else:
                            st.error("μœ μ‚¬ 문제λ₯Ό 생성할 수 μ—†μŠ΅λ‹ˆλ‹€.")
                            if st.button("❌ λ‹«κΈ°", key=f"close_error_{i}"):
                                st.session_state[f"show_similar_question_{i}"] = False
                                st.rerun()
                                # ν™”λ©΄ μ•„λž˜ μ—¬λ°± μΆ”κ°€
                            st.markdown("<br>" * 5, unsafe_allow_html=True)  # 5μ€„μ˜ 빈 쀄 μΆ”κ°€
                            st.markdown("""
                                <div style="height: 100px;">
                                </div>
                                """, unsafe_allow_html=True)  # μΆ”κ°€ μ—¬λ°±
if __name__ == "__main__":
    main()

# random_state 42μ—μ„œ μ •λ‹΅
    # D C A A C
    # A B B B B