Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,44 +11,117 @@ from io import BytesIO
|
|
| 11 |
from fuzzywuzzy import process
|
| 12 |
import kaleido # Ensure kaleido is imported
|
| 13 |
|
| 14 |
-
#
|
| 15 |
ATTRIBUTE_MAPPING = {
|
| 16 |
"Product name": "Productname__c",
|
| 17 |
-
"
|
| 18 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"Voltage": "Voltage__c",
|
| 20 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"Stage": "Stage__c",
|
| 22 |
-
"
|
| 23 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
-
#
|
| 27 |
PRODUCT_NAMES = [
|
| 28 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
]
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"""
|
| 40 |
-
result = ocr.ocr(image_path)
|
| 41 |
extracted_text = []
|
| 42 |
-
|
| 43 |
for line in result[0]:
|
| 44 |
-
extracted_text.append(line[1][0])
|
| 45 |
-
|
| 46 |
return "\n".join(extracted_text)
|
| 47 |
|
|
|
|
| 48 |
def match_product_name(extracted_text):
|
| 49 |
-
"""
|
| 50 |
-
Uses fuzzy matching to find the best product name match from extracted text.
|
| 51 |
-
"""
|
| 52 |
best_match = None
|
| 53 |
best_score = 0
|
| 54 |
|
|
@@ -60,45 +133,6 @@ def match_product_name(extracted_text):
|
|
| 60 |
|
| 61 |
return best_match if best_score >= 70 else None # Threshold of 70 for a match
|
| 62 |
|
| 63 |
-
def match_model_name(extracted_text):
|
| 64 |
-
"""
|
| 65 |
-
Uses fuzzy matching to find the best model name match from extracted text.
|
| 66 |
-
"""
|
| 67 |
-
best_match = None
|
| 68 |
-
best_score = 0
|
| 69 |
-
|
| 70 |
-
for line in extracted_text.split("\n"):
|
| 71 |
-
match, score = process.extractOne(line, MODEL_NAMES)
|
| 72 |
-
if score > best_score:
|
| 73 |
-
best_match = match
|
| 74 |
-
best_score = score
|
| 75 |
-
|
| 76 |
-
return best_match if best_score >= 70 else None # Threshold of 70 for a match
|
| 77 |
-
|
| 78 |
-
def extract_attributes(extracted_text):
|
| 79 |
-
"""
|
| 80 |
-
Extracts key attributes like Model, H.P, Stage, Voltage, etc., from the extracted text.
|
| 81 |
-
"""
|
| 82 |
-
attributes = {}
|
| 83 |
-
|
| 84 |
-
for text in extracted_text.split("\n"):
|
| 85 |
-
if "Model" in text:
|
| 86 |
-
attributes["Model"] = text.split("Model")[-1].strip(" :")
|
| 87 |
-
elif "H.P" in text or "HP" in text:
|
| 88 |
-
attributes["H.P"] = text.split("H.P")[-1].strip(" :")
|
| 89 |
-
elif "Stage" in text:
|
| 90 |
-
attributes["Stage"] = text.split("Stage")[-1].strip(" :")
|
| 91 |
-
elif "Outlet" in text:
|
| 92 |
-
attributes["Outlet"] = text.split("Outlet")[-1].strip(" :")
|
| 93 |
-
elif "Voltage" in text:
|
| 94 |
-
attributes["Voltage"] = text.split("Voltage")[-1].strip(" :")
|
| 95 |
-
elif "Phase" in text:
|
| 96 |
-
attributes["Phase"] = text.split("Phase")[-1].strip(" :")
|
| 97 |
-
elif "Year of Mfg" in text:
|
| 98 |
-
attributes["Year of Mfg"] = text.split("Year of Mfg")[-1].strip(" :")
|
| 99 |
-
|
| 100 |
-
return attributes
|
| 101 |
-
|
| 102 |
# Function to extract attributes and their values
|
| 103 |
def extract_attributes(extracted_text):
|
| 104 |
attributes = {}
|
|
|
|
| 11 |
from fuzzywuzzy import process
|
| 12 |
import kaleido # Ensure kaleido is imported
|
| 13 |
|
| 14 |
+
# Attribute mappings: readable names to Salesforce API names
|
| 15 |
ATTRIBUTE_MAPPING = {
|
| 16 |
"Product name": "Productname__c",
|
| 17 |
+
"Colour": "Colour__c",
|
| 18 |
+
"Motortype": "Motortype__c",
|
| 19 |
+
"Frequency": "Frequency__c",
|
| 20 |
+
"Grossweight": "Grossweight__c",
|
| 21 |
+
"Ratio": "Ratio__c",
|
| 22 |
+
"MotorFrame": "Motorframe__c",
|
| 23 |
+
"Model": "Model__c",
|
| 24 |
+
"Speed": "Speed__c",
|
| 25 |
+
"Quantity": "Quantity__c",
|
| 26 |
"Voltage": "Voltage__c",
|
| 27 |
+
"Material": "Material__c",
|
| 28 |
+
"Type": "Type__c",
|
| 29 |
+
"Horsepower": "Horsepower__c",
|
| 30 |
+
"Consignee": "Consignee__c",
|
| 31 |
+
"LOT": "LOT__c",
|
| 32 |
"Stage": "Stage__c",
|
| 33 |
+
"Outlet": "Outlet__c",
|
| 34 |
+
"Serialnumber": "Serialnumber__c",
|
| 35 |
+
"HeadSize": "Headsize__c",
|
| 36 |
+
"Deliverysize": "Deliverysize__c",
|
| 37 |
+
"Phase": "Phase__c",
|
| 38 |
+
"Size": "Size__c",
|
| 39 |
+
"MRP": "MRP__c",
|
| 40 |
+
"Usebefore": "Usebefore__c",
|
| 41 |
+
"Height": "Height__c",
|
| 42 |
+
"MaximumDischarge Flow": "Maximumdischargeflow__c",
|
| 43 |
+
"DischargeRange": "Dischargeflow__c",
|
| 44 |
+
"Assembledby": "Manufacturer__c",
|
| 45 |
+
"Manufacturedate": "Manufacturedate__c",
|
| 46 |
+
"Companyname": "Companyname__c",
|
| 47 |
+
"Customercarenumber": "Customercarenumber__c",
|
| 48 |
+
"SellerAddress": "Selleraddress__c",
|
| 49 |
+
"Selleremail": "Selleremail__c",
|
| 50 |
+
"GSTIN": "GSTIN__c",
|
| 51 |
+
"Totalamount": "Totalamount__c",
|
| 52 |
+
"Paymentstatus": "Paymentstatus__c",
|
| 53 |
+
"Paymentmethod": "Paymentstatus__c",
|
| 54 |
+
"Invoicedate": "Manufacturedate__c",
|
| 55 |
+
"Warranty": "Warranty__c",
|
| 56 |
+
"Brand": "Brand__c",
|
| 57 |
+
"Motorhorsepower": "Motorhorsepower__c",
|
| 58 |
+
"Power": "Power__c",
|
| 59 |
+
"Motorphase": "Motorphase__c",
|
| 60 |
+
"Enginetype": "Enginetype__c",
|
| 61 |
+
"Tankcapacity": "Tankcapacity__c",
|
| 62 |
+
"Head": "Head__c",
|
| 63 |
+
"Usage/Application": "Usage_Application__c",
|
| 64 |
+
"Volts": "volts__c",
|
| 65 |
+
"Hertz": "Hertz__c",
|
| 66 |
+
"Frame": "frame__c",
|
| 67 |
+
"Mounting": "Mounting__c",
|
| 68 |
+
"Tollfreenumber": "Tollfreenumber__c",
|
| 69 |
+
"Pipesize": "Pipesize__c",
|
| 70 |
+
"Manufacturer": "Manufacturer__c",
|
| 71 |
+
"Office": "Office__c",
|
| 72 |
+
"SRnumber": "SRnumber__c",
|
| 73 |
+
"TypeOfEndUse": "TypeOfEndUse__c",
|
| 74 |
+
"Model Name": "Model_Name_Number__c",
|
| 75 |
+
"coolingmethod": "coolingmethod__c"
|
| 76 |
}
|
| 77 |
|
| 78 |
+
# List of product names to match
|
| 79 |
PRODUCT_NAMES = [
|
| 80 |
+
"Centrifugal mono block pump", "SINGLE PHASE MOTOR STARTER", "EasyPact EZC 100",
|
| 81 |
+
"Openwell Submersible Pumpset", "Electric Motor", "Self Priming Pump",
|
| 82 |
+
"Control panel for single phase submersible pumps", "MOTOR", "Submersible pump set",
|
| 83 |
+
"Fusion submersible pump set", "DCT", "Shock proof water proof", "CG COMMERCIAL MOTORS", "Fusion",
|
| 84 |
+
"control panel for single phase submerisible pumps",
|
| 85 |
+
"single phase digital starter dry run and timer panel", "5HP AV1 XL Kirloskar Pump",
|
| 86 |
+
"Phase stainless steel submersible pump", "Submersible pump", "WB15X",
|
| 87 |
+
"Vtype self priming pump", "SP SHINE DISC", "havells submersible pump",
|
| 88 |
+
"Havells open well Submersible pump", "Bertolini pump CK3 90pp",
|
| 89 |
+
"WPA 772 Water Pump Assy", "bertolini TTL triplex high pressure plunger pumps",
|
| 90 |
+
"Generic plunger high pressure pump", "Apple Normal, Banana",
|
| 91 |
+
"Cast Iron KSb centrifugal pump", "5.5kw Water Pump",
|
| 92 |
+
"KSB reliable i line centrifuged pumps", "Apple Normal, Orange, Banana",
|
| 93 |
+
"Positive API 6745 hydraulic diaphragm pump", "1/2 inch Fuel Hose Pipe", "Kirloskar Water Pump",
|
| 94 |
+
"Rotodel motor pump", "PVC Electrical Insulation Materials",
|
| 95 |
+
"Electric kirloskar domestic water pump", "Electrical Insulation Materials",
|
| 96 |
+
"sellowell motor pump", "bhupathi submersible pump set",
|
| 97 |
+
"Flowshine Submersible pump set", "Index submersible pump",
|
| 98 |
+
"Wintoss Plastic Electric Switch Board", "Electric 18 watt ujagar cooler pump",
|
| 99 |
+
"Generator Service", "LG WM FHT1207ZWL, LG REF GL-S292RSCY",
|
| 100 |
+
"Water tank, Filters, Water Pump", "MS Control Submersible Panel",
|
| 101 |
+
"Centrifugal Monoblock Pumps", "Electric Motor with Pump BodyBlue and White",
|
| 102 |
+
"Various Repair and Maintenance Parts", "Earthmax Pump",
|
| 103 |
+
"Water Tank, Filters, Water Pump", "Centrifugal Water Pump for Agriculture",
|
| 104 |
+
"mono block pumps"
|
| 105 |
]
|
| 106 |
|
| 107 |
+
# Salesforce credentials
|
| 108 |
+
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 109 |
+
SALESFORCE_PASSWORD = "Venkat12345@"
|
| 110 |
+
SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
|
| 111 |
+
|
| 112 |
+
# Initialize PaddleOCR
|
| 113 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 114 |
|
| 115 |
+
# Function to extract text using PaddleOCR
|
| 116 |
+
def extract_text(image):
|
| 117 |
+
result = ocr.ocr(image)
|
|
|
|
|
|
|
| 118 |
extracted_text = []
|
|
|
|
| 119 |
for line in result[0]:
|
| 120 |
+
extracted_text.append(line[1][0])
|
|
|
|
| 121 |
return "\n".join(extracted_text)
|
| 122 |
|
| 123 |
+
# Function to match product name using fuzzy matching
|
| 124 |
def match_product_name(extracted_text):
|
|
|
|
|
|
|
|
|
|
| 125 |
best_match = None
|
| 126 |
best_score = 0
|
| 127 |
|
|
|
|
| 133 |
|
| 134 |
return best_match if best_score >= 70 else None # Threshold of 70 for a match
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# Function to extract attributes and their values
|
| 137 |
def extract_attributes(extracted_text):
|
| 138 |
attributes = {}
|