Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,6 @@ import matplotlib.pyplot as plt
|
|
| 10 |
from io import BytesIO
|
| 11 |
from fuzzywuzzy import process
|
| 12 |
import kaleido # Ensure kaleido is imported
|
| 13 |
-
import schedule
|
| 14 |
-
import time
|
| 15 |
-
from twilio.rest import Client
|
| 16 |
|
| 17 |
# Attribute mappings: readable names to Salesforce API names
|
| 18 |
ATTRIBUTE_MAPPING = {
|
|
@@ -89,12 +86,6 @@ SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
|
| 89 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
| 90 |
SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
|
| 91 |
|
| 92 |
-
# Twilio credentials
|
| 93 |
-
TWILIO_ACCOUNT_SID = 'AC13c539258acfc3502a91666337ac53c7'
|
| 94 |
-
TWILIO_AUTH_TOKEN = '00b6ab917fabcdf9632b012528b36079'
|
| 95 |
-
TWILIO_PHONE_NUMBER = 'whatsapp:+19034027384'
|
| 96 |
-
WHATSAPP_NUMBER = 'whatsapp:+19034027384'
|
| 97 |
-
|
| 98 |
# Initialize PaddleOCR
|
| 99 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 100 |
|
|
@@ -290,63 +281,6 @@ def pull_data_from_salesforce(data_type):
|
|
| 290 |
except Exception as e:
|
| 291 |
return f"Error fetching data: {str(e)}", None, None, None
|
| 292 |
|
| 293 |
-
# Function to check stock levels and send alerts using Twilio
|
| 294 |
-
def check_stock_levels_and_send_alerts():
|
| 295 |
-
try:
|
| 296 |
-
sf = Salesforce(
|
| 297 |
-
username=SALESFORCE_USERNAME,
|
| 298 |
-
password=SALESFORCE_PASSWORD,
|
| 299 |
-
security_token=SALESFORCE_SECURITY_TOKEN
|
| 300 |
-
)
|
| 301 |
-
|
| 302 |
-
# Query Inventory_Management__c
|
| 303 |
-
inventory_query = "SELECT Productname__c, Current_Stocks__c FROM Inventory_Management__c WHERE Current_Stocks__c < 10"
|
| 304 |
-
inventory_records = sf.query_all(inventory_query).get("records", [])
|
| 305 |
-
|
| 306 |
-
# Query Un_Billable__c
|
| 307 |
-
unbillable_query = "SELECT Productname__c, Current_Stock__c FROM Un_Billable__c WHERE Current_Stock__c < 10"
|
| 308 |
-
unbillable_records = sf.query_all(unbillable_query).get("records", [])
|
| 309 |
-
|
| 310 |
-
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
|
| 311 |
-
|
| 312 |
-
for record in inventory_records:
|
| 313 |
-
product_name = record["Productname__c"]
|
| 314 |
-
current_stock = record["Current_Stocks__c"]
|
| 315 |
-
message_body = f"Urgent Stock Alert! The following products have stock levels below 10 and require immediate attention:\n\n" \
|
| 316 |
-
f"Product Name: {product_name}\n" \
|
| 317 |
-
f"Current Stock: {current_stock}\n" \
|
| 318 |
-
f"Please review and restock accordingly.\n\n" \
|
| 319 |
-
f"Thank you!"
|
| 320 |
-
|
| 321 |
-
client.messages.create(
|
| 322 |
-
body=message_body,
|
| 323 |
-
from_=TWILIO_PHONE_NUMBER,
|
| 324 |
-
to=WHATSAPP_NUMBER
|
| 325 |
-
)
|
| 326 |
-
|
| 327 |
-
for record in unbillable_records:
|
| 328 |
-
product_name = record["Productname__c"]
|
| 329 |
-
current_stock = record["Current_Stock__c"]
|
| 330 |
-
message_body = f"Urgent Stock Alert! The following products have stock levels below 10 and require immediate attention:\n\n" \
|
| 331 |
-
f"Product Name: {product_name}\n" \
|
| 332 |
-
f"Current Stock: {current_stock}\n" \
|
| 333 |
-
f"Please review and restock accordingly.\n\n" \
|
| 334 |
-
f"Thank you!"
|
| 335 |
-
|
| 336 |
-
client.messages.create(
|
| 337 |
-
body=message_body,
|
| 338 |
-
from_=TWILIO_PHONE_NUMBER,
|
| 339 |
-
to=WHATSAPP_NUMBER
|
| 340 |
-
)
|
| 341 |
-
|
| 342 |
-
print("Stock alerts sent successfully.")
|
| 343 |
-
except Exception as e:
|
| 344 |
-
print(f"Error checking stock levels and sending alerts: {str(e)}")
|
| 345 |
-
|
| 346 |
-
# Schedule the stock level check every 5 hours
|
| 347 |
-
schedule.every(5).minutes.do(check_stock_levels_and_send_alerts)
|
| 348 |
-
|
| 349 |
-
|
| 350 |
# Gradio Interface
|
| 351 |
def app():
|
| 352 |
with gr.Blocks() as demo:
|
|
@@ -389,10 +323,4 @@ def app():
|
|
| 389 |
return demo
|
| 390 |
|
| 391 |
if __name__ == "__main__":
|
| 392 |
-
|
| 393 |
-
app().launch(share=True)
|
| 394 |
-
|
| 395 |
-
# Run the scheduled tasks
|
| 396 |
-
while True:
|
| 397 |
-
schedule.run_pending()
|
| 398 |
-
time.sleep(1)
|
|
|
|
| 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 = {
|
|
|
|
| 86 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
| 87 |
SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# Initialize PaddleOCR
|
| 90 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 91 |
|
|
|
|
| 281 |
except Exception as e:
|
| 282 |
return f"Error fetching data: {str(e)}", None, None, None
|
| 283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
# Gradio Interface
|
| 285 |
def app():
|
| 286 |
with gr.Blocks() as demo:
|
|
|
|
| 323 |
return demo
|
| 324 |
|
| 325 |
if __name__ == "__main__":
|
| 326 |
+
app().launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|