Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,6 @@
|
|
| 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 |
-
|
| 10 |
import logging
|
| 11 |
-
logging.getLogger("transformers").setLevel(logging.ERROR)
|
| 12 |
-
|
| 13 |
import pdfplumber
|
| 14 |
import pandas as pd
|
| 15 |
import numpy as np
|
|
@@ -21,9 +12,18 @@ import uuid
|
|
| 21 |
from datetime import datetime
|
| 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")
|
|
@@ -33,7 +33,6 @@ print(f"Salesforce login info: username={SF_USERNAME}")
|
|
| 33 |
|
| 34 |
# Salesforce connection with error handling
|
| 35 |
try:
|
| 36 |
-
# Default domain is "login" (production); use domain="test" for sandbox/Developer Edition orgs
|
| 37 |
sf = Salesforce(
|
| 38 |
username=SF_USERNAME,
|
| 39 |
password=SF_PASSWORD,
|
|
@@ -42,7 +41,7 @@ try:
|
|
| 42 |
print("Salesforce login successful.")
|
| 43 |
except SalesforceAuthenticationFailed as e:
|
| 44 |
print(f"Salesforce authentication failed: {e}")
|
| 45 |
-
sf = None
|
| 46 |
|
| 47 |
# Initialize Hugging Face NER pipeline (force CPU)
|
| 48 |
ner_pipeline = pipeline("ner", model="dslim/bert-base-NER", tokenizer="dslim/bert-base-NER", device=-1)
|
|
@@ -141,11 +140,11 @@ def process_invoice(pdf_file):
|
|
| 141 |
|
| 142 |
output = {
|
| 143 |
"Invoice_Record__c": {
|
| 144 |
-
"Name": vendor_name,
|
| 145 |
"Invoice_Amount__c": amount,
|
| 146 |
"Invoice_Date__c": str(invoice_date),
|
| 147 |
"Fraud_Score__c": fraud_score,
|
| 148 |
-
"
|
| 149 |
"Flagged__c": fraud_score > 50,
|
| 150 |
"Status__c": "Flagged" if fraud_score > 50 else "Cleared"
|
| 151 |
},
|
|
@@ -158,18 +157,21 @@ def process_invoice(pdf_file):
|
|
| 158 |
|
| 159 |
# Save to JSON file
|
| 160 |
output_file = "fraud_detection_results.json"
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Create record in Salesforce if logged in successfully
|
| 165 |
if sf is not None:
|
| 166 |
try:
|
| 167 |
sf.Invoice_Record__c.create({
|
| 168 |
-
"Name": vendor_name,
|
| 169 |
"Invoice_Amount__c": amount,
|
| 170 |
"Invoice_Date__c": str(invoice_date),
|
| 171 |
"Fraud_Score__c": fraud_score,
|
| 172 |
-
"
|
| 173 |
"Flagged__c": fraud_score > 50,
|
| 174 |
"Status__c": "Flagged" if fraud_score > 50 else "Cleared"
|
| 175 |
})
|
|
@@ -198,4 +200,4 @@ iface = gr.Interface(
|
|
| 198 |
)
|
| 199 |
|
| 200 |
if __name__ == "__main__":
|
| 201 |
-
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import logging
|
|
|
|
|
|
|
| 4 |
import pdfplumber
|
| 5 |
import pandas as pd
|
| 6 |
import numpy as np
|
|
|
|
| 12 |
from datetime import datetime
|
| 13 |
import re
|
| 14 |
import gradio as gr
|
|
|
|
| 15 |
from simple_salesforce import Salesforce, SalesforceAuthenticationFailed
|
| 16 |
|
| 17 |
+
# Load environment variables from .env file
|
| 18 |
+
load_dotenv()
|
| 19 |
+
|
| 20 |
+
# Configure environment for CPU usage
|
| 21 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "" # Disable GPU usage
|
| 22 |
+
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" # Disable oneDNN optimizations
|
| 23 |
+
|
| 24 |
+
# Set up logging to suppress transformers warnings
|
| 25 |
+
logging.getLogger("transformers").setLevel(logging.ERROR)
|
| 26 |
+
|
| 27 |
# Read Salesforce credentials from environment variables
|
| 28 |
SF_USERNAME = os.getenv("SF_USERNAME")
|
| 29 |
SF_PASSWORD = os.getenv("SF_PASSWORD")
|
|
|
|
| 33 |
|
| 34 |
# Salesforce connection with error handling
|
| 35 |
try:
|
|
|
|
| 36 |
sf = Salesforce(
|
| 37 |
username=SF_USERNAME,
|
| 38 |
password=SF_PASSWORD,
|
|
|
|
| 41 |
print("Salesforce login successful.")
|
| 42 |
except SalesforceAuthenticationFailed as e:
|
| 43 |
print(f"Salesforce authentication failed: {e}")
|
| 44 |
+
sf = None
|
| 45 |
|
| 46 |
# Initialize Hugging Face NER pipeline (force CPU)
|
| 47 |
ner_pipeline = pipeline("ner", model="dslim/bert-base-NER", tokenizer="dslim/bert-base-NER", device=-1)
|
|
|
|
| 140 |
|
| 141 |
output = {
|
| 142 |
"Invoice_Record__c": {
|
| 143 |
+
"Name": vendor_name,
|
| 144 |
"Invoice_Amount__c": amount,
|
| 145 |
"Invoice_Date__c": str(invoice_date),
|
| 146 |
"Fraud_Score__c": fraud_score,
|
| 147 |
+
"FRAUD_REASON__c": fraud_reasoning, # Corrected field name
|
| 148 |
"Flagged__c": fraud_score > 50,
|
| 149 |
"Status__c": "Flagged" if fraud_score > 50 else "Cleared"
|
| 150 |
},
|
|
|
|
| 157 |
|
| 158 |
# Save to JSON file
|
| 159 |
output_file = "fraud_detection_results.json"
|
| 160 |
+
try:
|
| 161 |
+
with open(output_file, "w") as f:
|
| 162 |
+
json.dump([output], f, indent=2)
|
| 163 |
+
except Exception as e:
|
| 164 |
+
print(f"Error saving JSON file: {e}")
|
| 165 |
|
| 166 |
# Create record in Salesforce if logged in successfully
|
| 167 |
if sf is not None:
|
| 168 |
try:
|
| 169 |
sf.Invoice_Record__c.create({
|
| 170 |
+
"Name": vendor_name,
|
| 171 |
"Invoice_Amount__c": amount,
|
| 172 |
"Invoice_Date__c": str(invoice_date),
|
| 173 |
"Fraud_Score__c": fraud_score,
|
| 174 |
+
"FRAUD_REASON__c": fraud_reasoning, # Corrected field name
|
| 175 |
"Flagged__c": fraud_score > 50,
|
| 176 |
"Status__c": "Flagged" if fraud_score > 50 else "Cleared"
|
| 177 |
})
|
|
|
|
| 200 |
)
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
+
iface.launch(share=False) # share=False for local execution
|