Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def process_image(image):
|
2 |
if image is None:
|
3 |
return "β No image provided", "", None, gr.update(visible=True)
|
@@ -8,11 +27,9 @@ def process_image(image):
|
|
8 |
ist = pytz.timezone('Asia/Kolkata')
|
9 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
10 |
|
11 |
-
# Handle fallback errors
|
12 |
if not weight or ("No valid" in weight and "OCR:" not in weight):
|
13 |
return "β Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
|
14 |
|
15 |
-
# Extract number and unit
|
16 |
numeric_match = re.search(r'\d{1,5}(?:\.\d{1,3})?', weight)
|
17 |
if not numeric_match:
|
18 |
return "β Could not extract number", "", image, gr.update(visible=True)
|
@@ -20,11 +37,9 @@ def process_image(image):
|
|
20 |
numeric_value = float(numeric_match.group())
|
21 |
unit = "kg" if "kg" in weight.lower() else "g"
|
22 |
|
23 |
-
# Save image
|
24 |
image_path = "snapshot.jpg"
|
25 |
image.save(image_path)
|
26 |
|
27 |
-
# Create Salesforce record
|
28 |
record = sf.Weight_Log__c.create({
|
29 |
"Captured_Weight__c": numeric_value,
|
30 |
"Captured_Unit__c": unit,
|
@@ -33,7 +48,6 @@ def process_image(image):
|
|
33 |
"Status__c": "Confirmed"
|
34 |
})
|
35 |
|
36 |
-
# Upload image to Salesforce
|
37 |
with open(image_path, "rb") as f:
|
38 |
encoded_image = base64.b64encode(f.read()).decode("utf-8")
|
39 |
|
@@ -57,3 +71,29 @@ def process_image(image):
|
|
57 |
return weight, timestamp, image, gr.update(visible=False)
|
58 |
except Exception as e:
|
59 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
from datetime import datetime
|
5 |
+
import pytz
|
6 |
+
from ocr_engine import extract_weight
|
7 |
+
from simple_salesforce import Salesforce
|
8 |
+
import base64
|
9 |
+
import re
|
10 |
+
import os
|
11 |
+
|
12 |
+
# Salesforce credentials
|
13 |
+
SF_USERNAME = "[email protected]"
|
14 |
+
SF_PASSWORD = "autoweight@32"
|
15 |
+
SF_TOKEN = "UgiHKWT0aoZRX9gvTYDjAiRY"
|
16 |
+
|
17 |
+
# Connect to Salesforce
|
18 |
+
sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_TOKEN)
|
19 |
+
|
20 |
def process_image(image):
|
21 |
if image is None:
|
22 |
return "β No image provided", "", None, gr.update(visible=True)
|
|
|
27 |
ist = pytz.timezone('Asia/Kolkata')
|
28 |
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
|
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 |
numeric_match = re.search(r'\d{1,5}(?:\.\d{1,3})?', weight)
|
34 |
if not numeric_match:
|
35 |
return "β Could not extract number", "", image, gr.update(visible=True)
|
|
|
37 |
numeric_value = float(numeric_match.group())
|
38 |
unit = "kg" if "kg" in weight.lower() else "g"
|
39 |
|
|
|
40 |
image_path = "snapshot.jpg"
|
41 |
image.save(image_path)
|
42 |
|
|
|
43 |
record = sf.Weight_Log__c.create({
|
44 |
"Captured_Weight__c": numeric_value,
|
45 |
"Captured_Unit__c": unit,
|
|
|
48 |
"Status__c": "Confirmed"
|
49 |
})
|
50 |
|
|
|
51 |
with open(image_path, "rb") as f:
|
52 |
encoded_image = base64.b64encode(f.read()).decode("utf-8")
|
53 |
|
|
|
71 |
return weight, timestamp, image, gr.update(visible=False)
|
72 |
except Exception as e:
|
73 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
74 |
+
|
75 |
+
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
|
76 |
+
gr.Markdown("""
|
77 |
+
<h1 style='text-align: center; color: #2e7d32;'>π· Auto Weight Logger</h1>
|
78 |
+
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and logs it into Salesforce.</p>
|
79 |
+
<hr style='border: 1px solid #ddd;' />
|
80 |
+
""")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
image_input = gr.Image(type="pil", label="π Upload or Capture Image")
|
84 |
+
|
85 |
+
detect_btn = gr.Button("π Detect Weight")
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
weight_out = gr.Textbox(label="π¦ Detected Weight", placeholder="e.g., 75.5 kg", show_copy_button=True)
|
89 |
+
time_out = gr.Textbox(label="π Captured At (IST)", placeholder="e.g., 2025-07-01 12:00:00")
|
90 |
+
|
91 |
+
snapshot = gr.Image(label="πΈ Snapshot Preview")
|
92 |
+
retake_btn = gr.Button("π Retake / Try Again", visible=False)
|
93 |
+
|
94 |
+
detect_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot, retake_btn])
|
95 |
+
retake_btn.click(fn=lambda: ("", "", None, gr.update(visible=False)),
|
96 |
+
inputs=[], outputs=[weight_out, time_out, snapshot, retake_btn])
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
demo.launch()
|