Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from paddleocr import PaddleOCR
|
| 3 |
from PIL import Image
|
|
@@ -145,13 +148,11 @@ def extract_attributes(extracted_text):
|
|
| 145 |
|
| 146 |
return attributes
|
| 147 |
|
| 148 |
-
# Function to filter attributes for valid Salesforce fields
|
| 149 |
def filter_valid_attributes(attributes, valid_fields):
|
| 150 |
return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
|
| 151 |
|
| 152 |
-
|
| 153 |
-
#π Function to interact with Salesforce based on mode and type
|
| 154 |
-
|
| 155 |
def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
| 156 |
try:
|
| 157 |
sf = Salesforce(
|
|
@@ -164,6 +165,7 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 164 |
object_name = None
|
| 165 |
field_name = None
|
| 166 |
product_field_name = "Product_Name__c"
|
|
|
|
| 167 |
|
| 168 |
if mode == "Entry":
|
| 169 |
if entry_type == "Sales":
|
|
@@ -183,85 +185,94 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 183 |
if not object_name or not field_name:
|
| 184 |
return "Invalid mode or entry type."
|
| 185 |
|
| 186 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
product_name = match_product_name(extracted_text)
|
|
|
|
|
|
|
|
|
|
| 188 |
if not product_name:
|
| 189 |
return "Product name could not be matched from the extracted text."
|
| 190 |
|
|
|
|
|
|
|
| 191 |
# Handling "Exit" Mode (Updating Records)
|
| 192 |
if mode == "Exit":
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
response = sf.query(query)
|
| 195 |
|
| 196 |
if response["records"]:
|
| 197 |
record_id = response["records"][0]["Id"]
|
| 198 |
-
updated_quantity = quantity
|
| 199 |
-
|
| 200 |
-
return f"β
Updated record for product '{product_name}' in {object_name}. New {field_name}: {updated_quantity}."
|
| 201 |
else:
|
| 202 |
-
return f"β No matching record found for product '{product_name}' in {object_name}."
|
| 203 |
|
| 204 |
# Handling "Entry" Mode (Creating Records)
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
}
|
| 210 |
-
# Export data to Salesforce
|
| 211 |
-
sf.__getattr__(object_name).create(attributes)
|
| 212 |
return f"β
Data successfully exported to Salesforce object {object_name}."
|
| 213 |
-
|
| 214 |
-
except Exception as e:
|
| 215 |
-
return f"β Error interacting with Salesforce: {str(e)}"
|
| 216 |
|
| 217 |
except Exception as e:
|
| 218 |
-
return f"β Error
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
sf = Salesforce(
|
| 224 |
username=SALESFORCE_USERNAME,
|
| 225 |
password=SALESFORCE_PASSWORD,
|
| 226 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 227 |
)
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
|
| 239 |
# Rename columns for better readability
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
# Unified function to handle image processing and Salesforce interaction
|
| 267 |
def process_image(image, mode, entry_type, quantity):
|
|
|
|
| 1 |
+
HUGGING FACE ALL FUNCTIONALITIES WORKING
|
| 2 |
+
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
from paddleocr import PaddleOCR
|
| 6 |
from PIL import Image
|
|
|
|
| 148 |
|
| 149 |
return attributes
|
| 150 |
|
| 151 |
+
# Function to filter attributes for valid Salesforce fields
|
| 152 |
def filter_valid_attributes(attributes, valid_fields):
|
| 153 |
return {ATTRIBUTE_MAPPING[key]: value for key, value in attributes.items() if ATTRIBUTE_MAPPING[key] in valid_fields}
|
| 154 |
|
| 155 |
+
#π Function to interact with Salesforce based on mode and type
|
|
|
|
|
|
|
| 156 |
def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
| 157 |
try:
|
| 158 |
sf = Salesforce(
|
|
|
|
| 165 |
object_name = None
|
| 166 |
field_name = None
|
| 167 |
product_field_name = "Product_Name__c"
|
| 168 |
+
model_field_name = "Modal_Name__c" # Correct field for model name
|
| 169 |
|
| 170 |
if mode == "Entry":
|
| 171 |
if entry_type == "Sales":
|
|
|
|
| 185 |
if not object_name or not field_name:
|
| 186 |
return "Invalid mode or entry type."
|
| 187 |
|
| 188 |
+
# Get valid fields for the specified Salesforce object
|
| 189 |
+
sf_object = sf.__getattr__(object_name)
|
| 190 |
+
schema = sf_object.describe()
|
| 191 |
+
valid_fields = {field["name"] for field in schema["fields"]}
|
| 192 |
+
|
| 193 |
+
# Extract product name and attributes
|
| 194 |
product_name = match_product_name(extracted_text)
|
| 195 |
+
attributes = extract_attributes(extracted_text)
|
| 196 |
+
model_name = attributes.get("Model Name", "").strip()
|
| 197 |
+
|
| 198 |
if not product_name:
|
| 199 |
return "Product name could not be matched from the extracted text."
|
| 200 |
|
| 201 |
+
attributes["Product name"] = product_name
|
| 202 |
+
|
| 203 |
# Handling "Exit" Mode (Updating Records)
|
| 204 |
if mode == "Exit":
|
| 205 |
+
# Query should only match exact product name or exact model name
|
| 206 |
+
query_conditions = []
|
| 207 |
+
if model_name:
|
| 208 |
+
query_conditions.append(f"{model_field_name} = '{model_name}'")
|
| 209 |
+
query_conditions.append(f"{product_field_name} = '{product_name}'")
|
| 210 |
+
|
| 211 |
+
query = f"SELECT Id, {field_name} FROM {object_name} WHERE {' OR '.join(query_conditions)} LIMIT 1"
|
| 212 |
response = sf.query(query)
|
| 213 |
|
| 214 |
if response["records"]:
|
| 215 |
record_id = response["records"][0]["Id"]
|
| 216 |
+
updated_quantity = quantity # Overwrite the quantity
|
| 217 |
+
sf_object.update(record_id, {field_name: updated_quantity})
|
| 218 |
+
return f"β
Updated record for product '{product_name}' ({model_name}) in {object_name}. New {field_name}: {updated_quantity}."
|
| 219 |
else:
|
| 220 |
+
return f"β No matching record found for product '{product_name}' ({model_name}) in {object_name}."
|
| 221 |
|
| 222 |
# Handling "Entry" Mode (Creating Records)
|
| 223 |
+
else:
|
| 224 |
+
filtered_attributes = filter_valid_attributes(attributes, valid_fields)
|
| 225 |
+
filtered_attributes[field_name] = quantity
|
| 226 |
+
sf_object.create(filtered_attributes)
|
|
|
|
|
|
|
|
|
|
| 227 |
return f"β
Data successfully exported to Salesforce object {object_name}."
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
+
return f"β Error interacting with Salesforce: {str(e)}"
|
| 231 |
+
# Function to pull structured data from Salesforce and display as a table
|
| 232 |
+
def pull_data_from_salesforce():
|
| 233 |
+
try:
|
| 234 |
+
sf = Salesforce(
|
|
|
|
| 235 |
username=SALESFORCE_USERNAME,
|
| 236 |
password=SALESFORCE_PASSWORD,
|
| 237 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 238 |
)
|
| 239 |
+
|
| 240 |
+
query = "SELECT Product_Name__c, Modal_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
|
| 241 |
+
response = sf.query_all(query)
|
| 242 |
+
|
| 243 |
+
records = response.get("records", [])
|
| 244 |
+
if not records:
|
| 245 |
+
return "No data found in Salesforce.", None, None, None
|
| 246 |
+
|
| 247 |
+
df = pd.DataFrame(records)
|
| 248 |
+
df = df.drop(columns=['attributes'], errors='ignore')
|
| 249 |
|
| 250 |
# Rename columns for better readability
|
| 251 |
+
df.rename(columns={
|
| 252 |
+
"Product_Name__c": "Product Name",
|
| 253 |
+
"Modal_Name__c": "Model Name",
|
| 254 |
+
"Current_Stocks__c": "Current Stocks"
|
| 255 |
+
}, inplace=True)
|
| 256 |
+
|
| 257 |
+
excel_path = "salesforce_data.xlsx"
|
| 258 |
+
df.to_excel(excel_path, index=False)
|
| 259 |
+
|
| 260 |
+
# Generate interactive vertical bar graph using Matplotlib
|
| 261 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
| 262 |
+
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
|
| 263 |
+
ax.set_title("Stock Distribution by Product Name")
|
| 264 |
+
ax.set_xlabel("Product Name")
|
| 265 |
+
ax.set_ylabel("Current Stocks")
|
| 266 |
+
plt.xticks(rotation=45, ha="right", fontsize=10)
|
| 267 |
+
plt.tight_layout()
|
| 268 |
+
buffer = BytesIO()
|
| 269 |
+
plt.savefig(buffer, format="png")
|
| 270 |
+
buffer.seek(0)
|
| 271 |
+
img = Image.open(buffer)
|
| 272 |
+
|
| 273 |
+
return "Data successfully retrieved.", df, excel_path, img
|
| 274 |
+
except Exception as e:
|
| 275 |
+
return f"Error fetching data: {str(e)}", None, None, None
|
| 276 |
|
| 277 |
# Unified function to handle image processing and Salesforce interaction
|
| 278 |
def process_image(image, mode, entry_type, quantity):
|