Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from paddleocr import PaddleOCR
|
| 3 |
from PIL import Image
|
|
@@ -13,7 +16,7 @@ import kaleido # Ensure kaleido is imported
|
|
| 13 |
|
| 14 |
# Attribute mappings: readable names to Salesforce API names
|
| 15 |
ATTRIBUTE_MAPPING = {
|
| 16 |
-
"
|
| 17 |
"Colour": "Colour__c",
|
| 18 |
"Motortype": "Motortype__c",
|
| 19 |
"Frequency": "Frequency__c",
|
|
@@ -161,6 +164,8 @@ 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 |
if mode == "Entry":
|
| 166 |
if entry_type == "Sales":
|
|
@@ -169,72 +174,60 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 169 |
elif entry_type == "Non-Sales":
|
| 170 |
object_name = "UNBILLING_DATA__c"
|
| 171 |
field_name = "TotalQuantity__c"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
schema = sf_object.describe()
|
| 176 |
-
valid_fields = {field["name"] for field in schema["fields"]}
|
| 177 |
-
|
| 178 |
-
# Extract attributes from the extracted text
|
| 179 |
-
attributes = extract_attributes(extracted_text)
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
if sf_field_name in valid_fields:
|
| 191 |
-
mapped_attributes[sf_field_name] = value # Only keep valid fields
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
mapped_attributes["Product_Name__c"] = product_name # Product Name field for Sales
|
| 196 |
-
elif entry_type == "Non-Sales":
|
| 197 |
-
mapped_attributes["Productname__c"] = product_name # Product Name field for Non-Sales
|
| 198 |
-
mapped_attributes[field_name] = quantity # Quantity field
|
| 199 |
|
| 200 |
-
|
| 201 |
-
if not mapped_attributes:
|
| 202 |
-
return "❌ No valid attributes found to export."
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
except Exception as e:
|
| 209 |
return f"❌ Error interacting with Salesforce: {str(e)}"
|
| 210 |
-
|
| 211 |
-
elif mode == "Exit":
|
| 212 |
-
if entry_type == "Sales":
|
| 213 |
-
object_name = "Inventory_Management__c"
|
| 214 |
-
field_name = "Quantity_Sold__c"
|
| 215 |
-
elif entry_type == "Non-Sales":
|
| 216 |
-
object_name = "Un_Billable__c"
|
| 217 |
-
field_name = "Sold_Out__c"
|
| 218 |
-
|
| 219 |
-
# Extract product name
|
| 220 |
-
product_name = match_product_name(extracted_text)
|
| 221 |
-
if not product_name:
|
| 222 |
-
return "Product name could not be matched from the extracted text."
|
| 223 |
-
|
| 224 |
-
query = f"SELECT Id, {field_name} FROM {object_name} WHERE Product_Name__c = '{product_name}' LIMIT 1"
|
| 225 |
-
response = sf.query(query)
|
| 226 |
-
|
| 227 |
-
if response["records"]:
|
| 228 |
-
record_id = response["records"][0]["Id"]
|
| 229 |
-
updated_quantity = quantity
|
| 230 |
-
sf.__getattr__(object_name).update(record_id, {field_name: updated_quantity})
|
| 231 |
-
return f"✅ Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
|
| 232 |
-
else:
|
| 233 |
-
return f"❌ No matching record found for product '{product_name}' in {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:
|
|
|
|
| 1 |
+
HUGGING FACE ALL FUNCTIONALITIES WORKING
|
| 2 |
+
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
from paddleocr import PaddleOCR
|
| 6 |
from PIL import Image
|
|
|
|
| 16 |
|
| 17 |
# Attribute mappings: readable names to Salesforce API names
|
| 18 |
ATTRIBUTE_MAPPING = {
|
| 19 |
+
"Product name": "Productname__c",
|
| 20 |
"Colour": "Colour__c",
|
| 21 |
"Motortype": "Motortype__c",
|
| 22 |
"Frequency": "Frequency__c",
|
|
|
|
| 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":
|
|
|
|
| 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:
|