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 numeric
|
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)
|
@@ -38,20 +38,20 @@ def process_image(image):
|
|
38 |
numeric_value = float(numeric_match.group())
|
39 |
unit = "kg" if "kg" in weight.lower() else "g"
|
40 |
|
41 |
-
# Save 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 |
|
@@ -79,7 +79,7 @@ def process_image(image):
|
|
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>
|
82 |
-
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR.</p>
|
83 |
<hr style='border: 1px solid #ddd;'/>
|
84 |
""")
|
85 |
|
|
|
26 |
ist = pytz.timezone('Asia/Kolkata')
|
27 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
28 |
|
29 |
+
# Check for valid weight
|
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 part 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)
|
|
|
38 |
numeric_value = float(numeric_match.group())
|
39 |
unit = "kg" if "kg" in weight.lower() else "g"
|
40 |
|
41 |
+
# Save image
|
42 |
image_path = "snapshot.jpg"
|
43 |
image.save(image_path)
|
44 |
|
45 |
+
# Create Salesforce record
|
46 |
record = sf.Weight_Log__c.create({
|
47 |
"Captured_Weight__c": numeric_value,
|
48 |
+
"Captured_Unit__c": unit, # ✅ make sure this exists 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 to Salesforce
|
55 |
with open(image_path, "rb") as f:
|
56 |
encoded_image = base64.b64encode(f.read()).decode("utf-8")
|
57 |
|
|
|
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>
|
82 |
+
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and logs it into Salesforce.</p>
|
83 |
<hr style='border: 1px solid #ddd;'/>
|
84 |
""")
|
85 |
|