Spaces:
Sleeping
Sleeping
Delete app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208import osfrom paddleocr import PaddleOCRfrom PIL import Imageimport gradio as grimport requestsimport refrom simple_salesforce import Salesforceimport pandas as pdimport matplotlib.pyplot as pltfrom io import BytesIOfrom fuzzywuzzy import processimport base64
# Salesforce credentialsSALESFORCE_USERNAME = "venkatramana@sandbox.com"SALESFORCE_PASSWORD = "Venkat12345@"SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
# Initialize PaddleOCRocr = PaddleOCR(use_angle_cls=True, lang='en')
# Attribute mappings and product names (from the original app.py)# Attribute mappings: readable names to Salesforce API namesATTRIBUTE_MAPPING = { "Product name": "Productname__c", "Colour": "Colour__c", "Motortype": "Motortype__c", "Frequency": "Frequency__c", "Grossweight": "Grossweight__c", "Ratio": "Ratio__c", "MotorFrame": "Motorframe__c", "Model": "Model__c", "Speed": "Speed__c", "Quantity": "Quantity__c", "Voltage": "Voltage__c", "Material": "Material__c", "Type": "Type__c", "Horsepower": "Horsepower__c", "Consignee": "Consignee__c", "LOT": "LOT__c", "Stage": "Stage__c", "Outlet": "Outlet__c", "Serialnumber": "Serialnumber__c", "HeadSize": "Headsize__c", "Deliverysize": "Deliverysize__c", "Phase": "Phase__c", "Size": "Size__c", "MRP": "MRP__c", "Usebefore": "Usebefore__c", "Height": "Height__c", "MaximumDischarge Flow": "Maximumdischargeflow__c", "DischargeRange": "Dischargeflow__c", "Assembledby": "Manufacturer__c", "Manufacturedate": "Manufacturedate__c", "Companyname": "Companyname__c", "Customercarenumber": "Customercarenumber__c", "SellerAddress": "Selleraddress__c", "Selleremail": "Selleremail__c", "GSTIN": "GSTIN__c", "Totalamount": "Totalamount__c", "Paymentstatus": "Paymentstatus__c", "Paymentmethod": "Paymentstatus__c", "Invoicedate": "Manufacturedate__c", "Warranty": "Warranty__c", "Brand": "Brand__c", "Motorhorsepower": "Motorhorsepower__c", "Power": "Power__c", "Motorphase": "Motorphase__c", "Enginetype": "Enginetype__c", "Tankcapacity": "Tankcapacity__c", "Head": "Head__c", "Usage/Application": "Usage_Application__c", "Volts": "volts__c", "Hertz": "Hertz__c", "Frame": "frame__c", "Mounting": "Mounting__c", "Tollfreenumber": "Tollfreenumber__c", "Pipesize": "Pipesize__c", "Manufacturer": "Manufacturer__c", "Office": "Office__c", "SRnumber": "SRnumber__c", "TypeOfEndUse": "TypeOfEndUse__c", "Model Name": "Model_Name_Number__c", "coolingmethod": "coolingmethod__c"}
# List of product names to matchPRODUCT_NAMES = [ "Centrifugal mono block pump", "SINGLE PHASE MOTOR STARTER", "EasyPact EZC 100", "Openwell Submersible Pumpset", "Electric Motor", "Self Priming Pump", "Control panel for single phase submersible pumps", "MOTOR", "Submersible pump set", "Fusion submersible pump set", "DCT", "Shock proof water proof", "CG COMMERCIAL MOTORS", "Fusion", "control panel for single phase submerisible pumps", "single phase digital starter dry run and timer panel", "5HP AV1 XL Kirloskar Pump", "Phase stainless steel submersible pump", "Submersible pump", "WB15X", "Vtype self priming pump", "SP SHINE DISC", "havells submersible pump", "Havells open well Submersible pump", "Bertolini pump CK3 90pp", "WPA 772 Water Pump Assy", "bertolini TTL triplex high pressure plunger pumps", "Generic plunger high pressure pump", "Apple Normal, Banana", "Cast Iron KSb centrifugal pump", "5.5kw Water Pump", "KSB reliable i line centrifuged pumps", "Apple Normal, Orange, Banana", "Positive API 6745 hydraulic diaphragm pump", "1/2 inch Fuel Hose Pipe", "Kirloskar Water Pump", "Rotodel motor pump", "PVC Electrical Insulation Materials", "Electric kirloskar domestic water pump", "Electrical Insulation Materials", "sellowell motor pump", "bhupathi submersible pump set", "Flowshine Submersible pump set", "Index submersible pump", "Wintoss Plastic Electric Switch Board", "Electric 18 watt ujagar cooler pump", "Generator Service", "LG WM FHT1207ZWL, LG REF GL-S292RSCY", "Water tank, Filters, Water Pump", "MS Control Submersible Panel", "Centrifugal Monoblock Pumps", "Electric Motor with Pump BodyBlue and White", "Various Repair and Maintenance Parts", "Earthmax Pump", "Water Tank, Filters, Water Pump", "Centrifugal Water Pump for Agriculture", "mono block pumps"]
# Function to extract text using PaddleOCRdef extract_text(image): result = ocr.ocr(image) extracted_text = [] for line in result[0]: extracted_text.append(line[1][0]) return "\n".join(extracted_text)
# Function to match product namedef match_product_name(extracted_text): best_match = None best_score = 0
for line in extracted_text.split("\n"): match, score = process.extractOne(line, PRODUCT_NAMES) if score > best_score: best_match = match best_score = score
return best_match if best_score >= 70 else None
# Function to process the image and interact with Salesforcedef process_image(image, mode, entry_type, quantity): extracted_text = extract_text(image) if not extracted_text: return "No text detected in the image.", None
product_name = match_product_name(extracted_text) attributes = {} if product_name: attributes["Product name"] = product_name
return f"Extracted Text:\n{extracted_text}", f"Product Name: {product_name}"
# Gradio Interfacedef app(): return gr.Interface( fn=process_image, inputs=[ gr.HTML(""" <div> <h3>Rear Camera Input</h3> <video id='rear-camera' autoplay></video> <button id='capture-btn'>Capture</button> <canvas id='camera-canvas' style='display:none;'></canvas> <script> const video = document.getElementById('rear-camera'); const canvas = document.getElementById('camera-canvas'); const captureBtn = document.getElementById('capture-btn'); navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } }) .then(stream => { video.srcObject = stream; }) .catch(err => { console.error('Error accessing rear camera:', err); alert('Unable to access the rear camera. Please check permissions.'); });
captureBtn.addEventListener('click', () => { const ctx = canvas.getContext('2d'); canvas.width = video.videoWidth; canvas.height = video.videoHeight; ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
const imageData = canvas.toDataURL('image/png'); const imageInput = document.querySelector('input[type="file"]'); fetch(imageData) .then(res => res.blob()) .then(blob => { const file = new File([blob], 'captured_image.png', { type: blob.type }); const dt = new DataTransfer(); dt.items.add(file); imageInput.files = dt.files; imageInput.dispatchEvent(new Event('change', { bubbles: true })); }); }); </script> </div> "), gr.Dropdown(label="Mode", choices=["Entry", "Exit"], value="Entry"), gr.Radio(label="Entry Type", choices=["Sales", "Non-Sales"], value="Sales"), gr.Number(label="Quantity", value=1, interactive=True), ], outputs=[ gr.Text(label="Extracted Text"), gr.Text(label="Result") ], title="Rear Camera with OCR and Salesforce Integration", description="Use the rear camera to capture images, extract text, and interact with Salesforce." )
if __name__ == "__main__":app().launch(share=True)app.pyDeleted-208