Sanjayraju30 commited on
Commit
f50878a
Β·
verified Β·
1 Parent(s): 25cb585

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -6,6 +6,7 @@ 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]"
@@ -19,22 +20,32 @@ def process_image(image):
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
 
28
- if not weight or "No valid" in weight:
 
29
  return "❌ Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
30
 
 
 
 
 
 
 
 
 
31
  # Save image temporarily
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"
@@ -65,7 +76,6 @@ def process_image(image):
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>
@@ -79,7 +89,7 @@ with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: whi
79
  detect_btn = gr.Button("πŸš€ Detect Weight")
80
 
81
  with gr.Row():
82
- weight_out = gr.Textbox(label="πŸ“¦ Detected Weight", placeholder="e.g., 97.9 kg", show_copy_button=True)
83
  time_out = gr.Textbox(label="πŸ•’ Captured At (IST)", placeholder="e.g., 2025-07-01 12:00:00")
84
 
85
  snapshot = gr.Image(label="πŸ“Έ Snapshot Preview")
 
6
  from simple_salesforce import Salesforce
7
  import base64
8
  import os
9
+ import re
10
 
11
  # Salesforce credentials
12
  SF_USERNAME = "[email protected]"
 
20
  if image is None:
21
  return "❌ No image provided", "", None, gr.update(visible=True)
22
  try:
 
23
  weight = extract_weight(image)
24
+ print("🧠 Final OCR Result:", weight)
25
 
26
  ist = pytz.timezone('Asia/Kolkata')
27
  timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
28
 
29
+ # Allow valid results even if debug message is included
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 value
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)
37
+
38
+ numeric_value = float(numeric_match.group())
39
+ unit = "kg" if "kg" in weight.lower() else "g"
40
+
41
  # Save image temporarily
42
  image_path = "snapshot.jpg"
43
  image.save(image_path)
44
 
45
  # Create Weight_Log__c record
46
  record = sf.Weight_Log__c.create({
47
+ "Captured_Weight__c": numeric_value,
48
+ "Captured_Unit__c": unit, # Optional: create this field in Salesforce
49
  "Captured_At__c": datetime.now(ist).isoformat(),
50
  "Device_ID__c": "DEVICE-001",
51
  "Status__c": "Confirmed"
 
76
  except Exception as e:
77
  return f"Error: {str(e)}", "", None, gr.update(visible=True)
78
 
 
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>
 
89
  detect_btn = gr.Button("πŸš€ Detect Weight")
90
 
91
  with gr.Row():
92
+ weight_out = gr.Textbox(label="πŸ“¦ Detected Weight", placeholder="e.g., 75.5 kg", show_copy_button=True)
93
  time_out = gr.Textbox(label="πŸ•’ Captured At (IST)", placeholder="e.g., 2025-07-01 12:00:00")
94
 
95
  snapshot = gr.Image(label="πŸ“Έ Snapshot Preview")