import gradio as gr from PIL import Image from datetime import datetime import pytz from ocr_engine import extract_weight from simple_salesforce import Salesforce import base64 import os # Salesforce credentials (for safety, you can later store these in environment variables) SF_USERNAME = "Autoweightlogger@sathkrutha.com" SF_PASSWORD = "autoweight@32" SF_TOKEN = "UgiHKWT0aoZRX9gvTYDjAiRY" # Connect to Salesforce sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=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') timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST") if not weight or "No valid" in weight: return "❌ Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True) # Save snapshot image to base64 (optional field, depends on usage) buffered_image = image.copy() buffered_image.save("snapshot.jpg") with open("snapshot.jpg", "rb") as img_file: snapshot_base64 = base64.b64encode(img_file.read()).decode('utf-8') # Create record in Salesforce sf.Weight_Log__c.create({ "Captured_Weight__c": float(weight.replace("kg", "").strip()), "Captured_At__c": datetime.now(ist).isoformat(), "Snapshot_Image__c": snapshot_base64, "Device_ID__c": "DEVICE-001", # Static or dynamic based on your design "Status__c": "Captured" }) return weight, timestamp, image, gr.update(visible=False) except Exception as e: return f"Error: {str(e)}", "", None, gr.update(visible=True) with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo: gr.Markdown("""
Upload or capture a digital weight image. Detects weight using AI OCR.