Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from ocr_engine import extract_weight
|
|
6 |
from simple_salesforce import Salesforce
|
7 |
import base64
|
8 |
import os
|
|
|
9 |
|
10 |
# Salesforce credentials
|
11 |
SF_USERNAME = "[email protected]"
|
@@ -19,22 +20,32 @@ def process_image(image):
|
|
19 |
if image is None:
|
20 |
return "β No image provided", "", None, gr.update(visible=True)
|
21 |
try:
|
22 |
-
# Extract weight using OCR (your updated function should handle any clear image)
|
23 |
weight = extract_weight(image)
|
|
|
24 |
|
25 |
ist = pytz.timezone('Asia/Kolkata')
|
26 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
27 |
|
28 |
-
|
|
|
29 |
return "β Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Save image temporarily
|
32 |
image_path = "snapshot.jpg"
|
33 |
image.save(image_path)
|
34 |
|
35 |
# Create Weight_Log__c record
|
36 |
record = sf.Weight_Log__c.create({
|
37 |
-
"Captured_Weight__c":
|
|
|
38 |
"Captured_At__c": datetime.now(ist).isoformat(),
|
39 |
"Device_ID__c": "DEVICE-001",
|
40 |
"Status__c": "Confirmed"
|
@@ -65,7 +76,6 @@ def process_image(image):
|
|
65 |
except Exception as e:
|
66 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
67 |
|
68 |
-
# Updated header: Removed "and logs it into Salesforce"
|
69 |
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
|
70 |
gr.Markdown("""
|
71 |
<h1 style='text-align: center; color: #2e7d32;'>π· Auto Weight Logger</h1>
|
@@ -79,7 +89,7 @@ with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: whi
|
|
79 |
detect_btn = gr.Button("π Detect Weight")
|
80 |
|
81 |
with gr.Row():
|
82 |
-
weight_out = gr.Textbox(label="π¦ Detected Weight", placeholder="e.g.,
|
83 |
time_out = gr.Textbox(label="π Captured At (IST)", placeholder="e.g., 2025-07-01 12:00:00")
|
84 |
|
85 |
snapshot = gr.Image(label="πΈ Snapshot Preview")
|
|
|
6 |
from simple_salesforce import Salesforce
|
7 |
import base64
|
8 |
import os
|
9 |
+
import re
|
10 |
|
11 |
# Salesforce credentials
|
12 |
SF_USERNAME = "[email protected]"
|
|
|
20 |
if image is None:
|
21 |
return "β No image provided", "", None, gr.update(visible=True)
|
22 |
try:
|
|
|
23 |
weight = extract_weight(image)
|
24 |
+
print("π§ Final OCR Result:", weight)
|
25 |
|
26 |
ist = pytz.timezone('Asia/Kolkata')
|
27 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
28 |
|
29 |
+
# Allow valid results even if debug message is included
|
30 |
+
if not weight or ("No valid" in weight and "OCR:" not in weight):
|
31 |
return "β Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
|
32 |
|
33 |
+
# Extract numeric value
|
34 |
+
numeric_match = re.search(r'\d{1,5}(?:\.\d{1,3})?', weight)
|
35 |
+
if not numeric_match:
|
36 |
+
return "β Could not extract number", "", image, gr.update(visible=True)
|
37 |
+
|
38 |
+
numeric_value = float(numeric_match.group())
|
39 |
+
unit = "kg" if "kg" in weight.lower() else "g"
|
40 |
+
|
41 |
# Save image temporarily
|
42 |
image_path = "snapshot.jpg"
|
43 |
image.save(image_path)
|
44 |
|
45 |
# Create Weight_Log__c record
|
46 |
record = sf.Weight_Log__c.create({
|
47 |
+
"Captured_Weight__c": numeric_value,
|
48 |
+
"Captured_Unit__c": unit, # Optional: create this field in Salesforce
|
49 |
"Captured_At__c": datetime.now(ist).isoformat(),
|
50 |
"Device_ID__c": "DEVICE-001",
|
51 |
"Status__c": "Confirmed"
|
|
|
76 |
except Exception as e:
|
77 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
78 |
|
|
|
79 |
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
|
80 |
gr.Markdown("""
|
81 |
<h1 style='text-align: center; color: #2e7d32;'>π· Auto Weight Logger</h1>
|
|
|
89 |
detect_btn = gr.Button("π Detect Weight")
|
90 |
|
91 |
with gr.Row():
|
92 |
+
weight_out = gr.Textbox(label="π¦ Detected Weight", placeholder="e.g., 75.5 kg", show_copy_button=True)
|
93 |
time_out = gr.Textbox(label="π Captured At (IST)", placeholder="e.g., 2025-07-01 12:00:00")
|
94 |
|
95 |
snapshot = gr.Image(label="πΈ Snapshot Preview")
|