tonyhui2234 commited on
Commit
f492c51
·
verified ·
1 Parent(s): 03c3f46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -2
app.py CHANGED
@@ -1,4 +1,166 @@
1
- import gradio as gr
2
  import time
 
 
 
 
 
3
 
4
- print('hi')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
  import time
3
+ import random
4
+ import pandas as pd
5
+ import requests
6
+ from io import BytesIO
7
+ from PIL import Image
8
 
9
+ # Define maximum dimensions for the fortune image (in pixels)
10
+ MAX_SIZE = (400, 400)
11
+
12
+ # Set page configuration
13
+ 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)