Entz commited on
Commit
020375c
·
verified ·
1 Parent(s): 990935d

Upload index.py

Browse files
Files changed (1) hide show
  1. index.py +85 -86
index.py CHANGED
@@ -5,17 +5,9 @@ import time
5
  import random
6
  import os
7
 
8
- # Load environment variables
9
  API_URL = os.getenv("API_URL")
10
 
11
- # Read content from text files
12
- def read_content_from_file(file_path):
13
- try:
14
- with open(file_path, 'r') as file:
15
- return file.read()
16
- except FileNotFoundError:
17
- return f"Error: {file_path} not found."
18
-
19
  TRANSITION_MESSAGES = [
20
  "Now we are moving onto question {number}!",
21
  "Time to switch to question {number}!",
@@ -27,7 +19,7 @@ TRANSITION_MESSAGES = [
27
  def start_registration():
28
  print("Starting registration...")
29
  try:
30
- response = requests.post(f"{API_URL}/start_registration", timeout=1)
31
  response.raise_for_status()
32
  data = response.json()
33
  print("API Response:", data)
@@ -76,8 +68,8 @@ def submit_response():
76
  if st.session_state.prev_question != st.session_state.current_question:
77
  st.session_state.question_number += 1
78
  st.session_state.answer = ""
79
- st.session_state.skip_address = False
80
- st.session_state.skip_phone = False
81
  st.rerun()
82
  except requests.RequestException as e:
83
  print(f"Error submitting response: {e}")
@@ -109,86 +101,93 @@ def edit_field(field, value):
109
  print(f"Error editing field: {e}")
110
  st.error(f"Error editing field: {e}")
111
 
112
- def main():
113
- st.title("AI-Powered Registration System")
114
- # Create two tabs
115
- tab1, tab2 = st.tabs(["Introduction", "Registration"])
116
- # Tab 1: Introduction
117
- with tab1:
118
- # st.subheader("About This Application")
119
- intro_content = read_content_from_file("tab1.txt")
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  st.markdown(intro_content)
121
- # Tab 2: Registration Application
122
- with tab2:
123
- registration_content = read_content_from_file("tab2.txt")
124
- st.markdown(registration_content)
125
- st.markdown("**Developed by [email protected]**")
126
- if st.session_state.get("summary"):
127
- st.success("Registration Complete!")
128
- st.subheader("Summary")
129
- for key, value in st.session_state.summary.items():
130
- st.write(f"**{key}**: {value}")
131
- new_value = st.text_input(f"Edit {key}", key=f"edit_{key}")
132
- if st.button(f"Update {key}", key=f"update_{key}"):
133
- edit_field(key, new_value)
134
- col1, col2 = st.columns(2)
135
- with col1:
136
- if st.button("Next Registration", key="next_reg"):
137
- start_registration()
138
- with col2:
139
- if st.button("End Session", key="end_sess"):
140
- # Custom logic for ending session can be added here
141
- st.session_state.session_id = None
142
- st.session_state.summary = None
143
- st.session_state.current_question = ""
144
- st.rerun()
145
- else:
146
- if st.session_state.get("current_question"):
147
- if st.session_state.get("feedback"):
148
- st.error(st.session_state.feedback)
149
- if st.session_state.get("prev_question") and st.session_state.prev_question != st.session_state.current_question:
150
- transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
151
- st.info(transition_msg)
152
- time.sleep(1)
153
- st.subheader(f"Question {st.session_state.question_number}: {st.session_state.current_question}")
154
- is_address_question = st.session_state.current_question == "What is your address?"
155
- is_phone_question = st.session_state.current_question == "What is your phone number?"
156
- if is_address_question or is_phone_question:
157
- st.info(
158
- f"This question is optional. Check the box to skip, or enter your information below.\n"
159
- 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"
160
- 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). Avoid +44 formats."
161
- )
 
 
 
 
 
 
162
  if is_address_question:
163
- st.session_state.skip_address = st.checkbox("Skip this question", value=st.session_state.skip_address, key=f"skip_addr_{st.session_state.question_number}")
164
  elif is_phone_question:
165
- st.session_state.skip_phone = st.checkbox("Skip this question", value=st.session_state.skip_phone, key=f"skip_phone_{st.session_state.question_number}")
166
- st.components.v1.html("""
 
167
  document.addEventListener('keypress', function(e) {
168
  if (e.key === 'Enter' && document.activeElement.tagName === 'INPUT') {
169
  e.preventDefault();
170
- const submitButton = document.querySelector('button[key="submit_button_' + window.st.session_state.question_number + '"]');
171
  if (submitButton && !submitButton.disabled) submitButton.click();
172
  }
173
  });
174
- """, height=0)
175
- st.session_state.answer = st.text_input("Your Answer", value="", key=f"answer_input_{st.session_state.question_number}")
176
- if st.button("Submit", key=f"submit_button_{st.session_state.question_number}"):
177
- submit_response()
178
- else:
179
- print("Waiting for session to initialize...")
180
-
181
- if __name__ == "__main__":
182
- # Initialize session state (correctly)
183
- if "session_id" not in st.session_state:
184
- st.session_state.session_id = None
185
- st.session_state.current_question = ""
186
- st.session_state.answer = ""
187
- st.session_state.feedback = ""
188
- st.session_state.summary = None
189
- st.session_state.skip_address = False
190
- st.session_state.skip_phone = False
191
- st.session_state.prev_question = ""
192
- st.session_state.question_number = 1
193
- start_registration()
194
- main()
 
5
  import random
6
  import os
7
 
8
+ load_dotenv()
9
  API_URL = os.getenv("API_URL")
10
 
 
 
 
 
 
 
 
 
11
  TRANSITION_MESSAGES = [
12
  "Now we are moving onto question {number}!",
13
  "Time to switch to question {number}!",
 
19
  def start_registration():
20
  print("Starting registration...")
21
  try:
22
+ response = requests.post(f"{API_URL}/start_registration")
23
  response.raise_for_status()
24
  data = response.json()
25
  print("API Response:", data)
 
68
  if st.session_state.prev_question != st.session_state.current_question:
69
  st.session_state.question_number += 1
70
  st.session_state.answer = ""
71
+ st.session_state.skip_address = False
72
+ st.session_state.skip_phone = False
73
  st.rerun()
74
  except requests.RequestException as e:
75
  print(f"Error submitting response: {e}")
 
101
  print(f"Error editing field: {e}")
102
  st.error(f"Error editing field: {e}")
103
 
104
+ if "session_state" not in st.session_state:
105
+ st.session_state.session_state = {}
106
+ st.session_state.session_id = None
107
+ st.session_state.current_question = ""
108
+ st.session_state.answer = ""
109
+ st.session_state.feedback = ""
110
+ st.session_state.summary = None
111
+ st.session_state.skip_address = False
112
+ st.session_state.skip_phone = False
113
+ st.session_state.prev_question = ""
114
+ st.session_state.question_number = 1
115
+
116
+ # Create two tabs
117
+ tab1, tab2 = st.tabs(["Introduction", "Registration"])
118
+
119
+ # Tab 1: Introduction
120
+ with tab1:
121
+ st.header("Welcome to the AI-Powered Registration System")
122
+ try:
123
+ with open("tab1.txt", "r") as file:
124
+ intro_content = file.read()
125
  st.markdown(intro_content)
126
+ except FileNotFoundError:
127
+ st.error("Introduction file (tab1.txt) not found. Please create the file with the introduction content.")
128
+ except Exception as e:
129
+ st.error(f"Error reading introduction file: {e}")
130
+
131
+ # Tab 2: Registration App
132
+ with tab2:
133
+ if st.session_state.session_id is None:
134
+ start_registration()
135
+
136
+ st.title("AI-Powered Registration System")
137
+
138
+ if st.session_state.summary:
139
+ st.success("Registration Complete!")
140
+ st.subheader("Summary")
141
+ for key, value in st.session_state.summary.items():
142
+ st.write(f"**{key}**: {value}")
143
+ new_value = st.text_input(f"Edit {key}", key=f"edit_{key}")
144
+ if st.button(f"Update {key}", key=f"update_{key}"):
145
+ edit_field(key, new_value)
146
+ col1, col2 = st.columns(2)
147
+ with col1:
148
+ if st.button("Next Registration", key="next_reg", on_click=start_registration):
149
+ pass
150
+ with col2:
151
+ if st.button("End Session", key="end_sess", on_click=start_registration):
152
+ pass
153
+ else:
154
+ if st.session_state.current_question:
155
+ if st.session_state.feedback:
156
+ st.error(st.session_state.feedback)
157
+ if st.session_state.prev_question and st.session_state.prev_question != st.session_state.current_question:
158
+ transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
159
+ st.info(transition_msg)
160
+ time.sleep(1)
161
+ print(f"Displaying question: {st.session_state.current_question}")
162
+ st.subheader(f"Question {st.session_state.question_number}: {st.session_state.current_question}")
163
+
164
+ is_address_question = st.session_state.current_question == "What is your address?"
165
+ is_phone_question = st.session_state.current_question == "What is your phone number?"
166
+
167
+ if is_address_question or is_phone_question:
168
+ st.info(
169
+ f"This question is optional. Check the box to skip, or enter your information below.\n"
170
+ 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"
171
+ 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."
172
+ )
173
  if is_address_question:
174
+ st.session_state.skip_address = st.checkbox("Skip this question", value=st.session_state.skip_address)
175
  elif is_phone_question:
176
+ st.session_state.skip_phone = st.checkbox("Skip this question", value=st.session_state.skip_phone)
177
+
178
+ st.components.v1.html("""
179
  document.addEventListener('keypress', function(e) {
180
  if (e.key === 'Enter' && document.activeElement.tagName === 'INPUT') {
181
  e.preventDefault();
182
+ const submitButton = document.querySelector('button[key="submit_button"]');
183
  if (submitButton && !submitButton.disabled) submitButton.click();
184
  }
185
  });
186
+ """, height=0)
187
+
188
+ st.session_state.answer = st.text_input("Your Answer", value="", key=f"answer_input_{st.session_state.question_number}")
189
+
190
+ if st.button("Submit", key="submit_button"):
191
+ submit_response()
192
+ else:
193
+ print("Waiting for session to initialize...")