File size: 2,674 Bytes
0590b95
d55b56b
 
9bbac2d
7926822
 
d55b56b
7926822
 
 
 
 
 
 
 
 
 
 
0590b95
d55b56b
a4391a6
9965439
7926822
91bcc4e
 
9bbac2d
7926822
 
 
2e709f8
b6c69fc
121b79e
2e709f8
7926822
 
 
 
 
 
 
 
 
 
 
91bcc4e
2e709f8
d55b56b
7926822
77bb0dd
 
 
7926822
77bb0dd
 
 
 
b61209f
77bb0dd
b61209f
136c114
77bb0dd
b6c69fc
2bbc794
9bbac2d
77bb0dd
2e709f8
f32159a
2e709f8
 
 
3a4eb5d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
from PIL import Image
from datetime import datetime
import pytz
import os
from simple_salesforce import Salesforce
from ocr_engine import extract_weight
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Salesforce connection
sf = Salesforce(
    username=os.getenv("SF_USERNAME"),
    password=os.getenv("SF_PASSWORD"),
    security_token=os.getenv("SF_TOKEN")
)

def process_image(image):
    if image is None:
        return "❌ No image provided", "", None, gr.update(visible=True)

    try:
        weight = extract_weight(image)
        ist = pytz.timezone('Asia/Kolkata')
        now = datetime.now(ist)
        timestamp_display = now.strftime("%Y-%m-%d %H:%M:%S IST")
        timestamp_iso = now.isoformat()

        if not weight or "No valid" in weight:
            return "❌ Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)

        # Create Salesforce record
        sf.Weight_Log__c.create({
            'Captured_Weight__c': float(weight.replace("kg", "").strip()),
            'Captured_At__c': timestamp_iso,
            'Device_ID__c': 'Device-01',  # Static or dynamic device ID
            'Snapshot_Image__c': 'Manual Entry',
            'Status__c': 'Captured'
        })

        return weight, timestamp_display, image, gr.update(visible=False)

    except Exception as e:
        return f"Error: {str(e)}", "", None, gr.update(visible=True)

# Gradio UI
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
    gr.Markdown("""
    <h1 style='text-align: center; color: #2e7d32;'>πŸ“· Auto Weight Logger</h1>
    <p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and stores the result in Salesforce.</p>
    <hr style='border: 1px solid #ddd;'/>
    """)

    with gr.Row():
        image_input = gr.Image(type="pil", label="πŸ“ Upload or Capture Image")

    detect_btn = gr.Button("πŸš€ Detect Weight")

    with gr.Row():
        weight_out = gr.Textbox(label="πŸ“¦ Detected Weight", placeholder="e.g., 97.9 kg", show_copy_button=True)
        time_out = gr.Textbox(label="πŸ•’ Captured At (IST)", placeholder="e.g., 2025-07-01 12:00:00")

    snapshot = gr.Image(label="πŸ“Έ Snapshot Preview")
    retake_btn = gr.Button("πŸ” Retake / Try Again", visible=False)

    detect_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot, retake_btn])
    retake_btn.click(fn=lambda: ("", "", None, gr.update(visible=False)),
                     inputs=[], outputs=[weight_out, time_out, snapshot, retake_btn])

demo.launch()