Upload index.py
Browse files
index.py
CHANGED
@@ -5,8 +5,7 @@ import time
|
|
5 |
import random
|
6 |
import os
|
7 |
|
8 |
-
|
9 |
-
|
10 |
API_URL = os.getenv("API_URL")
|
11 |
|
12 |
TRANSITION_MESSAGES = [
|
@@ -17,14 +16,34 @@ TRANSITION_MESSAGES = [
|
|
17 |
"Onward to question {number}!"
|
18 |
]
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def start_registration():
|
21 |
-
|
22 |
try:
|
23 |
headers = {"Origin": "https://entz-council-3.hf.space"}
|
24 |
-
response = requests.post(f"{API_URL}/start_registration", headers=headers, timeout=
|
25 |
response.raise_for_status()
|
26 |
data = response.json()
|
27 |
-
print("API Response:", data)
|
28 |
st.session_state.session_id = data["session_id"]
|
29 |
st.session_state.current_question = data["message"]
|
30 |
st.session_state.feedback = ""
|
@@ -34,14 +53,10 @@ def start_registration():
|
|
34 |
st.session_state.skip_phone = False
|
35 |
st.session_state.prev_question = ""
|
36 |
st.session_state.question_number = 1
|
|
|
37 |
except requests.RequestException as e:
|
38 |
-
print(f"Error starting registration: {e}, Response: {getattr(e.response, 'text', 'No response')}")
|
39 |
st.error(f"Error starting registration: {e}")
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
def submit_response():
|
46 |
if not st.session_state.session_id:
|
47 |
st.error("No active session. Please start registration.")
|
@@ -56,12 +71,10 @@ def submit_response():
|
|
56 |
"answer": st.session_state.answer,
|
57 |
"skip_steps": skip_steps
|
58 |
}
|
59 |
-
print("Submitting response with payload:", payload)
|
60 |
try:
|
61 |
response = requests.post(f"{API_URL}/submit_response", json=payload)
|
62 |
response.raise_for_status()
|
63 |
data = response.json()
|
64 |
-
print("API Response:", data)
|
65 |
if data.get("message") == "Registration complete!":
|
66 |
st.session_state.summary = data["summary"]
|
67 |
st.session_state.current_question = ""
|
@@ -76,9 +89,8 @@ def submit_response():
|
|
76 |
st.session_state.answer = ""
|
77 |
st.session_state.skip_address = False
|
78 |
st.session_state.skip_phone = False
|
79 |
-
st.
|
80 |
except requests.RequestException as e:
|
81 |
-
print(f"Error submitting response: {e}")
|
82 |
st.error(f"Error submitting response: {e}")
|
83 |
|
84 |
def edit_field(field, value):
|
@@ -90,77 +102,78 @@ def edit_field(field, value):
|
|
90 |
"field_to_edit": field,
|
91 |
"new_value": value
|
92 |
}
|
93 |
-
print("Editing field with payload:", payload)
|
94 |
try:
|
95 |
response = requests.post(f"{API_URL}/edit_field", json=payload)
|
96 |
response.raise_for_status()
|
97 |
data = response.json()
|
98 |
-
print("API Response:", data)
|
99 |
st.session_state.feedback = data.get("validation_feedback", "")
|
100 |
st.session_state.summary = data.get("summary", st.session_state.summary)
|
101 |
if data.get("message") == "Needs clarification":
|
102 |
st.error(f"Clarification needed for {field}: {data['validation_feedback']}")
|
103 |
elif data.get("message") == "Field updated successfully!":
|
104 |
st.success("Database updated.")
|
105 |
-
st.
|
106 |
except requests.RequestException as e:
|
107 |
-
print(f"Error editing field: {e}")
|
108 |
st.error(f"Error editing field: {e}")
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
st.session_state
|
113 |
-
|
114 |
-
st.session_state
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
#
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
st.
|
134 |
-
st.
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
154 |
st.error(st.session_state.feedback)
|
155 |
if st.session_state.prev_question and st.session_state.prev_question != st.session_state.current_question:
|
156 |
transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
|
157 |
st.info(transition_msg)
|
158 |
time.sleep(1)
|
159 |
-
|
160 |
st.subheader(f"Question {st.session_state.question_number}: {st.session_state.current_question}")
|
161 |
|
162 |
-
is_address_question = st.session_state.current_question == "
|
163 |
-
is_phone_question = st.session_state.current_question == "
|
164 |
|
165 |
if is_address_question or is_phone_question:
|
166 |
st.info(
|
@@ -169,23 +182,30 @@ else:
|
|
169 |
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."
|
170 |
)
|
171 |
if is_address_question:
|
172 |
-
st.session_state.skip_address = st.checkbox("Skip this question", value=st.session_state.skip_address)
|
173 |
elif is_phone_question:
|
174 |
-
st.session_state.skip_phone = st.checkbox("Skip this question", value=st.session_state.skip_phone)
|
175 |
-
|
176 |
-
st.components.v1.html("""
|
177 |
-
document.addEventListener('keypress', function(e) {
|
178 |
-
if (e.key === 'Enter' && document.activeElement.tagName === 'INPUT') {
|
179 |
-
e.preventDefault();
|
180 |
-
const submitButton = document.querySelector('button[key="submit_button"]');
|
181 |
-
if (submitButton && !submitButton.disabled) submitButton.click();
|
182 |
-
}
|
183 |
-
});
|
184 |
-
""", height=0)
|
185 |
|
186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
-
if st.button("Submit", key="
|
189 |
submit_response()
|
|
|
190 |
else:
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import random
|
6 |
import os
|
7 |
|
8 |
+
# --- CONFIGURATION ---
|
|
|
9 |
API_URL = os.getenv("API_URL")
|
10 |
|
11 |
TRANSITION_MESSAGES = [
|
|
|
16 |
"Onward to question {number}!"
|
17 |
]
|
18 |
|
19 |
+
TAB1_FILE = "tab1.txt"
|
20 |
+
|
21 |
+
# --- HELPER FUNCTIONS ---
|
22 |
+
|
23 |
+
def read_content_from_file(file_path):
|
24 |
+
try:
|
25 |
+
with open(file_path, 'r') as file:
|
26 |
+
return file.read()
|
27 |
+
except FileNotFoundError:
|
28 |
+
return f"Error: {file_path} not found."
|
29 |
+
|
30 |
+
def reset_registration_state():
|
31 |
+
# Remove all registration-related session state keys
|
32 |
+
for key in [
|
33 |
+
"session_id", "current_question", "answer", "feedback", "summary",
|
34 |
+
"skip_address", "skip_phone", "prev_question", "question_number",
|
35 |
+
"edit_field_values"
|
36 |
+
]:
|
37 |
+
if key in st.session_state:
|
38 |
+
del st.session_state[key]
|
39 |
+
|
40 |
def start_registration():
|
41 |
+
reset_registration_state()
|
42 |
try:
|
43 |
headers = {"Origin": "https://entz-council-3.hf.space"}
|
44 |
+
response = requests.post(f"{API_URL}/start_registration", headers=headers, timeout=2)
|
45 |
response.raise_for_status()
|
46 |
data = response.json()
|
|
|
47 |
st.session_state.session_id = data["session_id"]
|
48 |
st.session_state.current_question = data["message"]
|
49 |
st.session_state.feedback = ""
|
|
|
53 |
st.session_state.skip_phone = False
|
54 |
st.session_state.prev_question = ""
|
55 |
st.session_state.question_number = 1
|
56 |
+
st.session_state.edit_field_values = {}
|
57 |
except requests.RequestException as e:
|
|
|
58 |
st.error(f"Error starting registration: {e}")
|
59 |
+
|
|
|
|
|
|
|
|
|
60 |
def submit_response():
|
61 |
if not st.session_state.session_id:
|
62 |
st.error("No active session. Please start registration.")
|
|
|
71 |
"answer": st.session_state.answer,
|
72 |
"skip_steps": skip_steps
|
73 |
}
|
|
|
74 |
try:
|
75 |
response = requests.post(f"{API_URL}/submit_response", json=payload)
|
76 |
response.raise_for_status()
|
77 |
data = response.json()
|
|
|
78 |
if data.get("message") == "Registration complete!":
|
79 |
st.session_state.summary = data["summary"]
|
80 |
st.session_state.current_question = ""
|
|
|
89 |
st.session_state.answer = ""
|
90 |
st.session_state.skip_address = False
|
91 |
st.session_state.skip_phone = False
|
92 |
+
st.experimental_rerun()
|
93 |
except requests.RequestException as e:
|
|
|
94 |
st.error(f"Error submitting response: {e}")
|
95 |
|
96 |
def edit_field(field, value):
|
|
|
102 |
"field_to_edit": field,
|
103 |
"new_value": value
|
104 |
}
|
|
|
105 |
try:
|
106 |
response = requests.post(f"{API_URL}/edit_field", json=payload)
|
107 |
response.raise_for_status()
|
108 |
data = response.json()
|
|
|
109 |
st.session_state.feedback = data.get("validation_feedback", "")
|
110 |
st.session_state.summary = data.get("summary", st.session_state.summary)
|
111 |
if data.get("message") == "Needs clarification":
|
112 |
st.error(f"Clarification needed for {field}: {data['validation_feedback']}")
|
113 |
elif data.get("message") == "Field updated successfully!":
|
114 |
st.success("Database updated.")
|
115 |
+
st.experimental_rerun()
|
116 |
except requests.RequestException as e:
|
|
|
117 |
st.error(f"Error editing field: {e}")
|
118 |
|
119 |
+
def ensure_registration_state():
|
120 |
+
# Initialize session state if not already present
|
121 |
+
if "session_id" not in st.session_state:
|
122 |
+
start_registration()
|
123 |
+
if "edit_field_values" not in st.session_state:
|
124 |
+
st.session_state.edit_field_values = {}
|
125 |
+
|
126 |
+
# --- MAIN APP LOGIC ---
|
127 |
+
|
128 |
+
def run_registration_app():
|
129 |
+
ensure_registration_state()
|
130 |
+
|
131 |
+
st.title("AI-Powered Registration System")
|
132 |
+
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]**")
|
133 |
+
|
134 |
+
# --- REGISTRATION COMPLETE ---
|
135 |
+
if st.session_state.summary:
|
136 |
+
st.success("Registration Complete!")
|
137 |
+
st.subheader("Summary")
|
138 |
+
for key, value in st.session_state.summary.items():
|
139 |
+
# Use a unique key for each edit field
|
140 |
+
edit_key = f"edit_{key}"
|
141 |
+
# Use session state to persist edit field values
|
142 |
+
if edit_key not in st.session_state.edit_field_values:
|
143 |
+
st.session_state.edit_field_values[edit_key] = ""
|
144 |
+
st.write(f"**{key}**: {value}")
|
145 |
+
st.session_state.edit_field_values[edit_key] = st.text_input(
|
146 |
+
f"Edit {key}",
|
147 |
+
value=st.session_state.edit_field_values[edit_key],
|
148 |
+
key=edit_key
|
149 |
+
)
|
150 |
+
if st.button(f"Update {key}", key=f"update_{key}"):
|
151 |
+
edit_field(key, st.session_state.edit_field_values[edit_key])
|
152 |
+
|
153 |
+
col1, col2 = st.columns(2)
|
154 |
+
with col1:
|
155 |
+
if st.button("Next Registration", key="next_reg"):
|
156 |
+
# Reset registration and rerun
|
157 |
+
start_registration()
|
158 |
+
st.experimental_rerun()
|
159 |
+
with col2:
|
160 |
+
if st.button("End Session", key="end_sess"):
|
161 |
+
reset_registration_state()
|
162 |
+
st.success("Session ended. Please select 'Registration' tab to start again.")
|
163 |
+
|
164 |
+
# --- REGISTRATION IN PROGRESS ---
|
165 |
+
elif st.session_state.current_question:
|
166 |
+
if st.session_state.feedback and st.session_state.feedback != "Registration complete!":
|
167 |
st.error(st.session_state.feedback)
|
168 |
if st.session_state.prev_question and st.session_state.prev_question != st.session_state.current_question:
|
169 |
transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
|
170 |
st.info(transition_msg)
|
171 |
time.sleep(1)
|
172 |
+
|
173 |
st.subheader(f"Question {st.session_state.question_number}: {st.session_state.current_question}")
|
174 |
|
175 |
+
is_address_question = st.session_state.current_question.strip().lower() == "what is your address?"
|
176 |
+
is_phone_question = st.session_state.current_question.strip().lower() == "what is your phone number?"
|
177 |
|
178 |
if is_address_question or is_phone_question:
|
179 |
st.info(
|
|
|
182 |
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."
|
183 |
)
|
184 |
if is_address_question:
|
185 |
+
st.session_state.skip_address = st.checkbox("Skip this question", value=st.session_state.skip_address, key="skip_address_checkbox")
|
186 |
elif is_phone_question:
|
187 |
+
st.session_state.skip_phone = st.checkbox("Skip this question", value=st.session_state.skip_phone, key="skip_phone_checkbox")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
+
# Unique key for each question to avoid double entry
|
190 |
+
answer_key = f"answer_input_q{st.session_state.question_number}"
|
191 |
+
st.session_state.answer = st.text_input(
|
192 |
+
"Your Answer",
|
193 |
+
value=st.session_state.answer,
|
194 |
+
key=answer_key
|
195 |
+
)
|
196 |
|
197 |
+
if st.button("Submit", key=f"submit_button_q{st.session_state.question_number}"):
|
198 |
submit_response()
|
199 |
+
|
200 |
else:
|
201 |
+
st.info("Initializing registration session...")
|
202 |
+
|
203 |
+
# --- STREAMLIT TABS ---
|
204 |
+
|
205 |
+
tab1, tab2 = st.tabs(["Intro", "Registration"])
|
206 |
+
|
207 |
+
with tab1:
|
208 |
+
st.markdown(read_content_from_file(TAB1_FILE))
|
209 |
+
|
210 |
+
with tab2:
|
211 |
+
run_registration_app()
|