Sanjayraju30 commited on
Commit
19e20f2
Β·
verified Β·
1 Parent(s): 04384ee

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +7 -16
src/streamlit_app.py CHANGED
@@ -6,31 +6,26 @@ import urllib.parse
6
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
- # --------------------------------
10
- # βœ… Session state to manage reset
11
- # --------------------------------
12
  if "clear" not in st.session_state:
13
  st.session_state.clear = False
14
 
15
- # πŸ”˜ Clear / Retake Photo Button
16
  if st.button("πŸ” Clear / Retake Photo"):
17
  st.session_state.clear = True
18
- st.experimental_rerun() # or st.rerun() if supported
19
 
20
- # 🧼 Skip camera input for one run if clearing
21
  if st.session_state.clear:
22
  st.session_state.clear = False
23
- st.stop() # skip rest, rerun will show fresh input
24
 
25
- # --------------------------------
26
  # πŸ“· Camera Input
27
- # --------------------------------
28
  img_data = st.camera_input("πŸ“· Capture the weight display")
29
 
30
  if img_data:
31
  st.success("βœ… Image captured successfully!")
32
 
33
- # ⚠️ Optional: file size check
34
  if len(img_data.getvalue()) > 5 * 1024 * 1024:
35
  st.error("❌ Image too large (>5MB). Please try again.")
36
  st.stop()
@@ -38,26 +33,22 @@ if img_data:
38
  image = Image.open(img_data)
39
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
40
 
41
- # πŸ” Run OCR
42
  with st.spinner("πŸ” Extracting weight..."):
43
  weight, confidence = extract_weight_from_image(image)
44
 
45
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
46
 
47
- # ⚠️ If confidence too low, allow retake
48
  if not weight or confidence < 80:
49
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
50
  if st.button("πŸ” Retake Photo"):
51
  st.session_state.clear = True
52
- st.experimental_rerun()
53
  st.stop()
54
 
55
- # βœ… Successful result
56
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
57
 
58
- # πŸ”— Salesforce redirection setup
59
  device_id = "BAL-001"
60
- image_url = "" # Optional: you can later host image here
61
 
62
  encoded_weight = urllib.parse.quote(str(weight))
63
  encoded_device = urllib.parse.quote(device_id)
 
6
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
+ # βœ… Manage camera state
 
 
10
  if "clear" not in st.session_state:
11
  st.session_state.clear = False
12
 
13
+ # πŸ”˜ Clear / Retake Photo button
14
  if st.button("πŸ” Clear / Retake Photo"):
15
  st.session_state.clear = True
16
+ st.rerun() # βœ… use st.rerun, not experimental_rerun
17
 
18
+ # 🧼 Skip this run if clearing
19
  if st.session_state.clear:
20
  st.session_state.clear = False
21
+ st.stop()
22
 
 
23
  # πŸ“· Camera Input
 
24
  img_data = st.camera_input("πŸ“· Capture the weight display")
25
 
26
  if img_data:
27
  st.success("βœ… Image captured successfully!")
28
 
 
29
  if len(img_data.getvalue()) > 5 * 1024 * 1024:
30
  st.error("❌ Image too large (>5MB). Please try again.")
31
  st.stop()
 
33
  image = Image.open(img_data)
34
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
35
 
 
36
  with st.spinner("πŸ” Extracting weight..."):
37
  weight, confidence = extract_weight_from_image(image)
38
 
39
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
40
 
 
41
  if not weight or confidence < 80:
42
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
43
  if st.button("πŸ” Retake Photo"):
44
  st.session_state.clear = True
45
+ st.rerun()
46
  st.stop()
47
 
 
48
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
49
 
 
50
  device_id = "BAL-001"
51
+ image_url = ""
52
 
53
  encoded_weight = urllib.parse.quote(str(weight))
54
  encoded_device = urllib.parse.quote(device_id)