gopichandra commited on
Commit
f447e55
·
verified ·
1 Parent(s): 3e6ddad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -76
app.py CHANGED
@@ -1,3 +1,6 @@
 
 
 
1
  import os
2
  from paddleocr import PaddleOCR
3
  from PIL import Image
@@ -158,83 +161,73 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
158
  security_token=SALESFORCE_SECURITY_TOKEN
159
  )
160
 
161
- # Function to extract attributes and their values
162
- def extract_attributes(extracted_text):
163
- attributes = {}
164
-
165
- for readable_attr, sf_attr in ATTRIBUTE_MAPPING.items():
166
- pattern = rf"{re.escape(readable_attr)}[:\-]?\s*(.+)"
167
- match = re.search(pattern, extracted_text, re.IGNORECASE)
168
- if match:
169
- attributes[readable_attr] = match.group(1).strip()
170
-
171
- return attributes
172
-
173
- # Function to filter attributes for valid Salesforce fields
174
- def filter_valid_attributes(attributes, valid_fields):
175
- return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
176
- # Function to interact with Salesforce based on mode and type
177
- def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
178
- try:
179
- sf = Salesforce(
180
- username=SALESFORCE_USERNAME,
181
- password=SALESFORCE_PASSWORD,
182
- security_token=SALESFORCE_SECURITY_TOKEN
183
- )
184
-
185
- # Mapping mode and entry_type to Salesforce object and field
186
- object_name = None
187
- field_name = None
188
- product_field_name = "Product_Name__c"
189
- if mode == "Entry":
190
- if entry_type == "Sales":
191
- object_name = "VENKATA_RAMANA_MOTORS__c"
192
- field_name = "Quantity__c"
193
- elif entry_type == "Non-Sales":
194
- object_name = "UNBILLING_DATA__c"
195
- field_name = "TotalQuantity__c"
196
- elif mode == "Exit":
197
- if entry_type == "Sales":
198
- object_name = "Inventory_Management__c"
199
- field_name = "Quantity_Sold__c"
200
- elif entry_type == "Non-Sales":
201
- object_name = "Un_Billable__c"
202
- field_name = "Sold_Out__c"
203
- if not object_name or not field_name:
204
- return "Invalid mode or entry type."
205
-
206
- # Get valid fields for the specified Salesforce object
207
- sf_object = sf.__getattr__(object_name)
208
- schema = sf_object.describe()
209
- valid_fields = {field["name"] for field in schema["fields"]}
210
-
211
- # Extract product name from the extracted text
212
- product_name = match_product_name(extracted_text)
213
- if not product_name:
214
- return "Product name could not be matched from the extracted text."
215
- # Handling "Exit" Mode (Updating Records)
216
- if mode == "Exit":
217
- query = f"SELECT Id, {field_name} FROM {object_name} WHERE {product_field_name} = '{product_name}' LIMIT 1"
218
- response = sf.query(query)
219
-
220
- if response["records"]:
221
- record_id = response["records"][0]["Id"]
222
- updated_quantity = quantity # Overwrite the quantity
223
- sf_object.update(record_id, {field_name: updated_quantity})
224
- return f"✅ Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
225
- else:
226
- return f"❌ No matching record found for product '{product_name}' in {object_name}."
227
-
228
- # Handling "Entry" Mode (Creating Records)
229
- else:
230
- filtered_attributes = filter_valid_attributes(attributes, valid_fields)
231
- filtered_attributes[field_name] = quantity
232
- sf_object.create(filtered_attributes)
233
- return f"✅ Data successfully exported to Salesforce object {object_name}."
234
-
235
- except Exception as e:
236
- return f"❌ Error interacting with Salesforce: {str(e)}"
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  # Function to pull structured data from Salesforce and display as a table
239
  def pull_data_from_salesforce():
240
  try:
@@ -332,3 +325,4 @@ def app():
332
 
333
  if __name__ == "__main__":
334
  app().launch(share=True)
 
 
1
+ HUGGING FACE ALL FUNCTIONALITIES WORKING
2
+
3
+
4
  import os
5
  from paddleocr import PaddleOCR
6
  from PIL import Image
 
161
  security_token=SALESFORCE_SECURITY_TOKEN
162
  )
163
 
164
+ # Mapping mode and entry_type to Salesforce object and field
165
+ object_name = None
166
+ field_name = None
167
+ product_field_name = "Product_Name__c"
168
+ model_field_name = "Modal_Name__c" # Correct field for model name
169
+
170
+ if mode == "Entry":
171
+ if entry_type == "Sales":
172
+ object_name = "VENKATA_RAMANA_MOTORS__c"
173
+ field_name = "Quantity__c"
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"
180
+ field_name = "Quantity_Sold__c"
181
+ elif entry_type == "Non-Sales":
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
+ # Get valid fields for the specified Salesforce object
189
+ sf_object = sf.__getattr__(object_name)
190
+ schema = sf_object.describe()
191
+ valid_fields = {field["name"] for field in schema["fields"]}
192
+
193
+ # Extract product name and attributes
194
+ product_name = match_product_name(extracted_text)
195
+ attributes = extract_attributes(extracted_text)
196
+ model_name = attributes.get("Model Name", "").strip()
197
+
198
+ if not product_name:
199
+ return "Product name could not be matched from the extracted text."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
 
201
+ attributes["Product name"] = product_name
202
+
203
+ # Handling "Exit" Mode (Updating Records)
204
+ if mode == "Exit":
205
+ # Query should only match exact product name or exact model name
206
+ query_conditions = []
207
+ if model_name:
208
+ query_conditions.append(f"{model_field_name} = '{model_name}'")
209
+ query_conditions.append(f"{product_field_name} = '{product_name}'")
210
+
211
+ query = f"SELECT Id, {field_name} FROM {object_name} WHERE {' OR '.join(query_conditions)} LIMIT 1"
212
+ response = sf.query(query)
213
+
214
+ if response["records"]:
215
+ record_id = response["records"][0]["Id"]
216
+ updated_quantity = quantity # Overwrite the quantity
217
+ sf_object.update(record_id, {field_name: updated_quantity})
218
+ return f"✅ Updated record for product '{product_name}' ({model_name}) in {object_name}. New {field_name}: {updated_quantity}."
219
+ else:
220
+ return f"❌ No matching record found for product '{product_name}' ({model_name}) in {object_name}."
221
+
222
+ # Handling "Entry" Mode (Creating Records)
223
+ else:
224
+ filtered_attributes = filter_valid_attributes(attributes, valid_fields)
225
+ filtered_attributes[field_name] = quantity
226
+ sf_object.create(filtered_attributes)
227
+ return f"✅ Data successfully exported to Salesforce object {object_name}."
228
+
229
+ except Exception as e:
230
+ return f"❌ Error interacting with Salesforce: {str(e)}"
231
  # Function to pull structured data from Salesforce and display as a table
232
  def pull_data_from_salesforce():
233
  try:
 
325
 
326
  if __name__ == "__main__":
327
  app().launch(share=True)
328
+