tonyhui2234 commited on
Commit
cb43061
·
verified ·
1 Parent(s): 66425bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -17
app.py CHANGED
@@ -4,19 +4,15 @@ import pandas as pd
4
  import requests
5
  from io import BytesIO
6
  from PIL import Image
7
-
8
- import os
9
- print("Current Working Directory:", os.getcwd())
10
- print("Files in the directory:", os.listdir('.'))
11
-
12
 
13
  # Define maximum dimensions for the fortune image (in pixels)
14
  MAX_SIZE = (400, 400)
15
 
16
  # Set page configuration
17
- st.set_page_config(page_title="Fortune Stick Enquiry", layout="wide")
18
 
19
- st.title("Fortune Stick Enquiry")
20
 
21
  # Initialize session state variables
22
  if "submitted_text" not in st.session_state:
@@ -25,9 +21,10 @@ if "fortune_number" not in st.session_state:
25
  st.session_state.fortune_number = None
26
  if "fortune_row" not in st.session_state:
27
  st.session_state.fortune_row = None
 
28
  if "fortune_data" not in st.session_state:
29
  try:
30
- st.session_state.fortune_data = pd.read_csv(r"/home/user/app/detail.csv")
31
  except Exception as e:
32
  st.error(f"Error loading CSV: {e}")
33
  st.session_state.fortune_data = None
@@ -35,9 +32,34 @@ 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
 
39
- # Callback function for the submit button:
40
  def submit_text_callback():
 
 
 
 
 
 
 
 
 
 
 
41
  st.session_state.submitted_text = True
42
  # Randomly generate a number from 1 to 100
43
  st.session_state.fortune_number = random.randint(1, 100)
@@ -99,13 +121,12 @@ with left_col:
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):
@@ -114,7 +135,6 @@ with left_col:
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 ----
@@ -132,17 +152,17 @@ with right_col:
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
 
 
4
  import requests
5
  from io import BytesIO
6
  from PIL import Image
7
+ from transformers import pipeline
 
 
 
 
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:
 
21
  st.session_state.fortune_number = None
22
  if "fortune_row" not in st.session_state:
23
  st.session_state.fortune_row = None
24
+
25
  if "fortune_data" not in st.session_state:
26
  try:
27
+ st.session_state.fortune_data = pd.read_csv("detail.csv")
28
  except Exception as e:
29
  st.error(f"Error loading CSV: {e}")
30
  st.session_state.fortune_data = None
 
32
  if "stick_clicked" not in st.session_state:
33
  st.session_state.stick_clicked = False
34
 
35
+ # Update the check functions to use the "question" parameter properly.
36
+ def check_sentence_is_english_model(question):
37
+ pipe = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
38
+ # If the model predicts the label "en", we consider it English.
39
+ if pipe(question)[0]['label'] == 'en':
40
+ return True
41
+ return False
42
+
43
+ def check_sentence_is_question_model(question):
44
+ pipe = pipeline("text-classification", model="shahrukhx01/question-vs-statement-classifier")
45
+ # If the model predicts "LABEL_1", we consider it a question.
46
+ if pipe(question)[0]['label'] == 'LABEL_1':
47
+ return True
48
+ return False
49
 
50
+ # Callback function for the submit button (no parameters now)
51
  def submit_text_callback():
52
+ # Get the user input from session state
53
+ question = st.session_state.get("user_sentence", "")
54
+
55
+ if not check_sentence_is_english_model(question):
56
+ st.error("Please enter in English")
57
+ return
58
+
59
+ if not check_sentence_is_question_model(question):
60
+ st.error("This is not a question. Please enter another question")
61
+ return
62
+
63
  st.session_state.submitted_text = True
64
  # Randomly generate a number from 1 to 100
65
  st.session_state.fortune_number = random.randint(1, 100)
 
121
  left_bottom = st.container()
122
  # Top container: Input area and submit button
123
  with left_top:
124
+ # Text area for user input (saved into session_state with key "user_sentence")
125
  user_sentence = st.text_area("Enter your question in English", key="user_sentence", height=150)
126
+ # The callback no longer requires a parameter; it accesses st.session_state directly.
127
  st.button("submit", key="submit_button", on_click=submit_text_callback)
128
+ # Left Bottom: Centered "Cfu Explain" button
 
129
  if st.session_state.submitted_text:
 
130
  with left_bottom:
131
  # Add vertical spacing to approximately center the button vertically
132
  for _ in range(5):
 
135
  with col2:
136
  st.button("Cfu Explain", key="stick_button", on_click=stick_enquiry_callback)
137
  if st.session_state.stick_clicked:
 
138
  st.text_area(' ', value="Here are the stick enquiry words...", height=300, disabled=True)
139
 
140
  # ---- Right Column ----
 
152
  st.image(img_from_url, use_container_width=False)
153
  else:
154
  # Fallback: display a default image if download fails
155
+ img = load_and_resize_image("error.png")
156
  if img:
157
  st.image(img, use_container_width=False)
158
  else:
159
  # Fallback: display a default image if no header link is provided
160
+ img = load_and_resize_image("error.png")
161
  if img:
162
  st.image(img, use_container_width=False)
163
  else:
164
  # Before submit, show the default image
165
+ img = load_and_resize_image("fortune.png")
166
  if img:
167
  st.image(img, caption="Your Fortune", use_container_width=False)
168