Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,9 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
os.environ["CUDA_VISIBLE_DEVICES"] = "" # Disable GPU usage
|
| 3 |
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" # Disable oneDNN optimizations
|
| 4 |
|
|
@@ -17,15 +22,28 @@ from datetime import datetime
|
|
| 17 |
import re
|
| 18 |
import gradio as gr
|
| 19 |
|
| 20 |
-
from simple_salesforce import Salesforce
|
| 21 |
|
| 22 |
-
# Salesforce
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Initialize Hugging Face NER pipeline (force CPU)
|
| 31 |
ner_pipeline = pipeline("ner", model="dslim/bert-base-NER", tokenizer="dslim/bert-base-NER", device=-1)
|
|
@@ -146,21 +164,25 @@ def process_invoice(pdf_file):
|
|
| 146 |
with open(output_file, "w") as f:
|
| 147 |
json.dump([output], f, indent=2)
|
| 148 |
|
| 149 |
-
# Create record in Salesforce
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
return json.dumps(output, indent=2)
|
| 166 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
|
| 4 |
+
# Load environment variables from .env file
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
os.environ["CUDA_VISIBLE_DEVICES"] = "" # Disable GPU usage
|
| 8 |
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" # Disable oneDNN optimizations
|
| 9 |
|
|
|
|
| 22 |
import re
|
| 23 |
import gradio as gr
|
| 24 |
|
| 25 |
+
from simple_salesforce import Salesforce, SalesforceAuthenticationFailed
|
| 26 |
|
| 27 |
+
# Read Salesforce credentials from environment variables
|
| 28 |
+
SF_USERNAME = os.getenv("SF_USERNAME")
|
| 29 |
+
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
| 30 |
+
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 31 |
+
SF_DOMAIN = os.getenv("SF_DOMAIN") or "login" # default to 'login' for production
|
| 32 |
+
|
| 33 |
+
print(f"Salesforce login info: username={SF_USERNAME}, domain={SF_DOMAIN}")
|
| 34 |
+
|
| 35 |
+
# Salesforce connection with error handling
|
| 36 |
+
try:
|
| 37 |
+
sf = Salesforce(
|
| 38 |
+
username=SF_USERNAME,
|
| 39 |
+
password=SF_PASSWORD,
|
| 40 |
+
security_token=SF_SECURITY_TOKEN,
|
| 41 |
+
domain=SF_DOMAIN
|
| 42 |
+
)
|
| 43 |
+
print("Salesforce login successful.")
|
| 44 |
+
except SalesforceAuthenticationFailed as e:
|
| 45 |
+
print(f"Salesforce authentication failed: {e}")
|
| 46 |
+
sf = None # To avoid crashing later
|
| 47 |
|
| 48 |
# Initialize Hugging Face NER pipeline (force CPU)
|
| 49 |
ner_pipeline = pipeline("ner", model="dslim/bert-base-NER", tokenizer="dslim/bert-base-NER", device=-1)
|
|
|
|
| 164 |
with open(output_file, "w") as f:
|
| 165 |
json.dump([output], f, indent=2)
|
| 166 |
|
| 167 |
+
# Create record in Salesforce if logged in successfully
|
| 168 |
+
if sf is not None:
|
| 169 |
+
try:
|
| 170 |
+
sf.Invoice_Record__c.create({
|
| 171 |
+
"Vendor_Name__c": vendor_name,
|
| 172 |
+
"Invoice_Amount__c": amount,
|
| 173 |
+
"Invoice_Date__c": str(invoice_date),
|
| 174 |
+
"Items_Listed__c": items_listed,
|
| 175 |
+
"Fraud_Score__c": fraud_score,
|
| 176 |
+
"Fraud_Reasoning__c": fraud_reasoning,
|
| 177 |
+
"Flagged__c": fraud_score > 50,
|
| 178 |
+
"Reviewed_By__c": None,
|
| 179 |
+
"Status__c": "Flagged" if fraud_score > 50 else "Cleared"
|
| 180 |
+
})
|
| 181 |
+
print("Salesforce record created successfully.")
|
| 182 |
+
except Exception as e:
|
| 183 |
+
print(f"Salesforce record creation failed: {e}")
|
| 184 |
+
else:
|
| 185 |
+
print("Salesforce connection not available, skipping record creation.")
|
| 186 |
|
| 187 |
return json.dumps(output, indent=2)
|
| 188 |
|