Update app.py
Browse files
app.py
CHANGED
@@ -64,107 +64,107 @@ def submit_text_callback():
|
|
64 |
"HeaderLink": None
|
65 |
}
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
#
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
|
90 |
-
|
91 |
-
|
92 |
|
93 |
-
#
|
94 |
-
|
95 |
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
#
|
101 |
-
|
102 |
-
|
103 |
|
104 |
-
|
105 |
-
#
|
106 |
|
107 |
-
|
108 |
-
#
|
109 |
-
|
110 |
-
#
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
#
|
118 |
-
|
119 |
|
120 |
-
#
|
121 |
-
|
122 |
-
#
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
#
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
#
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
#
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
#
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
|
149 |
-
#
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
|
157 |
-
#
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
|
166 |
-
#
|
167 |
-
|
168 |
|
169 |
-
#
|
170 |
-
|
|
|
64 |
"HeaderLink": None
|
65 |
}
|
66 |
|
67 |
+
# Function to load and resize local images using Pillow
|
68 |
+
def load_and_resize_image(path, max_size=MAX_SIZE):
|
69 |
+
try:
|
70 |
+
img = Image.open(path)
|
71 |
+
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
72 |
+
return img
|
73 |
+
except Exception as e:
|
74 |
+
st.error(f"Error loading image: {e}")
|
75 |
+
return None
|
76 |
|
77 |
+
# Function to download image from URL and resize it
|
78 |
+
def download_and_resize_image(url, max_size=MAX_SIZE):
|
79 |
+
try:
|
80 |
+
response = requests.get(url)
|
81 |
+
response.raise_for_status()
|
82 |
+
image_bytes = BytesIO(response.content)
|
83 |
+
img = Image.open(image_bytes)
|
84 |
+
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
85 |
+
return img
|
86 |
+
except Exception as e:
|
87 |
+
st.error(f"Error loading image from URL: {e}")
|
88 |
+
return None
|
89 |
|
90 |
+
def stick_enquiry_callback():
|
91 |
+
st.session_state.stick_clicked = True
|
92 |
|
93 |
+
# Main layout: Left (input) and Right (fortune display)
|
94 |
+
left_col, _, right_col = st.columns([3, 1, 5])
|
95 |
|
96 |
+
# ---- Left Column ----
|
97 |
+
with left_col:
|
98 |
+
left_top = st.container()
|
99 |
+
left_bottom = st.container()
|
100 |
+
# Top container: Input area and submit button
|
101 |
+
with left_top:
|
102 |
+
user_sentence = st.text_area("Enter your question in English", key="user_sentence", height=150)
|
103 |
|
104 |
+
st.button("submit", key="submit_button", on_click=submit_text_callback)
|
105 |
+
# (The previous Stick Enquiry button has been removed)
|
106 |
|
107 |
+
if st.session_state.submitted_text:
|
108 |
+
# Left Bottom: Centered "解籤/Stick Enquiry" button
|
109 |
+
with left_bottom:
|
110 |
+
# Add vertical spacing to approximately center the button vertically
|
111 |
+
for _ in range(5):
|
112 |
+
st.write("")
|
113 |
+
col1, col2, col3 = st.columns(3)
|
114 |
+
with col2:
|
115 |
+
st.button("Cfu Explain", key="stick_button", on_click=stick_enquiry_callback)
|
116 |
+
if st.session_state.stick_clicked:
|
117 |
+
# st.write("Here are the stick enquiry words...") # Placeholder text
|
118 |
+
st.text_area(' ', value="Here are the stick enquiry words...", height=300, disabled=True)
|
119 |
|
120 |
+
# ---- Right Column ----
|
121 |
+
with right_col:
|
122 |
+
# Top container: Fortune image area
|
123 |
+
with st.container():
|
124 |
+
col_left, col_center, col_right = st.columns([1, 2, 1])
|
125 |
+
with col_center:
|
126 |
+
if st.session_state.submitted_text and st.session_state.fortune_row:
|
127 |
+
header_link = st.session_state.fortune_row.get("HeaderLink")
|
128 |
+
if header_link:
|
129 |
+
# Download the image from the URL
|
130 |
+
img_from_url = download_and_resize_image(header_link)
|
131 |
+
if img_from_url:
|
132 |
+
st.image(img_from_url, use_container_width=False)
|
133 |
+
else:
|
134 |
+
# Fallback: display a default image if download fails
|
135 |
+
img = load_and_resize_image(r"/home/user/app/fortune/error.png")
|
136 |
+
if img:
|
137 |
+
st.image(img, use_container_width=False)
|
138 |
+
else:
|
139 |
+
# Fallback: display a default image if no header link is provided
|
140 |
+
img = load_and_resize_image(r"/home/user/app/fortune/error.png")
|
141 |
+
if img:
|
142 |
+
st.image(img, use_container_width=False)
|
143 |
+
else:
|
144 |
+
# Before submit, show the default image
|
145 |
+
img = load_and_resize_image(r"/home/user/app/fortune.png")
|
146 |
+
if img:
|
147 |
+
st.image(img, caption="Your Fortune", use_container_width=False)
|
148 |
|
149 |
+
# Bottom container: Display fortune details using text areas
|
150 |
+
with st.container():
|
151 |
+
if st.session_state.fortune_row:
|
152 |
+
header_text = st.session_state.fortune_row.get("Header", "N/A")
|
153 |
+
luck_text = st.session_state.fortune_row.get("Luck", "N/A")
|
154 |
+
description_text = st.session_state.fortune_row.get("Description", "No description available.")
|
155 |
+
detail_text = st.session_state.fortune_row.get("Detail", "No detail available.")
|
156 |
|
157 |
+
# Create a summary with larger text using HTML styling
|
158 |
+
summary = f"""
|
159 |
+
<div style="font-size: 28px; font-weight: bold;">
|
160 |
+
Fortune stick number: {st.session_state.fortune_number}<br>
|
161 |
+
Luck: {luck_text}
|
162 |
+
</div>
|
163 |
+
"""
|
164 |
+
st.markdown(summary, unsafe_allow_html=True)
|
165 |
|
166 |
+
# Second text area: Description
|
167 |
+
st.text_area("Description", value=description_text, height=150, disabled=True)
|
168 |
|
169 |
+
# Third text area: Detail
|
170 |
+
st.text_area("Detail", value=detail_text, height=150, disabled=True)
|