PongsakornSET commited on
Commit
ac006b7
1 Parent(s): 34049ba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+ import pandas as pd
4
+
5
+ # Load the vocabulary data
6
+ df = pd.read_csv("word1_v2.csv")
7
+ vocab_dict = dict()
8
+ for line in df.iterrows():
9
+ vocab_dict[line[1]['meaning']] = (line[1]['vocab'], line[1]['pos'])
10
+
11
+ # Function to generate a new question
12
+ def generate_question():
13
+ thai_word, (correct_answer, correct_part_of_speech) = random.choice(list(vocab_dict.items()))
14
+
15
+ # Generate wrong answers
16
+ wrong_answers = random.sample([v[0] for v in vocab_dict.values()], 4)
17
+ while correct_answer in wrong_answers:
18
+ wrong_answers = random.sample([v[0] for v in vocab_dict.values()], 4)
19
+
20
+ options = wrong_answers + [correct_answer]
21
+ random.shuffle(options)
22
+
23
+ return thai_word, correct_answer, correct_part_of_speech, options
24
+
25
+ # Initial configuration
26
+ if 'question_data' not in st.session_state:
27
+ st.session_state.question_data = generate_question()
28
+
29
+ thai_word, correct_answer, correct_part_of_speech, options = st.session_state.question_data
30
+
31
+ # Display question
32
+ st.write(f"คำว่า '{thai_word}' (ชนิดคำ: {correct_part_of_speech}) คือคำว่าอะไร?")
33
+
34
+ # Display answer options as buttons
35
+ selected_answer = st.radio("เลือกคำตอบของคุณ:", options)
36
+
37
+ # When user selects an answer
38
+ if st.button("ตรวจคำตอบ"):
39
+ if selected_answer == correct_answer:
40
+ st.success("ถูกต้อง!")
41
+ else:
42
+ st.error(f"ผิด! คำตอบที่ถูกต้องคือ: {correct_answer}")
43
+
44
+ if st.button("คำถามถัดไป"):
45
+ st.session_state.question_data = generate_question()
46
+ st.experimental_rerun()