Sanjayraju30 commited on
Commit
f970466
Β·
verified Β·
1 Parent(s): 51be189

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # State initialization
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 mode selector
20
- st.radio("πŸ“Έ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
21
 
22
- # Clear/reset
23
- if st.button("πŸ” Clear / Retake Photo"):
24
- st.session_state.image_bytes = None
25
  st.session_state.camera_key = str(uuid.uuid4())
 
26
 
27
- # Get image input
28
- if st.session_state.image_bytes is None:
29
- uploaded_image = None
30
 
31
- if st.session_state.input_mode == "Camera":
32
- uploaded_image = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
33
- if uploaded_image is not None:
34
- st.session_state.image_bytes = uploaded_image.getvalue()
 
35
 
36
- elif st.session_state.input_mode == "Upload":
37
- uploaded_image = st.file_uploader("πŸ“ Upload image of weight display", type=["jpg", "jpeg", "png"])
38
- if uploaded_image is not None:
39
- st.session_state.image_bytes = uploaded_image.read()
 
40
 
41
- # Process and OCR
42
- if st.session_state.image_bytes:
43
  try:
44
- image = Image.open(io.BytesIO(st.session_state.image_bytes))
45
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
46
 
47
- if len(st.session_state.image_bytes) > 5 * 1024 * 1024:
48
- st.error("❌ Image too large (>5MB). Please try again.")
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)