Sanjayraju30 commited on
Commit
1d1e3da
Β·
verified Β·
1 Parent(s): c09df48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -5
app.py CHANGED
@@ -2,18 +2,97 @@ import gradio as gr
2
  from datetime import datetime
3
  import pytz
4
  from ocr_engine import extract_weight_from_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def process_image(img):
7
  if img is None:
8
- return "No image uploaded", None, None
9
-
10
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
 
11
  weight, confidence = extract_weight_from_image(img)
12
- return f"{weight} kg (Confidence: {confidence}%)", ist_time, img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  with gr.Blocks(title="βš–οΈ Auto Weight Logger") as demo:
15
  gr.Markdown("## βš–οΈ Auto Weight Logger")
16
- gr.Markdown("πŸ“· Upload or capture an image of a digital weight scale.")
17
 
18
  with gr.Row():
19
  image_input = gr.Image(type="pil", label="Upload / Capture Image")
@@ -23,7 +102,28 @@ with gr.Blocks(title="βš–οΈ Auto Weight Logger") as demo:
23
  timestamp = gr.Textbox(label="πŸ•’ Captured At (IST)")
24
  snapshot = gr.Image(label="πŸ“Έ Snapshot Image")
25
 
 
 
 
 
26
  submit = gr.Button("πŸ” Detect Weight")
27
- submit.click(process_image, inputs=image_input, outputs=[output_weight, timestamp, snapshot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  demo.launch()
 
2
  from datetime import datetime
3
  import pytz
4
  from ocr_engine import extract_weight_from_image
5
+ from PIL import Image
6
+ import io
7
+ import base64
8
+ from simple_salesforce import Salesforce
9
+ import logging
10
+
11
+ # Set up logging
12
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
+
14
+ # Salesforce configuration (replace with your credentials)
15
+ SF_USERNAME = "your_salesforce_username"
16
+ SF_PASSWORD = "your_salesforce_password"
17
+ SF_SECURITY_TOKEN = "your_salesforce_security_token"
18
+ SF_DOMAIN = "login" # or "test" for sandbox
19
+
20
+ def connect_to_salesforce():
21
+ try:
22
+ sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN, domain=SF_DOMAIN)
23
+ logging.info("Connected to Salesforce successfully")
24
+ return sf
25
+ except Exception as e:
26
+ logging.error(f"Salesforce connection failed: {str(e)}")
27
+ return None
28
+
29
+ def resize_image(img, max_size_mb=5):
30
+ """Resize image to ensure size < 5MB while preserving quality."""
31
+ try:
32
+ img_bytes = io.BytesIO()
33
+ img.save(img_bytes, format="PNG")
34
+ size_mb = len(img_bytes.getvalue()) / (1024 * 1024)
35
+ if size_mb <= max_size_mb:
36
+ return img, img_bytes.getvalue()
37
+
38
+ # Resize proportionally
39
+ scale = 0.9
40
+ while size_mb > max_size_mb:
41
+ w, h = img.size
42
+ img = img.resize((int(w * scale), int(h * scale)), Image.Resampling.LANCZOS)
43
+ img_bytes = io.BytesIO()
44
+ img.save(img_bytes, format="PNG")
45
+ size_mb = len(img_bytes.getvalue()) / (1024 * 1024)
46
+ scale *= 0.9
47
+ logging.info(f"Resized image to {size_mb:.2f} MB")
48
+ return img, img_bytes.getvalue()
49
+ except Exception as e:
50
+ logging.error(f"Image resizing failed: {str(e)}")
51
+ return img, None
52
 
53
  def process_image(img):
54
  if img is None:
55
+ return "No image uploaded", None, None, None, gr.update(visible=False), gr.update(visible=False)
56
+
57
  ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
58
+ img, img_bytes = resize_image(img)
59
  weight, confidence = extract_weight_from_image(img)
60
+
61
+ if weight == "Not detected" or confidence < 70:
62
+ return f"{weight} (Confidence: {confidence}%)", ist_time, img, None, gr.update(visible=True), gr.update(visible=False)
63
+
64
+ # Encode image for preview
65
+ img_buffer = io.BytesIO(img_bytes)
66
+ img_base64 = base64.b64encode(img_buffer.getvalue()).decode()
67
+ return f"{weight} kg (Confidence: {confidence}%)", ist_time, img, img_base64, gr.update(visible=True), gr.update(visible=True)
68
+
69
+ def save_to_salesforce(weight_text, img_base64):
70
+ try:
71
+ sf = connect_to_salesforce()
72
+ if sf is None:
73
+ return "Failed to connect to Salesforce"
74
+
75
+ # Extract weight from text (remove "kg" and confidence)
76
+ weight = float(weight_text.split(" ")[0])
77
+ ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
78
+
79
+ # Create custom object record (adjust object and fields as needed)
80
+ record = {
81
+ "Name": f"Weight_Log_{ist_time}",
82
+ "Weight__c": weight,
83
+ "Timestamp__c": ist_time,
84
+ "Image__c": img_base64
85
+ }
86
+ result = sf.Weight_Log__c.create(record)
87
+ logging.info(f"Salesforce record created: {result}")
88
+ return "Successfully saved to Salesforce"
89
+ except Exception as e:
90
+ logging.error(f"Salesforce save failed: {str(e)}")
91
+ return f"Failed to save to Salesforce: {str(e)}"
92
 
93
  with gr.Blocks(title="βš–οΈ Auto Weight Logger") as demo:
94
  gr.Markdown("## βš–οΈ Auto Weight Logger")
95
+ gr.Markdown("πŸ“· Upload or capture an image of a digital weight scale (max 5MB).")
96
 
97
  with gr.Row():
98
  image_input = gr.Image(type="pil", label="Upload / Capture Image")
 
102
  timestamp = gr.Textbox(label="πŸ•’ Captured At (IST)")
103
  snapshot = gr.Image(label="πŸ“Έ Snapshot Image")
104
 
105
+ with gr.Row():
106
+ confirm_button = gr.Button("βœ… Confirm and Save to Salesforce", visible=False)
107
+ status = gr.Textbox(label="Save Status", visible=False)
108
+
109
  submit = gr.Button("πŸ” Detect Weight")
110
+ submit.click(
111
+ fn=process_image,
112
+ inputs=image_input,
113
+ outputs=[output_weight, timestamp, snapshot, gr.State(), confirm_button, status]
114
+ )
115
+ confirm_button.click(
116
+ fn=save_to_salesforce,
117
+ inputs=[output_weight, gr.State()],
118
+ outputs=status
119
+ )
120
+
121
+ gr.Markdown("""
122
+ ### Instructions
123
+ - Upload a clear, well-lit image of a digital weight scale display.
124
+ - Ensure the image is < 5MB (automatically resized if larger).
125
+ - Review the detected weight and click 'Confirm and Save to Salesforce' to log the data.
126
+ - The application works on both desktop and mobile browsers.
127
+ """)
128
 
129
  demo.launch()