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+
import os
|
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 |
+
import re
|
11 |
+
|
12 |
+
|
13 |
+
logging.basicConfig(level=logging.DEBUG)
|
14 |
+
|
15 |
+
|
16 |
+
# Streamlit ํ์ด์ง ๊ธฐ๋ณธ ์ค์
|
17 |
+
st.set_page_config(
|
18 |
+
page_title="MisconcepTutor",
|
19 |
+
layout="wide",
|
20 |
+
initial_sidebar_state="expanded"
|
21 |
+
)
|
22 |
+
|
23 |
+
@st.cache_resource
|
24 |
+
def load_answer_verifier():
|
25 |
+
"""๋ต์ ๊ฒ์ฆ ๋ชจ๋ธ ๋ก๋"""
|
26 |
+
from src.ThirdModule.module3 import AnswerVerifier
|
27 |
+
return AnswerVerifier()
|
28 |
+
|
29 |
+
# ๊ฒฝ๋ก ์ค์
|
30 |
+
base_path = os.path.dirname(os.path.abspath(__file__))
|
31 |
+
data_path = os.path.join(base_path, 'Data')
|
32 |
+
misconception_csv_path = os.path.join(data_path, 'misconception_mapping.csv')
|
33 |
+
|
34 |
+
# ๋ก๊น
์ค์
|
35 |
+
logging.basicConfig(level=logging.INFO)
|
36 |
+
logger = logging.getLogger(__name__)
|
37 |
+
|
38 |
+
# ์ธ์
์ํ ์ด๊ธฐํ - ๊ฐ์ฅ ๋จผ์ ์คํ๋๋๋ก ์ต์๋จ์ ๋ฐฐ์น
|
39 |
+
if 'initialized' not in st.session_state:
|
40 |
+
st.session_state.initialized = True
|
41 |
+
st.session_state.wrong_questions = []
|
42 |
+
st.session_state.misconceptions = []
|
43 |
+
st.session_state.current_question_index = 0
|
44 |
+
st.session_state.generated_questions = []
|
45 |
+
st.session_state.current_step = 'initial'
|
46 |
+
st.session_state.selected_wrong_answer = None
|
47 |
+
st.session_state.questions = []
|
48 |
+
logger.info("Session state initialized")
|
49 |
+
|
50 |
+
# ๋ฌธ์ ์์ฑ๊ธฐ ์ด๊ธฐํ
|
51 |
+
@st.cache_resource
|
52 |
+
def load_question_generator():
|
53 |
+
"""๋ฌธ์ ์์ฑ ๋ชจ๋ธ ๋ก๋"""
|
54 |
+
if not os.path.exists(misconception_csv_path):
|
55 |
+
st.error(f"CSV ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค: {misconception_csv_path}")
|
56 |
+
raise FileNotFoundError(f"CSV ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค: {misconception_csv_path}")
|
57 |
+
return SimilarQuestionGenerator(misconception_csv_path=misconception_csv_path)
|
58 |
+
|
59 |
+
# CSV ๋ฐ์ดํฐ ๋ก๋ ํจ์
|
60 |
+
@st.cache_data
|
61 |
+
def load_data(data_file = '/train.csv'):
|
62 |
+
try:
|
63 |
+
file_path = os.path.join(data_path, data_file.lstrip('/'))
|
64 |
+
df = pd.read_csv(file_path)
|
65 |
+
logger.info(f"Data loaded successfully from {file_path}")
|
66 |
+
return df
|
67 |
+
except FileNotFoundError:
|
68 |
+
st.error(f"ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค: {data_file}")
|
69 |
+
logger.error(f"File not found: {data_file}")
|
70 |
+
return None
|
71 |
+
|
72 |
+
def start_quiz():
|
73 |
+
"""ํด์ฆ ์์ ๋ฐ ์ด๊ธฐํ"""
|
74 |
+
df = load_data()
|
75 |
+
if df is None or df.empty:
|
76 |
+
st.error("๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ์
์ ํ์ธํด์ฃผ์ธ์.")
|
77 |
+
return
|
78 |
+
|
79 |
+
st.session_state.questions = df.sample(n=10, random_state=42)
|
80 |
+
st.session_state.current_step = 'quiz'
|
81 |
+
st.session_state.current_question_index = 0
|
82 |
+
st.session_state.wrong_questions = []
|
83 |
+
st.session_state.misconceptions = []
|
84 |
+
st.session_state.generated_questions = []
|
85 |
+
logger.info("Quiz started")
|
86 |
+
|
87 |
+
|
88 |
+
def generate_similar_question(wrong_q, misconception_id, generator):
|
89 |
+
"""์ ์ฌ ๋ฌธ์ ์์ฑ"""
|
90 |
+
logger.info(f"Generating similar question for misconception_id: {misconception_id}")
|
91 |
+
|
92 |
+
# ์
๋ ฅ ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ
|
93 |
+
if not isinstance(wrong_q, dict):
|
94 |
+
logger.error(f"Invalid wrong_q type: {type(wrong_q)}")
|
95 |
+
st.error("์ ์ฌ ๋ฌธ์ ์์ฑ์ ํ์ํ ๋ฐ์ดํฐ ํ์์ด ์๋ชป๋์์ต๋๋ค.")
|
96 |
+
return None
|
97 |
+
|
98 |
+
try:
|
99 |
+
# misconception_id๊ฐ ์๊ฑฐ๋ NaN์ธ ๊ฒฝ์ฐ ๋ค๋ฅธ misconception ์ฌ์ฉ
|
100 |
+
if pd.isna(misconception_id):
|
101 |
+
logger.info("Original misconception_id is NaN, trying to find alternative")
|
102 |
+
# ํ์ฌ๊น์ง ๋์จ misconception๋ค ์ค์์ ์ ํ
|
103 |
+
available_misconceptions = [m for m in st.session_state.misconceptions if not pd.isna(m)]
|
104 |
+
|
105 |
+
if available_misconceptions:
|
106 |
+
# ๊ฐ์ฅ ์ต๊ทผ์ ๋์จ misconception ์ ํ
|
107 |
+
misconception_id = available_misconceptions[-1]
|
108 |
+
logger.info(f"Using alternative misconception_id: {misconception_id}")
|
109 |
+
else:
|
110 |
+
# ๊ธฐ๋ณธ misconception ID ์ฌ์ฉ (์: ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ misconception)
|
111 |
+
misconception_id = 2001 # ์ ์ ํ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์์ ํ์
|
112 |
+
logger.info(f"Using default misconception_id: {misconception_id}")
|
113 |
+
|
114 |
+
# ๋ฐ์ดํฐ ์ค๋น (ํํ ๋ณํ ๋ฐฉ์ง)
|
115 |
+
input_data = {
|
116 |
+
'construct_name': str(wrong_q.get('ConstructName', '')),
|
117 |
+
'subject_name': str(wrong_q.get('SubjectName', '')),
|
118 |
+
'question_text': str(wrong_q.get('QuestionText', '')),
|
119 |
+
'correct_answer_text': str(wrong_q.get(f'Answer{wrong_q["CorrectAnswer"]}Text', '')),
|
120 |
+
'wrong_answer_text': str(wrong_q.get(f'Answer{st.session_state.selected_wrong_answer}Text', '')),
|
121 |
+
'misconception_id': int(misconception_id)
|
122 |
+
}
|
123 |
+
|
124 |
+
logger.info(f"Prepared input data: {input_data}")
|
125 |
+
|
126 |
+
with st.spinner("๐ ์ ์ฌ ๋ฌธ์ ๋ฅผ ์์ฑํ๊ณ ์์ต๋๋ค..."):
|
127 |
+
# ์ ์ฌ ๋ฌธ์ ์์ฑ ํธ์ถ
|
128 |
+
generated_q, _ = generator.generate_similar_question_with_text(
|
129 |
+
construct_name=input_data['construct_name'],
|
130 |
+
subject_name=input_data['subject_name'],
|
131 |
+
question_text=input_data['question_text'],
|
132 |
+
correct_answer_text=input_data['correct_answer_text'],
|
133 |
+
wrong_answer_text=input_data['wrong_answer_text'],
|
134 |
+
misconception_id=input_data['misconception_id']
|
135 |
+
)
|
136 |
+
|
137 |
+
if generated_q:
|
138 |
+
verifier = load_answer_verifier()
|
139 |
+
with st.status("๐ค AI๊ฐ ๋ฌธ์ ๋ฅผ ๊ฒํ ํ๊ณ ์์ต๋๋ค..."):
|
140 |
+
st.write("๋ต์์ ์ ํ์ฑ์ ๊ฒ์ฆํ๊ณ ์์ต๋๋ค...")
|
141 |
+
verified_answer = verifier.verify_answer(
|
142 |
+
question=generated_q.question,
|
143 |
+
choices=generated_q.choices
|
144 |
+
)
|
145 |
+
|
146 |
+
if verified_answer:
|
147 |
+
logger.info(f"Answer verified: {verified_answer}")
|
148 |
+
st.write("โ
๊ฒ์ฆ ์๋ฃ!")
|
149 |
+
result = {
|
150 |
+
'question': generated_q.question,
|
151 |
+
'choices': generated_q.choices,
|
152 |
+
'correct': verified_answer,
|
153 |
+
'explanation': generated_q.explanation
|
154 |
+
}
|
155 |
+
st.session_state['current_similar_question_answer'] = verified_answer
|
156 |
+
return result
|
157 |
+
else:
|
158 |
+
logger.warning("Answer verification failed, using original answer")
|
159 |
+
st.write("โ ๏ธ ๊ฒ์ฆ์ ์คํจํ์ต๋๋ค. ์๋ณธ ๋ต์์ ์ฌ์ฉํฉ๋๋ค.")
|
160 |
+
result = {
|
161 |
+
'question': generated_q.question,
|
162 |
+
'choices': generated_q.choices,
|
163 |
+
'correct': generated_q.correct_answer,
|
164 |
+
'explanation': generated_q.explanation
|
165 |
+
}
|
166 |
+
st.session_state['current_similar_question_answer'] = generated_q.correct_answer
|
167 |
+
return result
|
168 |
+
|
169 |
+
except Exception as e:
|
170 |
+
logger.error(f"Error in generate_similar_question: {str(e)}")
|
171 |
+
st.error(f"๋ฌธ์ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
172 |
+
return None
|
173 |
+
|
174 |
+
return None
|
175 |
+
|
176 |
+
# ์์
|
177 |
+
def handle_answer(answer, current_q):
|
178 |
+
"""๋ต๋ณ ์ฒ๋ฆฌ"""
|
179 |
+
if answer != current_q['CorrectAnswer']:
|
180 |
+
wrong_q_dict = current_q.to_dict()
|
181 |
+
st.session_state.wrong_questions.append(wrong_q_dict)
|
182 |
+
st.session_state.selected_wrong_answer = answer
|
183 |
+
|
184 |
+
misconception_key = f'Misconception{answer}Id'
|
185 |
+
misconception_id = current_q.get(misconception_key)
|
186 |
+
st.session_state.misconceptions.append(misconception_id)
|
187 |
+
|
188 |
+
st.session_state.current_question_index += 1
|
189 |
+
if st.session_state.current_question_index >= len(st.session_state.questions):
|
190 |
+
st.session_state.current_step = 'review'
|
191 |
+
else:
|
192 |
+
st.session_state.current_step = 'quiz'
|
193 |
+
|
194 |
+
# ์์
|
195 |
+
def display_math_content(content):
|
196 |
+
"""
|
197 |
+
Display mathematical content with proper formatting.
|
198 |
+
|
199 |
+
Args:
|
200 |
+
content (str): The math content to display
|
201 |
+
"""
|
202 |
+
# Convert LaTeX to plain text for display
|
203 |
+
from pylatexenc.latex2text import LatexNodes2Text
|
204 |
+
|
205 |
+
# Clean and format the content
|
206 |
+
formatted_content = LatexNodes2Text().latex_to_text(content)
|
207 |
+
st.markdown(f'<div class="math-container">{formatted_content}</div>', unsafe_allow_html=True)
|
208 |
+
|
209 |
+
# ์ถ๊ฐ
|
210 |
+
def add_custom_css():
|
211 |
+
st.markdown(
|
212 |
+
"""
|
213 |
+
<style>
|
214 |
+
.problem-header {
|
215 |
+
color: #FF6B6B;
|
216 |
+
font-size: 24px;
|
217 |
+
font-weight: bold;
|
218 |
+
margin-bottom: 20px;
|
219 |
+
}
|
220 |
+
|
221 |
+
.math-container {
|
222 |
+
background-color: #f9f9f9;
|
223 |
+
padding: 20px;
|
224 |
+
border-radius: 10px;
|
225 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
226 |
+
margin-bottom: 20px;
|
227 |
+
}
|
228 |
+
|
229 |
+
.options-container {
|
230 |
+
display: grid;
|
231 |
+
grid-template-columns: 1fr 1fr;
|
232 |
+
gap: 20px;
|
233 |
+
}
|
234 |
+
|
235 |
+
.option {
|
236 |
+
background-color: white;
|
237 |
+
border: 1px solid #ddd;
|
238 |
+
border-radius: 8px;
|
239 |
+
padding: 15px;
|
240 |
+
text-align: center;
|
241 |
+
font-size: 18px;
|
242 |
+
cursor: pointer;
|
243 |
+
transition: background-color 0.3s;
|
244 |
+
}
|
245 |
+
|
246 |
+
.option:hover {
|
247 |
+
background-color: #f8f9fa;
|
248 |
+
}
|
249 |
+
</style>
|
250 |
+
""",
|
251 |
+
unsafe_allow_html=True
|
252 |
+
)
|
253 |
+
|
254 |
+
def display_question(question, answers):
|
255 |
+
"""Display question and options with LaTeX formatting"""
|
256 |
+
st.markdown('<div class="problem-header">Problem:</div>', unsafe_allow_html=True)
|
257 |
+
display_math_content(question)
|
258 |
+
|
259 |
+
col1, col2 = st.columns(2)
|
260 |
+
for i, (col, opt) in enumerate([(col1, 'A'), (col1, 'C'), (col2, 'B'), (col2, 'D')]):
|
261 |
+
with col:
|
262 |
+
st.markdown(f"### {opt})")
|
263 |
+
display_option_content(answers[opt])
|
264 |
+
if st.button("์ ํ", key=f"btn_{opt}"):
|
265 |
+
handle_answer(opt, st.session_state.questions.iloc[st.session_state.current_question_index])
|
266 |
+
st.rerun()
|
267 |
+
|
268 |
+
def display_option_content(option_text):
|
269 |
+
"""Process and display option content with LaTeX formatting"""
|
270 |
+
from pylatexenc.latex2text import LatexNodes2Text
|
271 |
+
formatted_content = LatexNodes2Text().latex_to_text(option_text)
|
272 |
+
st.markdown(f'<div class="math-container">{formatted_content}</div>', unsafe_allow_html=True)
|
273 |
+
|
274 |
+
# ์ถ๊ฐ
|
275 |
+
def update_similar_question_display(new_question, i, answered=False):
|
276 |
+
"""Display similar question and its options"""
|
277 |
+
display_math_content(new_question['question'])
|
278 |
+
|
279 |
+
col1, col2 = st.columns(2)
|
280 |
+
for j, (col, opt) in enumerate([(col1, 'A'), (col1, 'C'), (col2, 'B'), (col2, 'D')]):
|
281 |
+
with col:
|
282 |
+
st.markdown(f"### {opt})")
|
283 |
+
display_option_content(new_question['choices'][opt])
|
284 |
+
if not answered:
|
285 |
+
if st.button("์ ํ", key=f"similar_{opt}_{i}"):
|
286 |
+
handle_similar_answer(opt, i)
|
287 |
+
st.rerun()
|
288 |
+
|
289 |
+
def main():
|
290 |
+
"""๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
๋ก์ง"""
|
291 |
+
st.title("MisconcepTutor")
|
292 |
+
|
293 |
+
# Generator ์ด๊ธฐํ
|
294 |
+
generator = load_question_generator()
|
295 |
+
|
296 |
+
# ์ด๊ธฐ ํ๋ฉด
|
297 |
+
if st.session_state.current_step == 'initial':
|
298 |
+
st.write("#### ํ์ต์ ์์ํ๊ฒ ์ต๋๋ค. 10๊ฐ์ ๋ฌธ์ ๋ฅผ ํ์ด๋ณผ๊น์?")
|
299 |
+
if st.button("ํ์ต ์์", key="start_quiz"):
|
300 |
+
start_quiz()
|
301 |
+
st.rerun()
|
302 |
+
|
303 |
+
# ํด์ฆ ํ๋ฉด
|
304 |
+
elif st.session_state.current_step == 'quiz':
|
305 |
+
current_q = st.session_state.questions.iloc[st.session_state.current_question_index]
|
306 |
+
|
307 |
+
# ์งํ ์ํฉ ํ์
|
308 |
+
progress = st.session_state.current_question_index / 10
|
309 |
+
st.progress(progress)
|
310 |
+
st.write(f"### ๋ฌธ์ {st.session_state.current_question_index + 1}/10")
|
311 |
+
|
312 |
+
# ๋ฌธ์ ํ์
|
313 |
+
st.markdown("---")
|
314 |
+
question_row = current_q['QuestionText']
|
315 |
+
question_text = LatexNodes2Text().latex_to_text(current_q['QuestionText'])
|
316 |
+
|
317 |
+
answers ={
|
318 |
+
'A': current_q['AnswerAText'],
|
319 |
+
'B': current_q['AnswerBText'],
|
320 |
+
'C': current_q['AnswerCText'],
|
321 |
+
'D': current_q['AnswerDText']
|
322 |
+
}
|
323 |
+
|
324 |
+
display_question(question_text, answers)
|
325 |
+
|
326 |
+
|
327 |
+
# ๋ณต์ต ํ๋ฉด
|
328 |
+
elif st.session_state.current_step == 'review':
|
329 |
+
st.write("### ํ์ต ๊ฒฐ๊ณผ")
|
330 |
+
|
331 |
+
# ๊ฒฐ๊ณผ ํต๊ณ
|
332 |
+
col1, col2, col3 = st.columns(3)
|
333 |
+
col1.metric("์ด ๋ฌธ์ ์", 10)
|
334 |
+
col2.metric("๋ง์ ๋ฌธ์ ", 10 - len(st.session_state.wrong_questions))
|
335 |
+
col3.metric("ํ๋ฆฐ ๋ฌธ์ ", len(st.session_state.wrong_questions))
|
336 |
+
|
337 |
+
# ๊ฒฐ๊ณผ์ ๋ฐ๋ฅธ ๋ฉ์์ง ํ์
|
338 |
+
if len(st.session_state.wrong_questions) == 0:
|
339 |
+
st.balloons() # ์ถํ ํจ๊ณผ
|
340 |
+
st.success("๐ ์ถํํฉ๋๋ค! ๋ชจ๋ ๋ฌธ์ ๋ฅผ ๋ง์ถ์
จ์ด์!")
|
341 |
+
st.markdown("""
|
342 |
+
### ๐ ์ํ์์ด์ญ๋๋ค!
|
343 |
+
์๋ฒฝํ ์ ์๋ฅผ ๋ฐ์ผ์
จ๋ค์! ์ํ์ ๊ฐ๋
์ ์ ํํ๊ฒ ์ดํดํ๊ณ ๊ณ์ ๊ฒ ๊ฐ์ต๋๋ค.
|
344 |
+
""")
|
345 |
+
elif len(st.session_state.wrong_questions) <= 3:
|
346 |
+
st.success("์ ํ์
จ์ด์! ์กฐ๊ธ๋ง ๋ ์ฐ์ตํ๋ฉด ์๋ฒฝํ ๊ฑฐ์์!")
|
347 |
+
else:
|
348 |
+
st.info("์ฒ์ฒํ ๊ฐ๋
์ ๋ณต์ตํด๋ณด์์. ์ฐ์ตํ๋ค ๋ณด๋ฉด ๋์ด๋ ๊ฑฐ์์!")
|
349 |
+
|
350 |
+
# ๋ค๋น๊ฒ์ด์
๋ฒํผ
|
351 |
+
col1, col2 = st.columns(2)
|
352 |
+
with col1:
|
353 |
+
if st.button("๐ ์๋ก์ด ๋ฌธ์ ์ธํธ ์์ํ๊ธฐ", use_container_width=True):
|
354 |
+
start_quiz()
|
355 |
+
st.rerun()
|
356 |
+
with col2:
|
357 |
+
if st.button("๐ ์ฒ์์ผ๋ก ๋์๊ฐ๊ธฐ", use_container_width=True):
|
358 |
+
st.session_state.clear()
|
359 |
+
st.rerun()
|
360 |
+
|
361 |
+
# ํ๋ฆฐ ๋ฌธ์ ๋ถ์ ๋ถ๋ถ
|
362 |
+
if st.session_state.wrong_questions:
|
363 |
+
st.write("### โ๏ธ ํ๋ฆฐ ๋ฌธ์ ๋ถ์")
|
364 |
+
tabs = st.tabs([f"๐ ํ๋ฆฐ ๋ฌธ์ #{i + 1}" for i in range(len(st.session_state.wrong_questions))])
|
365 |
+
|
366 |
+
for i, (tab, (wrong_q, misconception_id)) in enumerate(zip(
|
367 |
+
tabs,
|
368 |
+
zip(st.session_state.wrong_questions, st.session_state.misconceptions)
|
369 |
+
)):
|
370 |
+
with tab:
|
371 |
+
st.write("**๐ ๋ฌธ์ :**")
|
372 |
+
display_math_content(wrong_q['QuestionText']) # ๋ฌธ์ ๋ ๋๋ง
|
373 |
+
st.write("**โ
์ ๋ต:**")
|
374 |
+
#display_math_content(wrong_q[f'Answer{wrong_q["CorrectAnswer"]}Text']) # ์ ๋ต ๋ ๏ฟฝ๏ฟฝ๋ง
|
375 |
+
display_option_content(wrong_q[f'Answer{wrong_q["CorrectAnswer"]}Text'])
|
376 |
+
|
377 |
+
st.write("---")
|
378 |
+
st.write("**๐ ๊ด๋ จ๋ Misconception:**")
|
379 |
+
if misconception_id and not pd.isna(misconception_id):
|
380 |
+
misconception_text = generator.get_misconception_text(misconception_id)
|
381 |
+
st.info(f"Misconception ID: {int(misconception_id)}\n\n{misconception_text}")
|
382 |
+
else:
|
383 |
+
st.info("Misconception ์ ๋ณด๊ฐ ์์ต๋๋ค.")
|
384 |
+
|
385 |
+
if st.button(f"๐ ์ ์ฌ ๋ฌธ์ ํ๊ธฐ", key=f"retry_{i}"):
|
386 |
+
st.session_state[f"show_similar_question_{i}"] = True
|
387 |
+
st.session_state[f"similar_question_answered_{i}"] = False
|
388 |
+
st.rerun()
|
389 |
+
|
390 |
+
if st.session_state.get(f"show_similar_question_{i}", False):
|
391 |
+
st.divider()
|
392 |
+
new_question = generate_similar_question(wrong_q, misconception_id, generator)
|
393 |
+
if new_question:
|
394 |
+
st.write("### ๐ฏ ์ ์ฌ ๋ฌธ์ ")
|
395 |
+
display_math_content(new_question['question']) # ํจ์ ๊ต์ฒด
|
396 |
+
|
397 |
+
# ๋ต๋ณ ์ํ ํ์ธ
|
398 |
+
answered = st.session_state.get(f"similar_question_answered_{i}", False)
|
399 |
+
update_similar_question_display(new_question, i, answered)
|
400 |
+
|
401 |
+
# ๋ณด๊ธฐ ํ์
|
402 |
+
st.write("**๋ณด๊ธฐ:**")
|
403 |
+
col1, col2 = st.columns(2)
|
404 |
+
|
405 |
+
|
406 |
+
# ๋ต๋ณํ์ง ์์ ๊ฒฝ์ฐ์๋ง ๋ฒํผ ํ์ฑํ
|
407 |
+
if not answered:
|
408 |
+
with col1:
|
409 |
+
for option in ['A', 'C']:
|
410 |
+
if st.button(
|
411 |
+
f"{option}) {LatexNodes2Text().latex_to_text(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}) {LatexNodes2Text().latex_to_text(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 |
+
# ๋ต๋ณํ ๊ฒฝ์ฐ ๊ฒฐ๊ณผ ํ์
|
439 |
+
if answered:
|
440 |
+
is_correct = st.session_state.get(f"is_correct_{i}", False)
|
441 |
+
correct_answer = st.session_state.get('current_similar_question_answer')
|
442 |
+
if is_correct:
|
443 |
+
st.success("โ
์ ๋ต์
๋๋ค!")
|
444 |
+
else:
|
445 |
+
st.error(f"โ ํ๋ ธ์ต๋๋ค. ์ ๋ต์ {correct_answer}์
๋๋ค.")
|
446 |
+
|
447 |
+
# ํด์ค ํ์
|
448 |
+
st.write("---")
|
449 |
+
st.write("**๐ ํด์ค:**", new_question['explanation'])
|
450 |
+
|
451 |
+
# ๋ค์ ํ๊ธฐ ๋ฒํผ
|
452 |
+
if st.button("๐ ๋ค์ ํ๊ธฐ", key=f"reset_{i}"):
|
453 |
+
st.session_state[f"similar_question_answered_{i}"] = False
|
454 |
+
st.session_state[f"selected_answer_{i}"] = None
|
455 |
+
st.session_state[f"is_correct_{i}"] = None
|
456 |
+
st.rerun()
|
457 |
+
|
458 |
+
# ๋ฌธ์ ๋ซ๊ธฐ ๋ฒํผ
|
459 |
+
if st.button("โ ๋ฌธ์ ๋ซ๊ธฐ", key=f"close_{i}"):
|
460 |
+
st.session_state[f"show_similar_question_{i}"] = False
|
461 |
+
st.session_state[f"similar_question_answered_{i}"] = False
|
462 |
+
st.session_state[f"selected_answer_{i}"] = None
|
463 |
+
st.session_state[f"is_correct_{i}"] = None
|
464 |
+
st.rerun()
|
465 |
+
|
466 |
+
# ํ๋ฉด ์๋ ์ฌ๋ฐฑ ์ถ๊ฐ
|
467 |
+
st.markdown("<br>" * 5, unsafe_allow_html=True) # 5์ค์ ๋น ์ค ์ถ๊ฐ
|
468 |
+
st.markdown("""
|
469 |
+
<div style="height: 100px;">
|
470 |
+
</div>
|
471 |
+
""", unsafe_allow_html=True) # ์ถ๊ฐ ์ฌ๋ฐฑ
|
472 |
+
else:
|
473 |
+
st.error("์ ์ฌ ๋ฌธ์ ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.")
|
474 |
+
if st.button("โ ๋ซ๊ธฐ", key=f"close_error_{i}"):
|
475 |
+
st.session_state[f"show_similar_question_{i}"] = False
|
476 |
+
st.rerun()
|
477 |
+
# ํ๋ฉด ์๋ ์ฌ๋ฐฑ ์ถ๊ฐ
|
478 |
+
st.markdown("<br>" * 5, unsafe_allow_html=True) # 5์ค์ ๋น ์ค ์ถ๊ฐ
|
479 |
+
st.markdown("""
|
480 |
+
<div style="height: 100px;">
|
481 |
+
</div>
|
482 |
+
""", unsafe_allow_html=True) # ์ถ๊ฐ ์ฌ๋ฐฑ
|
483 |
+
if __name__ == "__main__":
|
484 |
+
main()
|
485 |
+
|
486 |
+
# random_state 42์์ ์ ๋ต
|
487 |
+
# D C A A C
|
488 |
+
# A B B B B
|