Spaces:
Build error
Build error
File size: 8,445 Bytes
4c75aa1 7964c55 cdaf086 8ace6c0 e27d3ef cdaf086 8ace6c0 174399d 8ace6c0 6ac698b 29b77bd 0504d2d 5d16fd7 fba3ec7 5d16fd7 1599da8 e27d3ef 174399d fba3ec7 174399d 65c8859 0504d2d 6ac698b a5132a8 55a87f6 e27d3ef 0504d2d 174399d cdaf086 850c91c cdaf086 fba3ec7 29b77bd 5d16fd7 fba3ec7 e27d3ef fba3ec7 e27d3ef fba3ec7 5d16fd7 e27d3ef 5d16fd7 fba3ec7 1370cc9 fba3ec7 1657b71 fba3ec7 5d16fd7 d30b8f4 e27d3ef fba3ec7 1599da8 d30b8f4 1599da8 fba3ec7 6570958 0504d2d fba3ec7 6685602 cdaf086 fba3ec7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
import os
from paddleocr import PaddleOCR
from PIL import Image
import gradio as gr
import requests
import re
from simple_salesforce import Salesforce
import pandas as pd
import matplotlib.pyplot as plt
from io import BytesIO
from fuzzywuzzy import process
import kaleido
# Attribute mappings: readable names to Salesforce API names
ATTRIBUTE_MAPPING = {
"Product name": "Productname__c",
"Colour": "Colour__c",
"Motortype": "Motortype__c",
"Frequency": "Frequency__c",
"Grossweight": "Grossweight__c",
"Ratio": "Ratio__c",
"MotorFrame": "Motorframe__c",
"Model": "Model__c",
"Speed": "Speed__c",
"Quantity": "Quantity__c",
"Voltage": "Voltage__c",
"Material": "Material__c",
"Type": "Type__c",
"Horsepower": "Horsepower__c",
"Consignee": "Consignee__c",
"LOT": "LOT__c",
"Stage": "Stage__c",
"Outlet": "Outlet__c",
"Serialnumber": "Serialnumber__c",
"HeadSize": "Headsize__c",
"Deliverysize": "Deliverysize__c",
"Phase": "Phase__c",
"Size": "Size__c",
"MRP": "MRP__c",
"Usebefore": "Usebefore__c",
"Height": "Height__c",
"MaximumDischarge Flow": "Maximumdischargeflow__c",
"DischargeRange": "Dischargeflow__c",
"Assembledby": "Manufacturer__c",
"Manufacturedate": "Manufacturedate__c",
"Companyname": "Companyname__c",
"Customercarenumber": "Customercarenumber__c",
"SellerAddress": "Selleraddress__c",
"Selleremail": "Selleremail__c",
"GSTIN": "GSTIN__c",
"Totalamount": "Totalamount__c",
"Paymentstatus": "Paymentstatus__c",
"Paymentmethod": "Paymentstatus__c",
"Invoicedate": "Manufacturedate__c",
"Warranty": "Warranty__c",
"Brand": "Brand__c",
"Motorhorsepower": "Motorhorsepower__c",
"Power": "Power__c",
"Motorphase": "Motorphase__c",
"Enginetype": "Enginetype__c",
"Tankcapacity": "Tankcapacity__c",
"Head": "Head__c",
"Usage/Application": "Usage_Application__c",
"Volts": "volts__c",
"Hertz": "Hertz__c",
"Frame": "frame__c",
"Mounting": "Mounting__c",
"Tollfreenumber": "Tollfreenumber__c",
"Pipesize": "Pipesize__c",
"Manufacturer": "Manufacturer__c",
"Office": "Office__c",
"SRnumber": "SRnumber__c",
"TypeOfEndUse": "TypeOfEndUse__c",
"Model Name": "Model_Name_Number__c",
"coolingmethod": "coolingmethod__c",
"H.P.": "H_p__c"
}
# List of product names to match
PRODUCT_NAMES = [
"Fusion", "Agroking", "CG commercial motors", "Jaguar", "Gaurav"
]
# Salesforce credentials
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
SALESFORCE_PASSWORD = "Seta12345@"
SALESFORCE_SECURITY_TOKEN = "Drl0jchCwLBfvX4ODMeFDksP"
# Initialize PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='en')
# Function to extract text using PaddleOCR
def extract_text(image):
result = ocr.ocr(image)
extracted_text = []
for line in result[0]:
extracted_text.append(line[1][0])
return "\n".join(extracted_text)
# Function to match product name using fuzzy matching
def match_product_name(extracted_text):
best_match = None
best_score = 0
for line in extracted_text.split("\n"):
match, score = process.extractOne(line, PRODUCT_NAMES)
if score > best_score:
best_match = match
best_score = score
return best_match if best_score >= 70 else None
# Function to extract attributes and their values
def extract_attributes(extracted_text):
attributes = {}
for readable_attr, sf_attr in ATTRIBUTE_MAPPING.items():
pattern = rf"{re.escape(readable_attr)}[:\-]?\s*(.+)"
match = re.search(pattern, extracted_text, re.IGNORECASE)
if match:
attributes[readable_attr] = match.group(1).strip()
return attributes
# Function to filter attributes for valid Salesforce fields
def filter_valid_attributes(attributes, valid_fields):
return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
# Function to pull structured data from Salesforce and display as a table
def pull_data_from_salesforce(data_type):
try:
sf = Salesforce(
username=SALESFORCE_USERNAME,
password=SALESFORCE_PASSWORD,
security_token=SALESFORCE_SECURITY_TOKEN
)
if data_type == "Inventory":
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stocks__c, soldstock__c, Price__c, Last_Modified_Date__c FROM Inventory_Management__c LIMIT 100"
else:
query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stock__c, soldstock__c, Price__c, Last_Modified_Date__c FROM Un_Billable__c LIMIT 100"
response = sf.query_all(query)
records = response.get("records", [])
if not records:
return "No data found in Salesforce.", None, None, None
df = pd.DataFrame(records)
df = df.drop(columns=['attributes'], errors='ignore')
# Rename columns for better readability
df.rename(columns={
"Productname__c": "Product Name",
"Model__c": "Model",
"H_p__c": "H.P",
"Stage__c": "Stage",
"Current_Stocks__c": "Current Stocks",
"Current_Stock__c": "Current Stocks",
"soldstock__c": "Sold Stock",
"Price__c": "Price",
"Last_Modified_Date__c": "Last Modified Date"
}, inplace=True)
excel_path = "salesforce_data.xlsx"
df.to_excel(excel_path, index=False)
# Generate interactive vertical bar graph using Matplotlib
fig, ax = plt.subplots(figsize=(12, 8))
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
ax.set_title("Stock Distribution by Product Name")
ax.set_xlabel("Product Name")
ax.set_ylabel("Current Stocks")
plt.xticks(rotation=45, ha="right", fontsize=10)
plt.tight_layout()
buffer = BytesIO()
plt.savefig(buffer, format="png")
buffer.seek(0)
img = Image.open(buffer)
return df, excel_path, img
except Exception as e:
return f"Error fetching data: {str(e)}", None, None, None
# Gradio Interface
def app():
with gr.Blocks() as demo:
with gr.Tab("π₯ OCR Processing"):
with gr.Row():
image_input = gr.Image(type="numpy", label="π Upload Image")
mode_input = gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry")
entry_type_input = gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales")
quantity_input = gr.Number(label="π’ Quantity", value=1, interactive=True)
extract_button = gr.Button("Extract Text and Attributes")
extracted_text_output = gr.Text(label="π Extracted Image Data")
editable_df_output = gr.Dataframe(label="βοΈ Edit Attributes (Key-Value Pairs)", interactive=True)
ok_button = gr.Button("OK")
result_output = gr.Text(label="π Result")
with gr.Tab("π Salesforce Data"):
data_type_input = gr.Dropdown(label="Select Data Type", choices=["Inventory", "Unbilling"], value="Inventory")
pull_button = gr.Button("Pull Data from Salesforce")
salesforce_data_output = gr.Dataframe(label="π Salesforce Data")
excel_download_output = gr.File(label="π₯ Download Excel")
graph_output = gr.Image(label="π Stock Distribution Graph")
# Define button actions
extract_button.click(
fn=process_image,
inputs=[image_input, mode_input, entry_type_input, quantity_input],
outputs=[extracted_text_output, editable_df_output, result_output]
)
ok_button.click(
fn=export_to_salesforce,
inputs=[mode_input, entry_type_input, quantity_input, editable_df_output],
outputs=[result_output]
)
pull_button.click(
fn=pull_data_from_salesforce,
inputs=[data_type_input],
outputs=[salesforce_data_output, excel_download_output, graph_output]
)
return demo
if __name__ == "__main__":
app().launch(share=True) |