Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
|
|
|
| 4 |
import logging
|
| 5 |
-
from
|
|
|
|
| 6 |
|
| 7 |
-
# ๋ก๊น
์ค์
|
| 8 |
-
logging.basicConfig(level=logging.INFO)
|
| 9 |
-
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
# Streamlit ํ์ด์ง ๊ธฐ๋ณธ ์ค์
|
| 12 |
st.set_page_config(
|
|
@@ -20,7 +19,11 @@ base_path = os.path.dirname(os.path.abspath(__file__))
|
|
| 20 |
data_path = os.path.join(base_path, 'Data')
|
| 21 |
misconception_csv_path = os.path.join(data_path, 'misconception_mapping.csv')
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if 'initialized' not in st.session_state:
|
| 25 |
st.session_state.initialized = True
|
| 26 |
st.session_state.wrong_questions = []
|
|
@@ -35,14 +38,15 @@ if 'initialized' not in st.session_state:
|
|
| 35 |
# ๋ฌธ์ ์์ฑ๊ธฐ ์ด๊ธฐํ
|
| 36 |
@st.cache_resource
|
| 37 |
def load_question_generator():
|
|
|
|
| 38 |
if not os.path.exists(misconception_csv_path):
|
| 39 |
st.error(f"CSV ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค: {misconception_csv_path}")
|
| 40 |
raise FileNotFoundError(f"CSV ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค: {misconception_csv_path}")
|
| 41 |
return SimilarQuestionGenerator(misconception_csv_path=misconception_csv_path)
|
| 42 |
|
| 43 |
-
# CSV ๋ฐ์ดํฐ ๋ก๋
|
| 44 |
@st.cache_data
|
| 45 |
-
def load_data(data_file='/train.csv'):
|
| 46 |
try:
|
| 47 |
file_path = os.path.join(data_path, data_file.lstrip('/'))
|
| 48 |
df = pd.read_csv(file_path)
|
|
@@ -53,12 +57,13 @@ def load_data(data_file='/train.csv'):
|
|
| 53 |
logger.error(f"File not found: {data_file}")
|
| 54 |
return None
|
| 55 |
|
| 56 |
-
# ํด์ฆ ์์
|
| 57 |
def start_quiz():
|
|
|
|
| 58 |
df = load_data()
|
| 59 |
if df is None or df.empty:
|
| 60 |
st.error("๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ์
์ ํ์ธํด์ฃผ์ธ์.")
|
| 61 |
return
|
|
|
|
| 62 |
st.session_state.questions = df.sample(n=10, random_state=42)
|
| 63 |
st.session_state.current_step = 'quiz'
|
| 64 |
st.session_state.current_question_index = 0
|
|
@@ -67,8 +72,197 @@ def start_quiz():
|
|
| 67 |
st.session_state.generated_questions = []
|
| 68 |
logger.info("Quiz started")
|
| 69 |
|
| 70 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def handle_answer(answer, current_q):
|
|
|
|
| 72 |
if answer != current_q['CorrectAnswer']:
|
| 73 |
wrong_q_dict = current_q.to_dict()
|
| 74 |
st.session_state.wrong_questions.append(wrong_q_dict)
|
|
@@ -82,25 +276,34 @@ def handle_answer(answer, current_q):
|
|
| 82 |
if st.session_state.current_question_index >= 10:
|
| 83 |
st.session_state.current_step = 'review'
|
| 84 |
|
| 85 |
-
# ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
๋ก์ง
|
| 86 |
def main():
|
|
|
|
| 87 |
st.title("MisconcepTutor")
|
|
|
|
|
|
|
| 88 |
generator = load_question_generator()
|
| 89 |
|
|
|
|
| 90 |
if st.session_state.current_step == 'initial':
|
| 91 |
st.write("#### ํ์ต์ ์์ํ๊ฒ ์ต๋๋ค. 10๊ฐ์ ๋ฌธ์ ๋ฅผ ํ์ด๋ณผ๊น์?")
|
| 92 |
if st.button("ํ์ต ์์", key="start_quiz"):
|
| 93 |
start_quiz()
|
| 94 |
st.rerun()
|
| 95 |
|
|
|
|
| 96 |
elif st.session_state.current_step == 'quiz':
|
| 97 |
current_q = st.session_state.questions.iloc[st.session_state.current_question_index]
|
|
|
|
|
|
|
| 98 |
progress = st.session_state.current_question_index / 10
|
| 99 |
st.progress(progress)
|
| 100 |
st.write(f"### ๋ฌธ์ {st.session_state.current_question_index + 1}/10")
|
|
|
|
|
|
|
| 101 |
st.markdown("---")
|
| 102 |
st.write(current_q['QuestionText'])
|
| 103 |
-
|
|
|
|
| 104 |
col1, col2 = st.columns(2)
|
| 105 |
with col1:
|
| 106 |
if st.button(f"A) {current_q['AnswerAText']}", key="A"):
|
|
@@ -117,43 +320,85 @@ def main():
|
|
| 117 |
handle_answer('D', current_q)
|
| 118 |
st.rerun()
|
| 119 |
|
|
|
|
| 120 |
elif st.session_state.current_step == 'review':
|
| 121 |
st.write("### ํ์ต ๊ฒฐ๊ณผ")
|
|
|
|
|
|
|
| 122 |
col1, col2, col3 = st.columns(3)
|
| 123 |
col1.metric("์ด ๋ฌธ์ ์", 10)
|
| 124 |
col2.metric("๋ง์ ๋ฌธ์ ", 10 - len(st.session_state.wrong_questions))
|
| 125 |
col3.metric("ํ๋ฆฐ ๋ฌธ์ ", len(st.session_state.wrong_questions))
|
| 126 |
-
|
|
|
|
| 127 |
if len(st.session_state.wrong_questions) == 0:
|
| 128 |
-
st.balloons()
|
| 129 |
-
st.success("๐ ๋ชจ๋ ๋ฌธ์ ๋ฅผ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
elif len(st.session_state.wrong_questions) <= 3:
|
| 131 |
-
st.success("์ ํ์
จ์ด์! ์กฐ๊ธ๋ง ๋ ์ฐ์ตํ๋ฉด
|
| 132 |
else:
|
| 133 |
-
st.info("์ฒ์ฒํ ๊ฐ๋
์
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
if st.session_state.wrong_questions:
|
| 136 |
st.write("### โ๏ธ ํ๋ฆฐ ๋ฌธ์ ๋ถ์")
|
| 137 |
for i, (wrong_q, misconception_id) in enumerate(zip(
|
| 138 |
-
st.session_state.wrong_questions,
|
|
|
|
| 139 |
)):
|
| 140 |
with st.expander(f"๐ ํ๋ฆฐ ๋ฌธ์ #{i + 1}"):
|
|
|
|
| 141 |
st.write(wrong_q['QuestionText'])
|
| 142 |
-
st.write(
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 144 |
misconception_text = generator.get_misconception_text(misconception_id)
|
| 145 |
-
st.info(f"Misconception: {misconception_text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
if st.button(f"๐ ์ ์ฌ ๋ฌธ์ ํ๊ธฐ #{i + 1}", key=f"retry_{i}"):
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
st.write(
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
| 158 |
if __name__ == "__main__":
|
| 159 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
+
from src.SecondModule.module2 import SimilarQuestionGenerator
|
| 5 |
import logging
|
| 6 |
+
from typing import Optional, Tuple
|
| 7 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Streamlit ํ์ด์ง ๊ธฐ๋ณธ ์ค์
|
| 11 |
st.set_page_config(
|
|
|
|
| 19 |
data_path = os.path.join(base_path, 'Data')
|
| 20 |
misconception_csv_path = os.path.join(data_path, 'misconception_mapping.csv')
|
| 21 |
|
| 22 |
+
# ๋ก๊น
์ค์
|
| 23 |
+
logging.basicConfig(level=logging.INFO)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
# ์ธ์
์ํ ์ด๊ธฐํ - ๊ฐ์ฅ ๋จผ์ ์คํ๋๋๋ก ์ต์๋จ์ ๋ฐฐ์น
|
| 27 |
if 'initialized' not in st.session_state:
|
| 28 |
st.session_state.initialized = True
|
| 29 |
st.session_state.wrong_questions = []
|
|
|
|
| 38 |
# ๋ฌธ์ ์์ฑ๊ธฐ ์ด๊ธฐํ
|
| 39 |
@st.cache_resource
|
| 40 |
def load_question_generator():
|
| 41 |
+
"""๋ฌธ์ ์์ฑ ๋ชจ๋ธ ๋ก๋"""
|
| 42 |
if not os.path.exists(misconception_csv_path):
|
| 43 |
st.error(f"CSV ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค: {misconception_csv_path}")
|
| 44 |
raise FileNotFoundError(f"CSV ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค: {misconception_csv_path}")
|
| 45 |
return SimilarQuestionGenerator(misconception_csv_path=misconception_csv_path)
|
| 46 |
|
| 47 |
+
# CSV ๋ฐ์ดํฐ ๋ก๋ ํจ์
|
| 48 |
@st.cache_data
|
| 49 |
+
def load_data(data_file = '/train.csv'):
|
| 50 |
try:
|
| 51 |
file_path = os.path.join(data_path, data_file.lstrip('/'))
|
| 52 |
df = pd.read_csv(file_path)
|
|
|
|
| 57 |
logger.error(f"File not found: {data_file}")
|
| 58 |
return None
|
| 59 |
|
|
|
|
| 60 |
def start_quiz():
|
| 61 |
+
"""ํด์ฆ ์์ ๋ฐ ์ด๊ธฐํ"""
|
| 62 |
df = load_data()
|
| 63 |
if df is None or df.empty:
|
| 64 |
st.error("๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ์
์ ํ์ธํด์ฃผ์ธ์.")
|
| 65 |
return
|
| 66 |
+
|
| 67 |
st.session_state.questions = df.sample(n=10, random_state=42)
|
| 68 |
st.session_state.current_step = 'quiz'
|
| 69 |
st.session_state.current_question_index = 0
|
|
|
|
| 72 |
st.session_state.generated_questions = []
|
| 73 |
logger.info("Quiz started")
|
| 74 |
|
| 75 |
+
# def generate_similar_question(wrong_q, misconception_id, generator):
|
| 76 |
+
# """์ ์ฌ ๋ฌธ์ ์์ฑ"""
|
| 77 |
+
# if not isinstance(wrong_q, dict):
|
| 78 |
+
# logger.error(f"Invalid wrong_q type: {type(wrong_q)}")
|
| 79 |
+
# st.error("์ ์ฌ ๋ฌธ์ ์์ฑ์ ํ์ํ ๋ฐ์ดํฐ ํ์์ด ์๋ชป๋์์ต๋๋ค.")
|
| 80 |
+
# return None
|
| 81 |
+
|
| 82 |
+
# try:
|
| 83 |
+
# generated_q, _ = generator.generate_similar_question_with_text(
|
| 84 |
+
# construct_name=wrong_q['ConstructName'],
|
| 85 |
+
# subject_name=wrong_q['SubjectName'],
|
| 86 |
+
# question_text=wrong_q['QuestionText'],
|
| 87 |
+
# correct_answer_text=wrong_q[f'Answer{wrong_q["CorrectAnswer"]}Text'],
|
| 88 |
+
# wrong_answer_text=wrong_q[f'Answer{st.session_state.selected_wrong_answer}Text'],
|
| 89 |
+
# misconception_id=misconception_id
|
| 90 |
+
# )
|
| 91 |
+
|
| 92 |
+
# if generated_q:
|
| 93 |
+
# return {
|
| 94 |
+
# 'question': generated_q.question,
|
| 95 |
+
# 'choices': generated_q.choices,
|
| 96 |
+
# 'correct': generated_q.correct_answer,
|
| 97 |
+
# 'explanation': generated_q.explanation
|
| 98 |
+
# }
|
| 99 |
+
# except Exception as e:
|
| 100 |
+
# logger.error(f"Error generating similar question: {e}")
|
| 101 |
+
# st.error(f"๋ฌธ์ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.")
|
| 102 |
+
# return None
|
| 103 |
+
|
| 104 |
+
# def generate_similar_question(wrong_q, misconception_id, generator):
|
| 105 |
+
# """์ ์ฌ ๋ฌธ์ ์์ฑ"""
|
| 106 |
+
# # ์
๋ ฅ ๋ฐ์ดํฐ ํ์ธ
|
| 107 |
+
# logger.info(f"Generating similar question for misconception_id: {misconception_id}")
|
| 108 |
+
# logger.info(f"Wrong question data type: {type(wrong_q)}")
|
| 109 |
+
# logger.info(f"Wrong question data: {wrong_q}")
|
| 110 |
+
|
| 111 |
+
# # wrong_q๊ฐ ๋์
๋๋ฆฌ๊ฐ ์๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
|
| 112 |
+
# if not isinstance(wrong_q, dict):
|
| 113 |
+
# logger.error(f"Invalid wrong_q type: {type(wrong_q)}")
|
| 114 |
+
# st.error("์ ์ฌ ๋ฌธ์ ์์ฑ์ ํ์ํ ๋ฐ์ดํฐ ํ์์ด ์๋ชป๋์์ต๋๋ค.")
|
| 115 |
+
# return None
|
| 116 |
+
|
| 117 |
+
# # misconception_id NaN ์ฒดํฌ
|
| 118 |
+
# if pd.isna(misconception_id):
|
| 119 |
+
# logger.warning("misconception_id ๊ฐ์ด NaN์
๋๋ค. ๊ธฐ๋ณธ ๋ฌธ์ ๋ฅผ ๋ฐํํฉ๋๋ค.")
|
| 120 |
+
# return {
|
| 121 |
+
# 'question': f"[์ ์ฌ ๋ฌธ์ ] {wrong_q.get('QuestionText', '๋ฌธ์ ๊ฐ ์ ๊ณต๋์ง ์์์ต๋๋ค.')}",
|
| 122 |
+
# 'choices': {
|
| 123 |
+
# 'A': "๋ณด๊ธฐ A",
|
| 124 |
+
# 'B': "๋ณด๊ธฐ B",
|
| 125 |
+
# 'C': "๋ณด๊ธฐ C",
|
| 126 |
+
# 'D': "๋ณด๊ธฐ D"
|
| 127 |
+
# },
|
| 128 |
+
# 'correct': 'A',
|
| 129 |
+
# 'explanation': "Misconception ID๊ฐ ์ ๊ณต๋์ง ์์ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฅผ ๋ฐํํฉ๋๋ค."
|
| 130 |
+
# }
|
| 131 |
+
|
| 132 |
+
# try:
|
| 133 |
+
# # ์ ์ฌ ๋ฌธ์ ์์ฑ
|
| 134 |
+
# #logger.info(f"At least I'm in")
|
| 135 |
+
|
| 136 |
+
# #logger.info(f"Type of wrong_q: {type(wrong_q)}")
|
| 137 |
+
# #logger.info(f"Content of wrong_q: {wrong_q}")
|
| 138 |
+
# #logger.info(f"Type of misconception_id: {type(misconception_id)}")
|
| 139 |
+
# #logger.info(f"Value of misconception_id: {misconception_id}")
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# # input ์ ๋ฆฌ
|
| 143 |
+
# construct_name=wrong_q['ConstructName'],
|
| 144 |
+
# subject_name=wrong_q['SubjectName'],
|
| 145 |
+
# question_text=wrong_q['QuestionText'],
|
| 146 |
+
# correct_answer_text=wrong_q[f'Answer{wrong_q["CorrectAnswer"]}Text'],
|
| 147 |
+
# wrong_answer_text=wrong_q[f'Answer{st.session_state.selected_wrong_answer}Text'],
|
| 148 |
+
# misconception_id=int(misconception_id)
|
| 149 |
+
# #logger.info(f"Input Arguments: construct_name={construct_name}, subject_name={subject_name}, question_text={question_text}, correct_answer_text={correct_answer_text}, wrong_answer_text={wrong_answer_text}, misconception_id={misconception_id}")
|
| 150 |
+
|
| 151 |
+
# construct_name = construct_name[0] if isinstance(construct_name, tuple) else construct_name
|
| 152 |
+
# subject_name = subject_name[0] if isinstance(subject_name, tuple) else subject_name
|
| 153 |
+
# question_text = question_text[0] if isinstance(question_text, tuple) else question_text
|
| 154 |
+
# correct_answer_text = correct_answer_text[0] if isinstance(correct_answer_text, tuple) else correct_answer_text
|
| 155 |
+
# wrong_answer_text = wrong_answer_text[0] if isinstance(wrong_answer_text, tuple) else wrong_answer_text
|
| 156 |
+
# #logger.info(f"construct_name={construct_name}, Type={type(construct_name)}, subject_name={subject_name}, Type={type(subject_name)}")
|
| 157 |
+
# #logger.info(f"question_text={question_text}, Type={type(question_text)}, correct_answer_text={correct_answer_text}, Type={type(correct_answer_text)}")
|
| 158 |
+
# #logger.info(f"wrong_answer_text={wrong_answer_text}, Type={type(wrong_answer_text)}")
|
| 159 |
+
# print("Debugging log block reached")
|
| 160 |
+
|
| 161 |
+
# #logger.info(f"Cleaned Input Arguments: construct_name={construct_name}, subject_name={subject_name}, question_text={question_text}, correct_answer_text={correct_answer_text}, wrong_answer_text={wrong_answer_text}, misconception_id={misconception_id}")
|
| 162 |
+
# #logger.debug(f"Cleaned arguments: {construct_name}, {subject_name}, {question_text}, {correct_answer_text}, {wrong_answer_text}, {misconception_id}")
|
| 163 |
+
|
| 164 |
+
# # generated_q, _ = generator.generate_similar_question_with_text(
|
| 165 |
+
# # construct_name=wrong_q['ConstructName'],
|
| 166 |
+
# # subject_name=wrong_q['SubjectName'],
|
| 167 |
+
# # question_text=wrong_q['QuestionText'],
|
| 168 |
+
# # correct_answer_text=wrong_q[f'Answer{wrong_q["CorrectAnswer"]}Text'],
|
| 169 |
+
# # wrong_answer_text=wrong_q[f'Answer{st.session_state.selected_wrong_answer}Text'],
|
| 170 |
+
# # misconception_id=int(misconception_id)
|
| 171 |
+
# # )
|
| 172 |
+
# logger.info(f"Inputs: construct_name={construct_name}, subject_name={subject_name}, question_text={question_text}, correct_answer_text={correct_answer_text}, wrong_answer_text={wrong_answer_text}, misconception_id={misconception_id}")
|
| 173 |
+
|
| 174 |
+
# generated_q, _ = generator.generate_similar_question_with_text(
|
| 175 |
+
# construct_name,
|
| 176 |
+
# subject_name,
|
| 177 |
+
# question_text,
|
| 178 |
+
# correct_answer_text,
|
| 179 |
+
# wrong_answer_text,
|
| 180 |
+
# misconception_id
|
| 181 |
+
# )
|
| 182 |
+
# logger.info(f"Generated question type: {type(generated_q)}")
|
| 183 |
+
|
| 184 |
+
# if generated_q:
|
| 185 |
+
# logger.info(f"At least there is something generated")
|
| 186 |
+
# return {
|
| 187 |
+
# 'question': generated_q.question,
|
| 188 |
+
# 'choices': generated_q.choices,
|
| 189 |
+
# 'correct': generated_q.correct_answer,
|
| 190 |
+
# 'explanation': generated_q.explanation
|
| 191 |
+
# }
|
| 192 |
+
# except Exception as e:
|
| 193 |
+
# # ๋๋ฒ๊น
์ ์ํ ์์ธ ์ค๋ฅ ์ถ๋ ฅ
|
| 194 |
+
# logger.error(f"Error generating similar question: {e}")
|
| 195 |
+
# st.error(f"๋ฌธ์ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. {e}")
|
| 196 |
+
# return None
|
| 197 |
+
|
| 198 |
+
# # ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐํ
|
| 199 |
+
# return {
|
| 200 |
+
# 'question': f"[์ ์ฌ ๋ฌธ์ ] {wrong_q.get('QuestionText', '๋ฌธ์ ๊ฐ ์ ๊ณต๋์ง ์์์ต๋๋ค.')}",
|
| 201 |
+
# 'choices': {
|
| 202 |
+
# 'A': "์๋ก์ด ๋ณด๊ธฐ A",
|
| 203 |
+
# 'B': "์๋ก์ด ๋ณด๊ธฐ B",
|
| 204 |
+
# 'C': "์๋ก์ด ๋ณด๊ธฐ C",
|
| 205 |
+
# 'D': "์๋ก์ด ๋ณด๊ธฐ D"
|
| 206 |
+
# },
|
| 207 |
+
# 'correct': 'A',
|
| 208 |
+
# 'explanation': f"์ด ๋ฌธ์ ๋ Misconception ID {misconception_id}์ ๊ด๋ จ๋์ด ์์ต๋๋ค."
|
| 209 |
+
# }
|
| 210 |
+
|
| 211 |
+
def generate_similar_question(wrong_q, misconception_id, generator):
|
| 212 |
+
"""์ ์ฌ ๋ฌธ์ ์์ฑ"""
|
| 213 |
+
logger.info(f"Generating similar question for misconception_id: {misconception_id}")
|
| 214 |
+
|
| 215 |
+
# ์
๋ ฅ ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ
|
| 216 |
+
if not isinstance(wrong_q, dict):
|
| 217 |
+
logger.error(f"Invalid wrong_q type: {type(wrong_q)}")
|
| 218 |
+
st.error("์ ์ฌ ๋ฌธ์ ์์ฑ์ ํ์ํ ๋ฐ์ดํฐ ํ์์ด ์๋ชป๋์์ต๋๋ค.")
|
| 219 |
+
return None
|
| 220 |
+
|
| 221 |
+
# misconception_id๊ฐ ์ ํจํ์ง ํ์ธ
|
| 222 |
+
if pd.isna(misconception_id):
|
| 223 |
+
logger.warning("misconception_id is NaN")
|
| 224 |
+
return None
|
| 225 |
+
|
| 226 |
+
try:
|
| 227 |
+
# ๋ฐ์ดํฐ ์ค๋น (ํํ ๋ณํ ๋ฐฉ์ง)
|
| 228 |
+
input_data = {
|
| 229 |
+
'construct_name': str(wrong_q.get('ConstructName', '')),
|
| 230 |
+
'subject_name': str(wrong_q.get('SubjectName', '')),
|
| 231 |
+
'question_text': str(wrong_q.get('QuestionText', '')),
|
| 232 |
+
'correct_answer_text': str(wrong_q.get(f'Answer{wrong_q["CorrectAnswer"]}Text', '')),
|
| 233 |
+
'wrong_answer_text': str(wrong_q.get(f'Answer{st.session_state.selected_wrong_answer}Text', '')),
|
| 234 |
+
'misconception_id': int(misconception_id)
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
logger.info(f"Prepared input data: {input_data}")
|
| 238 |
+
|
| 239 |
+
# ์ ์ฌ ๋ฌธ์ ์์ฑ ํธ์ถ
|
| 240 |
+
generated_q, _ = generator.generate_similar_question_with_text(
|
| 241 |
+
construct_name=input_data['construct_name'],
|
| 242 |
+
subject_name=input_data['subject_name'],
|
| 243 |
+
question_text=input_data['question_text'],
|
| 244 |
+
correct_answer_text=input_data['correct_answer_text'],
|
| 245 |
+
wrong_answer_text=input_data['wrong_answer_text'],
|
| 246 |
+
misconception_id=input_data['misconception_id']
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
if generated_q:
|
| 250 |
+
return {
|
| 251 |
+
'question': generated_q.question,
|
| 252 |
+
'choices': generated_q.choices,
|
| 253 |
+
'correct': generated_q.correct_answer,
|
| 254 |
+
'explanation': generated_q.explanation
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
logger.error(f"Error in generate_similar_question: {str(e)}")
|
| 259 |
+
st.error(f"๋ฌธ์ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}")
|
| 260 |
+
return None
|
| 261 |
+
|
| 262 |
+
return None
|
| 263 |
+
|
| 264 |
def handle_answer(answer, current_q):
|
| 265 |
+
"""๋ต๋ณ ์ฒ๋ฆฌ"""
|
| 266 |
if answer != current_q['CorrectAnswer']:
|
| 267 |
wrong_q_dict = current_q.to_dict()
|
| 268 |
st.session_state.wrong_questions.append(wrong_q_dict)
|
|
|
|
| 276 |
if st.session_state.current_question_index >= 10:
|
| 277 |
st.session_state.current_step = 'review'
|
| 278 |
|
|
|
|
| 279 |
def main():
|
| 280 |
+
"""๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
๋ก์ง"""
|
| 281 |
st.title("MisconcepTutor")
|
| 282 |
+
|
| 283 |
+
# Generator ์ด๊ธฐํ
|
| 284 |
generator = load_question_generator()
|
| 285 |
|
| 286 |
+
# ์ด๊ธฐ ํ๋ฉด
|
| 287 |
if st.session_state.current_step == 'initial':
|
| 288 |
st.write("#### ํ์ต์ ์์ํ๊ฒ ์ต๋๋ค. 10๊ฐ์ ๋ฌธ์ ๋ฅผ ํ์ด๋ณผ๊น์?")
|
| 289 |
if st.button("ํ์ต ์์", key="start_quiz"):
|
| 290 |
start_quiz()
|
| 291 |
st.rerun()
|
| 292 |
|
| 293 |
+
# ํด์ฆ ํ๋ฉด
|
| 294 |
elif st.session_state.current_step == 'quiz':
|
| 295 |
current_q = st.session_state.questions.iloc[st.session_state.current_question_index]
|
| 296 |
+
|
| 297 |
+
# ์งํ ์ํฉ ํ์
|
| 298 |
progress = st.session_state.current_question_index / 10
|
| 299 |
st.progress(progress)
|
| 300 |
st.write(f"### ๋ฌธ์ {st.session_state.current_question_index + 1}/10")
|
| 301 |
+
|
| 302 |
+
# ๋ฌธ์ ํ์
|
| 303 |
st.markdown("---")
|
| 304 |
st.write(current_q['QuestionText'])
|
| 305 |
+
|
| 306 |
+
# ๋ณด๊ธฐ ํ์
|
| 307 |
col1, col2 = st.columns(2)
|
| 308 |
with col1:
|
| 309 |
if st.button(f"A) {current_q['AnswerAText']}", key="A"):
|
|
|
|
| 320 |
handle_answer('D', current_q)
|
| 321 |
st.rerun()
|
| 322 |
|
| 323 |
+
# ๋ณต์ต ํ๋ฉด
|
| 324 |
elif st.session_state.current_step == 'review':
|
| 325 |
st.write("### ํ์ต ๊ฒฐ๊ณผ")
|
| 326 |
+
|
| 327 |
+
# ๊ฒฐ๊ณผ ํต๊ณ
|
| 328 |
col1, col2, col3 = st.columns(3)
|
| 329 |
col1.metric("์ด ๋ฌธ์ ์", 10)
|
| 330 |
col2.metric("๋ง์ ๋ฌธ์ ", 10 - len(st.session_state.wrong_questions))
|
| 331 |
col3.metric("ํ๋ฆฐ ๋ฌธ์ ", len(st.session_state.wrong_questions))
|
| 332 |
+
|
| 333 |
+
# ๊ฒฐ๊ณผ์ ๋ฐ๋ฅธ ๋ฉ์์ง ํ์
|
| 334 |
if len(st.session_state.wrong_questions) == 0:
|
| 335 |
+
st.balloons() # ์ถํ ํจ๊ณผ
|
| 336 |
+
st.success("๐ ์ถํํฉ๋๋ค! ๋ชจ๋ ๋ฌธ์ ๋ฅผ ๋ง์ถ์
จ์ด์!")
|
| 337 |
+
st.markdown("""
|
| 338 |
+
### ๐ ์ํ์์ด์ญ๋๋ค!
|
| 339 |
+
์๋ฒฝํ ์ ์๋ฅผ ๋ฐ์ผ์
จ๋ค์! ์ํ์ ๊ฐ๋
์ ์ ํํ๊ฒ ์ดํดํ๊ณ ๊ณ์ ๊ฒ ๊ฐ์ต๋๋ค.
|
| 340 |
+
""")
|
| 341 |
elif len(st.session_state.wrong_questions) <= 3:
|
| 342 |
+
st.success("์ ํ์
จ์ด์! ์กฐ๊ธ๋ง ๋ ์ฐ์ตํ๋ฉด ์๋ฒฝํ ๊ฑฐ์์!")
|
| 343 |
else:
|
| 344 |
+
st.info("์ฒ์ฒํ ๊ฐ๋
์ ๋ณต์ตํด๋ณด์์. ์ฐ์ตํ๋ค ๋ณด๋ฉด ๋์ด๋ ๊ฑฐ์์!")
|
| 345 |
+
|
| 346 |
+
# ๋ค๋น๊ฒ์ด์
๋ฒํผ
|
| 347 |
+
col1, col2 = st.columns(2)
|
| 348 |
+
with col1:
|
| 349 |
+
if st.button("๐ ์๋ก์ด ๋ฌธ์ ์ธํธ ์์ํ๊ธฐ", use_container_width=True):
|
| 350 |
+
start_quiz()
|
| 351 |
+
st.rerun()
|
| 352 |
+
with col2:
|
| 353 |
+
if st.button("๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ ์ฒ์์ผ๋ก ๋์๊ฐ๊ธฐ", use_container_width=True):
|
| 354 |
+
st.session_state.clear()
|
| 355 |
+
st.rerun()
|
| 356 |
+
|
| 357 |
+
# ํ๋ฆฐ ๋ฌธ์ ๋ถ์
|
| 358 |
if st.session_state.wrong_questions:
|
| 359 |
st.write("### โ๏ธ ํ๋ฆฐ ๋ฌธ์ ๋ถ์")
|
| 360 |
for i, (wrong_q, misconception_id) in enumerate(zip(
|
| 361 |
+
st.session_state.wrong_questions,
|
| 362 |
+
st.session_state.misconceptions
|
| 363 |
)):
|
| 364 |
with st.expander(f"๐ ํ๋ฆฐ ๋ฌธ์ #{i + 1}"):
|
| 365 |
+
st.write("**๐ ๋ฌธ์ :**")
|
| 366 |
st.write(wrong_q['QuestionText'])
|
| 367 |
+
st.write("**โ
์ ๋ต:**", wrong_q['CorrectAnswer'])
|
| 368 |
+
|
| 369 |
+
st.write("---")
|
| 370 |
+
st.write("**๐ ๊ด๋ จ๋ Misconception:**")
|
| 371 |
+
if misconception_id and not pd.isna(misconception_id):
|
| 372 |
misconception_text = generator.get_misconception_text(misconception_id)
|
| 373 |
+
st.info(f"Misconception ID: {int(misconception_id)}\n\n{misconception_text}")
|
| 374 |
+
else:
|
| 375 |
+
st.info("Misconception ์ ๋ณด๊ฐ ์์ต๋๋ค.")
|
| 376 |
+
# try:
|
| 377 |
+
# misconception_text = generator.get_misconception_text(misconception_id)
|
| 378 |
+
# st.info(f"Misconception ID: {int(misconception_id)}\n\n{misconception_text}")
|
| 379 |
+
# except Exception as e:
|
| 380 |
+
# logger.error(f"Error in get_misconception_text: {e}")
|
| 381 |
+
# st.info("Misconception ์ ๋ณด๊ฐ ์์ต๋๋ค.")
|
| 382 |
+
# return None, None
|
| 383 |
+
|
| 384 |
+
# ์ ์ฌ ๋ฌธ์ ์์ฑ ๋ฒํผ
|
| 385 |
if st.button(f"๐ ์ ์ฌ ๋ฌธ์ ํ๊ธฐ #{i + 1}", key=f"retry_{i}"):
|
| 386 |
+
with st.spinner("์ ์ฌ ๋ฌธ์ ๋ฅผ ์์ฑํ๊ณ ์์ต๋๋ค..."):
|
| 387 |
+
logger.info(f"Generating similar question for misconception_id: {misconception_id}")
|
| 388 |
+
new_question = generate_similar_question(wrong_q, misconception_id, generator)
|
| 389 |
+
if new_question:
|
| 390 |
+
st.write("### ๐ฏ ์ ์ฌ ๋ฌธ์ ")
|
| 391 |
+
st.write(new_question['question'])
|
| 392 |
+
st.write("**๋ณด๊ธฐ:**")
|
| 393 |
+
for choice, text in new_question['choices'].items():
|
| 394 |
+
st.write(f"{choice}) {text}")
|
| 395 |
+
st.write("**โ
์ ๋ต:**", new_question['correct'])
|
| 396 |
+
st.write("**๐ ํด์ค:**", new_question['explanation'])
|
| 397 |
+
else:
|
| 398 |
+
st.error("์ ์ฌ ๋ฌธ์ ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.")
|
| 399 |
if __name__ == "__main__":
|
| 400 |
main()
|
| 401 |
+
|
| 402 |
+
# random_state 42์์ ์ ๋ต
|
| 403 |
+
# D C A A C
|
| 404 |
+
# A B B B B
|