Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -385,15 +385,26 @@ def load_user_data_source(user_id):
|
|
| 385 |
st.error(f"Error loading user data source: {e}")
|
| 386 |
return []
|
| 387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
|
| 390 |
def store_brand_tonality(user_id, message):
|
| 391 |
try:
|
| 392 |
tonality_id = str(uuid.uuid4())
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
st.session_state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
display_save_confirmation("Brand Tonality")
|
| 398 |
|
| 399 |
except Exception as e:
|
|
@@ -403,10 +414,15 @@ def store_brand_tonality(user_id, message):
|
|
| 403 |
def store_trustbuilder(user_id, message):
|
| 404 |
try:
|
| 405 |
trustbuilder_id = str(uuid.uuid4())
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
st.session_state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
display_save_confirmation("TrustBuilder")
|
| 411 |
|
| 412 |
except Exception as e:
|
|
@@ -414,49 +430,65 @@ def store_trustbuilder(user_id, message):
|
|
| 414 |
|
| 415 |
|
| 416 |
def load_user_content(user_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
try:
|
| 418 |
-
user_data = db.child(
|
| 419 |
if user_data:
|
|
|
|
| 420 |
st.session_state.update(user_data)
|
| 421 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
| 423 |
except Exception as e:
|
| 424 |
-
st.error(f"Error loading saved content: {e}")
|
| 425 |
|
| 426 |
|
| 427 |
def save_content(user_id, content, content_type):
|
|
|
|
|
|
|
|
|
|
| 428 |
try:
|
| 429 |
entry_id = str(uuid.uuid4())
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
#
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
except Exception as e:
|
| 436 |
-
|
| 437 |
-
|
| 438 |
|
| 439 |
def display_saved_trustbuilders():
|
| 440 |
-
|
| 441 |
-
Display
|
| 442 |
-
"""
|
| 443 |
st.sidebar.subheader("Saved TrustBuilders")
|
| 444 |
-
trustbuilders = st.session_state.get("
|
| 445 |
for trustbuilder_id, trustbuilder_data in trustbuilders.items():
|
| 446 |
-
|
| 447 |
-
st.sidebar.markdown(
|
| 448 |
-
if st.sidebar.button("
|
| 449 |
-
delete_entry("
|
| 450 |
-
st.rerun()
|
| 451 |
|
|
|
|
| 452 |
st.sidebar.subheader("Saved Brand Tonality")
|
| 453 |
-
brand_tonality = st.session_state.get("
|
| 454 |
for tonality_id, tonality_data in brand_tonality.items():
|
| 455 |
-
|
| 456 |
-
st.sidebar.markdown(
|
| 457 |
-
if st.sidebar.button("
|
| 458 |
-
delete_entry("
|
| 459 |
-
st.rerun()
|
| 460 |
|
| 461 |
|
| 462 |
def download_link(content, filename):
|
|
@@ -527,13 +559,12 @@ def side():
|
|
| 527 |
# Save Custom Content section
|
| 528 |
st.header("Save Custom Content")
|
| 529 |
custom_content = st.text_area("Enter content to save")
|
| 530 |
-
|
| 531 |
if st.button("Save as Brand Tonality"):
|
| 532 |
-
save_content(st.session_state["wix_user_id"], custom_content, "
|
| 533 |
-
display_save_confirmation("Brand Tonality")
|
| 534 |
if st.button("Save as TrustBuilder"):
|
| 535 |
-
save_content(st.session_state["wix_user_id"], custom_content, "
|
| 536 |
-
|
|
|
|
| 537 |
|
| 538 |
|
| 539 |
|
|
@@ -1137,15 +1168,22 @@ if "brand_tonality" not in st.session_state:
|
|
| 1137 |
|
| 1138 |
# Load saved entries upon user login
|
| 1139 |
def retrieve_user_data(user_id):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1140 |
try:
|
| 1141 |
-
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
|
|
|
|
|
|
|
|
|
| 1147 |
except Exception as e:
|
| 1148 |
-
st.error("Error loading saved
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
|
@@ -1175,47 +1213,63 @@ def handle_memory_queries(prompt):
|
|
| 1175 |
|
| 1176 |
# Retrieve saved TrustBuilders
|
| 1177 |
elif "show my saved trustbuilders" in prompt or "find my saved trustbuilders" in prompt:
|
| 1178 |
-
|
| 1179 |
-
|
|
|
|
|
|
|
|
|
|
| 1180 |
else:
|
| 1181 |
return "You haven't saved any TrustBuilders yet."
|
| 1182 |
|
| 1183 |
# Retrieve saved Brand Tonality
|
| 1184 |
elif "show my saved brand tonality" in prompt or "find my saved brand tonality" in prompt:
|
| 1185 |
-
|
| 1186 |
-
|
|
|
|
|
|
|
|
|
|
| 1187 |
else:
|
| 1188 |
return "You haven't saved any Brand Tonality entries yet."
|
| 1189 |
-
|
| 1190 |
-
return None
|
| 1191 |
|
| 1192 |
|
| 1193 |
def delete_entry(category, entry_id, session_key):
|
|
|
|
|
|
|
|
|
|
| 1194 |
try:
|
|
|
|
| 1195 |
user_id = st.session_state["wix_user_id"]
|
|
|
|
|
|
|
| 1196 |
db.child("users").child(user_id).child(category).child(entry_id).remove()
|
| 1197 |
-
|
| 1198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1199 |
except Exception as e:
|
| 1200 |
st.error(f"Error deleting entry: {e}")
|
| 1201 |
|
|
|
|
| 1202 |
# Function to download TrustBuilder as a .md file
|
| 1203 |
def download_trustbuilder_as_md(content, trustbuilder_id):
|
| 1204 |
b64_content = base64.b64encode(content.encode()).decode()
|
| 1205 |
download_link = f'<a href="data:text/markdown;base64,{b64_content}" download="TrustBuilder_{trustbuilder_id}.md">Download</a>'
|
| 1206 |
st.sidebar.markdown(download_link, unsafe_allow_html=True)
|
| 1207 |
|
| 1208 |
-
|
|
|
|
|
|
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
# Function to update the message counter in a static location
|
| 1213 |
-
message_counter_placeholder = st.sidebar.empty()
|
| 1214 |
|
| 1215 |
|
| 1216 |
-
def update_message_counter():
|
| 1217 |
-
remaining_messages = st.session_state["message_limit"] - st.session_state["used_messages"]
|
| 1218 |
-
message_counter_placeholder.markdown(f"**Messages Left**: {remaining_messages} / {st.session_state['message_limit']}")
|
| 1219 |
|
| 1220 |
def initialize_wix_only_user(wix_user_id, email):
|
| 1221 |
try:
|
|
@@ -1288,11 +1342,11 @@ if st.session_state.get("wix_user_id") and st.session_state.get("email"):
|
|
| 1288 |
if save_as_trustbuilder or save_as_tonality:
|
| 1289 |
user_id = st.session_state["wix_user_id"]
|
| 1290 |
if save_as_trustbuilder:
|
| 1291 |
-
|
| 1292 |
-
display_save_confirmation("TrustBuilder")
|
| 1293 |
elif save_as_tonality:
|
| 1294 |
-
|
| 1295 |
-
|
|
|
|
| 1296 |
else:
|
| 1297 |
# Generate a response with AI for general queries
|
| 1298 |
with st.chat_message("user"):
|
|
|
|
| 385 |
st.error(f"Error loading user data source: {e}")
|
| 386 |
return []
|
| 387 |
|
| 388 |
+
def update_message_counter():
|
| 389 |
+
remaining_messages = st.session_state["message_limit"] - st.session_state["used_messages"]
|
| 390 |
+
message_counter_placeholder = st.sidebar.empty()
|
| 391 |
+
|
| 392 |
+
message_counter_placeholder.markdown(f"**Messages Left**: {remaining_messages} / {st.session_state['message_limit']}")
|
| 393 |
+
|
| 394 |
|
| 395 |
|
| 396 |
def store_brand_tonality(user_id, message):
|
| 397 |
try:
|
| 398 |
tonality_id = str(uuid.uuid4())
|
| 399 |
+
# Save to Firebase
|
| 400 |
+
db.child("users").child(user_id).child("BrandTonality").child(tonality_id).set({"message": message})
|
| 401 |
+
|
| 402 |
+
# Update `st.session_state` for immediate sidebar display
|
| 403 |
+
if "BrandTonality" not in st.session_state:
|
| 404 |
+
st.session_state["BrandTonality"] = {}
|
| 405 |
+
st.session_state["BrandTonality"][tonality_id] = {"message": message}
|
| 406 |
+
|
| 407 |
+
# Confirmation
|
| 408 |
display_save_confirmation("Brand Tonality")
|
| 409 |
|
| 410 |
except Exception as e:
|
|
|
|
| 414 |
def store_trustbuilder(user_id, message):
|
| 415 |
try:
|
| 416 |
trustbuilder_id = str(uuid.uuid4())
|
| 417 |
+
# Save to Firebase
|
| 418 |
+
db.child("users").child(user_id).child("TrustBuilder").child(trustbuilder_id).set({"message": message})
|
| 419 |
+
|
| 420 |
+
# Update `st.session_state` for immediate sidebar display
|
| 421 |
+
if "TrustBuilder" not in st.session_state:
|
| 422 |
+
st.session_state["TrustBuilder"] = {}
|
| 423 |
+
st.session_state["TrustBuilder"][trustbuilder_id] = {"message": message}
|
| 424 |
+
|
| 425 |
+
# Confirmation
|
| 426 |
display_save_confirmation("TrustBuilder")
|
| 427 |
|
| 428 |
except Exception as e:
|
|
|
|
| 430 |
|
| 431 |
|
| 432 |
def load_user_content(user_id):
|
| 433 |
+
"""
|
| 434 |
+
Load all content for a user from Firebase, ensuring each user has a single root
|
| 435 |
+
containing TrustBuilder, BrandTonality, and other data fields like email, message limits, etc.
|
| 436 |
+
"""
|
| 437 |
try:
|
| 438 |
+
user_data = db.child("users").child(user_id).get().val()
|
| 439 |
if user_data:
|
| 440 |
+
# Update session state with all user data
|
| 441 |
st.session_state.update(user_data)
|
| 442 |
+
|
| 443 |
+
# Load TrustBuilder and BrandTonality into session state for display
|
| 444 |
+
st.session_state["TrustBuilder"] = user_data.get("TrustBuilder", {})
|
| 445 |
+
st.session_state["BrandTonality"] = user_data.get("BrandTonality", {})
|
| 446 |
+
|
| 447 |
except Exception as e:
|
|
|
|
| 448 |
|
| 449 |
|
| 450 |
def save_content(user_id, content, content_type):
|
| 451 |
+
"""
|
| 452 |
+
Save TrustBuilder or Brand Tonality to Firebase and update session state, with a clean confirmation.
|
| 453 |
+
"""
|
| 454 |
try:
|
| 455 |
entry_id = str(uuid.uuid4())
|
| 456 |
+
firebase_key = "TrustBuilder" if content_type.lower() == "trustbuilder" else "BrandTonality"
|
| 457 |
+
|
| 458 |
+
# Save to Firebase
|
| 459 |
+
db.child("users").child(user_id).child(firebase_key).child(entry_id).set({"message": content})
|
| 460 |
+
|
| 461 |
+
# Update `st.session_state` for immediate display in sidebar and chatbot
|
| 462 |
+
if firebase_key not in st.session_state:
|
| 463 |
+
st.session_state[firebase_key] = {}
|
| 464 |
+
st.session_state[firebase_key][entry_id] = {"message": content}
|
| 465 |
+
|
| 466 |
+
# Display save confirmation without showing any ID or debug information
|
| 467 |
+
display_save_confirmation(content_type.capitalize())
|
| 468 |
+
|
| 469 |
except Exception as e:
|
| 470 |
+
st.error(f"Could not save {content_type}. Please try again. Error: {e}")
|
| 471 |
+
|
| 472 |
|
| 473 |
def display_saved_trustbuilders():
|
| 474 |
+
|
| 475 |
+
# Display TrustBuilders with delete buttons
|
|
|
|
| 476 |
st.sidebar.subheader("Saved TrustBuilders")
|
| 477 |
+
trustbuilders = st.session_state.get("TrustBuilder", {})
|
| 478 |
for trustbuilder_id, trustbuilder_data in trustbuilders.items():
|
| 479 |
+
message = trustbuilder_data.get("message", "No message available")
|
| 480 |
+
st.sidebar.markdown(f"- {message[:50]}...")
|
| 481 |
+
if st.sidebar.button("Delete", key=f"del_tb_{trustbuilder_id}"):
|
| 482 |
+
delete_entry("TrustBuilder", trustbuilder_id, "TrustBuilder")
|
|
|
|
| 483 |
|
| 484 |
+
# Display Brand Tonality entries with delete buttons
|
| 485 |
st.sidebar.subheader("Saved Brand Tonality")
|
| 486 |
+
brand_tonality = st.session_state.get("BrandTonality", {})
|
| 487 |
for tonality_id, tonality_data in brand_tonality.items():
|
| 488 |
+
message = tonality_data.get("message", "No message available")
|
| 489 |
+
st.sidebar.markdown(f"- {message[:50]}...")
|
| 490 |
+
if st.sidebar.button("Delete", key=f"del_bt_{tonality_id}"):
|
| 491 |
+
delete_entry("BrandTonality", tonality_id, "BrandTonality")
|
|
|
|
| 492 |
|
| 493 |
|
| 494 |
def download_link(content, filename):
|
|
|
|
| 559 |
# Save Custom Content section
|
| 560 |
st.header("Save Custom Content")
|
| 561 |
custom_content = st.text_area("Enter content to save")
|
|
|
|
| 562 |
if st.button("Save as Brand Tonality"):
|
| 563 |
+
save_content(st.session_state["wix_user_id"], custom_content, "brandtonality")
|
|
|
|
| 564 |
if st.button("Save as TrustBuilder"):
|
| 565 |
+
save_content(st.session_state["wix_user_id"], custom_content, "trustbuilder")
|
| 566 |
+
|
| 567 |
+
|
| 568 |
|
| 569 |
|
| 570 |
|
|
|
|
| 1168 |
|
| 1169 |
# Load saved entries upon user login
|
| 1170 |
def retrieve_user_data(user_id):
|
| 1171 |
+
"""
|
| 1172 |
+
Load all content for a user from Firebase, ensuring each user has a single root
|
| 1173 |
+
containing TrustBuilder, BrandTonality, and other data fields like email, message limits, etc.
|
| 1174 |
+
"""
|
| 1175 |
try:
|
| 1176 |
+
user_data = db.child("users").child(user_id).get().val()
|
| 1177 |
+
if user_data:
|
| 1178 |
+
# Update session state with all user data
|
| 1179 |
+
st.session_state.update(user_data)
|
| 1180 |
+
|
| 1181 |
+
# Load TrustBuilder and BrandTonality into session state for display
|
| 1182 |
+
st.session_state["TrustBuilder"] = user_data.get("TrustBuilder", {})
|
| 1183 |
+
st.session_state["BrandTonality"] = user_data.get("BrandTonality", {})
|
| 1184 |
+
|
| 1185 |
except Exception as e:
|
| 1186 |
+
st.error(f"Error loading saved content: {e}")
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
|
|
|
| 1213 |
|
| 1214 |
# Retrieve saved TrustBuilders
|
| 1215 |
elif "show my saved trustbuilders" in prompt or "find my saved trustbuilders" in prompt:
|
| 1216 |
+
trustbuilders = st.session_state.get("TrustBuilder", {})
|
| 1217 |
+
if trustbuilders:
|
| 1218 |
+
# Return only the content messages without any IDs or extra details
|
| 1219 |
+
response = "\n".join([f"- {entry['message']}" for entry in trustbuilders.values()])
|
| 1220 |
+
return f"Here are your saved TrustBuilders:\n{response}"
|
| 1221 |
else:
|
| 1222 |
return "You haven't saved any TrustBuilders yet."
|
| 1223 |
|
| 1224 |
# Retrieve saved Brand Tonality
|
| 1225 |
elif "show my saved brand tonality" in prompt or "find my saved brand tonality" in prompt:
|
| 1226 |
+
brand_tonality = st.session_state.get("BrandTonality", {})
|
| 1227 |
+
if brand_tonality:
|
| 1228 |
+
# Return only the content messages without any IDs or extra details
|
| 1229 |
+
response = "\n".join([f"- {entry['message']}" for entry in brand_tonality.values()])
|
| 1230 |
+
return f"Here are your saved Brand Tonality entries:\n{response}"
|
| 1231 |
else:
|
| 1232 |
return "You haven't saved any Brand Tonality entries yet."
|
| 1233 |
+
|
| 1234 |
+
return None # If no recognized command, proceed with general handling
|
| 1235 |
|
| 1236 |
|
| 1237 |
def delete_entry(category, entry_id, session_key):
|
| 1238 |
+
"""
|
| 1239 |
+
Deletes an entry from Firebase and updates the session state to reflect the deletion.
|
| 1240 |
+
"""
|
| 1241 |
try:
|
| 1242 |
+
# Get user ID from session state
|
| 1243 |
user_id = st.session_state["wix_user_id"]
|
| 1244 |
+
|
| 1245 |
+
# Remove the entry from Firebase
|
| 1246 |
db.child("users").child(user_id).child(category).child(entry_id).remove()
|
| 1247 |
+
|
| 1248 |
+
# Remove from session state and refresh sidebar display
|
| 1249 |
+
if session_key in st.session_state and entry_id in st.session_state[session_key]:
|
| 1250 |
+
del st.session_state[session_key][entry_id]
|
| 1251 |
+
|
| 1252 |
+
# Refresh the app to immediately reflect the change in the sidebar
|
| 1253 |
+
st.rerun()
|
| 1254 |
except Exception as e:
|
| 1255 |
st.error(f"Error deleting entry: {e}")
|
| 1256 |
|
| 1257 |
+
|
| 1258 |
# Function to download TrustBuilder as a .md file
|
| 1259 |
def download_trustbuilder_as_md(content, trustbuilder_id):
|
| 1260 |
b64_content = base64.b64encode(content.encode()).decode()
|
| 1261 |
download_link = f'<a href="data:text/markdown;base64,{b64_content}" download="TrustBuilder_{trustbuilder_id}.md">Download</a>'
|
| 1262 |
st.sidebar.markdown(download_link, unsafe_allow_html=True)
|
| 1263 |
|
| 1264 |
+
def handle_save_trustbuilder_or_tonality(user_id, content, type_name):
|
| 1265 |
+
save_content(user_id, content, type_name)
|
| 1266 |
+
display_saved_trustbuilders()
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
# Function to update the message counter in a static location
|
|
|
|
| 1271 |
|
| 1272 |
|
|
|
|
|
|
|
|
|
|
| 1273 |
|
| 1274 |
def initialize_wix_only_user(wix_user_id, email):
|
| 1275 |
try:
|
|
|
|
| 1342 |
if save_as_trustbuilder or save_as_tonality:
|
| 1343 |
user_id = st.session_state["wix_user_id"]
|
| 1344 |
if save_as_trustbuilder:
|
| 1345 |
+
handle_save_trustbuilder_or_tonality(user_id, prompt, "trustbuilder")
|
|
|
|
| 1346 |
elif save_as_tonality:
|
| 1347 |
+
handle_save_trustbuilder_or_tonality(user_id, prompt, "brandtonality")
|
| 1348 |
+
|
| 1349 |
+
|
| 1350 |
else:
|
| 1351 |
# Generate a response with AI for general queries
|
| 1352 |
with st.chat_message("user"):
|