Entz commited on
Commit
2d30ab3
·
verified ·
1 Parent(s): 7c417b8

Upload index.py

Browse files
Files changed (1) hide show
  1. index.py +69 -51
index.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- os.environ["HOME"] = "/tmp" # <--- Fix for HuggingFace/permissions
3
 
4
  import streamlit as st
5
  import requests
@@ -16,6 +16,15 @@ TRANSITION_MESSAGES = [
16
  "Onward to question {number}!"
17
  ]
18
 
 
 
 
 
 
 
 
 
 
19
  def reset_session_state():
20
  keys = [
21
  "session_id", "current_question", "answer", "feedback", "summary",
@@ -123,6 +132,7 @@ def edit_field(field, value):
123
  print(f"Error editing field: {e}")
124
  st.error(f"Error editing field: {e}")
125
 
 
126
  if "session_id" not in st.session_state:
127
  st.session_state.session_id = None
128
  st.session_state.current_question = ""
@@ -140,55 +150,63 @@ if st.session_state.session_id is None:
140
  st.title("AI-Powered Registration System")
141
  st.markdown("*** If 403 or other connection errors, please refresh the page every 1 minute, because the backend server is being spun up. Developed by [email protected]**")
142
 
143
- if st.session_state.summary:
144
- st.success("Registration Complete!")
145
- st.subheader("Summary")
146
- for key, value in st.session_state.summary.items():
147
- st.write(f"**{key}**: {value}")
148
- new_value = st.text_input(f"Edit {key}", key=f"edit_{key}")
149
- if st.button(f"Update {key}", key=f"update_{key}"):
150
- edit_field(key, new_value)
151
- col1, col2 = st.columns(2)
152
- with col1:
153
- if st.button("Next Registration", key="next_reg"):
154
- reset_session_state()
155
- start_registration()
156
- st.rerun()
157
- with col2:
158
- if st.button("End Session", key="end_sess"):
159
- reset_session_state()
160
- st.success("Session ended. You may close the tab.")
161
- else:
162
- if st.session_state.current_question:
163
- if st.session_state.feedback:
164
- st.error(st.session_state.feedback)
165
- if st.session_state.prev_question and st.session_state.prev_question != st.session_state.current_question:
166
- transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
167
- st.info(transition_msg)
168
- time.sleep(1)
169
- st.subheader(f"Question {st.session_state.question_number}: {st.session_state.current_question}")
170
-
171
- is_address_question = st.session_state.current_question == "What is your address?"
172
- is_phone_question = st.session_state.current_question == "What is your phone number?"
173
-
174
- if is_address_question or is_phone_question:
175
- st.info(
176
- f"This question is optional. Check the box to skip, or enter your information below.\n"
177
- f"- For address: Include house number (e.g., 123), street name (e.g., High Street), town/city (e.g., London), and postcode (e.g., SW1A 1AA). Example: 123 High Street, London, SW1A 1AA.\n"
178
- f"- For phone: Use 10 digits for landlines (e.g., 020 123 4567) or 11 digits for mobiles starting with 07 (e.g., 07700 900 123). Do not use +44 or other region numbers."
179
- )
180
- if is_address_question:
181
- st.session_state.skip_address = st.checkbox("Skip this question", value=st.session_state.skip_address)
182
- elif is_phone_question:
183
- st.session_state.skip_phone = st.checkbox("Skip this question", value=st.session_state.skip_phone)
184
-
185
- st.session_state.answer = st.text_input(
186
- "Your Answer",
187
- value=st.session_state.answer,
188
- key=f"answer_input_{st.session_state.question_number}"
189
- )
190
 
191
- if st.button("Submit", key="submit_button"):
192
- submit_response()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  else:
194
- st.info("Initializing session, please wait...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ os.environ["HOME"] = "/tmp" # Fix for permission issues on platforms like HuggingFace
3
 
4
  import streamlit as st
5
  import requests
 
16
  "Onward to question {number}!"
17
  ]
18
 
19
+ # Utility to read intro content from file
20
+ def read_intro_file(filepath="tab1.txt"):
21
+ try:
22
+ with open(filepath, "r", encoding="utf-8") as f:
23
+ return f.read()
24
+ except FileNotFoundError:
25
+ return "Intro file not found."
26
+
27
+ # Reset session state keys
28
  def reset_session_state():
29
  keys = [
30
  "session_id", "current_question", "answer", "feedback", "summary",
 
132
  print(f"Error editing field: {e}")
133
  st.error(f"Error editing field: {e}")
134
 
135
+ # Initialize session state if not present
136
  if "session_id" not in st.session_state:
137
  st.session_state.session_id = None
138
  st.session_state.current_question = ""
 
150
  st.title("AI-Powered Registration System")
151
  st.markdown("*** If 403 or other connection errors, please refresh the page every 1 minute, because the backend server is being spun up. Developed by [email protected]**")
152
 
153
+ # Create two tabs
154
+ tab1, tab2 = st.tabs(["Introduction", "Registration"])
155
+
156
+ with tab1:
157
+ intro_text = read_intro_file()
158
+ st.markdown(intro_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
+ with tab2:
161
+ if st.session_state.summary:
162
+ st.success("Registration Complete!")
163
+ st.subheader("Summary")
164
+ for key, value in st.session_state.summary.items():
165
+ st.write(f"**{key}**: {value}")
166
+ new_value = st.text_input(f"Edit {key}", key=f"edit_{key}")
167
+ if st.button(f"Update {key}", key=f"update_{key}"):
168
+ edit_field(key, new_value)
169
+ col1, col2 = st.columns(2)
170
+ with col1:
171
+ if st.button("Next Registration", key="next_reg"):
172
+ reset_session_state()
173
+ start_registration()
174
+ st.rerun()
175
+ with col2:
176
+ if st.button("End Session", key="end_sess"):
177
+ reset_session_state()
178
+ st.success("Session ended. You may close the tab.")
179
  else:
180
+ if st.session_state.current_question:
181
+ if st.session_state.feedback:
182
+ st.error(st.session_state.feedback)
183
+ if st.session_state.prev_question and st.session_state.prev_question != st.session_state.current_question:
184
+ transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
185
+ st.info(transition_msg)
186
+ time.sleep(1)
187
+ st.subheader(f"Question {st.session_state.question_number}: {st.session_state.current_question}")
188
+
189
+ is_address_question = st.session_state.current_question == "What is your address?"
190
+ is_phone_question = st.session_state.current_question == "What is your phone number?"
191
+
192
+ if is_address_question or is_phone_question:
193
+ st.info(
194
+ f"This question is optional. Check the box to skip, or enter your information below.\n"
195
+ f"- For address: Include house number (e.g., 123), street name (e.g., High Street), town/city (e.g., London), and postcode (e.g., SW1A 1AA). Example: 123 High Street, London, SW1A 1AA.\n"
196
+ f"- For phone: Use 10 digits for landlines (e.g., 020 123 4567) or 11 digits for mobiles starting with 07 (e.g., 07700 900 123). Do not use +44 or other region numbers."
197
+ )
198
+ if is_address_question:
199
+ st.session_state.skip_address = st.checkbox("Skip this question", value=st.session_state.skip_address)
200
+ elif is_phone_question:
201
+ st.session_state.skip_phone = st.checkbox("Skip this question", value=st.session_state.skip_phone)
202
+
203
+ st.session_state.answer = st.text_input(
204
+ "Your Answer",
205
+ value=st.session_state.answer,
206
+ key=f"answer_input_{st.session_state.question_number}"
207
+ )
208
+
209
+ if st.button("Submit", key="submit_button"):
210
+ submit_response()
211
+ else:
212
+ st.info("Initializing session, please wait...")