Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import pandas as pd
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from io import BytesIO
|
| 11 |
from fuzzywuzzy import process
|
|
|
|
| 12 |
|
| 13 |
# Attribute mappings: readable names to Salesforce API names
|
| 14 |
ATTRIBUTE_MAPPING = {
|
|
@@ -79,7 +80,7 @@ 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
|
| 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",
|
|
@@ -161,25 +162,21 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 161 |
object_name = None
|
| 162 |
field_name = None
|
| 163 |
product_field_name = "Product_Name__c" # Correct field for product name in the object
|
| 164 |
-
model_field_name = "
|
| 165 |
|
| 166 |
if mode == "Entry":
|
| 167 |
if entry_type == "Sales":
|
| 168 |
-
object_name = "
|
| 169 |
-
field_name = "
|
| 170 |
elif entry_type == "Non-Sales":
|
| 171 |
-
object_name = "
|
| 172 |
-
field_name = "
|
| 173 |
elif mode == "Exit":
|
| 174 |
if entry_type == "Sales":
|
| 175 |
-
object_name = "
|
| 176 |
-
|
| 177 |
-
model_field_name = "Modal_Name__c"
|
| 178 |
-
field_name = "Quantity_Sold__c"
|
| 179 |
elif entry_type == "Non-Sales":
|
| 180 |
-
object_name = "
|
| 181 |
-
product_field_name = "Product_Name__c"
|
| 182 |
-
model_field_name = "Model_Name__c"
|
| 183 |
field_name = "Sold_Out__c"
|
| 184 |
|
| 185 |
if not object_name or not field_name:
|
|
@@ -200,60 +197,56 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 200 |
attributes["Product name"] = product_name
|
| 201 |
|
| 202 |
if mode == "Exit":
|
| 203 |
-
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"
|
| 204 |
response = sf.query(query)
|
| 205 |
|
| 206 |
if response["records"]:
|
| 207 |
record_id = response["records"][0]["Id"]
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
| 211 |
else:
|
| 212 |
return f"No matching record found for product '{product_name}' in {object_name}."
|
| 213 |
else:
|
| 214 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 215 |
filtered_attributes[field_name] = quantity
|
| 216 |
sf_object.create(filtered_attributes)
|
| 217 |
-
return f"Data successfully exported to Salesforce object {object_name}."
|
| 218 |
|
| 219 |
except Exception as e:
|
| 220 |
-
return f"Error interacting with Salesforce: {str(e)}"
|
| 221 |
|
| 222 |
-
# Function to pull data from Salesforce
|
| 223 |
-
def
|
| 224 |
try:
|
| 225 |
sf = Salesforce(
|
| 226 |
username=SALESFORCE_USERNAME,
|
| 227 |
password=SALESFORCE_PASSWORD,
|
| 228 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 229 |
)
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
return None
|
| 253 |
-
|
| 254 |
-
# Function to generate a bar graph from Salesforce data
|
| 255 |
-
def generate_bar_graph(df):
|
| 256 |
-
try:
|
| 257 |
fig, ax = plt.subplots(figsize=(12, 8))
|
| 258 |
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
|
| 259 |
ax.set_title("Stock Distribution by Product Name")
|
|
@@ -265,9 +258,10 @@ def generate_bar_graph(df):
|
|
| 265 |
plt.savefig(buffer, format="png")
|
| 266 |
buffer.seek(0)
|
| 267 |
img = Image.open(buffer)
|
| 268 |
-
|
|
|
|
| 269 |
except Exception as e:
|
| 270 |
-
return None
|
| 271 |
|
| 272 |
# Unified function to handle image processing and Salesforce interaction
|
| 273 |
def process_image(image, mode, entry_type, quantity):
|
|
@@ -288,43 +282,35 @@ def process_image(image, mode, entry_type, quantity):
|
|
| 288 |
|
| 289 |
# Gradio Interface
|
| 290 |
def app():
|
| 291 |
-
df = format_salesforce_data()
|
| 292 |
-
table_component = None
|
| 293 |
-
bar_graph_component = None
|
| 294 |
-
|
| 295 |
-
if df is not None:
|
| 296 |
-
table_component = df.to_html(index=False)
|
| 297 |
-
bar_graph_image = generate_bar_graph(df)
|
| 298 |
-
if bar_graph_image:
|
| 299 |
-
bar_graph_component = bar_graph_image
|
| 300 |
-
|
| 301 |
return gr.TabbedInterface([
|
| 302 |
gr.Interface(
|
| 303 |
fn=process_image,
|
| 304 |
inputs=[
|
| 305 |
-
gr.Image(type="numpy", label="Upload Image"),
|
| 306 |
-
gr.Dropdown(label="Mode", choices=["Entry", "Exit"], value="Entry"),
|
| 307 |
-
gr.Radio(label="Entry Type", choices=["Sales", "Non-Sales"], value="Sales"),
|
| 308 |
-
gr.Number(label="Quantity", value=1, interactive=True),
|
| 309 |
],
|
| 310 |
outputs=[
|
| 311 |
-
gr.Text(label="Image Data
|
| 312 |
-
gr.Text(label="Result")
|
| 313 |
],
|
| 314 |
-
title="
|
| 315 |
-
description="
|
| 316 |
),
|
| 317 |
gr.Interface(
|
| 318 |
-
fn=
|
| 319 |
inputs=[],
|
| 320 |
outputs=[
|
| 321 |
-
gr.
|
| 322 |
-
gr.
|
|
|
|
|
|
|
| 323 |
],
|
| 324 |
-
title="Salesforce Data",
|
| 325 |
-
description="View
|
| 326 |
)
|
| 327 |
-
], ["Processing", "Salesforce Data"])
|
| 328 |
|
| 329 |
if __name__ == "__main__":
|
| 330 |
-
app().launch(share=True)
|
|
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from io import BytesIO
|
| 11 |
from fuzzywuzzy import process
|
| 12 |
+
import kaleido # Ensure kaleido is imported
|
| 13 |
|
| 14 |
# Attribute mappings: readable names to Salesforce API names
|
| 15 |
ATTRIBUTE_MAPPING = {
|
|
|
|
| 80 |
"Centrifugal mono block pump", "SINGLE PHASE MOTOR STARTER", "EasyPact EZC 100",
|
| 81 |
"Openwell Submersible Pumpset", "Electric Motor", "Self Priming Pump",
|
| 82 |
"Control panel for single phase submersible pumps", "MOTOR", "Submersible pump set",
|
| 83 |
+
"Fusion submersible pump set", "DCT", "Shock proof water proof", "CG COMMERCIAL MOTORS", "Fusion",
|
| 84 |
"control panel for single phase submerisible pumps",
|
| 85 |
"single phase digital starter dry run and timer panel", "5HP AV1 XL Kirloskar Pump",
|
| 86 |
"Phase stainless steel submersible pump", "Submersible pump", "WB15X",
|
|
|
|
| 162 |
object_name = None
|
| 163 |
field_name = None
|
| 164 |
product_field_name = "Product_Name__c" # Correct field for product name in the object
|
| 165 |
+
model_field_name = "Model_Name__c" # Correct field for model name in the object
|
| 166 |
|
| 167 |
if mode == "Entry":
|
| 168 |
if entry_type == "Sales":
|
| 169 |
+
object_name = "Inventory__c"
|
| 170 |
+
field_name = "Stock_Quantity__c"
|
| 171 |
elif entry_type == "Non-Sales":
|
| 172 |
+
object_name = "Unbilled_Inventory__c"
|
| 173 |
+
field_name = "Unbilled_Quantity__c"
|
| 174 |
elif mode == "Exit":
|
| 175 |
if entry_type == "Sales":
|
| 176 |
+
object_name = "Inventory__c"
|
| 177 |
+
field_name = "Sold_Quantity__c"
|
|
|
|
|
|
|
| 178 |
elif entry_type == "Non-Sales":
|
| 179 |
+
object_name = "Unbilled_Inventory__c"
|
|
|
|
|
|
|
| 180 |
field_name = "Sold_Out__c"
|
| 181 |
|
| 182 |
if not object_name or not field_name:
|
|
|
|
| 197 |
attributes["Product name"] = product_name
|
| 198 |
|
| 199 |
if mode == "Exit":
|
| 200 |
+
query = f"SELECT Id, {field_name}, Stock_Quantity__c FROM {object_name} WHERE {product_field_name} = '{product_name}' OR {model_field_name} = '{attributes.get('Model Name', '')}' LIMIT 1"
|
| 201 |
response = sf.query(query)
|
| 202 |
|
| 203 |
if response["records"]:
|
| 204 |
record_id = response["records"][0]["Id"]
|
| 205 |
+
current_stock = response["records"][0].get("Stock_Quantity__c", 0)
|
| 206 |
+
updated_quantity = current_stock - quantity # Deduct sold quantity
|
| 207 |
+
sf_object.update(record_id, {field_name: quantity, "Stock_Quantity__c": updated_quantity})
|
| 208 |
+
return f"Updated record for product '{product_name}' in {object_name}. New {field_name}: {quantity}, Updated Stock: {updated_quantity}."
|
| 209 |
else:
|
| 210 |
return f"No matching record found for product '{product_name}' in {object_name}."
|
| 211 |
else:
|
| 212 |
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 213 |
filtered_attributes[field_name] = quantity
|
| 214 |
sf_object.create(filtered_attributes)
|
| 215 |
+
return f"β
Data successfully exported to Salesforce object {object_name}."
|
| 216 |
|
| 217 |
except Exception as e:
|
| 218 |
+
return f"β Error interacting with Salesforce: {str(e)}"
|
| 219 |
|
| 220 |
+
# Function to pull structured data from Salesforce and display as a table
|
| 221 |
+
def pull_data_from_salesforce():
|
| 222 |
try:
|
| 223 |
sf = Salesforce(
|
| 224 |
username=SALESFORCE_USERNAME,
|
| 225 |
password=SALESFORCE_PASSWORD,
|
| 226 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 227 |
)
|
| 228 |
+
|
| 229 |
+
query = "SELECT Product_Name__c, Model_Name__c, Stock_Quantity__c FROM Inventory__c LIMIT 100"
|
| 230 |
+
response = sf.query_all(query)
|
| 231 |
+
|
| 232 |
+
records = response.get("records", [])
|
| 233 |
+
if not records:
|
| 234 |
+
return "No data found in Salesforce.", None, None, None
|
| 235 |
+
|
| 236 |
+
df = pd.DataFrame(records)
|
| 237 |
+
df = df.drop(columns=['attributes'], errors='ignore')
|
| 238 |
+
|
| 239 |
+
# Rename columns for better readability
|
| 240 |
+
df.rename(columns={
|
| 241 |
+
"Product_Name__c": "Product Name",
|
| 242 |
+
"Model_Name__c": "Model Name",
|
| 243 |
+
"Stock_Quantity__c": "Current Stocks"
|
| 244 |
+
}, inplace=True)
|
| 245 |
+
|
| 246 |
+
excel_path = "salesforce_data.xlsx"
|
| 247 |
+
df.to_excel(excel_path, index=False)
|
| 248 |
+
|
| 249 |
+
# Generate interactive vertical bar graph using Matplotlib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
fig, ax = plt.subplots(figsize=(12, 8))
|
| 251 |
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
|
| 252 |
ax.set_title("Stock Distribution by Product Name")
|
|
|
|
| 258 |
plt.savefig(buffer, format="png")
|
| 259 |
buffer.seek(0)
|
| 260 |
img = Image.open(buffer)
|
| 261 |
+
|
| 262 |
+
return "Data successfully retrieved.", df, excel_path, img
|
| 263 |
except Exception as e:
|
| 264 |
+
return f"Error fetching data: {str(e)}", None, None, None
|
| 265 |
|
| 266 |
# Unified function to handle image processing and Salesforce interaction
|
| 267 |
def process_image(image, mode, entry_type, quantity):
|
|
|
|
| 282 |
|
| 283 |
# Gradio Interface
|
| 284 |
def app():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
return gr.TabbedInterface([
|
| 286 |
gr.Interface(
|
| 287 |
fn=process_image,
|
| 288 |
inputs=[
|
| 289 |
+
gr.Image(type="numpy", label="π Upload Image"),
|
| 290 |
+
gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry"),
|
| 291 |
+
gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales"),
|
| 292 |
+
gr.Number(label="π’ Quantity", value=1, interactive=True),
|
| 293 |
],
|
| 294 |
outputs=[
|
| 295 |
+
gr.Text(label="π Extracted Image Data"),
|
| 296 |
+
gr.Text(label="π Result")
|
| 297 |
],
|
| 298 |
+
title="π’ Inventory Management",
|
| 299 |
+
description="π¦ Inventory Management System"
|
| 300 |
),
|
| 301 |
gr.Interface(
|
| 302 |
+
fn=pull_data_from_salesforce,
|
| 303 |
inputs=[],
|
| 304 |
outputs=[
|
| 305 |
+
gr.Text(label="Status"),
|
| 306 |
+
gr.Dataframe(label="π¦ Salesforce Data Table"),
|
| 307 |
+
gr.File(label="Download Salesforce Data"),
|
| 308 |
+
gr.Image(label="π Stock Distribution Bar Graph")
|
| 309 |
],
|
| 310 |
+
title="π Salesforce Data Export",
|
| 311 |
+
description="View, visualize (zoom-in/out), and download Salesforce data (Product Name, Model Name, Current Stocks)."
|
| 312 |
)
|
| 313 |
+
], ["π₯ OCR Processing", "π Salesforce Data Export"])
|
| 314 |
|
| 315 |
if __name__ == "__main__":
|
| 316 |
+
app().launch(share=True)
|