diff --git a/README.md b/README.md index c90dfb8..d4ad9e1 100644 --- a/README.md +++ b/README.md @@ -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 - hindeshnit19@gmail.com ## 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. diff --git a/examples.txt b/examples.txt new file mode 100644 index 0000000..3c76180 --- /dev/null +++ b/examples.txt @@ -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 + + diff --git a/gross_salary.png b/gross_salary.png new file mode 100644 index 0000000..6ee0ef7 Binary files /dev/null and b/gross_salary.png differ diff --git a/itr_image.png b/itr_image.png new file mode 100644 index 0000000..1f2d8c8 Binary files /dev/null and b/itr_image.png differ diff --git a/loan_fraud.py b/loan_fraud.py new file mode 100644 index 0000000..35c1dc5 --- /dev/null +++ b/loan_fraud.py @@ -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() \ No newline at end of file diff --git a/other_salary.png b/other_salary.png new file mode 100644 index 0000000..5734ca4 Binary files /dev/null and b/other_salary.png differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7ccbc42 Binary files /dev/null and b/requirements.txt differ diff --git a/sms_example.png b/sms_example.png new file mode 100644 index 0000000..33c3d19 Binary files /dev/null and b/sms_example.png differ diff --git a/upi_fraud_check.py b/upi_fraud_check.py new file mode 100644 index 0000000..76b0485 --- /dev/null +++ b/upi_fraud_check.py @@ -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() diff --git a/use_case_sbi.png b/use_case_sbi.png new file mode 100644 index 0000000..c1b857d Binary files /dev/null and b/use_case_sbi.png differ