Sanjayraju30 commited on
Commit
10cd160
Β·
verified Β·
1 Parent(s): 8348064

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -47
app.py CHANGED
@@ -1,21 +1,3 @@
1
- import gradio as gr
2
- from PIL import Image
3
- from datetime import datetime
4
- import pytz
5
- from ocr_engine import extract_weight
6
- from simple_salesforce import Salesforce
7
- import base64
8
- import os
9
- import re
10
-
11
- # Salesforce credentials
12
- SF_USERNAME = "[email protected]"
13
- SF_PASSWORD = "autoweight@32"
14
- SF_TOKEN = "UgiHKWT0aoZRX9gvTYDjAiRY"
15
-
16
- # Connect to Salesforce
17
- sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_TOKEN)
18
-
19
  def process_image(image):
20
  if image is None:
21
  return "❌ No image provided", "", None, gr.update(visible=True)
@@ -26,7 +8,7 @@ def process_image(image):
26
  ist = pytz.timezone('Asia/Kolkata')
27
  timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
28
 
29
- # Handle missing or invalid output
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
 
@@ -42,16 +24,16 @@ def process_image(image):
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, # βœ… Must exist in Salesforce
49
  "Captured_At__c": datetime.now(ist).isoformat(),
50
  "Device_ID__c": "DEVICE-001",
51
  "Status__c": "Confirmed"
52
  })
53
 
54
- # Upload image
55
  with open(image_path, "rb") as f:
56
  encoded_image = base64.b64encode(f.read()).decode("utf-8")
57
 
@@ -75,28 +57,3 @@ def process_image(image):
75
  return weight, timestamp, image, gr.update(visible=False)
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>
82
- <p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and logs it into Salesforce.</p>
83
- <hr style='border: 1px solid #ddd;'/>
84
- """)
85
-
86
- with gr.Row():
87
- image_input = gr.Image(type="pil", label="πŸ“ Upload or Capture Image")
88
-
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")
96
- retake_btn = gr.Button("πŸ” Retake / Try Again", visible=False)
97
-
98
- detect_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot, retake_btn])
99
- retake_btn.click(fn=lambda: ("", "", None, gr.update(visible=False)),
100
- inputs=[], outputs=[weight_out, time_out, snapshot, retake_btn])
101
-
102
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def process_image(image):
2
  if image is None:
3
  return "❌ No image provided", "", None, gr.update(visible=True)
 
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
 
 
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,
31
  "Captured_At__c": datetime.now(ist).isoformat(),
32
  "Device_ID__c": "DEVICE-001",
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
  return weight, timestamp, image, gr.update(visible=False)
58
  except Exception as e:
59
  return f"Error: {str(e)}", "", None, gr.update(visible=True)