Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import pytz
|
|
5 |
from ocr_engine import extract_weight
|
6 |
from simple_salesforce import Salesforce
|
7 |
import base64
|
|
|
8 |
|
9 |
# Salesforce credentials
|
10 |
SF_USERNAME = "[email protected]"
|
@@ -25,20 +26,42 @@ def process_image(image):
|
|
25 |
if not weight or "No valid" in weight:
|
26 |
return "❌ Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
|
27 |
|
28 |
-
# Save image
|
29 |
-
|
30 |
-
|
31 |
-
snapshot_base64 = base64.b64encode(img_file.read()).decode('utf-8')
|
32 |
|
33 |
-
#
|
34 |
-
sf.Weight_Log__c.create({
|
35 |
"Captured_Weight__c": float(weight.replace("kg", "").strip()),
|
36 |
"Captured_At__c": datetime.now(ist).isoformat(),
|
37 |
-
"Snapshot_Image__c": snapshot_base64,
|
38 |
"Device_ID__c": "DEVICE-001",
|
39 |
"Status__c": "Captured"
|
40 |
})
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
return weight, timestamp, image, gr.update(visible=False)
|
43 |
except Exception as e:
|
44 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
@@ -46,7 +69,7 @@ def process_image(image):
|
|
46 |
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
|
47 |
gr.Markdown("""
|
48 |
<h1 style='text-align: center; color: #2e7d32;'>📷 Auto Weight Logger</h1>
|
49 |
-
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR.</p>
|
50 |
<hr style='border: 1px solid #ddd;'/>
|
51 |
""")
|
52 |
|
|
|
5 |
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]"
|
|
|
26 |
if not weight or "No valid" in weight:
|
27 |
return "❌ Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
|
28 |
|
29 |
+
# Save image temporarily
|
30 |
+
image_path = "snapshot.jpg"
|
31 |
+
image.save(image_path)
|
|
|
32 |
|
33 |
+
# Create Weight_Log__c record first
|
34 |
+
record = sf.Weight_Log__c.create({
|
35 |
"Captured_Weight__c": float(weight.replace("kg", "").strip()),
|
36 |
"Captured_At__c": datetime.now(ist).isoformat(),
|
|
|
37 |
"Device_ID__c": "DEVICE-001",
|
38 |
"Status__c": "Captured"
|
39 |
})
|
40 |
|
41 |
+
# Upload image as ContentVersion
|
42 |
+
with open(image_path, "rb") as f:
|
43 |
+
encoded_image = base64.b64encode(f.read()).decode("utf-8")
|
44 |
+
|
45 |
+
# Create ContentVersion
|
46 |
+
content = sf.ContentVersion.create({
|
47 |
+
"Title": f"Snapshot_{timestamp}",
|
48 |
+
"PathOnClient": "snapshot.jpg",
|
49 |
+
"VersionData": encoded_image
|
50 |
+
})
|
51 |
+
|
52 |
+
# Get ContentDocumentId
|
53 |
+
content_id = sf.query(
|
54 |
+
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content['id']}'"
|
55 |
+
)['records'][0]['ContentDocumentId']
|
56 |
+
|
57 |
+
# Link file to Weight_Log__c record via ContentDocumentLink
|
58 |
+
sf.ContentDocumentLink.create({
|
59 |
+
"ContentDocumentId": content_id,
|
60 |
+
"LinkedEntityId": record['id'],
|
61 |
+
"ShareType": "V",
|
62 |
+
"Visibility": "AllUsers"
|
63 |
+
})
|
64 |
+
|
65 |
return weight, timestamp, image, gr.update(visible=False)
|
66 |
except Exception as e:
|
67 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
|
|
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>
|
72 |
+
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and logs it into Salesforce.</p>
|
73 |
<hr style='border: 1px solid #ddd;'/>
|
74 |
""")
|
75 |
|