Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -26,11 +26,11 @@ def process_image(image):
|
|
26 |
ist = pytz.timezone('Asia/Kolkata')
|
27 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
28 |
|
29 |
-
#
|
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
|
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)
|
@@ -42,16 +42,16 @@ def process_image(image):
|
|
42 |
image_path = "snapshot.jpg"
|
43 |
image.save(image_path)
|
44 |
|
45 |
-
# Create
|
46 |
record = sf.Weight_Log__c.create({
|
47 |
"Captured_Weight__c": numeric_value,
|
48 |
-
"Captured_Unit__c": unit, # ✅
|
49 |
"Captured_At__c": datetime.now(ist).isoformat(),
|
50 |
"Device_ID__c": "DEVICE-001",
|
51 |
"Status__c": "Confirmed"
|
52 |
})
|
53 |
|
54 |
-
# Upload image
|
55 |
with open(image_path, "rb") as f:
|
56 |
encoded_image = base64.b64encode(f.read()).decode("utf-8")
|
57 |
|
|
|
26 |
ist = pytz.timezone('Asia/Kolkata')
|
27 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
28 |
|
29 |
+
# Handle missing or invalid output
|
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 number and unit
|
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)
|
|
|
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, # ✅ Must exist in Salesforce
|
49 |
"Captured_At__c": datetime.now(ist).isoformat(),
|
50 |
"Device_ID__c": "DEVICE-001",
|
51 |
"Status__c": "Confirmed"
|
52 |
})
|
53 |
|
54 |
+
# Upload image
|
55 |
with open(image_path, "rb") as f:
|
56 |
encoded_image = base64.b64encode(f.read()).decode("utf-8")
|
57 |
|