Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,6 +76,11 @@ ATTRIBUTE_MAPPING = {
|
|
| 76 |
"H.P.": "H_p__c"
|
| 77 |
}
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# Salesforce credentials
|
| 80 |
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 81 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
|
@@ -92,6 +97,21 @@ def extract_text(image):
|
|
| 92 |
extracted_text.append(line[1][0])
|
| 93 |
return "\n".join(extracted_text)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# Function to extract attributes and their values
|
| 96 |
def extract_attributes(extracted_text):
|
| 97 |
attributes = {}
|
|
@@ -254,15 +274,19 @@ def pull_data_from_salesforce(selected_object):
|
|
| 254 |
return f"Error fetching data: {str(e)}", None, None, None
|
| 255 |
|
| 256 |
# Unified function to handle image processing and Salesforce interaction
|
| 257 |
-
def process_image(image, mode, entry_type, quantity):
|
| 258 |
extracted_text = extract_text(image)
|
| 259 |
if not extracted_text:
|
| 260 |
return "No text detected in the image.", None, None
|
| 261 |
|
| 262 |
print("Extracted Text:", extracted_text) # Debugging: Print extracted text
|
| 263 |
|
|
|
|
| 264 |
attributes = extract_attributes(extracted_text)
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
# Combine extracted text and attributes for display
|
| 268 |
combined_text = extracted_text + "\n\nExtracted Attributes:\n"
|
|
@@ -284,16 +308,17 @@ def app():
|
|
| 284 |
mode_input = gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry")
|
| 285 |
entry_type_input = gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales")
|
| 286 |
quantity_input = gr.Number(label="π’ Quantity", value=1, interactive=True)
|
|
|
|
| 287 |
extracted_text_output = gr.Text(label="π Extracted Image Data")
|
| 288 |
attributes_output = gr.Dataframe(label="βοΈ Edit Attributes", headers=["Attribute", "Value"], interactive=True)
|
| 289 |
process_button = gr.Button("Process Image")
|
| 290 |
|
| 291 |
-
def process_image_and_display(image, mode, entry_type, quantity):
|
| 292 |
-
combined_text, attributes = process_image(image, mode, entry_type, quantity)
|
| 293 |
attributes_list = [[key, value] for key, value in attributes.items()]
|
| 294 |
return combined_text, attributes_list
|
| 295 |
|
| 296 |
-
process_button.click(process_image_and_display, inputs=[image_input, mode_input, entry_type_input, quantity_input], outputs=[extracted_text_output, attributes_output])
|
| 297 |
|
| 298 |
export_button = gr.Button("OK")
|
| 299 |
result_output = gr.Text(label="π Result")
|
|
|
|
| 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
|
| 85 |
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 86 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
|
|
|
| 97 |
extracted_text.append(line[1][0])
|
| 98 |
return "\n".join(extracted_text)
|
| 99 |
|
| 100 |
+
# Function to match product name using fuzzy matching
|
| 101 |
+
def match_product_name(extracted_text, threshold=70):
|
| 102 |
+
best_match = None
|
| 103 |
+
best_score = 0
|
| 104 |
+
|
| 105 |
+
print("Matching Process:") # Debugging: Print matching process
|
| 106 |
+
for line in extracted_text.split("\n"):
|
| 107 |
+
match, score = process.extractOne(line, PRODUCT_NAMES)
|
| 108 |
+
print(f"Line: {line}, Match: {match}, Score: {score}") # Debugging: Print each line and its match score
|
| 109 |
+
if score > best_score:
|
| 110 |
+
best_match = match
|
| 111 |
+
best_score = score
|
| 112 |
+
|
| 113 |
+
return best_match if best_score >= threshold else None # Adjustable threshold for a match
|
| 114 |
+
|
| 115 |
# Function to extract attributes and their values
|
| 116 |
def extract_attributes(extracted_text):
|
| 117 |
attributes = {}
|
|
|
|
| 274 |
return f"Error fetching data: {str(e)}", None, None, None
|
| 275 |
|
| 276 |
# Unified function to handle image processing and Salesforce interaction
|
| 277 |
+
def process_image(image, mode, entry_type, quantity, threshold=70):
|
| 278 |
extracted_text = extract_text(image)
|
| 279 |
if not extracted_text:
|
| 280 |
return "No text detected in the image.", None, None
|
| 281 |
|
| 282 |
print("Extracted Text:", extracted_text) # Debugging: Print extracted text
|
| 283 |
|
| 284 |
+
product_name = match_product_name(extracted_text, threshold)
|
| 285 |
attributes = extract_attributes(extracted_text)
|
| 286 |
+
if product_name:
|
| 287 |
+
attributes["Product name"] = product_name
|
| 288 |
+
else:
|
| 289 |
+
attributes["Product name"] = "Unknown"
|
| 290 |
|
| 291 |
# Combine extracted text and attributes for display
|
| 292 |
combined_text = extracted_text + "\n\nExtracted Attributes:\n"
|
|
|
|
| 308 |
mode_input = gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry")
|
| 309 |
entry_type_input = gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales")
|
| 310 |
quantity_input = gr.Number(label="π’ Quantity", value=1, interactive=True)
|
| 311 |
+
threshold_input = gr.Slider(label="π Matching Threshold", minimum=0, maximum=100, value=70, step=1)
|
| 312 |
extracted_text_output = gr.Text(label="π Extracted Image Data")
|
| 313 |
attributes_output = gr.Dataframe(label="βοΈ Edit Attributes", headers=["Attribute", "Value"], interactive=True)
|
| 314 |
process_button = gr.Button("Process Image")
|
| 315 |
|
| 316 |
+
def process_image_and_display(image, mode, entry_type, quantity, threshold):
|
| 317 |
+
combined_text, attributes = process_image(image, mode, entry_type, quantity, threshold)
|
| 318 |
attributes_list = [[key, value] for key, value in attributes.items()]
|
| 319 |
return combined_text, attributes_list
|
| 320 |
|
| 321 |
+
process_button.click(process_image_and_display, inputs=[image_input, mode_input, entry_type_input, quantity_input, threshold_input], outputs=[extracted_text_output, attributes_output])
|
| 322 |
|
| 323 |
export_button = gr.Button("OK")
|
| 324 |
result_output = gr.Text(label="π Result")
|