Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,113 +10,13 @@ import matplotlib.pyplot as plt
|
|
| 10 |
from io import BytesIO
|
| 11 |
from fuzzywuzzy import process
|
| 12 |
|
| 13 |
-
|
| 14 |
-
ATTRIBUTE_MAPPING = {
|
| 15 |
-
"Product name": "Productname__c",
|
| 16 |
-
"Colour": "Colour__c",
|
| 17 |
-
"Motortype": "Motortype__c",
|
| 18 |
-
"Frequency": "Frequency__c",
|
| 19 |
-
"Grossweight": "Grossweight__c",
|
| 20 |
-
"Ratio": "Ratio__c",
|
| 21 |
-
"MotorFrame": "Motorframe__c",
|
| 22 |
-
"Model": "Model__c",
|
| 23 |
-
"Speed": "Speed__c",
|
| 24 |
-
"Quantity": "Quantity__c",
|
| 25 |
-
"Voltage": "Voltage__c",
|
| 26 |
-
"Material": "Material__c",
|
| 27 |
-
"Type": "Type__c",
|
| 28 |
-
"Horsepower": "Horsepower__c",
|
| 29 |
-
"Consignee": "Consignee__c",
|
| 30 |
-
"LOT": "LOT__c",
|
| 31 |
-
"Stage": "Stage__c",
|
| 32 |
-
"Outlet": "Outlet__c",
|
| 33 |
-
"Serialnumber": "Serialnumber__c",
|
| 34 |
-
"HeadSize": "Headsize__c",
|
| 35 |
-
"Deliverysize": "Deliverysize__c",
|
| 36 |
-
"Phase": "Phase__c",
|
| 37 |
-
"Size": "Size__c",
|
| 38 |
-
"MRP": "MRP__c",
|
| 39 |
-
"Usebefore": "Usebefore__c",
|
| 40 |
-
"Height": "Height__c",
|
| 41 |
-
"MaximumDischarge Flow": "Maximumdischargeflow__c",
|
| 42 |
-
"DischargeRange": "Dischargeflow__c",
|
| 43 |
-
"Assembledby": "Manufacturer__c",
|
| 44 |
-
"Manufacturedate": "Manufacturedate__c",
|
| 45 |
-
"Companyname": "Companyname__c",
|
| 46 |
-
"Customercarenumber": "Customercarenumber__c",
|
| 47 |
-
"SellerAddress": "Selleraddress__c",
|
| 48 |
-
"Selleremail": "Selleremail__c",
|
| 49 |
-
"GSTIN": "GSTIN__c",
|
| 50 |
-
"Totalamount": "Totalamount__c",
|
| 51 |
-
"Paymentstatus": "Paymentstatus__c",
|
| 52 |
-
"Paymentmethod": "Paymentstatus__c",
|
| 53 |
-
"Invoicedate": "Manufacturedate__c",
|
| 54 |
-
"Warranty": "Warranty__c",
|
| 55 |
-
"Brand": "Brand__c",
|
| 56 |
-
"Motorhorsepower": "Motorhorsepower__c",
|
| 57 |
-
"Power": "Power__c",
|
| 58 |
-
"Motorphase": "Motorphase__c",
|
| 59 |
-
"Enginetype": "Enginetype__c",
|
| 60 |
-
"Tankcapacity": "Tankcapacity__c",
|
| 61 |
-
"Head": "Head__c",
|
| 62 |
-
"Usage/Application": "Usage_Application__c",
|
| 63 |
-
"Volts": "volts__c",
|
| 64 |
-
"Hertz": "Hertz__c",
|
| 65 |
-
"Frame": "frame__c",
|
| 66 |
-
"Mounting": "Mounting__c",
|
| 67 |
-
"Tollfreenumber": "Tollfreenumber__c",
|
| 68 |
-
"Pipesize": "Pipesize__c",
|
| 69 |
-
"Manufacturer": "Manufacturer__c",
|
| 70 |
-
"Office": "Office__c",
|
| 71 |
-
"SRnumber": "SRnumber__c",
|
| 72 |
-
"TypeOfEndUse": "TypeOfEndUse__c",
|
| 73 |
-
"Model Name": "Model_Name_Number__c",
|
| 74 |
-
"coolingmethod": "coolingmethod__c"
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
# List of product names to match
|
| 78 |
-
PRODUCT_NAMES = [
|
| 79 |
-
"Centrifugal mono block pump", "SINGLE PHASE MOTOR STARTER", "EasyPact EZC 100",
|
| 80 |
-
"Openwell Submersible Pumpset", "Electric Motor", "Self Priming Pump",
|
| 81 |
-
"Control panel for single phase submersible pumps", "MOTOR", "Submersible pump set",
|
| 82 |
-
"Fusion submersible pump set", "DCT", "Shock proof water proof", "CG COMMERCIAL MOTORS", "Fusion",
|
| 83 |
-
"control panel for single phase submerisible pumps",
|
| 84 |
-
"single phase digital starter dry run and timer panel", "5HP AV1 XL Kirloskar Pump",
|
| 85 |
-
"Phase stainless steel submersible pump", "Submersible pump", "WB15X",
|
| 86 |
-
"Vtype self priming pump", "SP SHINE DISC", "havells submersible pump",
|
| 87 |
-
"Havells open well Submersible pump", "Bertolini pump CK3 90pp",
|
| 88 |
-
"WPA 772 Water Pump Assy", "bertolini TTL triplex high pressure plunger pumps",
|
| 89 |
-
"Generic plunger high pressure pump", "Apple Normal, Banana",
|
| 90 |
-
"Cast Iron KSb centrifugal pump", "5.5kw Water Pump",
|
| 91 |
-
"KSB reliable i line centrifuged pumps", "Apple Normal, Orange, Banana",
|
| 92 |
-
"Positive API 6745 hydraulic diaphragm pump", "1/2 inch Fuel Hose Pipe", "Kirloskar Water Pump",
|
| 93 |
-
"Rotodel motor pump", "PVC Electrical Insulation Materials",
|
| 94 |
-
"Electric kirloskar domestic water pump", "Electrical Insulation Materials",
|
| 95 |
-
"sellowell motor pump", "bhupathi submersible pump set",
|
| 96 |
-
"Flowshine Submersible pump set", "Index submersible pump",
|
| 97 |
-
"Wintoss Plastic Electric Switch Board", "Electric 18 watt ujagar cooler pump",
|
| 98 |
-
"Generator Service", "LG WM FHT1207ZWL, LG REF GL-S292RSCY",
|
| 99 |
-
"Water tank, Filters, Water Pump", "MS Control Submersible Panel",
|
| 100 |
-
"Centrifugal Monoblock Pumps", "Electric Motor with Pump BodyBlue and White",
|
| 101 |
-
"Various Repair and Maintenance Parts", "Earthmax Pump",
|
| 102 |
-
"Water Tank, Filters, Water Pump", "Centrifugal Water Pump for Agriculture",
|
| 103 |
-
"mono block pumps"
|
| 104 |
-
]
|
| 105 |
-
|
| 106 |
-
#π Salesforce credentials
|
| 107 |
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 108 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
| 109 |
SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
|
| 110 |
|
| 111 |
-
|
| 112 |
-
ocr = PaddleOCR(
|
| 113 |
-
use_angle_cls=False, # Disable angle classification to reduce memory
|
| 114 |
-
lang='en',
|
| 115 |
-
det_model_dir=None,
|
| 116 |
-
rec_model_dir=None,
|
| 117 |
-
cls_model_dir=None
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
|
| 121 |
# Function to extract text using PaddleOCR
|
| 122 |
def extract_text(image):
|
|
@@ -126,365 +26,95 @@ def extract_text(image):
|
|
| 126 |
extracted_text.append(line[1][0])
|
| 127 |
return "\n".join(extracted_text)
|
| 128 |
|
| 129 |
-
# Function to
|
| 130 |
-
def
|
| 131 |
-
best_match = None
|
| 132 |
-
best_score = 0
|
| 133 |
-
|
| 134 |
-
for line in extracted_text.split("\n"):
|
| 135 |
-
match, score = process.extractOne(line, PRODUCT_NAMES)
|
| 136 |
-
if score > best_score:
|
| 137 |
-
best_match = match
|
| 138 |
-
best_score = score
|
| 139 |
-
|
| 140 |
-
return best_match if best_score >= 70 else None # Threshold of 70 for a match
|
| 141 |
-
|
| 142 |
-
# Function to extract attributes and their values
|
| 143 |
-
def extract_attributes(extracted_text):
|
| 144 |
-
attributes = {}
|
| 145 |
-
|
| 146 |
-
for readable_attr, sf_attr in ATTRIBUTE_MAPPING.items():
|
| 147 |
-
pattern = rf"{re.escape(readable_attr)}[:\-]?\s*(.+)"
|
| 148 |
-
match = re.search(pattern, extracted_text, re.IGNORECASE)
|
| 149 |
-
if match:
|
| 150 |
-
attributes[readable_attr] = match.group(1).strip()
|
| 151 |
-
|
| 152 |
-
return attributes
|
| 153 |
-
|
| 154 |
-
# Function to filter attributes for valid Salesforce fields
|
| 155 |
-
def filter_valid_attributes(attributes, valid_fields):
|
| 156 |
-
return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
|
| 157 |
-
|
| 158 |
-
#π Function to interact with Salesforce based on mode and type
|
| 159 |
-
def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
| 160 |
try:
|
| 161 |
sf = Salesforce(
|
| 162 |
username=SALESFORCE_USERNAME,
|
| 163 |
password=SALESFORCE_PASSWORD,
|
| 164 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 165 |
)
|
| 166 |
-
|
| 167 |
-
# Mapping mode and entry_type to Salesforce object and field
|
| 168 |
-
object_name = None
|
| 169 |
-
field_name = None
|
| 170 |
-
product_field_name = "Product_Name__c" # Correct field for product name in the object
|
| 171 |
-
model_field_name = "Modal_Name__c" # Correct field for model name in the object
|
| 172 |
-
|
| 173 |
-
if mode == "Entry":
|
| 174 |
-
if entry_type == "Sales":
|
| 175 |
-
object_name = "VENKATA_RAMANA_MOTORS__c"
|
| 176 |
-
field_name = "Quantity__c"
|
| 177 |
-
elif entry_type == "Non-Sales":
|
| 178 |
-
object_name = "UNBILLING_DATA__c"
|
| 179 |
-
field_name = "TotalQuantity__c"
|
| 180 |
-
elif mode == "Exit":
|
| 181 |
-
if entry_type == "Sales":
|
| 182 |
-
object_name = "Inventory_Management__c"
|
| 183 |
-
product_field_name = "Product_Name__c"
|
| 184 |
-
model_field_name = "Modal_Name__c"
|
| 185 |
-
field_name = "Quantity_Sold__c"
|
| 186 |
-
elif entry_type == "Non-Sales":
|
| 187 |
-
object_name = "Un_Billable__c"
|
| 188 |
-
product_field_name = "Product_Name__c"
|
| 189 |
-
model_field_name = "Model_Name__c"
|
| 190 |
-
field_name = "Sold_Out__c"
|
| 191 |
-
|
| 192 |
-
if not object_name or not field_name:
|
| 193 |
-
return "Invalid mode or entry type."
|
| 194 |
-
|
| 195 |
-
# Get valid fields for the specified Salesforce object
|
| 196 |
-
sf_object = sf.__getattr__(object_name)
|
| 197 |
-
schema = sf_object.describe()
|
| 198 |
-
valid_fields = {field["name"] for field in schema["fields"]}
|
| 199 |
-
|
| 200 |
-
# Extract product name or model number
|
| 201 |
-
product_name = match_product_name(extracted_text)
|
| 202 |
-
attributes = extract_attributes(extracted_text)
|
| 203 |
-
|
| 204 |
-
if not product_name:
|
| 205 |
-
return "Product name could not be matched from the extracted text."
|
| 206 |
-
|
| 207 |
-
attributes["Product name"] = product_name
|
| 208 |
-
|
| 209 |
-
if mode == "Exit":
|
| 210 |
-
query = f"SELECT Id, {field_name} FROM {object_name} WHERE {product_field_name} = '{product_name}' OR {model_field_name} = '{attributes.get('Model Name', '')}' LIMIT 1"
|
| 211 |
-
response = sf.query(query)
|
| 212 |
-
|
| 213 |
-
if response["records"]:
|
| 214 |
-
record_id = response["records"][0]["Id"]
|
| 215 |
-
updated_quantity = quantity # Overwrite the quantity, don't add
|
| 216 |
-
sf_object.update(record_id, {field_name: updated_quantity})
|
| 217 |
-
return f"Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
|
| 218 |
-
else:
|
| 219 |
-
return f"No matching record found for product '{product_name}' in {object_name}."
|
| 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)}"
|
| 228 |
-
|
| 229 |
-
# Function to pull data from Salesforce
|
| 230 |
-
def pull_data_from_salesforce():
|
| 231 |
-
try:
|
| 232 |
-
sf = Salesforce(
|
| 233 |
-
username=SALESFORCE_USERNAME,
|
| 234 |
-
password=SALESFORCE_PASSWORD,
|
| 235 |
-
security_token=SALESFORCE_SECURITY_TOKEN
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
query = "SELECT Product_Name__c, Modal_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
|
| 239 |
response = sf.query_all(query)
|
| 240 |
-
|
| 241 |
-
records = response.get("records", [])
|
| 242 |
-
if not records:
|
| 243 |
-
return "No data found in Salesforce.", None, None, None
|
| 244 |
-
|
| 245 |
df = pd.DataFrame(records)
|
| 246 |
-
df = df.
|
| 247 |
-
|
| 248 |
-
df.rename(columns={
|
| 249 |
"Product_Name__c": "Product Name",
|
| 250 |
"Modal_Name__c": "Model Name",
|
| 251 |
"Current_Stocks__c": "Current Stocks"
|
| 252 |
-
}
|
| 253 |
-
|
| 254 |
-
excel_path = "salesforce_data.xlsx"
|
| 255 |
-
df.to_excel(excel_path, index=False)
|
| 256 |
-
|
| 257 |
-
# Generate interactive bar graph using Plotly
|
| 258 |
-
fig = go.Figure()
|
| 259 |
-
fig.add_trace(go.Bar(
|
| 260 |
-
x=df['Product Name'],
|
| 261 |
-
y=df['Current Stocks'],
|
| 262 |
-
marker=dict(color='blue'),
|
| 263 |
-
hoverinfo='x+y',
|
| 264 |
-
hovertemplate='<b>Product Name:</b> %{x}<br><b>Current Stocks:</b> %{y}<extra></extra>',
|
| 265 |
-
text=df['Current Stocks'],
|
| 266 |
-
textposition='outside'
|
| 267 |
-
))
|
| 268 |
-
|
| 269 |
-
fig.update_layout(
|
| 270 |
-
title="Current Stocks of Products",
|
| 271 |
-
xaxis=dict(title="Product Name", tickangle=-45),
|
| 272 |
-
yaxis=dict(title="Stock Quantity"),
|
| 273 |
-
hovermode='x',
|
| 274 |
-
dragmode='zoom',
|
| 275 |
-
showlegend=False
|
| 276 |
-
)
|
| 277 |
-
|
| 278 |
-
return "Data successfully retrieved.", df, excel_path, fig
|
| 279 |
except Exception as e:
|
| 280 |
-
return
|
| 281 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
# Unified function to handle image processing and Salesforce interaction
|
| 284 |
def process_image(image, mode, entry_type, quantity):
|
| 285 |
extracted_text = extract_text(image)
|
| 286 |
if not extracted_text:
|
| 287 |
return "No text detected in the image.", None
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
attributes = extract_attributes(extracted_text)
|
| 291 |
-
if product_name:
|
| 292 |
-
attributes["Product name"] = product_name
|
| 293 |
-
|
| 294 |
-
# Interact with Salesforce
|
| 295 |
-
message = interact_with_salesforce(mode, entry_type, quantity, extracted_text)
|
| 296 |
-
|
| 297 |
-
numbered_output = "\n".join([f"{key}: {value}" for key, value in attributes.items()])
|
| 298 |
-
return f"Extracted Text:\n{extracted_text}\n\nAttributes and Values:\n{numbered_output}", message
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
import gradio as gr
|
| 302 |
-
import numpy as np
|
| 303 |
-
from PIL import Image
|
| 304 |
|
| 305 |
# Function to fetch and return Salesforce data
|
| 306 |
-
def
|
| 307 |
-
df =
|
| 308 |
if df is not None:
|
| 309 |
table_component = df.to_html(index=False)
|
| 310 |
-
|
| 311 |
-
return table_component,
|
| 312 |
return "<p>No Data Available</p>", None
|
| 313 |
|
| 314 |
-
import gradio as gr
|
| 315 |
-
|
| 316 |
def app():
|
| 317 |
-
with gr.Blocks(
|
| 318 |
-
/* General Styling */
|
| 319 |
-
.gradio-container {
|
| 320 |
-
background: #0b0f29; /* Deep Dark Blue */
|
| 321 |
-
font-family: 'Poppins', sans-serif;
|
| 322 |
-
color: white;
|
| 323 |
-
padding: 10px;
|
| 324 |
-
}
|
| 325 |
-
|
| 326 |
-
h1, h2, h3 {
|
| 327 |
-
text-align: center;
|
| 328 |
-
color: white;
|
| 329 |
-
font-weight: bold;
|
| 330 |
-
text-shadow: 0px 0px 10px rgba(255,255,255,0.8);
|
| 331 |
-
}
|
| 332 |
-
|
| 333 |
-
/* Cards & Elements */
|
| 334 |
-
.card {
|
| 335 |
-
background: rgba(255, 255, 255, 0.1);
|
| 336 |
-
border-radius: 15px;
|
| 337 |
-
padding: 20px;
|
| 338 |
-
width: 100%;
|
| 339 |
-
max-width: 900px;
|
| 340 |
-
margin: auto;
|
| 341 |
-
box-shadow: 0px 0px 25px rgba(0, 255, 255, 0.6);
|
| 342 |
-
transition: 0.3s;
|
| 343 |
-
}
|
| 344 |
-
|
| 345 |
-
.card:hover {
|
| 346 |
-
box-shadow: 0px 0px 30px rgba(0, 255, 255, 0.9);
|
| 347 |
-
}
|
| 348 |
-
|
| 349 |
-
/* Buttons */
|
| 350 |
-
.gradio-button {
|
| 351 |
-
background: linear-gradient(to right, #ff416c, #ff4b2b);
|
| 352 |
-
color: white !important;
|
| 353 |
-
border-radius: 10px !important;
|
| 354 |
-
font-weight: bold;
|
| 355 |
-
padding: 12px;
|
| 356 |
-
transition: all 0.3s ease-in-out;
|
| 357 |
-
box-shadow: 0px 0px 20px rgba(255, 0, 255, 0.6);
|
| 358 |
-
}
|
| 359 |
-
|
| 360 |
-
.gradio-button:hover {
|
| 361 |
-
background: linear-gradient(to right, #ff4b2b, #ff416c);
|
| 362 |
-
box-shadow: 0px 0px 30px rgba(255, 0, 255, 0.9);
|
| 363 |
-
}
|
| 364 |
-
|
| 365 |
-
/* Tabs */
|
| 366 |
-
.gradio-tab {
|
| 367 |
-
font-size: 16px;
|
| 368 |
-
color: white;
|
| 369 |
-
font-weight: bold;
|
| 370 |
-
text-shadow: 0px 0px 10px rgba(255, 255, 255, 0.6);
|
| 371 |
-
}
|
| 372 |
-
|
| 373 |
-
/* Scrollable Table */
|
| 374 |
-
.table-container {
|
| 375 |
-
width: 100%;
|
| 376 |
-
max-width: 900px;
|
| 377 |
-
max-height: 400px;
|
| 378 |
-
overflow-y: auto;
|
| 379 |
-
overflow-x: auto;
|
| 380 |
-
display: flex;
|
| 381 |
-
justify-content: center;
|
| 382 |
-
margin: auto;
|
| 383 |
-
}
|
| 384 |
-
|
| 385 |
-
table {
|
| 386 |
-
width: 100%;
|
| 387 |
-
border-collapse: collapse;
|
| 388 |
-
background: rgba(255, 255, 255, 0.1);
|
| 389 |
-
color: white;
|
| 390 |
-
min-width: 600px;
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
th, td {
|
| 394 |
-
padding: 12px;
|
| 395 |
-
text-align: left;
|
| 396 |
-
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
|
| 397 |
-
font-size: 14px;
|
| 398 |
-
}
|
| 399 |
-
|
| 400 |
-
th {
|
| 401 |
-
background: rgba(0, 255, 255, 0.2);
|
| 402 |
-
}
|
| 403 |
-
|
| 404 |
-
/* Mobile View */
|
| 405 |
-
@media (max-width: 768px) {
|
| 406 |
-
.gradio-container {
|
| 407 |
-
padding: 10px;
|
| 408 |
-
}
|
| 409 |
-
|
| 410 |
-
.card {
|
| 411 |
-
width: 100%;
|
| 412 |
-
max-width: 100%;
|
| 413 |
-
padding: 10px;
|
| 414 |
-
}
|
| 415 |
-
|
| 416 |
-
.table-container {
|
| 417 |
-
max-height: 350px;
|
| 418 |
-
overflow-y: scroll;
|
| 419 |
-
}
|
| 420 |
-
|
| 421 |
-
table {
|
| 422 |
-
width: 100%;
|
| 423 |
-
min-width: 100%;
|
| 424 |
-
}
|
| 425 |
-
|
| 426 |
-
th, td {
|
| 427 |
-
font-size: 12px;
|
| 428 |
-
padding: 8px;
|
| 429 |
-
}
|
| 430 |
-
}
|
| 431 |
-
|
| 432 |
-
""") as interface:
|
| 433 |
-
|
| 434 |
gr.Markdown("<h1>π’ VENKATARAMANA MOTORS</h1>")
|
| 435 |
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
)
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
title="Salesforce Data Visualization",
|
| 467 |
-
description="View, visualize, and download Salesforce data (Product Name, Model Name, Current Stocks)."
|
| 468 |
-
)
|
| 469 |
-
],
|
| 470 |
-
["Stock Entry & Processing", "Salesforce Data Visualization"]
|
| 471 |
-
)
|
| 472 |
-
|
| 473 |
-
return interface
|
| 474 |
-
import gradio as gr
|
| 475 |
-
|
| 476 |
-
def sample_function(input_text):
|
| 477 |
-
return f"Processed: {input_text}"
|
| 478 |
-
|
| 479 |
-
with gr.Blocks() as app:
|
| 480 |
-
gr.Markdown("## My Gradio App")
|
| 481 |
-
input_box = gr.Textbox(label="Enter something")
|
| 482 |
-
output_box = gr.Textbox(label="Output")
|
| 483 |
-
button = gr.Button("Submit")
|
| 484 |
-
button.click(sample_function, inputs=input_box, outputs=output_box)
|
| 485 |
|
| 486 |
if __name__ == "__main__":
|
| 487 |
-
app.launch(
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
|
|
|
| 10 |
from io import BytesIO
|
| 11 |
from fuzzywuzzy import process
|
| 12 |
|
| 13 |
+
# Salesforce credentials
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 15 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
| 16 |
SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
|
| 17 |
|
| 18 |
+
# Initialize PaddleOCR
|
| 19 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Function to extract text using PaddleOCR
|
| 22 |
def extract_text(image):
|
|
|
|
| 26 |
extracted_text.append(line[1][0])
|
| 27 |
return "\n".join(extracted_text)
|
| 28 |
|
| 29 |
+
# Function to fetch data from Salesforce and format it into a DataFrame
|
| 30 |
+
def fetch_salesforce_data():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
sf = Salesforce(
|
| 33 |
username=SALESFORCE_USERNAME,
|
| 34 |
password=SALESFORCE_PASSWORD,
|
| 35 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 36 |
)
|
| 37 |
+
query = "SELECT Product_Name__c, Modal_Name__c, Current_Stocks__c FROM MotorDataAPI__c"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
response = sf.query_all(query)
|
| 39 |
+
records = response["records"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
df = pd.DataFrame(records)
|
| 41 |
+
df = df.rename(columns={
|
|
|
|
|
|
|
| 42 |
"Product_Name__c": "Product Name",
|
| 43 |
"Modal_Name__c": "Model Name",
|
| 44 |
"Current_Stocks__c": "Current Stocks"
|
| 45 |
+
})
|
| 46 |
+
return df[["Product Name", "Model Name", "Current Stocks"]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
+
return None
|
| 49 |
|
| 50 |
+
# Function to generate a bar graph from Salesforce data
|
| 51 |
+
def generate_salesforce_bar_chart(df):
|
| 52 |
+
try:
|
| 53 |
+
plt.figure(figsize=(12, 6))
|
| 54 |
+
df.plot(kind='bar', x="Product Name", y="Current Stocks", legend=False)
|
| 55 |
+
plt.title("Stock Distribution by Product Name")
|
| 56 |
+
plt.xlabel("Product Name")
|
| 57 |
+
plt.ylabel("Current Stocks")
|
| 58 |
+
plt.xticks(rotation=45, ha="right")
|
| 59 |
+
plt.tight_layout()
|
| 60 |
+
buffer = BytesIO()
|
| 61 |
+
plt.savefig(buffer, format="png")
|
| 62 |
+
buffer.seek(0)
|
| 63 |
+
return Image.open(buffer)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return None
|
| 66 |
|
| 67 |
# Unified function to handle image processing and Salesforce interaction
|
| 68 |
def process_image(image, mode, entry_type, quantity):
|
| 69 |
extracted_text = extract_text(image)
|
| 70 |
if not extracted_text:
|
| 71 |
return "No text detected in the image.", None
|
| 72 |
+
|
| 73 |
+
return f"Extracted Text:\n{extracted_text}\n\nProcessing Completed.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Function to fetch and return Salesforce data
|
| 76 |
+
def get_salesforce_visualization():
|
| 77 |
+
df = fetch_salesforce_data()
|
| 78 |
if df is not None:
|
| 79 |
table_component = df.to_html(index=False)
|
| 80 |
+
bar_chart_image = generate_salesforce_bar_chart(df)
|
| 81 |
+
return table_component, bar_chart_image
|
| 82 |
return "<p>No Data Available</p>", None
|
| 83 |
|
|
|
|
|
|
|
| 84 |
def app():
|
| 85 |
+
with gr.Blocks() as interface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
gr.Markdown("<h1>π’ VENKATARAMANA MOTORS</h1>")
|
| 87 |
|
| 88 |
+
with gr.Tab("π₯ Stock Entry & Processing"):
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column():
|
| 91 |
+
gr.Markdown("<h3>π Upload & Process</h3>")
|
| 92 |
+
image_input = gr.Image(type="numpy", label="π Upload Image")
|
| 93 |
+
mode_dropdown = gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry")
|
| 94 |
+
entry_type_radio = gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales")
|
| 95 |
+
quantity_input = gr.Number(label="π’ Quantity", value=1, interactive=True)
|
| 96 |
+
|
| 97 |
+
with gr.Column():
|
| 98 |
+
gr.Markdown("<h3>π Processed Data</h3>")
|
| 99 |
+
image_view = gr.Text(label="π Image Data Viewer", interactive=False)
|
| 100 |
+
result_output = gr.Text(label="π Processed Result", interactive=False)
|
| 101 |
+
submit_button = gr.Button("π Process Image")
|
| 102 |
+
|
| 103 |
+
with gr.Tab("π Salesforce Data Overview"):
|
| 104 |
+
gr.Markdown("<h3>π Stock Table</h3>")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
salesforce_table = gr.HTML(label="π¦ Salesforce Data Table")
|
| 107 |
+
|
| 108 |
+
gr.Markdown("<h3>π Inventory Analytics</h3>")
|
| 109 |
+
with gr.Row():
|
| 110 |
+
salesforce_graph = gr.Image(type="pil", label="π Stock Distribution Bar Graph")
|
| 111 |
+
|
| 112 |
+
generate_button = gr.Button("β‘ Generate Data")
|
| 113 |
+
|
| 114 |
+
generate_button.click(fn=get_salesforce_visualization, inputs=[], outputs=[salesforce_table, salesforce_graph])
|
| 115 |
+
submit_button.click(fn=process_image, inputs=[image_input, mode_dropdown, entry_type_radio, quantity_input], outputs=[image_view, result_output])
|
| 116 |
+
|
| 117 |
+
return interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
+
app().launch(share=True)
|
|
|
|
|
|
|
|
|