Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,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 +161,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,40 +171,6 @@ 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 |
-
# Get valid fields from Salesforce object schema
|
| 174 |
-
sf_object = sf.__getattr__(object_name)
|
| 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 |
-
# Explicitly extract Product Name from the attributes
|
| 182 |
-
product_name = attributes.get("Productname", "").strip() # Extract 'Productname' from the text
|
| 183 |
-
if product_name:
|
| 184 |
-
# Ensure that the Product Name is mapped to the right Salesforce field
|
| 185 |
-
if entry_type == "Sales":
|
| 186 |
-
attributes["Product_Name__c"] = product_name # For VENKATA_RAMANA_MOTORS__c
|
| 187 |
-
elif entry_type == "Non-Sales":
|
| 188 |
-
attributes["Productname__c"] = product_name # For UNBILLING_DATA__c
|
| 189 |
-
|
| 190 |
-
# Convert extracted keys to match Salesforce API field names
|
| 191 |
-
mapped_attributes = {}
|
| 192 |
-
for key, value in attributes.items():
|
| 193 |
-
field_name_sf = key.replace(" ", "_") + "__c" # Convert to Salesforce format
|
| 194 |
-
if field_name_sf in valid_fields:
|
| 195 |
-
mapped_attributes[field_name_sf] = value # Only keep valid fields
|
| 196 |
-
|
| 197 |
-
mapped_attributes[field_name] = quantity # Ensure Quantity is added
|
| 198 |
-
|
| 199 |
-
if not mapped_attributes:
|
| 200 |
-
return "No valid attributes found to export."
|
| 201 |
-
|
| 202 |
-
# Creating a new record with all valid attributes
|
| 203 |
-
sf_object.create(mapped_attributes)
|
| 204 |
-
return f"β
Record created in {object_name} with extracted valid attributes and Quantity: {quantity}."
|
| 205 |
-
|
| 206 |
elif mode == "Exit":
|
| 207 |
if entry_type == "Sales":
|
| 208 |
object_name = "Inventory_Management__c"
|
|
@@ -211,21 +179,49 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 211 |
object_name = "Un_Billable__c"
|
| 212 |
field_name = "Sold_Out__c"
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
response = sf.query(query)
|
| 221 |
|
| 222 |
if response["records"]:
|
| 223 |
record_id = response["records"][0]["Id"]
|
| 224 |
-
updated_quantity = quantity
|
| 225 |
-
|
| 226 |
-
return f"β
Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
|
| 227 |
else:
|
| 228 |
-
return f"β No matching record found for product '{product_name}' in {object_name}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
except Exception as e:
|
| 231 |
return f"β Error interacting with Salesforce: {str(e)}"
|
|
@@ -298,7 +294,7 @@ def app():
|
|
| 298 |
gr.Interface(
|
| 299 |
fn=process_image,
|
| 300 |
inputs=[
|
| 301 |
-
gr.Image(type="numpy", label="π
|
| 302 |
gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry"),
|
| 303 |
gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales"),
|
| 304 |
gr.Number(label="π’ Quantity", value=1, interactive=True),
|
|
@@ -307,8 +303,8 @@ def app():
|
|
| 307 |
gr.Text(label="π Extracted Image Data"),
|
| 308 |
gr.Text(label="π Result")
|
| 309 |
],
|
| 310 |
-
title="π’
|
| 311 |
-
description="π¦
|
| 312 |
),
|
| 313 |
gr.Interface(
|
| 314 |
fn=pull_data_from_salesforce,
|
|
@@ -325,5 +321,4 @@ def app():
|
|
| 325 |
], ["π₯ OCR Processing", "π Salesforce Data Export"])
|
| 326 |
|
| 327 |
if __name__ == "__main__":
|
| 328 |
-
app().launch(share=True)
|
| 329 |
-
|
|
|
|
| 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",
|
|
|
|
| 161 |
# Mapping mode and entry_type to Salesforce object and field
|
| 162 |
object_name = None
|
| 163 |
field_name = None
|
| 164 |
+
product_field_name = "Product_Name__c"
|
| 165 |
+
model_field_name = "Modal_Name__c" # Correct field for model name
|
| 166 |
|
| 167 |
if mode == "Entry":
|
| 168 |
if entry_type == "Sales":
|
|
|
|
| 171 |
elif entry_type == "Non-Sales":
|
| 172 |
object_name = "UNBILLING_DATA__c"
|
| 173 |
field_name = "TotalQuantity__c"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
elif mode == "Exit":
|
| 175 |
if entry_type == "Sales":
|
| 176 |
object_name = "Inventory_Management__c"
|
|
|
|
| 179 |
object_name = "Un_Billable__c"
|
| 180 |
field_name = "Sold_Out__c"
|
| 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
|
| 186 |
+
sf_object = sf.__getattr__(object_name)
|
| 187 |
+
schema = sf_object.describe()
|
| 188 |
+
valid_fields = {field["name"] for field in schema["fields"]}
|
| 189 |
|
| 190 |
+
# Extract product name and attributes
|
| 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."
|
| 197 |
+
|
| 198 |
+
attributes["Product name"] = product_name
|
| 199 |
+
|
| 200 |
+
# Handling "Exit" Mode (Updating Records)
|
| 201 |
+
if mode == "Exit":
|
| 202 |
+
# Query should only match exact product name or exact model 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, {field_name} FROM {object_name} WHERE {' OR '.join(query_conditions)} LIMIT 1"
|
| 209 |
response = sf.query(query)
|
| 210 |
|
| 211 |
if response["records"]:
|
| 212 |
record_id = response["records"][0]["Id"]
|
| 213 |
+
updated_quantity = quantity # Overwrite the quantity
|
| 214 |
+
sf_object.update(record_id, {field_name: updated_quantity})
|
| 215 |
+
return f"β
Updated record for product '{product_name}' ({model_name}) in {object_name}. New {field_name}: {updated_quantity}."
|
| 216 |
else:
|
| 217 |
+
return f"β No matching record found for product '{product_name}' ({model_name}) in {object_name}."
|
| 218 |
+
|
| 219 |
+
# Handling "Entry" Mode (Creating Records)
|
| 220 |
+
else:
|
| 221 |
+
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 222 |
+
filtered_attributes[field_name] = quantity
|
| 223 |
+
sf_object.create(filtered_attributes)
|
| 224 |
+
return f"β
Data successfully exported to Salesforce object {object_name}."
|
| 225 |
|
| 226 |
except Exception as e:
|
| 227 |
return f"β Error interacting with Salesforce: {str(e)}"
|
|
|
|
| 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 |
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 |
], ["π₯ OCR Processing", "π Salesforce Data Export"])
|
| 322 |
|
| 323 |
if __name__ == "__main__":
|
| 324 |
+
app().launch(share=True)
|
|
|