Sanjayraju30 commited on
Commit
ae762c2
Β·
verified Β·
1 Parent(s): 334cd1a

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +59 -26
src/streamlit_app.py CHANGED
@@ -1,30 +1,63 @@
1
  import streamlit as st
2
  from PIL import Image
 
 
3
 
4
- st.title("πŸ“Έ Auto Weight Logger with Retake Option")
5
-
6
- # Session state to track if image is captured
7
- if 'captured' not in st.session_state:
8
- st.session_state.captured = False
9
- if 'image' not in st.session_state:
10
- st.session_state.image = None
11
-
12
- # Function to reset camera
13
- def retake():
14
- st.session_state.captured = False
15
- st.session_state.image = None
16
-
17
- # Only show camera if image is not captured
18
- if not st.session_state.captured:
19
- image = st.camera_input("Take a picture")
20
- if image:
21
- st.session_state.image = image
22
- st.session_state.captured = True
23
-
24
- # If image is captured, show options
25
- if st.session_state.captured and st.session_state.image:
26
- st.image(st.session_state.image, caption="Captured Image", use_column_width=True)
27
- st.button("Retake", on_click=retake)
28
- st.success("βœ… Image captured successfully.")
29
- # You can add OCR or Salesforce upload button here
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from PIL import Image
3
+ from ocr_engine import extract_weight_from_image
4
+ import urllib.parse
5
 
6
+ st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
7
+ st.title("βš–οΈ Auto Weight Logger")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # βœ… Session state to control camera
10
+ if "retake" not in st.session_state:
11
+ st.session_state.retake = False
12
+
13
+ # πŸ”˜ Retake Button
14
+ if st.button("πŸ” Clear / Retake Photo"):
15
+ st.session_state.retake = True
16
+ st.rerun()
17
+
18
+ # βœ… If this is the rerun after clearing, reset and skip camera once
19
+ if st.session_state.retake:
20
+ st.session_state.retake = False
21
+ st.rerun()
22
+
23
+ # βœ… At this point, camera will always be shown
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()
32
+
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.retake = 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 = "" # Optional
52
+
53
+ encoded_weight = urllib.parse.quote(str(weight))
54
+ encoded_device = urllib.parse.quote(device_id)
55
+ encoded_image_url = urllib.parse.quote(image_url)
56
+
57
+ salesforce_url = (
58
+ "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
59
+ f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
60
+ )
61
+
62
+ st.markdown("### πŸ“€ Send to Salesforce")
63
+ st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)