Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,9 @@ def process_image(image):
|
|
19 |
if image is None:
|
20 |
return "❌ No image provided", "", None, gr.update(visible=True)
|
21 |
try:
|
|
|
22 |
weight = extract_weight(image)
|
|
|
23 |
ist = pytz.timezone('Asia/Kolkata')
|
24 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
25 |
|
@@ -30,12 +32,12 @@ def process_image(image):
|
|
30 |
image_path = "snapshot.jpg"
|
31 |
image.save(image_path)
|
32 |
|
33 |
-
# Create Weight_Log__c record
|
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": "Confirmed"
|
39 |
})
|
40 |
|
41 |
# Upload image as ContentVersion
|
@@ -48,12 +50,10 @@ def process_image(image):
|
|
48 |
"VersionData": encoded_image
|
49 |
})
|
50 |
|
51 |
-
# Get ContentDocumentId
|
52 |
content_id = sf.query(
|
53 |
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content['id']}'"
|
54 |
)['records'][0]['ContentDocumentId']
|
55 |
|
56 |
-
# Link file to Weight_Log__c record via ContentDocumentLink
|
57 |
sf.ContentDocumentLink.create({
|
58 |
"ContentDocumentId": content_id,
|
59 |
"LinkedEntityId": record['id'],
|
@@ -65,10 +65,11 @@ def process_image(image):
|
|
65 |
except Exception as e:
|
66 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
67 |
|
|
|
68 |
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
|
69 |
gr.Markdown("""
|
70 |
<h1 style='text-align: center; color: #2e7d32;'>📷 Auto Weight Logger</h1>
|
71 |
-
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR
|
72 |
<hr style='border: 1px solid #ddd;'/>
|
73 |
""")
|
74 |
|
|
|
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 |
|
|
|
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": float(weight.replace("kg", "").strip()),
|
38 |
"Captured_At__c": datetime.now(ist).isoformat(),
|
39 |
"Device_ID__c": "DEVICE-001",
|
40 |
+
"Status__c": "Confirmed"
|
41 |
})
|
42 |
|
43 |
# Upload image as ContentVersion
|
|
|
50 |
"VersionData": encoded_image
|
51 |
})
|
52 |
|
|
|
53 |
content_id = sf.query(
|
54 |
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content['id']}'"
|
55 |
)['records'][0]['ContentDocumentId']
|
56 |
|
|
|
57 |
sf.ContentDocumentLink.create({
|
58 |
"ContentDocumentId": content_id,
|
59 |
"LinkedEntityId": record['id'],
|
|
|
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>
|
72 |
+
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR.</p>
|
73 |
<hr style='border: 1px solid #ddd;'/>
|
74 |
""")
|
75 |
|