Sanjayraju30 commited on
Commit
c4b9b34
Β·
verified Β·
1 Parent(s): 82ae7fe

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +20 -10
src/streamlit_app.py CHANGED
@@ -10,28 +10,38 @@ st.title("βš–οΈ Auto Weight Logger")
10
  # Session state
11
  if "image_data" not in st.session_state:
12
  st.session_state.image_data = None
 
 
13
  if "camera_key" not in st.session_state:
14
  st.session_state.camera_key = str(uuid.uuid4())
15
 
16
- # Clear / Retake
 
 
 
17
  if st.button("πŸ” Clear / Retake Photo"):
18
  st.session_state.image_data = None
19
- st.session_state.camera_key = str(uuid.uuid4()) # new key to force reload
20
 
21
- # Show camera
22
  if st.session_state.image_data is None:
23
- img_data = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
 
 
 
 
24
  if img_data:
25
  st.session_state.image_data = img_data
26
 
27
- # Process image
28
  if st.session_state.image_data:
29
- st.success("βœ… Image captured successfully!")
30
  image = Image.open(st.session_state.image_data)
31
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
32
 
 
33
  if len(st.session_state.image_data.getvalue()) > 5 * 1024 * 1024:
34
- st.error("❌ Image too large (>5MB). Please try again.")
35
  st.stop()
36
 
37
  with st.spinner("πŸ” Extracting weight..."):
@@ -40,12 +50,12 @@ if st.session_state.image_data:
40
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
41
 
42
  if not weight or confidence < 80:
43
- st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
44
  else:
45
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
46
 
47
  device_id = "BAL-001"
48
- image_url = ""
49
 
50
  salesforce_url = (
51
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
@@ -56,6 +66,6 @@ if st.session_state.image_data:
56
  st.markdown("### πŸ“€ Send to Salesforce")
57
  st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
58
 
59
- if st.button("πŸ” Retake Photo"):
60
  st.session_state.image_data = None
61
  st.session_state.camera_key = str(uuid.uuid4())
 
10
  # Session state
11
  if "image_data" not in st.session_state:
12
  st.session_state.image_data = None
13
+ if "input_mode" not in st.session_state:
14
+ st.session_state.input_mode = "Camera"
15
  if "camera_key" not in st.session_state:
16
  st.session_state.camera_key = str(uuid.uuid4())
17
 
18
+ # Choose input mode
19
+ st.radio("πŸ“Έ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
20
+
21
+ # Reset image
22
  if st.button("πŸ” Clear / Retake Photo"):
23
  st.session_state.image_data = None
24
+ st.session_state.camera_key = str(uuid.uuid4()) # Reload webcam
25
 
26
+ # Input section
27
  if st.session_state.image_data is None:
28
+ if st.session_state.input_mode == "Camera":
29
+ img_data = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
30
+ else:
31
+ img_data = st.file_uploader("πŸ“ Upload an image of the weight display", type=["jpg", "jpeg", "png"])
32
+
33
  if img_data:
34
  st.session_state.image_data = img_data
35
 
36
+ # OCR and Display
37
  if st.session_state.image_data:
38
+ st.success("βœ… Image received successfully!")
39
  image = Image.open(st.session_state.image_data)
40
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
41
 
42
+ # Check image size
43
  if len(st.session_state.image_data.getvalue()) > 5 * 1024 * 1024:
44
+ st.error("❌ Image too large (>5MB). Please try a smaller file.")
45
  st.stop()
46
 
47
  with st.spinner("πŸ” Extracting weight..."):
 
50
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
51
 
52
  if not weight or confidence < 80:
53
+ st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please try again.")
54
  else:
55
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
56
 
57
  device_id = "BAL-001"
58
+ image_url = "" # Placeholder for now
59
 
60
  salesforce_url = (
61
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
 
66
  st.markdown("### πŸ“€ Send to Salesforce")
67
  st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
68
 
69
+ if st.button("πŸ” Retake / Upload Another"):
70
  st.session_state.image_data = None
71
  st.session_state.camera_key = str(uuid.uuid4())