Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +12 -11
src/streamlit_app.py
CHANGED
@@ -3,50 +3,51 @@ 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 |
img_data = st.camera_input("π· Capture the weight display")
|
10 |
|
11 |
if img_data:
|
12 |
-
# β
|
13 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
14 |
st.error("β Image too large (>5MB). Please try again.")
|
15 |
st.stop()
|
16 |
|
17 |
-
# β
|
18 |
image = Image.open(img_data)
|
19 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
20 |
|
21 |
-
# β
|
22 |
with st.spinner("π Extracting weight..."):
|
23 |
weight, confidence = extract_weight_from_image(image)
|
24 |
|
25 |
-
# β
|
|
|
|
|
|
|
26 |
if not weight or confidence < 80:
|
27 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the image.")
|
28 |
if st.button("π Try Again"):
|
29 |
st.experimental_rerun()
|
30 |
st.stop()
|
31 |
|
32 |
-
# β
Show
|
33 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
34 |
|
35 |
-
# β
|
36 |
device_id = "BAL-001"
|
37 |
-
image_url = ""
|
38 |
|
39 |
-
# β
Encode URL parameters
|
40 |
encoded_weight = urllib.parse.quote(str(weight))
|
41 |
encoded_device = urllib.parse.quote(device_id)
|
42 |
encoded_image_url = urllib.parse.quote(image_url)
|
43 |
|
44 |
-
# β
Salesforce link
|
45 |
salesforce_url = (
|
46 |
f"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
47 |
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
48 |
)
|
49 |
|
50 |
-
# β
Show
|
51 |
st.markdown("### π€ Send to Salesforce")
|
52 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
|
|
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 |
img_data = st.camera_input("π· Capture the weight display")
|
10 |
|
11 |
if img_data:
|
12 |
+
# β
Check file size (BRD requirement)
|
13 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
14 |
st.error("β Image too large (>5MB). Please try again.")
|
15 |
st.stop()
|
16 |
|
17 |
+
# β
Show the image to user
|
18 |
image = Image.open(img_data)
|
19 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
20 |
|
21 |
+
# β
Run OCR to get weight and confidence
|
22 |
with st.spinner("π Extracting weight..."):
|
23 |
weight, confidence = extract_weight_from_image(image)
|
24 |
|
25 |
+
# β
DEBUG: Uncomment this to see raw output
|
26 |
+
# st.write(f"DEBUG: weight = {weight}, confidence = {confidence}")
|
27 |
+
|
28 |
+
# β
Handle OCR failure or low confidence
|
29 |
if not weight or confidence < 80:
|
30 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the image.")
|
31 |
if st.button("π Try Again"):
|
32 |
st.experimental_rerun()
|
33 |
st.stop()
|
34 |
|
35 |
+
# β
Show success with weight and confidence
|
36 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
37 |
|
38 |
+
# β
Prepare Salesforce link
|
39 |
device_id = "BAL-001"
|
40 |
+
image_url = ""
|
41 |
|
|
|
42 |
encoded_weight = urllib.parse.quote(str(weight))
|
43 |
encoded_device = urllib.parse.quote(device_id)
|
44 |
encoded_image_url = urllib.parse.quote(image_url)
|
45 |
|
|
|
46 |
salesforce_url = (
|
47 |
f"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
48 |
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
49 |
)
|
50 |
|
51 |
+
# β
Show Salesforce confirmation link
|
52 |
st.markdown("### π€ Send to Salesforce")
|
53 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|