Update app.py
Browse files
app.py
CHANGED
|
@@ -72,36 +72,13 @@ ATTRIBUTE_MAPPING = {
|
|
| 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 |
-
|
| 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
|
|
@@ -161,8 +138,11 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 161 |
# Mapping mode and entry_type to Salesforce object and field
|
| 162 |
object_name = None
|
| 163 |
field_name = None
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
if mode == "Entry":
|
| 168 |
if entry_type == "Sales":
|
|
@@ -174,12 +154,12 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 174 |
elif mode == "Exit":
|
| 175 |
if entry_type == "Sales":
|
| 176 |
object_name = "Inventory_Management__c"
|
| 177 |
-
|
| 178 |
elif entry_type == "Non-Sales":
|
| 179 |
object_name = "Un_Billable__c"
|
| 180 |
-
|
| 181 |
|
| 182 |
-
if not object_name or not field_name:
|
| 183 |
return "Invalid mode or entry type."
|
| 184 |
|
| 185 |
# Get valid fields for the specified Salesforce object
|
|
@@ -191,6 +171,8 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 191 |
product_name = match_product_name(extracted_text)
|
| 192 |
attributes = extract_attributes(extracted_text)
|
| 193 |
model_name = attributes.get("Model Name", "").strip()
|
|
|
|
|
|
|
| 194 |
|
| 195 |
if not product_name:
|
| 196 |
return "Product name could not be matched from the extracted text."
|
|
@@ -199,20 +181,24 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 199 |
|
| 200 |
# Handling "Exit" Mode (Updating Records)
|
| 201 |
if mode == "Exit":
|
| 202 |
-
# Query should only match exact product name
|
| 203 |
query_conditions = []
|
| 204 |
if model_name:
|
| 205 |
query_conditions.append(f"{model_field_name} = '{model_name}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
query_conditions.append(f"{product_field_name} = '{product_name}'")
|
| 207 |
|
| 208 |
-
query = f"SELECT Id, {
|
| 209 |
response = sf.query(query)
|
| 210 |
|
| 211 |
if response["records"]:
|
| 212 |
record_id = response["records"][0]["Id"]
|
| 213 |
-
|
| 214 |
-
sf_object.update(record_id,
|
| 215 |
-
return f"β
Updated record for product '{product_name}' ({model_name}) in {object_name}.
|
| 216 |
else:
|
| 217 |
return f"β No matching record found for product '{product_name}' ({model_name}) in {object_name}."
|
| 218 |
|
|
@@ -225,6 +211,7 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 225 |
|
| 226 |
except Exception as e:
|
| 227 |
return f"β Error interacting with Salesforce: {str(e)}"
|
|
|
|
| 228 |
# Function to pull structured data from Salesforce and display as a table
|
| 229 |
def pull_data_from_salesforce():
|
| 230 |
try:
|
|
@@ -234,21 +221,29 @@ def pull_data_from_salesforce():
|
|
| 234 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 235 |
)
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
| 242 |
return "No data found in Salesforce.", None, None, None
|
| 243 |
-
|
|
|
|
| 244 |
df = pd.DataFrame(records)
|
| 245 |
df = df.drop(columns=['attributes'], errors='ignore')
|
| 246 |
|
| 247 |
# Rename columns for better readability
|
| 248 |
df.rename(columns={
|
| 249 |
"Product_Name__c": "Product Name",
|
| 250 |
-
"Modal_Name__c": "Model Name",
|
| 251 |
-
"
|
|
|
|
|
|
|
| 252 |
}, inplace=True)
|
| 253 |
|
| 254 |
excel_path = "salesforce_data.xlsx"
|
|
@@ -294,7 +289,7 @@ def app():
|
|
| 294 |
gr.Interface(
|
| 295 |
fn=process_image,
|
| 296 |
inputs=[
|
| 297 |
-
gr.Image(type="numpy", label="π
|
| 298 |
gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry"),
|
| 299 |
gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales"),
|
| 300 |
gr.Number(label="π’ Quantity", value=1, interactive=True),
|
|
@@ -303,8 +298,8 @@ def app():
|
|
| 303 |
gr.Text(label="π Extracted Image Data"),
|
| 304 |
gr.Text(label="π Result")
|
| 305 |
],
|
| 306 |
-
title="π’
|
| 307 |
-
description="π¦
|
| 308 |
),
|
| 309 |
gr.Interface(
|
| 310 |
fn=pull_data_from_salesforce,
|
|
@@ -321,5 +316,4 @@ def app():
|
|
| 321 |
], ["π₯ OCR Processing", "π Salesforce Data Export"])
|
| 322 |
|
| 323 |
if __name__ == "__main__":
|
| 324 |
-
app().launch(share=True)
|
| 325 |
-
|
|
|
|
| 72 |
"SRnumber": "SRnumber__c",
|
| 73 |
"TypeOfEndUse": "TypeOfEndUse__c",
|
| 74 |
"Model Name": "Model_Name_Number__c",
|
| 75 |
+
"coolingmethod": "coolingmethod__c",
|
| 76 |
+
"H.P.": "H_p__c"
|
| 77 |
}
|
| 78 |
|
| 79 |
# List of product names to match
|
| 80 |
PRODUCT_NAMES = [
|
| 81 |
+
"Fusion", "Agroking", "openwell", "CG commercial motors", "Jaguar", "Submersible pumps", "Gaurav"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
]
|
| 83 |
|
| 84 |
# Salesforce credentials
|
|
|
|
| 138 |
# Mapping mode and entry_type to Salesforce object and field
|
| 139 |
object_name = None
|
| 140 |
field_name = None
|
| 141 |
+
field_names = []
|
| 142 |
+
product_field_name = "Productname__c"
|
| 143 |
+
model_field_name = "Model__c"
|
| 144 |
+
stage_field_name = "Stage__c"
|
| 145 |
+
hp_field_name = "H_p__c"
|
| 146 |
|
| 147 |
if mode == "Entry":
|
| 148 |
if entry_type == "Sales":
|
|
|
|
| 154 |
elif mode == "Exit":
|
| 155 |
if entry_type == "Sales":
|
| 156 |
object_name = "Inventory_Management__c"
|
| 157 |
+
field_names = ["Quantity_Sold__c", "soldstock__c"]
|
| 158 |
elif entry_type == "Non-Sales":
|
| 159 |
object_name = "Un_Billable__c"
|
| 160 |
+
field_names = ["Sold_Out__c", "soldstock__c"]
|
| 161 |
|
| 162 |
+
if not object_name or (not field_name and not field_names):
|
| 163 |
return "Invalid mode or entry type."
|
| 164 |
|
| 165 |
# Get valid fields for the specified Salesforce object
|
|
|
|
| 171 |
product_name = match_product_name(extracted_text)
|
| 172 |
attributes = extract_attributes(extracted_text)
|
| 173 |
model_name = attributes.get("Model Name", "").strip()
|
| 174 |
+
stage = attributes.get("Stage", "").strip()
|
| 175 |
+
hp = attributes.get("H.P.", "").strip()
|
| 176 |
|
| 177 |
if not product_name:
|
| 178 |
return "Product name could not be matched from the extracted text."
|
|
|
|
| 181 |
|
| 182 |
# Handling "Exit" Mode (Updating Records)
|
| 183 |
if mode == "Exit":
|
| 184 |
+
# Query should only match exact product name, model name, stage, or hp
|
| 185 |
query_conditions = []
|
| 186 |
if model_name:
|
| 187 |
query_conditions.append(f"{model_field_name} = '{model_name}'")
|
| 188 |
+
if stage:
|
| 189 |
+
query_conditions.append(f"{stage_field_name} = '{stage}'")
|
| 190 |
+
if hp:
|
| 191 |
+
query_conditions.append(f"{hp_field_name} = '{hp}'")
|
| 192 |
query_conditions.append(f"{product_field_name} = '{product_name}'")
|
| 193 |
|
| 194 |
+
query = f"SELECT Id, {', '.join(field_names)} FROM {object_name} WHERE {' OR '.join(query_conditions)} LIMIT 1"
|
| 195 |
response = sf.query(query)
|
| 196 |
|
| 197 |
if response["records"]:
|
| 198 |
record_id = response["records"][0]["Id"]
|
| 199 |
+
updated_fields = {field: quantity for field in field_names}
|
| 200 |
+
sf_object.update(record_id, updated_fields)
|
| 201 |
+
return f"β
Updated record for product '{product_name}' ({model_name}) in {object_name}. Updated fields: {updated_fields}."
|
| 202 |
else:
|
| 203 |
return f"β No matching record found for product '{product_name}' ({model_name}) in {object_name}."
|
| 204 |
|
|
|
|
| 211 |
|
| 212 |
except Exception as e:
|
| 213 |
return f"β Error interacting with Salesforce: {str(e)}"
|
| 214 |
+
|
| 215 |
# Function to pull structured data from Salesforce and display as a table
|
| 216 |
def pull_data_from_salesforce():
|
| 217 |
try:
|
|
|
|
| 221 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 222 |
)
|
| 223 |
|
| 224 |
+
query_inventory = "SELECT Product_Name__c, Current_Stocks__c, soldstock__c FROM Inventory_Management__c LIMIT 100"
|
| 225 |
+
query_unbillable = "SELECT Product_Name__c, Current_Stock__c, soldstock__c FROM Un_Billable__c LIMIT 100"
|
| 226 |
+
|
| 227 |
+
response_inventory = sf.query_all(query_inventory)
|
| 228 |
+
response_unbillable = sf.query_all(query_unbillable)
|
| 229 |
|
| 230 |
+
records_inventory = response_inventory.get("records", [])
|
| 231 |
+
records_unbillable = response_unbillable.get("records", [])
|
| 232 |
+
|
| 233 |
+
if not records_inventory and not records_unbillable:
|
| 234 |
return "No data found in Salesforce.", None, None, None
|
| 235 |
+
|
| 236 |
+
records = records_inventory + records_unbillable
|
| 237 |
df = pd.DataFrame(records)
|
| 238 |
df = df.drop(columns=['attributes'], errors='ignore')
|
| 239 |
|
| 240 |
# Rename columns for better readability
|
| 241 |
df.rename(columns={
|
| 242 |
"Product_Name__c": "Product Name",
|
| 243 |
+
"Modal_Name__c": "Model Name (Inventory)",
|
| 244 |
+
"Model_Name__c": "Model Name (Unbillable)",
|
| 245 |
+
"Current_Stocks__c": "Current Stocks",
|
| 246 |
+
"soldstock__c": "Sold Stock"
|
| 247 |
}, inplace=True)
|
| 248 |
|
| 249 |
excel_path = "salesforce_data.xlsx"
|
|
|
|
| 289 |
gr.Interface(
|
| 290 |
fn=process_image,
|
| 291 |
inputs=[
|
| 292 |
+
gr.Image(type="numpy", label="πα΄α΄Κα΄α΄α΄
Ιͺα΄α΄Ι’α΄"),
|
| 293 |
gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry"),
|
| 294 |
gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales"),
|
| 295 |
gr.Number(label="π’ Quantity", value=1, interactive=True),
|
|
|
|
| 298 |
gr.Text(label="π Extracted Image Data"),
|
| 299 |
gr.Text(label="π Result")
|
| 300 |
],
|
| 301 |
+
title="π’ π½π¬π΅π²π¨π»π¨πΉπ¨π΄π¨π΅π¨ π΄πΆπ»πΆπΉπΊ",
|
| 302 |
+
description="π¦ πππππππππ ππππππππππ"
|
| 303 |
),
|
| 304 |
gr.Interface(
|
| 305 |
fn=pull_data_from_salesforce,
|
|
|
|
| 316 |
], ["π₯ OCR Processing", "π Salesforce Data Export"])
|
| 317 |
|
| 318 |
if __name__ == "__main__":
|
| 319 |
+
app().launch(share=True)
|
|
|