tonyhui2234 commited on
Commit
fed092d
·
verified ·
1 Parent(s): 6398408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -135
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import streamlit as st
2
- import time
3
  import random
4
  import pandas as pd
5
  import requests
@@ -14,153 +13,153 @@ st.set_page_config(page_title="Fortuen Stick Enquiry", layout="wide")
14
 
15
  st.title("Fortuen Stick Enquiry")
16
 
17
- # Initialize session state variables
18
- if "submitted_text" not in st.session_state:
19
- st.session_state.submitted_text = False
20
- if "fortune_number" not in st.session_state:
21
- st.session_state.fortune_number = None
22
- if "fortune_row" not in st.session_state:
23
- st.session_state.fortune_row = None
24
- if "fortune_data" not in st.session_state:
25
- try:
26
- st.session_state.fortune_data = pd.read_csv("detail.csv")
27
- except Exception as e:
28
- st.error(f"Error loading CSV: {e}")
29
- st.session_state.fortune_data = None
30
 
31
- if "stick_clicked" not in st.session_state:
32
- st.session_state.stick_clicked = False
33
 
34
 
35
- # Callback function for the submit button:
36
- def submit_text_callback():
37
- st.session_state.submitted_text = True
38
- # Randomly generate a number from 1 to 100
39
- st.session_state.fortune_number = random.randint(1, 100)
40
 
41
- # Look up the row in the CSV where CNumber matches the generated fortune number.
42
- df = st.session_state.fortune_data
43
- if df is not None:
44
- matching_row = df[df['CNumber'] == st.session_state.fortune_number]
45
- if not matching_row.empty:
46
- row = matching_row.iloc[0]
47
- st.session_state.fortune_row = {
48
- "Header": row.get("Header", "N/A"),
49
- "Luck": row.get("Luck", "N/A"),
50
- "Description": row.get("Description", "No description available."),
51
- "Detail": row.get("Detail", "No detail available."),
52
- "HeaderLink": row.get("link", None) # URL to the image
53
- }
54
- else:
55
- st.session_state.fortune_row = {
56
- "Header": "N/A",
57
- "Luck": "N/A",
58
- "Description": "No description available.",
59
- "Detail": "No detail available.",
60
- "HeaderLink": None
61
- }
62
 
63
- # Function to load and resize local images using Pillow
64
- def load_and_resize_image(path, max_size=MAX_SIZE):
65
- try:
66
- img = Image.open(path)
67
- img.thumbnail(max_size, Image.Resampling.LANCZOS)
68
- return img
69
- except Exception as e:
70
- st.error(f"Error loading image: {e}")
71
- return None
72
 
73
- # Function to download image from URL and resize it
74
- def download_and_resize_image(url, max_size=MAX_SIZE):
75
- try:
76
- response = requests.get(url)
77
- response.raise_for_status()
78
- image_bytes = BytesIO(response.content)
79
- img = Image.open(image_bytes)
80
- img.thumbnail(max_size, Image.Resampling.LANCZOS)
81
- return img
82
- except Exception as e:
83
- st.error(f"Error loading image from URL: {e}")
84
- return None
85
 
86
- def stick_enquiry_callback():
87
- st.session_state.stick_clicked = True
88
 
89
- # Main layout: Left (input) and Right (fortune display)
90
- left_col, _, right_col = st.columns([3, 1, 5])
91
 
92
- # ---- Left Column ----
93
- with left_col:
94
- left_top = st.container()
95
- left_bottom = st.container()
96
- # Top container: Input area and submit button
97
- with left_top:
98
- user_sentence = st.text_area("Enter your question in English", key="user_sentence", height=150)
99
 
100
- st.button("submit", key="submit_button", on_click=submit_text_callback)
101
- # (The previous Stick Enquiry button has been removed)
102
 
103
- if st.session_state.submitted_text:
104
- # Left Bottom: Centered "解籤/Stick Enquiry" button
105
- with left_bottom:
106
- # Add vertical spacing to approximately center the button vertically
107
- for _ in range(5):
108
- st.write("")
109
- col1, col2, col3 = st.columns(3)
110
- with col2:
111
- st.button("Cfu Explain", key="stick_button", on_click=stick_enquiry_callback)
112
- if st.session_state.stick_clicked:
113
- # st.write("Here are the stick enquiry words...") # Placeholder text
114
- st.text_area(' ', value="Here are the stick enquiry words...", height=300, disabled=True)
115
 
116
- # ---- Right Column ----
117
- with right_col:
118
- # Top container: Fortune image area
119
- with st.container():
120
- col_left, col_center, col_right = st.columns([1, 2, 1])
121
- with col_center:
122
- if st.session_state.submitted_text and st.session_state.fortune_row:
123
- header_link = st.session_state.fortune_row.get("HeaderLink")
124
- if header_link:
125
- # Download the image from the URL
126
- img_from_url = download_and_resize_image(header_link)
127
- if img_from_url:
128
- st.image(img_from_url, use_container_width=False)
129
- else:
130
- # Fallback: display a default image if download fails
131
- img = load_and_resize_image("error.png")
132
- if img:
133
- st.image(img, use_container_width=False)
134
- else:
135
- # Fallback: display a default image if no header link is provided
136
- img = load_and_resize_image("error.png")
137
- if img:
138
- st.image(img, use_container_width=False)
139
- else:
140
- # Before submit, show the default image
141
- img = load_and_resize_image("fortune.png")
142
- if img:
143
- st.image(img, caption="Your Fortune", use_container_width=False)
144
 
145
- # Bottom container: Display fortune details using text areas
146
- with st.container():
147
- if st.session_state.fortune_row:
148
- header_text = st.session_state.fortune_row.get("Header", "N/A")
149
- luck_text = st.session_state.fortune_row.get("Luck", "N/A")
150
- description_text = st.session_state.fortune_row.get("Description", "No description available.")
151
- detail_text = st.session_state.fortune_row.get("Detail", "No detail available.")
152
 
153
- # Create a summary with larger text using HTML styling
154
- summary = f"""
155
- <div style="font-size: 28px; font-weight: bold;">
156
- Fortune stick number: {st.session_state.fortune_number}<br>
157
- Luck: {luck_text}
158
- </div>
159
- """
160
- st.markdown(summary, unsafe_allow_html=True)
161
 
162
- # Second text area: Description
163
- st.text_area("Description", value=description_text, height=150, disabled=True)
164
 
165
- # Third text area: Detail
166
- st.text_area("Detail", value=detail_text, height=150, disabled=True)
 
1
  import streamlit as st
 
2
  import random
3
  import pandas as pd
4
  import requests
 
13
 
14
  st.title("Fortuen Stick Enquiry")
15
 
16
+ # # Initialize session state variables
17
+ # if "submitted_text" not in st.session_state:
18
+ # st.session_state.submitted_text = False
19
+ # if "fortune_number" not in st.session_state:
20
+ # st.session_state.fortune_number = None
21
+ # if "fortune_row" not in st.session_state:
22
+ # st.session_state.fortune_row = None
23
+ # if "fortune_data" not in st.session_state:
24
+ # try:
25
+ # st.session_state.fortune_data = pd.read_csv("detail.csv")
26
+ # except Exception as e:
27
+ # st.error(f"Error loading CSV: {e}")
28
+ # st.session_state.fortune_data = None
29
 
30
+ # if "stick_clicked" not in st.session_state:
31
+ # st.session_state.stick_clicked = False
32
 
33
 
34
+ # # Callback function for the submit button:
35
+ # def submit_text_callback():
36
+ # st.session_state.submitted_text = True
37
+ # # Randomly generate a number from 1 to 100
38
+ # st.session_state.fortune_number = random.randint(1, 100)
39
 
40
+ # # Look up the row in the CSV where CNumber matches the generated fortune number.
41
+ # df = st.session_state.fortune_data
42
+ # if df is not None:
43
+ # matching_row = df[df['CNumber'] == st.session_state.fortune_number]
44
+ # if not matching_row.empty:
45
+ # row = matching_row.iloc[0]
46
+ # st.session_state.fortune_row = {
47
+ # "Header": row.get("Header", "N/A"),
48
+ # "Luck": row.get("Luck", "N/A"),
49
+ # "Description": row.get("Description", "No description available."),
50
+ # "Detail": row.get("Detail", "No detail available."),
51
+ # "HeaderLink": row.get("link", None) # URL to the image
52
+ # }
53
+ # else:
54
+ # st.session_state.fortune_row = {
55
+ # "Header": "N/A",
56
+ # "Luck": "N/A",
57
+ # "Description": "No description available.",
58
+ # "Detail": "No detail available.",
59
+ # "HeaderLink": None
60
+ # }
61
 
62
+ # # Function to load and resize local images using Pillow
63
+ # def load_and_resize_image(path, max_size=MAX_SIZE):
64
+ # try:
65
+ # img = Image.open(path)
66
+ # img.thumbnail(max_size, Image.Resampling.LANCZOS)
67
+ # return img
68
+ # except Exception as e:
69
+ # st.error(f"Error loading image: {e}")
70
+ # return None
71
 
72
+ # # Function to download image from URL and resize it
73
+ # def download_and_resize_image(url, max_size=MAX_SIZE):
74
+ # try:
75
+ # response = requests.get(url)
76
+ # response.raise_for_status()
77
+ # image_bytes = BytesIO(response.content)
78
+ # img = Image.open(image_bytes)
79
+ # img.thumbnail(max_size, Image.Resampling.LANCZOS)
80
+ # return img
81
+ # except Exception as e:
82
+ # st.error(f"Error loading image from URL: {e}")
83
+ # return None
84
 
85
+ # def stick_enquiry_callback():
86
+ # st.session_state.stick_clicked = True
87
 
88
+ # # Main layout: Left (input) and Right (fortune display)
89
+ # left_col, _, right_col = st.columns([3, 1, 5])
90
 
91
+ # # ---- Left Column ----
92
+ # with left_col:
93
+ # left_top = st.container()
94
+ # left_bottom = st.container()
95
+ # # Top container: Input area and submit button
96
+ # with left_top:
97
+ # user_sentence = st.text_area("Enter your question in English", key="user_sentence", height=150)
98
 
99
+ # st.button("submit", key="submit_button", on_click=submit_text_callback)
100
+ # # (The previous Stick Enquiry button has been removed)
101
 
102
+ # if st.session_state.submitted_text:
103
+ # # Left Bottom: Centered "解籤/Stick Enquiry" button
104
+ # with left_bottom:
105
+ # # Add vertical spacing to approximately center the button vertically
106
+ # for _ in range(5):
107
+ # st.write("")
108
+ # col1, col2, col3 = st.columns(3)
109
+ # with col2:
110
+ # st.button("Cfu Explain", key="stick_button", on_click=stick_enquiry_callback)
111
+ # if st.session_state.stick_clicked:
112
+ # # st.write("Here are the stick enquiry words...") # Placeholder text
113
+ # st.text_area(' ', value="Here are the stick enquiry words...", height=300, disabled=True)
114
 
115
+ # # ---- Right Column ----
116
+ # with right_col:
117
+ # # Top container: Fortune image area
118
+ # with st.container():
119
+ # col_left, col_center, col_right = st.columns([1, 2, 1])
120
+ # with col_center:
121
+ # if st.session_state.submitted_text and st.session_state.fortune_row:
122
+ # header_link = st.session_state.fortune_row.get("HeaderLink")
123
+ # if header_link:
124
+ # # Download the image from the URL
125
+ # img_from_url = download_and_resize_image(header_link)
126
+ # if img_from_url:
127
+ # st.image(img_from_url, use_container_width=False)
128
+ # else:
129
+ # # Fallback: display a default image if download fails
130
+ # img = load_and_resize_image("error.png")
131
+ # if img:
132
+ # st.image(img, use_container_width=False)
133
+ # else:
134
+ # # Fallback: display a default image if no header link is provided
135
+ # img = load_and_resize_image("error.png")
136
+ # if img:
137
+ # st.image(img, use_container_width=False)
138
+ # else:
139
+ # # Before submit, show the default image
140
+ # img = load_and_resize_image("fortune.png")
141
+ # if img:
142
+ # st.image(img, caption="Your Fortune", use_container_width=False)
143
 
144
+ # # Bottom container: Display fortune details using text areas
145
+ # with st.container():
146
+ # if st.session_state.fortune_row:
147
+ # header_text = st.session_state.fortune_row.get("Header", "N/A")
148
+ # luck_text = st.session_state.fortune_row.get("Luck", "N/A")
149
+ # description_text = st.session_state.fortune_row.get("Description", "No description available.")
150
+ # detail_text = st.session_state.fortune_row.get("Detail", "No detail available.")
151
 
152
+ # # Create a summary with larger text using HTML styling
153
+ # summary = f"""
154
+ # <div style="font-size: 28px; font-weight: bold;">
155
+ # Fortune stick number: {st.session_state.fortune_number}<br>
156
+ # Luck: {luck_text}
157
+ # </div>
158
+ # """
159
+ # st.markdown(summary, unsafe_allow_html=True)
160
 
161
+ # # Second text area: Description
162
+ # st.text_area("Description", value=description_text, height=150, disabled=True)
163
 
164
+ # # Third text area: Detail
165
+ # st.text_area("Detail", value=detail_text, height=150, disabled=True)