import streamlit as st import random import pandas as pd # Load the vocabulary data df = pd.read_csv("word1_v2.csv") vocab_dict = dict() for line in df.iterrows(): vocab_dict[line[1]['meaning']] = (line[1]['vocab'], line[1]['pos']) # Function to generate a new question def generate_question(): thai_word, (correct_answer, correct_part_of_speech) = random.choice(list(vocab_dict.items())) # Generate wrong answers wrong_answers = random.sample([v[0] for v in vocab_dict.values()], 4) while correct_answer in wrong_answers: wrong_answers = random.sample([v[0] for v in vocab_dict.values()], 4) options = wrong_answers + [correct_answer] random.shuffle(options) return thai_word, correct_answer, correct_part_of_speech, options # Initial configuration if 'question_data' not in st.session_state: st.session_state.question_data = generate_question() st.session_state.answered = False thai_word, correct_answer, correct_part_of_speech, options = st.session_state.question_data # Display question st.write(f"คำว่า '{thai_word}' (ชนิดคำ: {correct_part_of_speech}) คือคำว่าอะไร?") # Display answer options as radio buttons selected_answer = st.radio("เลือกคำตอบของคุณ:", options) # Check answer and show the result if not st.session_state.answered and st.button("ตรวจคำตอบ"): st.session_state.answered = True if selected_answer == correct_answer: st.success("ถูกต้อง!") else: st.error(f"ผิด! คำตอบที่ถูกต้องคือ: {correct_answer}") # 'Next question' button to load a new question if st.session_state.answered and st.button("คำถามถัดไป"): st.session_state.question_data = generate_question() st.session_state.answered = False