Abhisesh7 commited on
Commit
233cbab
·
verified ·
1 Parent(s): a22977d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -14
app.py CHANGED
@@ -93,8 +93,8 @@ def detect_anomalies(df):
93
  df["is_anomaly"] = model.fit_predict(X_scaled)
94
  return df
95
 
96
- def calculate_fraud_score(amount, is_anomaly, items_listed):
97
- """Calculate fraud score based on amount, anomaly, and items."""
98
  score = 0.0
99
  reasoning = []
100
 
@@ -109,9 +109,9 @@ def calculate_fraud_score(amount, is_anomaly, items_listed):
109
  score += 30
110
  reasoning.append("Invoice flagged as an anomaly.")
111
 
112
- if len(items_listed.split()) > 100:
113
  score += 10
114
- reasoning.append("Excessive number of items listed.")
115
 
116
  return min(score, 100), "; ".join(reasoning)
117
 
@@ -123,33 +123,31 @@ def process_invoice(pdf_file):
123
 
124
  vendor_name, amount = extract_entities(text)
125
  invoice_date = datetime.now().date()
126
- items_listed = text[:500]
127
 
128
  data = {
129
  "invoice_id": str(uuid.uuid4()),
130
  "vendor_name": vendor_name,
131
  "amount": amount,
132
  "invoice_date": invoice_date,
133
- "items_listed": items_listed
134
  }
135
  df = pd.DataFrame([data])
136
 
137
  df = detect_anomalies(df)
138
 
139
  fraud_score, fraud_reasoning = calculate_fraud_score(
140
- df["amount"].iloc[0], df["is_anomaly"].iloc[0], items_listed
141
  )
142
 
143
  output = {
144
  "Invoice_Record__c": {
145
- "Vendor_Name__c": vendor_name,
146
  "Invoice_Amount__c": amount,
147
  "Invoice_Date__c": str(invoice_date),
148
- "Items_Listed__c": items_listed,
149
  "Fraud_Score__c": fraud_score,
150
  "Fraud_Reasoning__c": fraud_reasoning,
151
  "Flagged__c": fraud_score > 50,
152
- "Reviewed_By__c": None,
153
  "Status__c": "Flagged" if fraud_score > 50 else "Cleared"
154
  },
155
  "Entities": {
@@ -168,14 +166,12 @@ def process_invoice(pdf_file):
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.")
@@ -203,4 +199,4 @@ iface = gr.Interface(
203
  )
204
 
205
  if __name__ == "__main__":
206
- iface.launch()
 
93
  df["is_anomaly"] = model.fit_predict(X_scaled)
94
  return df
95
 
96
+ def calculate_fraud_score(amount, is_anomaly, text_length):
97
+ """Calculate fraud score based on amount, anomaly, and text length."""
98
  score = 0.0
99
  reasoning = []
100
 
 
109
  score += 30
110
  reasoning.append("Invoice flagged as an anomaly.")
111
 
112
+ if text_length > 500:
113
  score += 10
114
+ reasoning.append("Excessive text length in invoice.")
115
 
116
  return min(score, 100), "; ".join(reasoning)
117
 
 
123
 
124
  vendor_name, amount = extract_entities(text)
125
  invoice_date = datetime.now().date()
126
+ text_length = len(text)
127
 
128
  data = {
129
  "invoice_id": str(uuid.uuid4()),
130
  "vendor_name": vendor_name,
131
  "amount": amount,
132
  "invoice_date": invoice_date,
133
+ "text_length": text_length
134
  }
135
  df = pd.DataFrame([data])
136
 
137
  df = detect_anomalies(df)
138
 
139
  fraud_score, fraud_reasoning = calculate_fraud_score(
140
+ df["amount"].iloc[0], df["is_anomaly"].iloc[0], text_length
141
  )
142
 
143
  output = {
144
  "Invoice_Record__c": {
145
+ "Name": vendor_name, # Map vendor_name to Name field
146
  "Invoice_Amount__c": amount,
147
  "Invoice_Date__c": str(invoice_date),
 
148
  "Fraud_Score__c": fraud_score,
149
  "Fraud_Reasoning__c": fraud_reasoning,
150
  "Flagged__c": fraud_score > 50,
 
151
  "Status__c": "Flagged" if fraud_score > 50 else "Cleared"
152
  },
153
  "Entities": {
 
166
  if sf is not None:
167
  try:
168
  sf.Invoice_Record__c.create({
169
+ "Name": vendor_name, # Map vendor_name to Name field
170
  "Invoice_Amount__c": amount,
171
  "Invoice_Date__c": str(invoice_date),
 
172
  "Fraud_Score__c": fraud_score,
173
  "Fraud_Reasoning__c": fraud_reasoning,
174
  "Flagged__c": fraud_score > 50,
 
175
  "Status__c": "Flagged" if fraud_score > 50 else "Cleared"
176
  })
177
  print("Salesforce record created successfully.")
 
199
  )
200
 
201
  if __name__ == "__main__":
202
+ iface.launch()