Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import os | |
import logging | |
from src.SecondModule.module2 import SimilarQuestionGenerator, generate_similar_question | |
# λ‘κΉ μ€μ | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# Streamlit νμ΄μ§ κΈ°λ³Έ μ€μ | |
st.set_page_config( | |
page_title="MisconcepTutor", | |
layout="wide", | |
initial_sidebar_state="expanded" | |
) | |
# κ²½λ‘ μ€μ | |
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') | |
# μΈμ μν μ΄κΈ°ν | |
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") | |
# λ¬Έμ μμ±κΈ° μ΄κΈ°ν | |
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 λ°μ΄ν° λ‘λ | |
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 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' | |
# λ©μΈ μ ν리μΌμ΄μ λ‘μ§ | |
def main(): | |
st.title("MisconcepTutor") | |
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("---") | |
st.write(current_q['QuestionText']) | |
col1, col2 = st.columns(2) | |
with col1: | |
if st.button(f"A) {current_q['AnswerAText']}", key="A"): | |
handle_answer('A', current_q) | |
st.rerun() | |
if st.button(f"C) {current_q['AnswerCText']}", key="C"): | |
handle_answer('C', current_q) | |
st.rerun() | |
with col2: | |
if st.button(f"B) {current_q['AnswerBText']}", key="B"): | |
handle_answer('B', current_q) | |
st.rerun() | |
if st.button(f"D) {current_q['AnswerDText']}", key="D"): | |
handle_answer('D', 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("π λͺ¨λ λ¬Έμ λ₯Ό λ§μΆμ ¨μ΅λλ€!") | |
elif len(st.session_state.wrong_questions) <= 3: | |
st.success("μ νμ ¨μ΄μ! μ‘°κΈλ§ λ μ°μ΅νλ©΄ μλ²½ν΄μ§ κ±°μμ!") | |
else: | |
st.info("μ²μ²ν κ°λ μ 볡μ΅ν΄ 보μΈμ. μ°μ΅νλ©΄ λμμ§ κ²λλ€.") | |
if st.session_state.wrong_questions: | |
st.write("### βοΈ νλ¦° λ¬Έμ λΆμ") | |
for i, (wrong_q, misconception_id) in enumerate(zip( | |
st.session_state.wrong_questions, st.session_state.misconceptions | |
)): | |
with st.expander(f"π νλ¦° λ¬Έμ #{i + 1}"): | |
st.write(wrong_q['QuestionText']) | |
st.write(f"β μ λ΅: {wrong_q['CorrectAnswer']}") | |
if misconception_id: | |
misconception_text = generator.get_misconception_text(misconception_id) | |
st.info(f"Misconception: {misconception_text}") | |
if st.button(f"π μ μ¬ λ¬Έμ νκΈ° #{i + 1}", key=f"retry_{i}"): | |
new_question = generate_similar_question(wrong_q, misconception_id, generator) | |
if new_question: | |
st.write("### π― μ μ¬ λ¬Έμ ") | |
st.write(new_question['question']) | |
for choice, text in new_question['choices'].items(): | |
st.write(f"{choice}) {text}") | |
st.write(f"β μ λ΅: {new_question['correct']}") | |
st.write(f"π ν΄μ€: {new_question['explanation']}") | |
else: | |
st.error("μ μ¬ λ¬Έμ λ₯Ό μμ±ν μ μμ΅λλ€.") | |
if __name__ == "__main__": | |
main() | |