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

Update index.py

Browse files
Files changed (1) hide show
  1. index.py +43 -40
index.py CHANGED
@@ -1,11 +1,10 @@
 
 
 
1
  import streamlit as st
2
  import requests
3
- import json
4
  import time
5
  import random
6
- import os
7
-
8
-
9
 
10
  API_URL = os.getenv("API_URL")
11
 
@@ -17,11 +16,24 @@ TRANSITION_MESSAGES = [
17
  "Onward to question {number}!"
18
  ]
19
 
 
 
 
 
 
 
 
 
 
20
  def start_registration():
21
  print("Starting registration...")
22
  try:
23
  headers = {"Origin": "https://entz-council-3.hf.space"}
24
- response = requests.post(f"{API_URL}/start_registration", headers=headers, timeout=5)
 
 
 
 
25
  response.raise_for_status()
26
  data = response.json()
27
  print("API Response:", data)
@@ -37,11 +49,7 @@ def start_registration():
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.")
@@ -58,7 +66,11 @@ def submit_response():
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)
@@ -92,7 +104,11 @@ def edit_field(field, 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)
@@ -107,8 +123,7 @@ def edit_field(field, value):
107
  print(f"Error editing field: {e}")
108
  st.error(f"Error editing field: {e}")
109
 
110
- if "session_state" not in st.session_state:
111
- st.session_state.session_state = {}
112
  st.session_state.session_id = None
113
  st.session_state.current_question = ""
114
  st.session_state.answer = ""
@@ -122,16 +137,8 @@ if "session_state" not in st.session_state:
122
  if st.session_state.session_id is None:
123
  start_registration()
124
 
125
- # Read content from text files
126
- def read_content_from_file(file_path):
127
- try:
128
- with open(file_path, 'r') as file:
129
- return file.read()
130
- except FileNotFoundError:
131
- return f"Error: {file_path} not found."
132
-
133
  st.title("AI-Powered Registration System")
134
- 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]")
135
 
136
  if st.session_state.summary:
137
  st.success("Registration Complete!")
@@ -143,11 +150,14 @@ if st.session_state.summary:
143
  edit_field(key, new_value)
144
  col1, col2 = st.columns(2)
145
  with col1:
146
- if st.button("Next Registration", key="next_reg", on_click=start_registration):
147
- pass
 
 
148
  with col2:
149
- if st.button("End Session", key="end_sess", on_click=start_registration):
150
- pass
 
151
  else:
152
  if st.session_state.current_question:
153
  if st.session_state.feedback:
@@ -156,7 +166,6 @@ else:
156
  transition_msg = random.choice(TRANSITION_MESSAGES).format(number=st.session_state.question_number)
157
  st.info(transition_msg)
158
  time.sleep(1)
159
- print(f"Displaying question: {st.session_state.current_question}")
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 == "What is your address?"
@@ -173,19 +182,13 @@ else:
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
- st.session_state.answer = st.text_input("Your Answer", value="", key=f"answer_input_{st.session_state.question_number}")
187
 
188
  if st.button("Submit", key="submit_button"):
189
  submit_response()
190
  else:
191
- print("Waiting for session to initialize...")
 
1
+ import os
2
+ os.environ["HOME"] = "/tmp" # <--- Fix for HuggingFace/permissions
3
+
4
  import streamlit as st
5
  import requests
 
6
  import time
7
  import random
 
 
 
8
 
9
  API_URL = os.getenv("API_URL")
10
 
 
16
  "Onward to question {number}!"
17
  ]
18
 
19
+ def reset_session_state():
20
+ keys = [
21
+ "session_id", "current_question", "answer", "feedback", "summary",
22
+ "skip_address", "skip_phone", "prev_question", "question_number"
23
+ ]
24
+ for key in keys:
25
+ if key in st.session_state:
26
+ del st.session_state[key]
27
+
28
  def start_registration():
29
  print("Starting registration...")
30
  try:
31
  headers = {"Origin": "https://entz-council-3.hf.space"}
32
+ response = requests.post(
33
+ f"{API_URL}/start_registration",
34
+ headers=headers,
35
+ timeout=10
36
+ )
37
  response.raise_for_status()
38
  data = response.json()
39
  print("API Response:", data)
 
49
  except requests.RequestException as e:
50
  print(f"Error starting registration: {e}, Response: {getattr(e.response, 'text', 'No response')}")
51
  st.error(f"Error starting registration: {e}")
52
+
 
 
 
 
53
  def submit_response():
54
  if not st.session_state.session_id:
55
  st.error("No active session. Please start registration.")
 
66
  }
67
  print("Submitting response with payload:", payload)
68
  try:
69
+ response = requests.post(
70
+ f"{API_URL}/submit_response",
71
+ json=payload,
72
+ timeout=10
73
+ )
74
  response.raise_for_status()
75
  data = response.json()
76
  print("API Response:", data)
 
104
  }
105
  print("Editing field with payload:", payload)
106
  try:
107
+ response = requests.post(
108
+ f"{API_URL}/edit_field",
109
+ json=payload,
110
+ timeout=10
111
+ )
112
  response.raise_for_status()
113
  data = response.json()
114
  print("API Response:", data)
 
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 = ""
129
  st.session_state.answer = ""
 
137
  if st.session_state.session_id is None:
138
  start_registration()
139
 
 
 
 
 
 
 
 
 
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!")
 
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:
 
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?"
 
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...")