Skip to content

submission-FINCOP #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,53 @@

## README.md must consist of the following information:

#### Team Name -
#### Problem Statement -
#### Team Leader Email -
#### Team Name - FINCOP
#### Problem Statement - Real-Time High Scale Financial Fraud Risk Management
#### Team Leader Email - [email protected]

## A Brief of the Prototype:
This section must include UML Diagrams and prototype description
![use case](use_case_sbi.png)

## Tech Stack:
List Down all technologies used to Build the prototype
Following technologies were used to Build the prototype:
1. Langchain\
2. OpenAI API\
3. Gradio\
4. Pytesseract

## Step-by-Step Code Execution Instructions:
This Section must contain a set of instructions required to clone and run the prototype so that it can be tested and deeply analyzed

Step 1: Clone the repository using the following command:\
```
git clone https://github.com/hindesh-akash/Pitch-to-SBI-Hackathon.git
```

Step 2: Install the requirements:\
```
pip install -r requirements.txt
```

Step 3: **IMPORTANT**\
-> Setup your OpenAI key in the **openai.api_key** variable in "loan_fraud.py" and "upi_fraud_check.py"

Step 4: Open a new terminal

To check for UPI fraud, run the following code:

```
python upi_fraud_check.py
```
Then in the terminal click on the link generated for local host. Example- Running on local URL: http://127.0.0.1:7861

To check for Loan Portfolio fraud, run the following code:

```
python loan_fraud.py
```

Then in the terminal click on the link generated for local host. Example- Running on local URL: http://127.0.0.1:7861

**Explore the app!**

## What I Learned:
Write about the biggest learning you had while developing the prototype
While developing the prototype I learned that if we do not pay attention to our transactions regarding whom we are paying and by what means, we can easily fall prey to financial fraud. I learned how LLMs can be used to leverage fraud detection and how an OCR model can reduce the human task of detecting whether a loan applicant is a defaulter. Similarily I learned the usage of robust machine learning models to detect credit defaults.
11 changes: 11 additions & 0 deletions examples.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
loan application:

The loan application seeks funding for the construction of a residential property on a 5000 square feet plot of land . The funds will cover materials, labor, permits, and other essential expenses, resulting\
in a valuable and comfortable residence in a prime location.

Details:
Applicant name: John Doe
House construction site: New Colony, 100001
Total cost: 5,000,000


Binary file added gross_salary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added itr_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions loan_fraud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import gradio as gr
import pytesseract
import openai
import datetime

# Set your OpenAI API key
openai.api_key = "----ENTER YOUR OPENAI API KEY HERE----"

# Define the model
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]

def analyze_sms_fraud(input_image,loan_application):

salary_details = pytesseract.image_to_string(input_image)

prompt_financial_summary = f"""
I have extracted the information in triple backticks from an Income Tax Return (ITR) form image.\
Suppose you are an expert in Income Tax Return (ITR) audit.\
Perform the following tasks:\

1. Calculate the Gross Salaryfrom given data where the gross salary is the sum of all types of incomes\
whether it is taxable or not or it is Retirement benefit or not.\

2. Make a small financial summary of the applicant based on whatever you have calculated.\

The data is as follows: ```{salary_details}```
"""

financial_summary_applicant = get_completion(prompt_financial_summary)

fraud_detection_prompt = f"""
Suppose you are a loan processor at a bank.\
Based on the financial summary given in triple backticks and\
loan application details in triple askerisks, detect whether the context of financial summary matches the fact that\
the applicant will be able to repay the loan or not. Also note there is no pinode like 100001.\
If the applicant is unable to repay the loan, then possibly the applicant is fraud.\
If the applicant is a fraud, then output the reason for the same.\
And if the applicant is not a fraud, then output the reason for the same that how he/she can possibly repay the loan.\

Financial summary of applicant: ```{financial_summary_applicant}```

Loan application details: ***{loan_application}***
"""

fraud_detection_result = get_completion(fraud_detection_prompt)
return fraud_detection_result

# Define the interface
input_image = gr.inputs.Image(label="Input ITR Image")
loan_application = gr.inputs.Textbox(label="Loan Application Details")

iface = gr.Interface(
fn=analyze_sms_fraud,
inputs=[input_image, loan_application],
outputs=gr.outputs.Textbox(label="Fraud Detection Result"),
title="Loan Fraud Detection",
description="Analyze an ITR form image and loan application details for potential fraud using LangChain and OpenAI's GPT-3."
)

iface.launch()
Binary file added other_salary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added requirements.txt
Binary file not shown.
Binary file added sms_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions upi_fraud_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import gradio as gr
import pytesseract
import openai
import datetime

# Set your OpenAI API key
openai.api_key = "----ENTER YOUR OPENAI API KEY HERE----"

# Define the model
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]

def analyze_sms_fraud(input_text_or_image):

sms_text = pytesseract.image_to_string(input_text_or_image)

# Generate prompt for LangChain
fraud_sms_prompt = f"""
A user received the SMS message in triple backticks from someone claiming to be their bank,
Verify what information is being asked from the user. You can do it in following steps:
Step 1: Read the SMS message and identify if there is any the bank name.
Step 2: Identify is there is request for any personal information/ request for KYC etc.
Step 3: Identify if there is any request for OTP or PIN.
Step 4: Identify if there is any link in the SMS message and the link is secure or not.
Step 5: Identify if there is any request for money transfer.
Based on the above steps, identify if the SMS message is a fraud or not.
SMS message: ```{sms_text}```
"""

# Get fraud detection result from LangChain
detected_fraud = get_completion(fraud_sms_prompt)

return sms_text, detected_fraud


input_image = gr.inputs.Image(label="Input SMS Image")


iface = gr.Interface(
fn=analyze_sms_fraud,
inputs= input_image,
outputs=[
gr.outputs.Textbox(label="SMS Text"),
gr.outputs.Textbox(label="Detected Fraud Result")
],
title="SMS Fraud Detection",
description="Analyze an SMS message for potential fraud using LangChain and OpenAI's GPT-3."
)

iface.launch()
Binary file added use_case_sbi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.