Update app.py
Browse files
app.py
CHANGED
|
@@ -37,6 +37,35 @@ ATTRIBUTE_MAPPING = {
|
|
| 37 |
"Usage": "Usage_Application__c",
|
| 38 |
}
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# π§ Initialize PaddleOCR
|
| 41 |
try:
|
| 42 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
|
@@ -61,7 +90,7 @@ def match_product_name(extracted_text):
|
|
| 61 |
best_match, best_score = match, score
|
| 62 |
return best_match if best_score >= 70 else None # Threshold: 70
|
| 63 |
|
| 64 |
-
# Function to extract attributes from text
|
| 65 |
def extract_attributes(extracted_text):
|
| 66 |
attributes = {}
|
| 67 |
for readable_attr, sf_attr in ATTRIBUTE_MAPPING.items():
|
|
@@ -69,6 +98,12 @@ def extract_attributes(extracted_text):
|
|
| 69 |
match = re.search(pattern, extracted_text, re.IGNORECASE)
|
| 70 |
if match:
|
| 71 |
attributes[readable_attr] = match.group(1).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return attributes
|
| 73 |
|
| 74 |
# Function to export extracted data to Salesforce
|
|
@@ -80,20 +115,9 @@ def export_to_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 80 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 81 |
)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
if mode == "Entry":
|
| 87 |
-
object_name = "Inventory_Management__c" if entry_type == "Sales" else "Un_Billable__c"
|
| 88 |
-
field_name = "Quantity__c"
|
| 89 |
-
elif mode == "Exit":
|
| 90 |
-
object_name = "Inventory_Management__c" if entry_type == "Sales" else "Un_Billable__c"
|
| 91 |
-
field_name = "Quantity_Sold__c"
|
| 92 |
-
|
| 93 |
-
if not object_name or not field_name:
|
| 94 |
-
return "β Invalid mode or entry type."
|
| 95 |
|
| 96 |
-
# Extract Product Name & Attributes
|
| 97 |
product_name = match_product_name(extracted_text)
|
| 98 |
attributes = extract_attributes(extracted_text)
|
| 99 |
|
|
@@ -139,7 +163,7 @@ def process_image(image, mode, entry_type, quantity):
|
|
| 139 |
|
| 140 |
# Gradio App
|
| 141 |
def app():
|
| 142 |
-
with gr.Blocks() as interface:
|
| 143 |
gr.Markdown("<h1>π’ VENKATARAMANA MOTORS</h1>")
|
| 144 |
|
| 145 |
with gr.Tab("π₯ Stock Entry & Processing"):
|
|
@@ -153,7 +177,7 @@ def app():
|
|
| 153 |
with gr.Column():
|
| 154 |
image_view = gr.Text(label="π Extracted Attributes", interactive=False)
|
| 155 |
result_output = gr.Text(label="π Salesforce Export Result", interactive=False)
|
| 156 |
-
submit_button = gr.Button("π Process & Export")
|
| 157 |
|
| 158 |
# Button Click Events
|
| 159 |
submit_button.click(fn=process_image, inputs=[image_input, mode_dropdown, entry_type_radio, quantity_input], outputs=[image_view, result_output])
|
|
|
|
| 37 |
"Usage": "Usage_Application__c",
|
| 38 |
}
|
| 39 |
|
| 40 |
+
# π¨ **CSS Styling for UI**
|
| 41 |
+
CSS_STYLE = """
|
| 42 |
+
body {
|
| 43 |
+
background: linear-gradient(135deg, #141E30, #243B55);
|
| 44 |
+
color: white;
|
| 45 |
+
font-family: 'Poppins', sans-serif;
|
| 46 |
+
}
|
| 47 |
+
.gradio-container {
|
| 48 |
+
border-radius: 15px;
|
| 49 |
+
padding: 20px;
|
| 50 |
+
background: rgba(255, 255, 255, 0.2);
|
| 51 |
+
}
|
| 52 |
+
h1 {
|
| 53 |
+
text-align: center;
|
| 54 |
+
color: white;
|
| 55 |
+
text-shadow: 2px 2px 10px rgba(255, 255, 255, 0.8);
|
| 56 |
+
}
|
| 57 |
+
.gradio-button {
|
| 58 |
+
background: linear-gradient(to right, #ff416c, #ff4b2b);
|
| 59 |
+
color: white;
|
| 60 |
+
font-weight: bold;
|
| 61 |
+
border-radius: 10px;
|
| 62 |
+
padding: 12px;
|
| 63 |
+
}
|
| 64 |
+
.gradio-button:hover {
|
| 65 |
+
background: linear-gradient(to right, #ff4b2b, #ff416c);
|
| 66 |
+
}
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
# π§ Initialize PaddleOCR
|
| 70 |
try:
|
| 71 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
|
|
|
| 90 |
best_match, best_score = match, score
|
| 91 |
return best_match if best_score >= 70 else None # Threshold: 70
|
| 92 |
|
| 93 |
+
# Function to extract attributes from text (including quantity)
|
| 94 |
def extract_attributes(extracted_text):
|
| 95 |
attributes = {}
|
| 96 |
for readable_attr, sf_attr in ATTRIBUTE_MAPPING.items():
|
|
|
|
| 98 |
match = re.search(pattern, extracted_text, re.IGNORECASE)
|
| 99 |
if match:
|
| 100 |
attributes[readable_attr] = match.group(1).strip()
|
| 101 |
+
|
| 102 |
+
# Extract Quantity from text (e.g., "Quantity: 10")
|
| 103 |
+
quantity_match = re.search(r"\bQuantity[:\-]?\s*(\d+)", extracted_text, re.IGNORECASE)
|
| 104 |
+
if quantity_match:
|
| 105 |
+
attributes["Quantity"] = quantity_match.group(1)
|
| 106 |
+
|
| 107 |
return attributes
|
| 108 |
|
| 109 |
# Function to export extracted data to Salesforce
|
|
|
|
| 115 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 116 |
)
|
| 117 |
|
| 118 |
+
object_name = "Inventory_Management__c" if entry_type == "Sales" else "Un_Billable__c"
|
| 119 |
+
field_name = "Quantity__c" if mode == "Entry" else "Quantity_Sold__c"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
|
|
|
| 121 |
product_name = match_product_name(extracted_text)
|
| 122 |
attributes = extract_attributes(extracted_text)
|
| 123 |
|
|
|
|
| 163 |
|
| 164 |
# Gradio App
|
| 165 |
def app():
|
| 166 |
+
with gr.Blocks(css=CSS_STYLE) as interface:
|
| 167 |
gr.Markdown("<h1>π’ VENKATARAMANA MOTORS</h1>")
|
| 168 |
|
| 169 |
with gr.Tab("π₯ Stock Entry & Processing"):
|
|
|
|
| 177 |
with gr.Column():
|
| 178 |
image_view = gr.Text(label="π Extracted Attributes", interactive=False)
|
| 179 |
result_output = gr.Text(label="π Salesforce Export Result", interactive=False)
|
| 180 |
+
submit_button = gr.Button("π Process & Export", elem_classes="gradio-button")
|
| 181 |
|
| 182 |
# Button Click Events
|
| 183 |
submit_button.click(fn=process_image, inputs=[image_input, mode_dropdown, entry_type_radio, quantity_input], outputs=[image_view, result_output])
|