Update app.py
Browse files
app.py
CHANGED
@@ -2,33 +2,11 @@ import streamlit as st
|
|
2 |
import random
|
3 |
from collections import Counter
|
4 |
|
5 |
-
# Set up
|
6 |
st.set_page_config(page_title="Bầu Cua Tôm Cá", layout="centered")
|
7 |
st.title("Bầu Cua Tôm Cá")
|
8 |
|
9 |
-
# Set font and colors
|
10 |
-
st.markdown(
|
11 |
-
"""
|
12 |
-
<style>
|
13 |
-
.stButton>button {
|
14 |
-
font-size: 16px;
|
15 |
-
background-color: #FF0000;
|
16 |
-
color: #FFFFFF;
|
17 |
-
}
|
18 |
-
.stSelectbox, .stRadio {
|
19 |
-
font-size: 16px;
|
20 |
-
}
|
21 |
-
.stText {
|
22 |
-
font-size: 16px;
|
23 |
-
color: #000000;
|
24 |
-
}
|
25 |
-
</style>
|
26 |
-
""",
|
27 |
-
unsafe_allow_html=True
|
28 |
-
)
|
29 |
-
|
30 |
# UI elements
|
31 |
-
shake_count = st.selectbox("Số lần lắc", ["1000", "50", "100", "500"])
|
32 |
shake_info = st.empty()
|
33 |
result_label = st.empty()
|
34 |
|
@@ -41,50 +19,33 @@ for i, col in enumerate(cols):
|
|
41 |
with col:
|
42 |
selected_faces.append(st.radio(f"Chọn mặt {i+1}", dice_faces, index=dice_faces.index(default_faces[i])))
|
43 |
|
44 |
-
# Opposite and adjacent faces (not used in Streamlit version)
|
45 |
-
opposite_faces = {
|
46 |
-
"Bầu": "Cua",
|
47 |
-
"Cua": "Bầu",
|
48 |
-
"Nai": "Tôm",
|
49 |
-
"Tôm": "Nai",
|
50 |
-
"Gà": "Cá",
|
51 |
-
"Cá": "Gà"
|
52 |
-
}
|
53 |
-
|
54 |
-
adjacent_faces = {
|
55 |
-
"Bầu": ["Tôm", "Gà", "Nai", "Cá"],
|
56 |
-
"Cá": ["Bầu", "Nai", "Cua", "Tôm"],
|
57 |
-
"Gà": ["Bầu", "Tôm", "Cua", "Nai"],
|
58 |
-
"Tôm": ["Bầu", "Cá", "Cua", "Gà"],
|
59 |
-
"Nai": ["Bầu", "Gà", "Cua", "Cá"],
|
60 |
-
"Cua": ["Tôm", "Cá", "Nai", "Gà"]
|
61 |
-
}
|
62 |
-
|
63 |
def predict_result():
|
64 |
outcomes = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
result_counts = Counter(results)
|
70 |
sorted_results = sorted(result_counts.items(), key=lambda x: x[1], reverse=True)
|
71 |
-
|
72 |
-
result_text = "Kết quả:\n"
|
73 |
-
for outcome, count in sorted_results:
|
74 |
-
result_text += f"{outcome}: {count} ({count/shake_count_int:.2%})\n"
|
75 |
-
|
76 |
-
result_label.text(result_text)
|
77 |
-
shake_info.text(f"Số lần lắc: {shake_count}")
|
78 |
-
else:
|
79 |
-
result_label.text("Vui lòng chọn 3 mặt đầu tiên.")
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# Buttons
|
86 |
if st.button("Lắc"):
|
87 |
-
predict_result()
|
88 |
-
|
89 |
-
if st.button("Reset"):
|
90 |
-
reset_results()
|
|
|
2 |
import random
|
3 |
from collections import Counter
|
4 |
|
5 |
+
# Set up Streamlit app
|
6 |
st.set_page_config(page_title="Bầu Cua Tôm Cá", layout="centered")
|
7 |
st.title("Bầu Cua Tôm Cá")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# UI elements
|
|
|
10 |
shake_info = st.empty()
|
11 |
result_label = st.empty()
|
12 |
|
|
|
19 |
with col:
|
20 |
selected_faces.append(st.radio(f"Chọn mặt {i+1}", dice_faces, index=dice_faces.index(default_faces[i])))
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def predict_result():
|
23 |
outcomes = ["Bầu", "Cua", "Tôm", "Cá", "Gà", "Nai"]
|
24 |
+
results = []
|
25 |
+
shake_count = 0
|
26 |
+
|
27 |
+
while True:
|
28 |
+
shake_count += 1
|
29 |
+
results.append(random.choice(outcomes))
|
30 |
result_counts = Counter(results)
|
31 |
sorted_results = sorted(result_counts.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
if len(sorted_results) > 1:
|
34 |
+
rank_1, rank_2 = sorted_results[:2]
|
35 |
+
rank_1_pct = rank_1[1] / shake_count
|
36 |
+
rank_2_pct = rank_2[1] / shake_count
|
37 |
+
|
38 |
+
if (rank_1_pct - rank_2_pct) > 0.03: # Chênh lệch > 3%
|
39 |
+
break
|
40 |
+
|
41 |
+
# Hiển thị kết quả
|
42 |
+
result_text = f"Số lần lắc: {shake_count}\n\nKết quả:\n"
|
43 |
+
for outcome, count in sorted_results:
|
44 |
+
result_text += f"{outcome}: {count} ({count/shake_count:.2%})\n"
|
45 |
+
|
46 |
+
result_label.text(result_text)
|
47 |
+
shake_info.text(f"Tổng số lần lắc: {shake_count}")
|
48 |
|
49 |
# Buttons
|
50 |
if st.button("Lắc"):
|
51 |
+
predict_result()
|
|
|
|
|
|