File size: 9,065 Bytes
522c037 273c990 0e81284 522c037 273c990 522c037 e66d953 522c037 0ed6eb6 e66d953 0ed6eb6 522c037 273c990 522c037 0fa3cbb 522c037 e66d953 522c037 0ed6eb6 522c037 273c990 522c037 0fa3cbb 522c037 0fa3cbb 522c037 5d54d5e 522c037 0ed6eb6 522c037 273c990 6b46691 273c990 0ed6eb6 273c990 e66d953 273c990 e66d953 0ed6eb6 273c990 e66d953 0ed6eb6 273c990 6b46691 273c990 e66d953 273c990 e66d953 273c990 e66d953 273c990 0ed6eb6 273c990 e66d953 0ed6eb6 273c990 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
import streamlit as st
import requests
import json
import time
import random
import os
# Load environment variables
API_URL = os.getenv("API_URL")
# Read content from text files
def read_content_from_file(file_path):
try:
with open(file_path, 'r') as file:
return file.read()
except FileNotFoundError:
return f"Error: {file_path} not found."
TRANSITION_MESSAGES = [
"Now we are moving onto question {number}!",
"Time to switch to question {number}!",
"Let's proceed with question {number}!",
"Moving forward to question {number}!",
"Onward to question {number}!"
]
def reset_session_state():
"""Reset session state to start a new registration."""
st.session_state.session_id = None
st.session_state.current_question = ""
st.session_state.feedback = ""
st.session_state.summary = None
st.session_state.answer = ""
st.session_state.skip_address = False
st.session_state.skip_phone = False
st.session_state.prev_question = ""
st.session_state.question_number = 1
def start_registration():
print("Starting registration...")
try:
response = requests.post(f"{API_URL}/start_registration", timeout=60)
response.raise_for_status()
data = response.json()
print("API Response:", data)
st.session_state.session_id = data["session_id"]
st.session_state.current_question = data["message"]
st.session_state.question_number = 1 # Reset question number for new session
except requests.RequestException as e:
print(f"Error starting registration: {e}")
st.error(f"Error starting registration: {e}")
def submit_response():
if not st.session_state.session_id:
st.error("No active session. Please start registration.")
return
skip_steps = []
if st.session_state.get("skip_address", False):
skip_steps.append("ask_address")
if st.session_state.get("skip_phone", False):
skip_steps.append("ask_phone")
payload = {
"session_id": st.session_state.session_id,
"answer": st.session_state.answer,
"skip_steps": skip_steps
}
print("Submitting response with payload:", payload)
try:
response = requests.post(f"{API_URL}/submit_response", json=payload, timeout=60)
response.raise_for_status()
data = response.json()
print("API Response:", data)
if data.get("message") == "Registration complete!":
st.session_state.summary = data["summary"]
st.session_state.current_question = ""
st.session_state.feedback = "Registration complete!"
st.session_state.question_number = 1 # Reset for potential next session
else:
st.session_state.prev_question = st.session_state.current_question
st.session_state.current_question = data.get("next_question", "")
st.session_state.feedback = data.get("validation_feedback", "")
if st.session_state.prev_question != st.session_state.current_question:
st.session_state.question_number += 1
st.session_state.answer = ""
st.session_state.skip_address = False
st.session_state.skip_phone = False
st.rerun()
except requests.RequestException as e:
print(f"Error submitting response: {e}")
st.error(f"Error submitting response: {e}")
def edit_field(field, value):
if not st.session_state.session_id:
st.error("No active session.")
return
payload = {
"session_id": st.session_state.session_id,
"field_to_edit": field,
"new_value": value
}
print("Editing field with payload:", payload)
try:
response = requests.post(f"{API_URL}/edit_field", json=payload, timeout=60)
response.raise_for_status()
data = response.json()
print("API Response:", data)
st.session_state.feedback = data.get("validation_feedback", "")
st.session_state.summary = data.get("summary", st.session_state.summary)
if data.get("message") == "Needs clarification":
st.error(f"Clarification needed for {field}: {data['validation_feedback']}")
elif data.get("message") == "Field updated successfully!":
st.success("Database updated.")
st.rerun()
except requests.RequestException as e:
print(f"Error editing field: {e}")
st.error(f"Error editing field: {e}")
def main():
st.title("AI-Powered Registration System")
# Create two tabs
tab1, tab2 = st.tabs(["Introduction", "Registration"])
# Tab 1: Introduction
with tab1:
st.subheader("About This Application")
intro_content = read_content_from_file("tab1.txt")
st.markdown(intro_content)
# Tab 2: Registration Application
with tab2:
st.subheader("Registration")
registration_content = read_content_from_file("tab2.txt")
st.markdown(registration_content)
st.markdown("**Developed by [email protected]**")
if st.session_state.get("summary"):
st.success("Registration Complete!")
st.subheader("Summary")
for key, value in st.session_state.summary.items():
st.write(f"**{key}**: {value}")
new_value = st.text_input(f"Edit {key}", key=f"edit_{key}_{st.session_state.session_id}") # Unique key per session
if st.button(f"Update {key}", key=f"update_{key}_{st.session_state.session_id}"):
edit_field(key, new_value)
col1, col2 = st.columns(2)
with col1:
if st.button("Next Registration", key="next_reg", on_click=lambda: (reset_session_state(), start_registration(), st.rerun())):
pass
with col2:
if st.button("End Session", key="end_sess", on_click=lambda: (reset_session_state(), start_registration(), st.rerun())):
pass
else:
if st.session_state.get("current_question"):
if st.session_state.get("feedback"):
st.error(st.session_state.feedback)
if st.session_state.get("prev_question") and st.session_state.prev_question != st.session_state.current_question:
transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
st.info(transition_msg)
time.sleep(1)
st.subheader(f"Question {st.session_state.question_number}: {st.session_state.current_question}")
is_address_question = st.session_state.current_question == "What is your address?"
is_phone_question = st.session_state.current_question == "What is your phone number?"
if is_address_question or is_phone_question:
st.info(
f"This question is optional. Check the box to skip, or enter your information below.\n"
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"
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."
)
if is_address_question:
st.session_state.skip_address = st.checkbox("Skip this question", key=f"skip_address_{st.session_state.session_id}", value=st.session_state.skip_address)
elif is_phone_question:
st.session_state.skip_phone = st.checkbox("Skip this question", key=f"skip_phone_{st.session_state.session_id}", value=st.session_state.skip_phone)
st.components.v1.html("""
document.addEventListener('keypress', function(e) {
if (e.key === 'Enter' && document.activeElement.tagName === 'INPUT') {
e.preventDefault();
const submitButton = document.querySelector('button[key="submit_button"]');
if (submitButton && !submitButton.disabled) submitButton.click();
}
});
""", height=0)
st.session_state.answer = st.text_input("Your Answer", value="", key=f"answer_input_{st.session_state.question_number}_{st.session_state.session_id}") # Unique key per question and session
if st.button("Submit", key="submit_button"):
submit_response()
else:
print("Waiting for session to initialize...")
if __name__ == "__main__":
# Initialize session state
if "session_state" not in st.session_state:
st.session_state.session_state = {}
reset_session_state()
if st.session_state.session_id is None:
start_registration()
main() |