salesbot / app.py
abhishekDS's picture
Update app.py
c7598c7
import os
import openai
import gradio as gr
import PyPDF2
import json
from kor import Text, Number
from kor.nodes import Object, Text, Number
from kor.extraction import create_extraction_chain
from langchain.chat_models import ChatOpenAI
from langchain.callbacks import get_openai_callback
# Add a function to save conversation to a JSON file
# def save_conversation_to_json():
# with open("conversation_history.json", "w") as json_file:
# json.dump(chat_history, json_file, indent=4)
chat_history = []
# default max tokens = 1000
def get_completion(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # the degree of randomness of the model's output
)
# Collect usage information for this response
return response.choices[0].message["content"]
def conversation_interface(api_key,prompt):
openai.api_key = api_key
global chat_history
if not chat_history:
# Initialize conversation with system message
system_message = {'role':'system', 'content':"""
You are Davis, You act as Mortgage Loan Sales Assistant , Your job role is to collect information from customer step by step, solve there queries in between and finally provide them with the calculated mortgae loan amount they are eligible for. \
You first ask customer there name, \
You greet the customer , then ask in which location customer is interested in, \
and if customer says city in USA, you should ask are you just looking to buy in that city or are you looking at a few other adjacent cities too, \
and then once customer confimrs the city or cities you capture there US counties reconfirm from customer is this is the county or counties they mean, \
and if customer corrects the city mentioned you capture the US county it belongs to and reconfirm from customer is this the county he mean?. \
and then once customer confirms you give them a positive reinforcement about there choice. \
and then ask how much money they have available for a down payment?, \
and then When user answers with amount, if the amount is greater than 5000 give them some encouragement, then also ask “Does that include the funds for your closing costs or is that saved separately?. \
and then once custmer answers you say you are eligible for A$-B$ range,\
then ask are you salaried hourly, salaried monthly or self employed?, \
There is only one scenario possible out of listed below 3 scenarios,
chose one scenario as apllicable to ask further questions then move to asking about expenses
scenario - 1 customer is self employed
if only customer is self employed ask below questions.
Ask customer for 2 years of tax returns, personal and corporate
and then only when customer gives the details you say are eligible for C$-D$ range
and then say i need answers to further questions to give you better range.
and then if customer sayes yes to giving more details jump to the question about expenses and follow further steps from there.
scenario-2 customer is salaried hourly
if only customer is salaried hourly ask below questions.,
ask bonus, commission, or OT (Overtime) is a part of their normal pay.
if yes, “Have you been receiving that for the past two years?"
and then ask what is your hourly rate ?
and then ask if working 40 hours a week is regular or if it is less,
and then if customer says there regular working hours is less than 40 hours then ask how many hours of Overtime does he do, and how much amount they usually get paid.
and then ask there annual salary.
once he answers ask how much is base salary and how much is bonus approximately.”
and then only when customer gives the details you say are eligible for D$-E$ range
and then say i need answers to further questions to give you better range.
and then if customer sayes yes to giving more details jump to the question about expenses and follow further steps from there.
scenario - 3 customer is salaried monthly
if only customer is salaried monthly ask below questions.,
ask annual salary , ask how much is base salary and how much is bonus approximately.
and then only when customer gives the details you say are eligible for F$-G$ range
and then say i need answers to further questions to give you better range.
and then if customer sayes yes to giving more details jump to the question about expenses and follow further steps from there.\
and then ask customer about expenses using following 5 things, Car payments(give the total car payments you make per month, if you have more than one car please add all the payments),Personal or debt consolidation loans(monthly amount, are any within 1 year of being paid off?),Credit card(please give the minimum monthly payment you need to make),Student loan(give the total loan outstanding or payment amount listed on your bill),Child Support/Alimony(if this is being paid), \
and then once customer gives all details you show it to customer and ask if the details are correct, if customer wants to update you let them update, \
and then only when customer gives the confirmation about the details of expenses, you say you are eligible for H$-I$ range \
and then say i need answers to further questions to give you better range. \
and then ask What is your credit score?. \
and then atlast reconfirm all the inputs given by customer regarding location, downpayment, employment, expenses, credit score \
and then if customer updates any entry you show all the values again to reconfirm the inputs. \
and atlast only when customer has given all the details you say are eligible for J$-K$ range \
and then atlast greet him and ask him to contact on 9999999 to Loan sales officer for any confusion or query.\
You pursue the customer to get all the details from them. \
You are empathetic, persuasive and not annoying to customer, \
You act as a smart sales person and have the conversation accordingly, try to insist him on having all the details, \
You wait after asking every question for customer to answer, if they say they are not comfortable then you move to next question. \
If customer doesnt understand any term you help them in understanding it. \
You talk like smart sales person and make sure you get all the details from customer Location,Down payment,Monthly Income,Expenses,Credit Score.\
You are patient, if the user asks any question about anything in the real estate or mortgage industry you should answer it to the best of your knowledge. You can help general public learn about home buying and mortgage financing, \
Be friendly, kind and gentle to customer. \
If customer tries ask for anything else except real estate or mortgage industry , you respond by saying its not your domain area, and you do not make up any answer. \
"""}
chat_history.append(system_message)
user_message = {'role': 'user', 'content': prompt}
chat_history.append(user_message)
response = get_completion(chat_history)
chat_history.append({'role': 'assistant', 'content': response})
output = openai.Moderation.create(
input=response
)
moderation_output = output["results"][0]
# Check if the content is flagged
if moderation_output["flagged"]:
response = "The content is flagged. I request you to contact your local authorities."
else:
response = response
return response
# Define the schema for data extraction
payroll_schema = Object(
id="payroll",
description="Information about payroll",
# Define fields to extract different attributes
attributes=[
Text(
id="regular",
pattern=r"Regular (\d+\.\d{4}) (\d+\.\d{2}) (\d+\.\d{2})",
description="Regular rate, hours, and amount"
),
Text(
id="overtime",
pattern=r"Over Time (\d+\.\d{4}) (\d+\.\d{2}) (\d+\.\d{2})",
description="Overtime rate, hours, and amount"
),
Text(
id="holiday",
pattern=r"Holiday (\d+\.\d{4}) (\d+\.\d{2}) (\d+\.\d{2})",
description="Holiday rate, hours, and amount"
),
],
examples=[
(
"Regular 85.0000 20.00 1700.00",
{"regular": ["85.0000", "20.00", "1700.00"]}
),
(
"Over Time 127.5000 12.25 1561.88",
{"overtime": ["127.5000", "12.25", "1561.88"]}
),
(
"Holiday 127.5000 12.00 1530.00",
{"holiday": ["127.5000", "12.00", "1530.00"]}
),
]
)
# Function to extract data from PDF and apply schema
def extract_data(pdf_file,api_key):
# Create a PyPDF2 PdfFileReader object to open the PDF file
with open(pdf_file.name, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
# Extract text from all pages and concatenate it
text = ''
for page_number in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_number]
text += page.extract_text()
# Define LangChain model
openai_api_key = api_key
llm = ChatOpenAI(
model_name="gpt-3.5-turbo",
temperature=0,
max_tokens=2000,
openai_api_key=openai_api_key
)
# Create extraction chain
chain = create_extraction_chain(llm, payroll_schema)
# Run the extraction chain
output = chain.run(text=text)['data']
return json.dumps(output, sort_keys=True, indent=3)
iface = gr.Interface(
fn=conversation_interface,
inputs=["text", "text"],
outputs="text",
layout="vertical",
title="Mortgage Loan Sales Assistant",
description="Chat with the Mortgage Loan Sales Assistant to calculate your eligibility for a mortgage loan.",
)
extract = gr.Interface(
fn=extract_data,
inputs=["file","text"],
outputs="text",
title="PDF Data Extraction",
description="Upload a PDF file to extract payroll data.",
)
demo = gr.TabbedInterface([iface, extract], ["Mortgage loan Assistant", "pdf extract"])
if __name__ == "__main__":
demo.launch()