Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,8 @@ from datetime import datetime
|
|
| 14 |
from pathlib import Path
|
| 15 |
import streamlit as st
|
| 16 |
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# -----------------------
|
| 19 |
# Sidebar: Short User Manual
|
|
@@ -126,22 +128,19 @@ else:
|
|
| 126 |
# -----------------------
|
| 127 |
def get_latest_wound_data():
|
| 128 |
if not db:
|
| 129 |
-
return None
|
| 130 |
try:
|
| 131 |
data = db.child("data").get().val()
|
| 132 |
-
if not data:
|
| 133 |
-
return None
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
elif isinstance(data, dict):
|
| 139 |
-
latest_key = list(data.keys())[-1]
|
| 140 |
-
return data[latest_key]
|
| 141 |
-
return data
|
| 142 |
except Exception as e:
|
| 143 |
st.error(f"Error reading Firebase data: {e}")
|
| 144 |
-
return None
|
|
|
|
| 145 |
|
| 146 |
|
| 147 |
# -----------------------
|
|
@@ -149,7 +148,11 @@ def get_latest_wound_data():
|
|
| 149 |
# -----------------------
|
| 150 |
st.title("🩺 First Aid Assistant — Firebase Connected")
|
| 151 |
|
| 152 |
-
data = get_latest_wound_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if isinstance(data, str):
|
| 155 |
try:
|
|
@@ -189,7 +192,11 @@ else:
|
|
| 189 |
st.markdown("---")
|
| 190 |
if st.button("🔄 Refresh Firebase data"):
|
| 191 |
st.toast("Refreshing data from Firebase...", icon="🔁")
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
if data:
|
| 194 |
st.success("✅ Data refreshed successfully!")
|
| 195 |
|
|
@@ -646,29 +653,28 @@ def show_and_optionally_reset_config():
|
|
| 646 |
# Function: Save Victim Info to Firebase
|
| 647 |
def save_victim_info(victim: dict):
|
| 648 |
"""
|
| 649 |
-
Save
|
| 650 |
-
|
| 651 |
-
Returns the saved record (dict).
|
| 652 |
"""
|
| 653 |
if not db:
|
| 654 |
raise ValueError("Firebase not connected.")
|
| 655 |
|
| 656 |
-
|
|
|
|
|
|
|
|
|
|
| 657 |
|
| 658 |
-
# Unique ID for each victim record
|
| 659 |
-
record_id = f"victim_{int(time.time())}"
|
| 660 |
-
|
| 661 |
-
# Ensure wound_type & timestamp fields exist
|
| 662 |
victim_data = victim.copy()
|
| 663 |
-
victim_data["timestamp"] =
|
| 664 |
|
| 665 |
try:
|
| 666 |
-
#
|
| 667 |
-
db.child("victims").child(record_id).
|
| 668 |
return victim_data
|
| 669 |
except Exception as e:
|
| 670 |
raise RuntimeError(f"Failed to save victim info: {e}")
|
| 671 |
|
|
|
|
| 672 |
# Advanced section
|
| 673 |
if st.session_state.get("basic_done") and st.session_state.get("wound_type"):
|
| 674 |
st.header("Advanced assistance")
|
|
|
|
| 14 |
from pathlib import Path
|
| 15 |
import streamlit as st
|
| 16 |
|
| 17 |
+
if "firebase_record_id" not in st.session_state:
|
| 18 |
+
st.session_state.firebase_record_id = None
|
| 19 |
|
| 20 |
# -----------------------
|
| 21 |
# Sidebar: Short User Manual
|
|
|
|
| 128 |
# -----------------------
|
| 129 |
def get_latest_wound_data():
|
| 130 |
if not db:
|
| 131 |
+
return None, None
|
| 132 |
try:
|
| 133 |
data = db.child("data").get().val()
|
| 134 |
+
if not data or not isinstance(data, dict):
|
| 135 |
+
return None, None
|
| 136 |
+
|
| 137 |
+
latest_key = list(data.keys())[-1]
|
| 138 |
+
return latest_key, data[latest_key]
|
| 139 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
st.error(f"Error reading Firebase data: {e}")
|
| 142 |
+
return None, None
|
| 143 |
+
|
| 144 |
|
| 145 |
|
| 146 |
# -----------------------
|
|
|
|
| 148 |
# -----------------------
|
| 149 |
st.title("🩺 First Aid Assistant — Firebase Connected")
|
| 150 |
|
| 151 |
+
record_id, data = get_latest_wound_data()
|
| 152 |
+
|
| 153 |
+
if record_id:
|
| 154 |
+
st.session_state.firebase_record_id = record_id
|
| 155 |
+
|
| 156 |
|
| 157 |
if isinstance(data, str):
|
| 158 |
try:
|
|
|
|
| 192 |
st.markdown("---")
|
| 193 |
if st.button("🔄 Refresh Firebase data"):
|
| 194 |
st.toast("Refreshing data from Firebase...", icon="🔁")
|
| 195 |
+
record_id, data = get_latest_wound_data()
|
| 196 |
+
|
| 197 |
+
if record_id:
|
| 198 |
+
st.session_state.firebase_record_id = record_id
|
| 199 |
+
|
| 200 |
if data:
|
| 201 |
st.success("✅ Data refreshed successfully!")
|
| 202 |
|
|
|
|
| 653 |
# Function: Save Victim Info to Firebase
|
| 654 |
def save_victim_info(victim: dict):
|
| 655 |
"""
|
| 656 |
+
Save victim info using EXISTING Firebase record ID
|
| 657 |
+
(same ID as data/<ID>, e.g. 001).
|
|
|
|
| 658 |
"""
|
| 659 |
if not db:
|
| 660 |
raise ValueError("Firebase not connected.")
|
| 661 |
|
| 662 |
+
# Get existing record ID from session
|
| 663 |
+
record_id = st.session_state.get("firebase_record_id")
|
| 664 |
+
if not record_id:
|
| 665 |
+
raise ValueError("No Firebase record ID found.")
|
| 666 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 667 |
victim_data = victim.copy()
|
| 668 |
+
victim_data["timestamp"] = datetime.utcnow().strftime("%d/%m/%Y %H:%M:%S")
|
| 669 |
|
| 670 |
try:
|
| 671 |
+
# Update instead of set (do NOT overwrite AI result)
|
| 672 |
+
db.child("victims").child(record_id).update(victim_data)
|
| 673 |
return victim_data
|
| 674 |
except Exception as e:
|
| 675 |
raise RuntimeError(f"Failed to save victim info: {e}")
|
| 676 |
|
| 677 |
+
|
| 678 |
# Advanced section
|
| 679 |
if st.session_state.get("basic_done") and st.session_state.get("wound_type"):
|
| 680 |
st.header("Advanced assistance")
|