Sanjayraju30 commited on
Commit
b4bd6e7
·
verified ·
1 Parent(s): 560885d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -26
app.py CHANGED
@@ -2,54 +2,52 @@ import gradio as gr
2
  from PIL import Image
3
  from datetime import datetime
4
  import pytz
5
- import os
6
- from simple_salesforce import Salesforce
7
  from ocr_engine import extract_weight
8
- from dotenv import load_dotenv
 
 
9
 
10
- # Load environment variables
11
- load_dotenv()
 
 
12
 
13
- # Salesforce connection
14
- sf = Salesforce(
15
- username=os.getenv("SF_USERNAME"),
16
- password=os.getenv("SF_PASSWORD"),
17
- security_token=os.getenv("SF_TOKEN")
18
- )
19
 
20
  def process_image(image):
21
  if image is None:
22
  return "❌ No image provided", "", None, gr.update(visible=True)
23
-
24
  try:
25
  weight = extract_weight(image)
26
  ist = pytz.timezone('Asia/Kolkata')
27
- now = datetime.now(ist)
28
- timestamp_display = now.strftime("%Y-%m-%d %H:%M:%S IST")
29
- timestamp_iso = now.isoformat()
30
-
31
  if not weight or "No valid" in weight:
32
  return "❌ Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
33
 
34
- # Create Salesforce record
 
 
 
 
 
 
35
  sf.Weight_Log__c.create({
36
- 'Captured_Weight__c': float(weight.replace("kg", "").strip()),
37
- 'Captured_At__c': timestamp_iso,
38
- 'Device_ID__c': 'Device-01', # Static or dynamic device ID
39
- 'Snapshot_Image__c': 'Manual Entry',
40
- 'Status__c': 'Captured'
41
  })
42
 
43
- return weight, timestamp_display, image, gr.update(visible=False)
44
-
45
  except Exception as e:
46
  return f"Error: {str(e)}", "", None, gr.update(visible=True)
47
 
48
- # Gradio UI
49
  with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
50
  gr.Markdown("""
51
  <h1 style='text-align: center; color: #2e7d32;'>📷 Auto Weight Logger</h1>
52
- <p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and stores the result in Salesforce.</p>
53
  <hr style='border: 1px solid #ddd;'/>
54
  """)
55
 
 
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
 
10
+ # Salesforce credentials (for safety, you can later store these in environment variables)
11
+ SF_USERNAME = "[email protected]"
12
+ SF_PASSWORD = "autoweight@32"
13
+ SF_TOKEN = "UgiHKWT0aoZRX9gvTYDjAiRY"
14
 
15
+ # Connect to Salesforce
16
+ sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_TOKEN)
 
 
 
 
17
 
18
  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
  if not weight or "No valid" in weight:
26
  return "❌ Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
27
 
28
+ # Save snapshot image to base64 (optional field, depends on usage)
29
+ buffered_image = image.copy()
30
+ buffered_image.save("snapshot.jpg")
31
+ with open("snapshot.jpg", "rb") as img_file:
32
+ snapshot_base64 = base64.b64encode(img_file.read()).decode('utf-8')
33
+
34
+ # Create record in Salesforce
35
  sf.Weight_Log__c.create({
36
+ "Captured_Weight__c": float(weight.replace("kg", "").strip()),
37
+ "Captured_At__c": datetime.now(ist).isoformat(),
38
+ "Snapshot_Image__c": snapshot_base64,
39
+ "Device_ID__c": "DEVICE-001", # Static or dynamic based on your design
40
+ "Status__c": "Captured"
41
  })
42
 
43
+ return weight, timestamp, image, gr.update(visible=False)
 
44
  except Exception as e:
45
  return f"Error: {str(e)}", "", None, gr.update(visible=True)
46
 
 
47
  with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
48
  gr.Markdown("""
49
  <h1 style='text-align: center; color: #2e7d32;'>📷 Auto Weight Logger</h1>
50
+ <p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR.</p>
51
  <hr style='border: 1px solid #ddd;'/>
52
  """)
53