Update app.py
Browse files
app.py
CHANGED
@@ -6,15 +6,19 @@ from io import BytesIO
|
|
6 |
from PIL import Image
|
7 |
from transformers import pipeline
|
8 |
|
9 |
-
|
10 |
-
|
|
|
11 |
|
12 |
# Define maximum dimensions for the fortune image (in pixels)
|
13 |
MAX_SIZE = (400, 400)
|
14 |
|
|
|
|
|
|
|
|
|
15 |
# Set page configuration
|
16 |
st.set_page_config(page_title="Fortuen Stick Enquiry", layout="wide")
|
17 |
-
|
18 |
st.title("Fortuen Stick Enquiry")
|
19 |
|
20 |
# Initialize session state variables
|
@@ -24,10 +28,12 @@ if "fortune_number" not in st.session_state:
|
|
24 |
st.session_state.fortune_number = None
|
25 |
if "fortune_row" not in st.session_state:
|
26 |
st.session_state.fortune_row = None
|
|
|
|
|
27 |
|
28 |
if "fortune_data" not in st.session_state:
|
29 |
try:
|
30 |
-
st.session_state.fortune_data = pd.read_csv("detail.csv")
|
31 |
except Exception as e:
|
32 |
st.error(f"Error loading CSV: {e}")
|
33 |
st.session_state.fortune_data = None
|
@@ -35,35 +41,35 @@ if "fortune_data" not in st.session_state:
|
|
35 |
if "stick_clicked" not in st.session_state:
|
36 |
st.session_state.stick_clicked = False
|
37 |
|
38 |
-
# Update the check functions to use the "question" parameter properly.
|
39 |
def check_sentence_is_english_model(question):
|
40 |
-
|
41 |
-
# If the model predicts the label "en", we consider it English.
|
42 |
-
if pipe1(question)[0]['label'] == 'en':
|
43 |
-
return True
|
44 |
-
return False
|
45 |
|
46 |
def check_sentence_is_question_model(question):
|
47 |
-
|
48 |
-
# If the model predicts "LABEL_1", we consider it a question.
|
49 |
-
if pipe2(question)[0]['label'] == 'LABEL_1':
|
50 |
-
return True
|
51 |
-
return False
|
52 |
|
53 |
-
# Callback function for the submit button (no parameters now)
|
54 |
def submit_text_callback():
|
55 |
-
# Get the user input from session state
|
56 |
question = st.session_state.get("user_sentence", "")
|
|
|
|
|
57 |
|
58 |
if not check_sentence_is_english_model(question):
|
59 |
-
st.
|
|
|
60 |
return
|
61 |
|
62 |
if not check_sentence_is_question_model(question):
|
63 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
return
|
65 |
|
66 |
st.session_state.submitted_text = True
|
|
|
|
|
67 |
# Randomly generate a number from 1 to 100
|
68 |
st.session_state.fortune_number = random.randint(1, 100)
|
69 |
|
@@ -78,7 +84,7 @@ def submit_text_callback():
|
|
78 |
"Luck": row.get("Luck", "N/A"),
|
79 |
"Description": row.get("Description", "No description available."),
|
80 |
"Detail": row.get("Detail", "No detail available."),
|
81 |
-
"HeaderLink": row.get("link", None)
|
82 |
}
|
83 |
else:
|
84 |
st.session_state.fortune_row = {
|
@@ -89,7 +95,6 @@ def submit_text_callback():
|
|
89 |
"HeaderLink": None
|
90 |
}
|
91 |
|
92 |
-
# Function to load and resize local images using Pillow
|
93 |
def load_and_resize_image(path, max_size=MAX_SIZE):
|
94 |
try:
|
95 |
img = Image.open(path)
|
@@ -99,7 +104,6 @@ def load_and_resize_image(path, max_size=MAX_SIZE):
|
|
99 |
st.error(f"Error loading image: {e}")
|
100 |
return None
|
101 |
|
102 |
-
# Function to download image from URL and resize it
|
103 |
def download_and_resize_image(url, max_size=MAX_SIZE):
|
104 |
try:
|
105 |
response = requests.get(url)
|
@@ -122,16 +126,13 @@ left_col, _, right_col = st.columns([3, 1, 5])
|
|
122 |
with left_col:
|
123 |
left_top = st.container()
|
124 |
left_bottom = st.container()
|
125 |
-
# Top container: Input area and submit button
|
126 |
with left_top:
|
127 |
-
|
128 |
-
user_sentence = st.text_area("Enter your question in English", key="user_sentence", height=150)
|
129 |
-
# The callback no longer requires a parameter; it accesses st.session_state directly.
|
130 |
st.button("submit", key="submit_button", on_click=submit_text_callback)
|
131 |
-
|
|
|
132 |
if st.session_state.submitted_text:
|
133 |
with left_bottom:
|
134 |
-
# Add vertical spacing to approximately center the button vertically
|
135 |
for _ in range(5):
|
136 |
st.write("")
|
137 |
col1, col2, col3 = st.columns(3)
|
@@ -142,42 +143,33 @@ with left_col:
|
|
142 |
|
143 |
# ---- Right Column ----
|
144 |
with right_col:
|
145 |
-
# Top container: Fortune image area
|
146 |
with st.container():
|
147 |
col_left, col_center, col_right = st.columns([1, 2, 1])
|
148 |
with col_center:
|
149 |
if st.session_state.submitted_text and st.session_state.fortune_row:
|
150 |
header_link = st.session_state.fortune_row.get("HeaderLink")
|
151 |
if header_link:
|
152 |
-
# Download the image from the URL
|
153 |
img_from_url = download_and_resize_image(header_link)
|
154 |
if img_from_url:
|
155 |
st.image(img_from_url, use_container_width=False)
|
156 |
else:
|
157 |
-
|
158 |
-
img = load_and_resize_image("error.png")
|
159 |
if img:
|
160 |
st.image(img, use_container_width=False)
|
161 |
else:
|
162 |
-
|
163 |
-
img = load_and_resize_image("error.png")
|
164 |
if img:
|
165 |
st.image(img, use_container_width=False)
|
166 |
else:
|
167 |
-
|
168 |
-
img = load_and_resize_image("fortune.png")
|
169 |
if img:
|
170 |
st.image(img, caption="Your Fortune", use_container_width=False)
|
171 |
-
|
172 |
-
# Bottom container: Display fortune details using text areas
|
173 |
with st.container():
|
174 |
if st.session_state.fortune_row:
|
175 |
-
header_text = st.session_state.fortune_row.get("Header", "N/A")
|
176 |
luck_text = st.session_state.fortune_row.get("Luck", "N/A")
|
177 |
description_text = st.session_state.fortune_row.get("Description", "No description available.")
|
178 |
detail_text = st.session_state.fortune_row.get("Detail", "No detail available.")
|
179 |
|
180 |
-
# Create a summary with larger text using HTML styling
|
181 |
summary = f"""
|
182 |
<div style="font-size: 28px; font-weight: bold;">
|
183 |
Fortune stick number: {st.session_state.fortune_number}<br>
|
@@ -186,8 +178,5 @@ with right_col:
|
|
186 |
"""
|
187 |
st.markdown(summary, unsafe_allow_html=True)
|
188 |
|
189 |
-
# Second text area: Description
|
190 |
st.text_area("Description", value=description_text, height=150, disabled=True)
|
191 |
-
|
192 |
-
# Third text area: Detail
|
193 |
st.text_area("Detail", value=detail_text, height=150, disabled=True)
|
|
|
6 |
from PIL import Image
|
7 |
from transformers import pipeline
|
8 |
|
9 |
+
# Global pipelines
|
10 |
+
pipe_english = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
|
11 |
+
pipe_question = pipeline("text-classification", model="shahrukhx01/question-vs-statement-classifier")
|
12 |
|
13 |
# Define maximum dimensions for the fortune image (in pixels)
|
14 |
MAX_SIZE = (400, 400)
|
15 |
|
16 |
+
# Initialize button click count in session state
|
17 |
+
if "button_count_temp" not in st.session_state:
|
18 |
+
st.session_state.button_count_temp = 0
|
19 |
+
|
20 |
# Set page configuration
|
21 |
st.set_page_config(page_title="Fortuen Stick Enquiry", layout="wide")
|
|
|
22 |
st.title("Fortuen Stick Enquiry")
|
23 |
|
24 |
# Initialize session state variables
|
|
|
28 |
st.session_state.fortune_number = None
|
29 |
if "fortune_row" not in st.session_state:
|
30 |
st.session_state.fortune_row = None
|
31 |
+
if "error_message" not in st.session_state:
|
32 |
+
st.session_state.error_message = ""
|
33 |
|
34 |
if "fortune_data" not in st.session_state:
|
35 |
try:
|
36 |
+
st.session_state.fortune_data = pd.read_csv("/home/user/app/resources/detail.csv")
|
37 |
except Exception as e:
|
38 |
st.error(f"Error loading CSV: {e}")
|
39 |
st.session_state.fortune_data = None
|
|
|
41 |
if "stick_clicked" not in st.session_state:
|
42 |
st.session_state.stick_clicked = False
|
43 |
|
|
|
44 |
def check_sentence_is_english_model(question):
|
45 |
+
return pipe_english(question)[0]['label'] == 'en'
|
|
|
|
|
|
|
|
|
46 |
|
47 |
def check_sentence_is_question_model(question):
|
48 |
+
return pipe_question(question)[0]['label'] == 'LABEL_1'
|
|
|
|
|
|
|
|
|
49 |
|
|
|
50 |
def submit_text_callback():
|
|
|
51 |
question = st.session_state.get("user_sentence", "")
|
52 |
+
# Clear any previous error message
|
53 |
+
st.session_state.error_message = ""
|
54 |
|
55 |
if not check_sentence_is_english_model(question):
|
56 |
+
st.session_state.error_message = "Please enter in English!"
|
57 |
+
st.session_state.button_count_temp = 0
|
58 |
return
|
59 |
|
60 |
if not check_sentence_is_question_model(question):
|
61 |
+
st.session_state.error_message = "This is not a question. Please enter again!"
|
62 |
+
st.session_state.button_count_temp = 0
|
63 |
+
return
|
64 |
+
|
65 |
+
if st.session_state.button_count_temp == 0:
|
66 |
+
st.session_state.error_message = "Please take a moment to quietly reflect on your question in your mind, then click submit."
|
67 |
+
st.session_state.button_count_temp = 1
|
68 |
return
|
69 |
|
70 |
st.session_state.submitted_text = True
|
71 |
+
st.session_state.button_count_temp = 0 # Reset the counter once submission is accepted
|
72 |
+
|
73 |
# Randomly generate a number from 1 to 100
|
74 |
st.session_state.fortune_number = random.randint(1, 100)
|
75 |
|
|
|
84 |
"Luck": row.get("Luck", "N/A"),
|
85 |
"Description": row.get("Description", "No description available."),
|
86 |
"Detail": row.get("Detail", "No detail available."),
|
87 |
+
"HeaderLink": row.get("link", None)
|
88 |
}
|
89 |
else:
|
90 |
st.session_state.fortune_row = {
|
|
|
95 |
"HeaderLink": None
|
96 |
}
|
97 |
|
|
|
98 |
def load_and_resize_image(path, max_size=MAX_SIZE):
|
99 |
try:
|
100 |
img = Image.open(path)
|
|
|
104 |
st.error(f"Error loading image: {e}")
|
105 |
return None
|
106 |
|
|
|
107 |
def download_and_resize_image(url, max_size=MAX_SIZE):
|
108 |
try:
|
109 |
response = requests.get(url)
|
|
|
126 |
with left_col:
|
127 |
left_top = st.container()
|
128 |
left_bottom = st.container()
|
|
|
129 |
with left_top:
|
130 |
+
st.text_area("Enter your question in English", key="user_sentence", height=150)
|
|
|
|
|
131 |
st.button("submit", key="submit_button", on_click=submit_text_callback)
|
132 |
+
if st.session_state.error_message:
|
133 |
+
st.error(st.session_state.error_message)
|
134 |
if st.session_state.submitted_text:
|
135 |
with left_bottom:
|
|
|
136 |
for _ in range(5):
|
137 |
st.write("")
|
138 |
col1, col2, col3 = st.columns(3)
|
|
|
143 |
|
144 |
# ---- Right Column ----
|
145 |
with right_col:
|
|
|
146 |
with st.container():
|
147 |
col_left, col_center, col_right = st.columns([1, 2, 1])
|
148 |
with col_center:
|
149 |
if st.session_state.submitted_text and st.session_state.fortune_row:
|
150 |
header_link = st.session_state.fortune_row.get("HeaderLink")
|
151 |
if header_link:
|
|
|
152 |
img_from_url = download_and_resize_image(header_link)
|
153 |
if img_from_url:
|
154 |
st.image(img_from_url, use_container_width=False)
|
155 |
else:
|
156 |
+
img = load_and_resize_image("/home/user/app/resources/error.png")
|
|
|
157 |
if img:
|
158 |
st.image(img, use_container_width=False)
|
159 |
else:
|
160 |
+
img = load_and_resize_image("/home/user/app/resources/error.png")
|
|
|
161 |
if img:
|
162 |
st.image(img, use_container_width=False)
|
163 |
else:
|
164 |
+
img = load_and_resize_image("/home/user/app/resources/fortune.png")
|
|
|
165 |
if img:
|
166 |
st.image(img, caption="Your Fortune", use_container_width=False)
|
|
|
|
|
167 |
with st.container():
|
168 |
if st.session_state.fortune_row:
|
|
|
169 |
luck_text = st.session_state.fortune_row.get("Luck", "N/A")
|
170 |
description_text = st.session_state.fortune_row.get("Description", "No description available.")
|
171 |
detail_text = st.session_state.fortune_row.get("Detail", "No detail available.")
|
172 |
|
|
|
173 |
summary = f"""
|
174 |
<div style="font-size: 28px; font-weight: bold;">
|
175 |
Fortune stick number: {st.session_state.fortune_number}<br>
|
|
|
178 |
"""
|
179 |
st.markdown(summary, unsafe_allow_html=True)
|
180 |
|
|
|
181 |
st.text_area("Description", value=description_text, height=150, disabled=True)
|
|
|
|
|
182 |
st.text_area("Detail", value=detail_text, height=150, disabled=True)
|