Sanjayraju30 commited on
Commit
be301b7
Β·
verified Β·
1 Parent(s): f80289b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +31 -23
src/streamlit_app.py CHANGED
@@ -6,28 +6,34 @@ import urllib.parse
6
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
- # Session state
10
  if "image_data" not in st.session_state:
11
  st.session_state.image_data = None
12
 
13
- # Show camera if no image
 
 
 
 
14
  if st.session_state.image_data is None:
15
  img_data = st.camera_input("πŸ“· Capture the weight display")
16
-
17
- if img_data is not None:
18
  st.session_state.image_data = img_data
 
19
 
20
- # If image exists
21
  if st.session_state.image_data:
22
  st.success("βœ… Image captured successfully!")
23
 
24
- image = Image.open(st.session_state.image_data)
25
- st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
26
 
27
- if len(st.session_state.image_data.getvalue()) > 5 * 1024 * 1024:
28
  st.error("❌ Image too large (>5MB). Please try again.")
29
  st.stop()
30
 
 
 
 
31
  with st.spinner("πŸ” Extracting weight..."):
32
  weight, confidence = extract_weight_from_image(image)
33
 
@@ -35,23 +41,25 @@ if st.session_state.image_data:
35
 
36
  if not weight or confidence < 80:
37
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
 
 
 
 
38
 
39
- else:
40
- st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
41
 
42
- # Salesforce link
43
- device_id = "BAL-001"
44
- image_url = ""
45
 
46
- salesforce_url = (
47
- "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
48
- f"weight_logger_page?WeightInput={urllib.parse.quote(str(weight))}"
49
- f"&DeviceID={urllib.parse.quote(device_id)}&ImageURL={urllib.parse.quote(image_url)}"
50
- )
51
 
52
- st.markdown("### πŸ“€ Send to Salesforce")
53
- st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
 
 
54
 
55
- # Clear/Retake button
56
- if st.button("πŸ” Clear / Retake Photo"):
57
- st.session_state.image_data = None
 
6
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
+ # Initialize session state
10
  if "image_data" not in st.session_state:
11
  st.session_state.image_data = None
12
 
13
+ # Clear button to retake image
14
+ if st.button("πŸ” Clear / Retake Photo"):
15
+ st.session_state.image_data = None
16
+
17
+ # Show camera if no image captured
18
  if st.session_state.image_data is None:
19
  img_data = st.camera_input("πŸ“· Capture the weight display")
20
+ if img_data:
 
21
  st.session_state.image_data = img_data
22
+ st.experimental_rerun() # NOTE: Works fine in most Streamlit cloud spaces
23
 
24
+ # Show captured image and process
25
  if st.session_state.image_data:
26
  st.success("βœ… Image captured successfully!")
27
 
28
+ img_data = st.session_state.image_data
 
29
 
30
+ if len(img_data.getvalue()) > 5 * 1024 * 1024:
31
  st.error("❌ Image too large (>5MB). Please try again.")
32
  st.stop()
33
 
34
+ image = Image.open(img_data)
35
+ st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
36
+
37
  with st.spinner("πŸ” Extracting weight..."):
38
  weight, confidence = extract_weight_from_image(image)
39
 
 
41
 
42
  if not weight or confidence < 80:
43
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
44
+ if st.button("πŸ” Retake Photo"):
45
+ st.session_state.image_data = None
46
+ st.experimental_rerun()
47
+ st.stop()
48
 
49
+ st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
 
50
 
51
+ # 🌐 Salesforce URL
52
+ device_id = "BAL-001"
53
+ image_url = ""
54
 
55
+ encoded_weight = urllib.parse.quote(str(weight))
56
+ encoded_device = urllib.parse.quote(device_id)
57
+ encoded_image_url = urllib.parse.quote(image_url)
 
 
58
 
59
+ salesforce_url = (
60
+ "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
61
+ f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
62
+ )
63
 
64
+ st.markdown("### πŸ“€ Send to Salesforce")
65
+ st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)