gopichandra commited on
Commit
731bd25
·
verified ·
1 Parent(s): a01b0c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -16
app.py CHANGED
@@ -153,6 +153,72 @@ def filter_valid_attributes(attributes, valid_fields):
153
  #📊 Function to interact with Salesforce based on mode and type
154
 
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  # Function to interact with Salesforce based on mode and type
157
  def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
158
  try:
@@ -174,6 +240,16 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
174
  elif entry_type == "Non-Sales":
175
  object_name = "UNBILLING_DATA__c"
176
  field_name = "TotalQuantity__c"
 
 
 
 
 
 
 
 
 
 
177
  elif mode == "Exit":
178
  if entry_type == "Sales":
179
  object_name = "Inventory_Management__c"
@@ -182,16 +258,11 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
182
  object_name = "Un_Billable__c"
183
  field_name = "Sold_Out__c"
184
 
185
- if not object_name or not field_name:
186
- return "Invalid mode or entry type."
187
-
188
- # Extract product name
189
- product_name = match_product_name(extracted_text)
190
- if not product_name:
191
- return "Product name could not be matched from the extracted text."
192
 
193
- # Handling "Exit" Mode (Updating Records)
194
- if mode == "Exit":
195
  query = f"SELECT Id, {field_name} FROM {object_name} WHERE {product_field_name} = '{product_name}' LIMIT 1"
196
  response = sf.query(query)
197
 
@@ -203,15 +274,10 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
203
  else:
204
  return f"❌ No matching record found for product '{product_name}' in {object_name}."
205
 
206
- # Handling "Entry" Mode (Creating Records)
207
- else:
208
- attributes = {product_field_name: product_name, field_name: quantity}
209
- sf.__getattr__(object_name).create(attributes)
210
- return f"✅ Data successfully exported to Salesforce object {object_name}."
211
-
212
  except Exception as e:
213
  return f"❌ Error interacting with Salesforce: {str(e)}"
214
- # Function to pull structured data from Salesforce and display as a table
 
215
  def pull_data_from_salesforce():
216
  try:
217
  sf = Salesforce(
 
153
  #📊 Function to interact with Salesforce based on mode and type
154
 
155
 
156
+ import os
157
+ from paddleocr import PaddleOCR
158
+ from PIL import Image
159
+ import gradio as gr
160
+ import requests
161
+ import re
162
+ from simple_salesforce import Salesforce
163
+ import pandas as pd
164
+ import matplotlib.pyplot as plt
165
+ from io import BytesIO
166
+ from fuzzywuzzy import process
167
+ import kaleido # Ensure kaleido is imported
168
+
169
+ # Salesforce credentials
170
+ SALESFORCE_USERNAME = "venkatramana@sandbox.com"
171
+ SALESFORCE_PASSWORD = "Venkat12345@"
172
+ SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
173
+
174
+ # Initialize PaddleOCR
175
+ ocr = PaddleOCR(use_angle_cls=True, lang='en')
176
+
177
+ # Function to extract text using PaddleOCR
178
+ def extract_text(image):
179
+ result = ocr.ocr(image)
180
+ extracted_text = []
181
+ for line in result[0]:
182
+ extracted_text.append(line[1][0])
183
+ return "\n".join(extracted_text)
184
+
185
+ # Function to match product name using fuzzy matching
186
+ def match_product_name(extracted_text):
187
+ best_match = None
188
+ best_score = 0
189
+ PRODUCT_NAMES = [
190
+ "Centrifugal mono block pump", "SINGLE PHASE MOTOR STARTER", "EasyPact EZC 100",
191
+ "Openwell Submersible Pumpset", "Electric Motor", "Self Priming Pump",
192
+ "Control panel for single phase submersible pumps", "MOTOR", "Submersible pump set",
193
+ "Fusion submersible pump set", "DCT", "Shock proof water proof", "CG COMMERCIAL MOTORS", "Fusion",
194
+ "control panel for single phase submerisible pumps", "single phase digital starter dry run and timer panel",
195
+ "5HP AV1 XL Kirloskar Pump", "Phase stainless steel submersible pump", "Submersible pump", "WB15X",
196
+ "Vtype self priming pump", "SP SHINE DISC", "havells submersible pump",
197
+ "Havells open well Submersible pump", "Bertolini pump CK3 90pp",
198
+ "WPA 772 Water Pump Assy", "bertolini TTL triplex high pressure plunger pumps",
199
+ "Generic plunger high pressure pump", "Apple Normal, Banana",
200
+ "Cast Iron KSb centrifugal pump", "5.5kw Water Pump",
201
+ "KSB reliable i line centrifuged pumps", "Apple Normal, Orange, Banana",
202
+ "Positive API 6745 hydraulic diaphragm pump", "1/2 inch Fuel Hose Pipe", "Kirloskar Water Pump",
203
+ "Rotodel motor pump", "PVC Electrical Insulation Materials",
204
+ "Electric kirloskar domestic water pump", "Electrical Insulation Materials",
205
+ "sellowell motor pump", "bhupathi submersible pump set",
206
+ "Flowshine Submersible pump set", "Index submersible pump",
207
+ "Wintoss Plastic Electric Switch Board", "Electric 18 watt ujagar cooler pump",
208
+ "Generator Service", "LG WM FHT1207ZWL, LG REF GL-S292RSCY",
209
+ "Water tank, Filters, Water Pump", "MS Control Submersible Panel",
210
+ "Centrifugal Monoblock Pumps", "Electric Motor with Pump BodyBlue and White",
211
+ "Various Repair and Maintenance Parts", "Earthmax Pump",
212
+ "Water Tank, Filters, Water Pump", "Centrifugal Water Pump for Agriculture",
213
+ "mono block pumps"
214
+ ]
215
+ for line in extracted_text.split("\n"):
216
+ match, score = process.extractOne(line, PRODUCT_NAMES)
217
+ if score > best_score:
218
+ best_match = match
219
+ best_score = score
220
+ return best_match if best_score >= 70 else None # Threshold of 70 for a match
221
+
222
  # Function to interact with Salesforce based on mode and type
223
  def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
224
  try:
 
240
  elif entry_type == "Non-Sales":
241
  object_name = "UNBILLING_DATA__c"
242
  field_name = "TotalQuantity__c"
243
+
244
+ # Extract product name
245
+ product_name = match_product_name(extracted_text)
246
+ if not product_name:
247
+ return "Product name could not be matched from the extracted text."
248
+
249
+ # Creating a new record
250
+ sf.__getattr__(object_name).create({product_field_name: product_name, field_name: quantity})
251
+ return f"✅ Record created in {object_name} with Product: '{product_name}', Quantity: {quantity}."
252
+
253
  elif mode == "Exit":
254
  if entry_type == "Sales":
255
  object_name = "Inventory_Management__c"
 
258
  object_name = "Un_Billable__c"
259
  field_name = "Sold_Out__c"
260
 
261
+ # Extract product name
262
+ product_name = match_product_name(extracted_text)
263
+ if not product_name:
264
+ return "Product name could not be matched from the extracted text."
 
 
 
265
 
 
 
266
  query = f"SELECT Id, {field_name} FROM {object_name} WHERE {product_field_name} = '{product_name}' LIMIT 1"
267
  response = sf.query(query)
268
 
 
274
  else:
275
  return f"❌ No matching record found for product '{product_name}' in {object_name}."
276
 
 
 
 
 
 
 
277
  except Exception as e:
278
  return f"❌ Error interacting with Salesforce: {str(e)}"
279
+
280
+ # Function to pull structured data from Salesforce and display as a table
281
  def pull_data_from_salesforce():
282
  try:
283
  sf = Salesforce(