Huy0502 commited on
Commit
e2b47bc
·
verified ·
1 Parent(s): 4951abe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -72
app.py CHANGED
@@ -1,73 +1,73 @@
1
- import streamlit as st
2
- from random import random
3
- import pandas as pd
4
- from transformers import pipeline
5
-
6
- #-----------------------FUNCTIONS-----------------------
7
- # Đọc và xử lý dữ liệu
8
- @st.cache
9
- def load_data(file_path):
10
- df = pd.read_csv(file_path)
11
- return df
12
-
13
- # Đề xuất chủ đề sử dụng Zero-shot classification
14
- def suggest_topics(text, classifier, topics):
15
- prediction = classifier(text, topics)
16
- return prediction["labels"][0]
17
-
18
- # Thiết lập pipeline cho zero-shot classification
19
- classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
20
-
21
- # Các chủ đề có thể
22
- possible_topics = ["Kinh tế", "Du lịch", "Giao tiếp", "Công nghệ", "Giáo dục"]
23
-
24
- # Load data
25
- data_path = "Toeic.csv"
26
- df = load_data(data_path)
27
-
28
-
29
- #----------------------------------Frontend--------------------------------
30
- # Giao diện người dùng
31
- st.markdown("<h1 style='text-align: center;'>Chương trình ôn tập từ vựng Toeic</h1>", unsafe_allow_html=True)
32
- st.markdown("<h3 style='text-align: center;'>Dành cho bé iu của anh<3</h3>", unsafe_allow_html=True)
33
-
34
- # Thêm thanh bên để chọn chế độ ôn tập
35
- mode = st.sidebar.selectbox(
36
- "Chọn chế độ ôn tập:",
37
- ["Flashcard", "Trắc nghiệm", "Thử thách tốc độ", "Ôn tập theo chủ đề", "Kiểm tra"]
38
- )
39
-
40
- # Hiển thị flashcards
41
- if mode == "Flashcard":
42
- st.subheader("Flashcard Từ vựng")
43
- word = st.selectbox("Chọn từ:", df["Từ vựng"])
44
- if st.button("Hiện nghĩa"):
45
- definition = df[df["Từ vựng"] == word]["Nghĩa"].values[0]
46
- st.write("Nghĩa của từ:", definition)
47
-
48
- # Trắc nghiệm
49
- elif mode == "Trắc nghiệm":
50
- st.subheader("Trắc nghiệm")
51
- questions = df.sample(5)
52
- correct_answers = 0
53
- for i, row in questions.iterrows():
54
- options = df["Nghĩa"].sample(3).tolist() + [row["Nghĩa"]]
55
- shuffled_options = random.sample(options, len(options))
56
- user_answer = st.radio(f"{row['Từ vựng']} có nghĩa là gì?", options=shuffled_options, key=i)
57
- if st.button("Kiểm tra", key=i):
58
- if user_answer == row["Nghĩa"]:
59
- st.success("Đúng!")
60
- correct_answers += 1
61
- else:
62
- st.error("Sai!")
63
- st.write(f"Điểm của bạn: {correct_answers}/5")
64
-
65
- # Cập nhật chủ đề nếu cần
66
- if st.button("Cập nhật Chủ đề cho Từ Vựng"):
67
- for i, row in df.iterrows():
68
- if pd.isna(row["Chủ đề"]):
69
- df.at[i, "Chủ đề"] = suggest_topics(row["Từ vựng"], classifier, possible_topics)
70
- df.to_csv(data_path, index=False)
71
- st.success("Đã cập nhật chủ đề cho từ vựng!")
72
-
73
  st.markdown("<h3 style='text-align: center;'>Chúc bé iu của anh thi đậu nhé<3</h3>", unsafe_allow_html=True)
 
1
+ import streamlit as st
2
+ from random import random
3
+ import pandas as pd
4
+ from transformers import pipeline
5
+
6
+ #-----------------------FUNCTIONS-----------------------
7
+ # Đọc và xử lý dữ liệu
8
+ @st.cache_data
9
+ def load_data(file_path):
10
+ df = pd.read_csv(file_path)
11
+ return df
12
+
13
+ # Đề xuất chủ đề sử dụng Zero-shot classification
14
+ def suggest_topics(text, classifier, topics):
15
+ prediction = classifier(text, topics)
16
+ return prediction["labels"][0]
17
+
18
+ # Thiết lập pipeline cho zero-shot classification
19
+ classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
20
+
21
+ # Các chủ đề có thể
22
+ possible_topics = ["Kinh tế", "Du lịch", "Giao tiếp", "Công nghệ", "Giáo dục"]
23
+
24
+ # Load data
25
+ data_path = "Toeic.csv"
26
+ df = load_data(data_path)
27
+
28
+
29
+ #----------------------------------Frontend--------------------------------
30
+ # Giao diện người dùng
31
+ st.markdown("<h1 style='text-align: center;'>Chương trình ôn tập từ vựng Toeic</h1>", unsafe_allow_html=True)
32
+ st.markdown("<h3 style='text-align: center;'>Dành cho bé iu của anh<3</h3>", unsafe_allow_html=True)
33
+
34
+ # Thêm thanh bên để chọn chế độ ôn tập
35
+ mode = st.sidebar.selectbox(
36
+ "Chọn chế độ ôn tập:",
37
+ ["Flashcard", "Trắc nghiệm", "Thử thách tốc độ", "Ôn tập theo chủ đề", "Kiểm tra"]
38
+ )
39
+
40
+ # Hiển thị flashcards
41
+ if mode == "Flashcard":
42
+ st.subheader("Flashcard Từ vựng")
43
+ word = st.selectbox("Chọn từ:", df["Từ vựng"])
44
+ if st.button("Hiện nghĩa"):
45
+ definition = df[df["Từ vựng"] == word]["Nghĩa"].values[0]
46
+ st.write("Nghĩa của từ:", definition)
47
+
48
+ # Trắc nghiệm
49
+ elif mode == "Trắc nghiệm":
50
+ st.subheader("Trắc nghiệm")
51
+ questions = df.sample(5)
52
+ correct_answers = 0
53
+ for i, row in questions.iterrows():
54
+ options = df["Nghĩa"].sample(3).tolist() + [row["Nghĩa"]]
55
+ shuffled_options = random.sample(options, len(options))
56
+ user_answer = st.radio(f"{row['Từ vựng']} có nghĩa là gì?", options=shuffled_options, key=i)
57
+ if st.button("Kiểm tra", key=i):
58
+ if user_answer == row["Ngh��a"]:
59
+ st.success("Đúng!")
60
+ correct_answers += 1
61
+ else:
62
+ st.error("Sai!")
63
+ st.write(f"Điểm của bạn: {correct_answers}/5")
64
+
65
+ # Cập nhật chủ đề nếu cần
66
+ if st.button("Cập nhật Chủ đề cho Từ Vựng"):
67
+ for i, row in df.iterrows():
68
+ if pd.isna(row["Chủ đề"]):
69
+ df.at[i, "Chủ đề"] = suggest_topics(row["Từ vựng"], classifier, possible_topics)
70
+ df.to_csv(data_path, index=False)
71
+ st.success("Đã cập nhật chủ đề cho từ vựng!")
72
+
73
  st.markdown("<h3 style='text-align: center;'>Chúc bé iu của anh thi đậu nhé<3</h3>", unsafe_allow_html=True)