Upload index.py
Browse files
index.py
CHANGED
@@ -5,17 +5,9 @@ import time
|
|
5 |
import random
|
6 |
import os
|
7 |
|
8 |
-
|
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"
|
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 |
-
|
80 |
-
|
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 |
-
|
113 |
-
st.
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
st.markdown(intro_content)
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
st.
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
if st.
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
if is_address_question:
|
163 |
-
st.session_state.skip_address = st.checkbox("Skip this question", value=st.session_state.skip_address
|
164 |
elif is_phone_question:
|
165 |
-
st.session_state.skip_phone = st.checkbox("Skip this question", value=st.session_state.skip_phone
|
166 |
-
|
|
|
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="
|
171 |
if (submitButton && !submitButton.disabled) submitButton.click();
|
172 |
}
|
173 |
});
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
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...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|