Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -218,33 +218,58 @@ def interact_with_salesforce(mode, entry_type, quantity, extracted_text):
|
|
| 218 |
|
| 219 |
except Exception as e:
|
| 220 |
return f"❌ Error interacting with Salesforce: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
|
|
|
| 223 |
try:
|
| 224 |
sf = Salesforce(
|
| 225 |
username=SALESFORCE_USERNAME,
|
| 226 |
password=SALESFORCE_PASSWORD,
|
| 227 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 228 |
)
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
except Exception as e:
|
| 232 |
-
return f"Error
|
| 233 |
-
|
| 234 |
-
|
|
|
|
| 235 |
try:
|
| 236 |
-
fig, ax = plt.subplots(figsize=(12,
|
| 237 |
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
|
| 238 |
-
ax.set_title("Stock Distribution
|
| 239 |
ax.set_xlabel("Product Name")
|
| 240 |
-
ax.set_ylabel("
|
| 241 |
plt.xticks(rotation=45, ha="right", fontsize=10)
|
| 242 |
plt.tight_layout()
|
| 243 |
buffer = BytesIO()
|
| 244 |
plt.savefig(buffer, format="png")
|
| 245 |
buffer.seek(0)
|
| 246 |
-
|
| 247 |
-
|
| 248 |
except Exception as e:
|
| 249 |
return None
|
| 250 |
|
|
|
|
| 218 |
|
| 219 |
except Exception as e:
|
| 220 |
return f"❌ Error interacting with Salesforce: {str(e)}"
|
| 221 |
+
import pandas as pd
|
| 222 |
+
import matplotlib.pyplot as plt
|
| 223 |
+
from io import BytesIO
|
| 224 |
+
from PIL import Image
|
| 225 |
+
from simple_salesforce import Salesforce
|
| 226 |
+
|
| 227 |
+
# 📌 Salesforce Credentials
|
| 228 |
+
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 229 |
+
SALESFORCE_PASSWORD = "Venkat12345@"
|
| 230 |
+
SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
|
| 231 |
|
| 232 |
+
# Function to Fetch and Display Salesforce Data
|
| 233 |
+
def fetch_salesforce_data():
|
| 234 |
try:
|
| 235 |
sf = Salesforce(
|
| 236 |
username=SALESFORCE_USERNAME,
|
| 237 |
password=SALESFORCE_PASSWORD,
|
| 238 |
security_token=SALESFORCE_SECURITY_TOKEN
|
| 239 |
)
|
| 240 |
+
|
| 241 |
+
query = "SELECT Product_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
|
| 242 |
+
response = sf.query(query)
|
| 243 |
+
records = response.get('records', [])
|
| 244 |
+
|
| 245 |
+
if not records:
|
| 246 |
+
return "<p>No Data Found</p>", None
|
| 247 |
+
|
| 248 |
+
# Convert to DataFrame
|
| 249 |
+
df = pd.DataFrame(records)
|
| 250 |
+
df.rename(columns={'Product_Name__c': 'Product Name', 'Current_Stocks__c': 'Current Stocks'}, inplace=True)
|
| 251 |
+
df.drop(columns=['attributes'], inplace=True, errors='ignore')
|
| 252 |
+
|
| 253 |
+
return df.to_html(index=False), generate_inventory_graph(df)
|
| 254 |
+
|
| 255 |
except Exception as e:
|
| 256 |
+
return f"Error fetching data: {str(e)}", None
|
| 257 |
+
|
| 258 |
+
# Function to Generate Inventory Graph
|
| 259 |
+
def generate_inventory_graph(df):
|
| 260 |
try:
|
| 261 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 262 |
df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
|
| 263 |
+
ax.set_title("Inventory Stock Distribution")
|
| 264 |
ax.set_xlabel("Product Name")
|
| 265 |
+
ax.set_ylabel("Stock Quantity")
|
| 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 |
+
return Image.open(buffer)
|
| 272 |
+
|
| 273 |
except Exception as e:
|
| 274 |
return None
|
| 275 |
|