Update src/streamlit_app.py
Browse files- src/streamlit_app.py +26 -32
src/streamlit_app.py
CHANGED
@@ -8,44 +8,41 @@ import io
|
|
8 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
9 |
st.title("βοΈ Auto Weight Logger")
|
10 |
|
11 |
-
#
|
12 |
-
if "image_bytes" not in st.session_state:
|
13 |
-
st.session_state.image_bytes = None
|
14 |
-
if "input_mode" not in st.session_state:
|
15 |
-
st.session_state.input_mode = "Camera"
|
16 |
if "camera_key" not in st.session_state:
|
17 |
st.session_state.camera_key = str(uuid.uuid4())
|
18 |
|
19 |
-
# Input
|
20 |
-
st.radio("πΈ Select
|
21 |
|
22 |
-
# Clear
|
23 |
-
if st.button("π Clear / Retake
|
24 |
-
st.session_state.image_bytes = None
|
25 |
st.session_state.camera_key = str(uuid.uuid4())
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
uploaded_image = None
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
#
|
42 |
-
if
|
43 |
try:
|
44 |
-
image = Image.open(io.BytesIO(
|
45 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
46 |
|
47 |
-
if len(
|
48 |
-
st.error("β Image too large (>5MB). Please
|
49 |
st.stop()
|
50 |
|
51 |
with st.spinner("π Extracting weight..."):
|
@@ -58,8 +55,9 @@ if st.session_state.image_bytes:
|
|
58 |
else:
|
59 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
60 |
|
|
|
61 |
device_id = "BAL-001"
|
62 |
-
image_url = ""
|
63 |
|
64 |
salesforce_url = (
|
65 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
@@ -70,10 +68,6 @@ if st.session_state.image_bytes:
|
|
70 |
st.markdown("### π€ Send to Salesforce")
|
71 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
72 |
|
73 |
-
if st.button("π Retake / Upload Another"):
|
74 |
-
st.session_state.image_bytes = None
|
75 |
-
st.session_state.camera_key = str(uuid.uuid4())
|
76 |
-
|
77 |
except Exception as e:
|
78 |
-
st.error("β Failed to load or process image.")
|
79 |
st.exception(e)
|
|
|
8 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
9 |
st.title("βοΈ Auto Weight Logger")
|
10 |
|
11 |
+
# Session state for camera reload
|
|
|
|
|
|
|
|
|
12 |
if "camera_key" not in st.session_state:
|
13 |
st.session_state.camera_key = str(uuid.uuid4())
|
14 |
|
15 |
+
# Input method selector
|
16 |
+
input_method = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
17 |
|
18 |
+
# Clear button
|
19 |
+
if st.button("π Clear / Retake"):
|
|
|
20 |
st.session_state.camera_key = str(uuid.uuid4())
|
21 |
+
st.experimental_rerun()
|
22 |
|
23 |
+
image_bytes = None
|
24 |
+
image = None
|
|
|
25 |
|
26 |
+
# ----- CAMERA INPUT -----
|
27 |
+
if input_method == "Camera":
|
28 |
+
camera_image = st.camera_input("π· Capture the weight display", key=st.session_state.camera_key)
|
29 |
+
if camera_image is not None:
|
30 |
+
image_bytes = camera_image.getvalue()
|
31 |
|
32 |
+
# ----- FILE UPLOAD INPUT -----
|
33 |
+
elif input_method == "Upload":
|
34 |
+
uploaded_file = st.file_uploader("π Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
35 |
+
if uploaded_file is not None:
|
36 |
+
image_bytes = uploaded_file.read()
|
37 |
|
38 |
+
# ----- IMAGE PROCESSING -----
|
39 |
+
if image_bytes:
|
40 |
try:
|
41 |
+
image = Image.open(io.BytesIO(image_bytes))
|
42 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
43 |
|
44 |
+
if len(image_bytes) > 5 * 1024 * 1024:
|
45 |
+
st.error("β Image too large (>5MB). Please use a smaller one.")
|
46 |
st.stop()
|
47 |
|
48 |
with st.spinner("π Extracting weight..."):
|
|
|
55 |
else:
|
56 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
57 |
|
58 |
+
# Salesforce redirect link
|
59 |
device_id = "BAL-001"
|
60 |
+
image_url = "" # Optional
|
61 |
|
62 |
salesforce_url = (
|
63 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
|
68 |
st.markdown("### π€ Send to Salesforce")
|
69 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
70 |
|
|
|
|
|
|
|
|
|
71 |
except Exception as e:
|
72 |
+
st.error("β Failed to load or process the image.")
|
73 |
st.exception(e)
|