import streamlit as st
import pandas as pd
import numpy as np
from streamlit_option_menu import option_menu
from llm import OpenaiAPI
from pdfProcessor import PDFProcessor
from embeddingsProcessor import EmbeddingsProcessor
from similarityCalculator import SimilarityCalculator
from findUpdate import FindUpdate
from pdfDocumentProcessor import PDFDocumentProcessor
from streamlit_modal import Modal
import os
import time
import shutil
import base64
from findUpdate import FindUpdate # Import the FindUpdate class
from tempfile import NamedTemporaryFile
icon_url = "https://amosszeps.com/wp-content/uploads/2021/12/llyods-bank.png"
st.set_page_config(page_title="Contract AI", page_icon='https://www.adople.com/assets/img/logo/title2.png',initial_sidebar_state="collapsed")
openai = OpenaiAPI()
# CSS to make the option menu sticky and to target specific container class
st.markdown(
"""
""",
unsafe_allow_html=True,
)
# Create the sticky top horizontal option menu within the specified container
with st.container():
selected_main_option = option_menu(
menu_title=None,
options=["Dashboard", "Comparizer"],
icons=['list', 'patch-check-fill'],
menu_icon="cast",
default_index=0,
orientation="horizontal",
)
st.markdown('', unsafe_allow_html=True)
uploaded_file=''
# Display different content based on the menu selection
if selected_main_option == "Dashboard":
st.markdown("""
""", unsafe_allow_html=True)
button_value=''
uploaded_file=st.file_uploader("Upload your Document")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("Tags"):
button_value = 'Tags'
with col2:
if st.button("Clauses"):
button_value = 'Clauses'
with col3:
if st.button("Summarizer"):
button_value = 'Summarizer'
col4, col5, col6= st.columns(3)
with col4:
if st.button("Headings"):
button_value = 'Headings'
with col5:
if st.button("Extract Date"):
button_value = 'Extract Date'
with col6:
if st.button("Pdf to Json"):
button_value = 'Pdf to Json'
col7, col8, col9= st.columns(3)
with col7:
if st.button("Key Values"):button_value = 'Key Values'
with col8:
if st.button("Incorrect Sentences"):button_value = 'Incorrect Sentences'
with col9:
if st.button("Incomplete Sentences"):button_value = 'Incomplete Sentences'
col10, col11,_= st.columns(3)
with col10:
if st.button("Aggressive Content"):
button_value = 'Aggressive Content'
# with col11:
# if st.button("Contract Generator"):
# button_value = 'Contract Generator'
if button_value == '' or button_value == 'Home':
st.title('')
# Tags
elif button_value == 'Tags':
confirmationTag = Modal("Tags Extracter", key= "tag_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful Tags Extracter.
analyze the given contract to extract tags for following contract in triple backticks.
tags should be bullet points.contract :
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationTag.container():
st.write("Similarity Scores Table:")
st.write(get_response)
if st.button('Close Window'):
confirmationTag.close()
else:
st.toast("No Document is Uploaded.")
# Clauses
elif button_value == 'Clauses':
confirmationClauses = Modal("Clauses Extracter", key= "clauses_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful Cluases and SubCluases Extracter From Given Content
Extract clauses and sub-clauses from the provided contract PDF
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationClauses.container():
st.write("Similarity Scores Table:")
st.write(get_response)
if st.button('Close.'):
confirmationClauses.close()
else:
st.toast("No Document is Uploaded.")
# Summarizer
elif button_value == 'Summarizer':
confirmationSummarizer = Modal("summarizer Extracter", key= "summarizer_extract")
if uploaded_file is not None:
print('File Name : ', uploaded_file.name)
ftype = uploaded_file.name.split('.')
if ftype[-1] == 'pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1] == 'docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful summarizer.
Write a concise summary of the following contract:
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationSummarizer.container():
st.write("summarizer Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationSummarizer.close()
else:
st.toast("No Document is Uploaded.")
# Headings
elif button_value == 'Headings':
confirmationHeadings = Modal("Headings Extracter", key= "Headings_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful document assistant.
Extract Headings from given paragraph do not generate just extract the headings from paragraph.
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationHeadings.container():
st.write("Headings Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationHeadings.close()
else:
st.toast("No Document is Uploaded.")
# Extract Date
elif button_value == 'Extract Date':
confirmationExtractDate = Modal("Extract Date Extracter", key= "date_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful assistant.
Your task is Identify Dates and Durations Mentioned in the contract and extract that date and duration in key-value pair.
format:
date:
-extracted date
-
Durations:
-extracted Durations
-
- """},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationExtractDate.container():
st.write("Extract Date Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationExtractDate.close()
else:
st.toast("No Document is Uploaded.")
# Pdf to Json
elif button_value == 'Pdf to Json':
confirmationPdf = Modal("Pdf to Json Extracter", key= "Pdf to Json_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful assistant.
Your task is Get the text and analyse and split it into Topics and Content in json format.Give Proper Name to Topic dont give any Numbers and Dont Give any empty Contents.The Output Format Should Be very good."""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationPdf.container():
st.write("Pdf to Json Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationPdf.close()
else:
st.toast("No Document is Uploaded.")
# Key Values
elif button_value == 'Key Values':
confirmationKey= Modal("Key values Extracter", key= "key_values_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful Keywords Extracter..
analyze the given contract and Extract Keywords for following contract in triple backticks. tags should be bullet points.contract :
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationKey.container():
st.write("Key values Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationKey.close()
else:
st.toast("No Document is Uploaded.")
# Incorrect Sentences
elif button_value == 'Incorrect Sentences':
confirmationIncorrect = Modal("Incorrect Sentences Extracter", key= "Incorrect_Sentences_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful Error sentence finder.
list out the grammatical error sentence in the given text:
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationIncorrect.container():
st.write("Incorrect Sentences Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationIncorrect.close()
else:
st.toast("No Document is Uploaded.")
# Incomplete Sentences
elif button_value == 'Incomplete Sentences':
confirmationIncomplete = Modal("Incomplete Sentences Extracter", key= "Incomplete_Sentences_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful incomplete sentences finder.
list out the incomplete sentences in the following text:
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationIncomplete.container():
st.write("Incomplete Sentences Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationIncomplete.close()
else:
st.toast("No Document is Uploaded.")
# Aggressive Content
elif button_value == 'Aggressive Content':
confirmationAgg = Modal("Aggressive content Extracter", key= "Aggressive_content_extract")
if uploaded_file is not None:
print('File Name : ',uploaded_file.name)
ftype=uploaded_file.name.split('.')
if ftype[-1]=='pdf':
docs_data = openai.pdf_to_text_pypdf2(uploaded_file)
elif ftype[-1]=='docx':
docs_data = openai.docx_to_text(uploaded_file)
conversation = [{"role": "system", "content": """
You are a helpful Aggressive Terms Finder in Given Contract.
This is a contract document content. Your task is to find aggressive terms, warning terms and penalties in the given contract.
"""},
{"role": "user", "content": f"```contract: {docs_data}```"}]
get_response = openai.get_response(conversation)
if get_response == ['', None]:
st.toast("No Result is founded.")
else:
with confirmationAgg.container():
st.write("Aggressive content Extracter:")
st.write(get_response)
if st.button('Close.'):
confirmationAgg.close()
else:
st.toast("No Document is Uploaded.")
# Contract Generator
# elif button_value == 'Contract Generator':
# confirmationGen = Modal("Contract Generator", key= "contract_gen1")
# get_response=''
# contract_info=st.text_input("Enter Contract info")
# if contract_info not in None:
# conversation = [{"role": "system", "content": """You are a helpful assistant. Your task is creating a complete contract with important terms and condiations based on the contract information and type.
# the contract type given by user.
# generate a contract :
# """},
# {"role": "user", "content": f"```content: {contract_info}```"}]
# get_response = openai.get_response(conversation)
# if get_response == ['', None]:
# st.toast("No Result is founded.")
# else:
# with confirmationGen.container():
# st.write("Contract Template")
# st.write(get_response)
# if st.button('Close.'):
# confirmationGen.close()
# else:
# st.toast("Please Enter Contract Info")
elif selected_main_option == "Update Find":
processor = PDFDocumentProcessor()
processor.file_uploaders()
if processor.uploaded_agreement and processor.uploaded_template:
processor.save_uploaded_files()
elif selected_main_option == "Comparizer":
confirmationEdit = Modal("Contract Comparizer", key= "popUp_edit")
col1, col2 = st.columns(2)
# Clear the templates and contracts folders before uploading new files
templates_folder = './templates'
contracts_folder = './contracts'
SimilarityCalculator.clear_folder(templates_folder)
SimilarityCalculator.clear_folder(contracts_folder)
with col1:
st.header("Upload Templates")
uploaded_files_templates = st.file_uploader("PDF Template", accept_multiple_files=True, type=['pdf'])
os.makedirs(templates_folder, exist_ok=True)
for uploaded_file in uploaded_files_templates:
if SimilarityCalculator.save_uploaded_file(uploaded_file, templates_folder):
st.write(f"Saved: {uploaded_file.name}")
with col2:
st.header("Upload Contracts")
uploaded_files_contracts = st.file_uploader("PDF Contracts", key="contracts", accept_multiple_files=True, type=['pdf'])
os.makedirs(contracts_folder, exist_ok=True)
for uploaded_file in uploaded_files_contracts:
if SimilarityCalculator.save_uploaded_file(uploaded_file, contracts_folder):
st.write(f"Saved: {uploaded_file.name}")
model_name = 'sentence-transformers/multi-qa-mpnet-base-dot-v1'
if st.button("Compute Similarities"):
pdf_processor = PDFProcessor()
embedding_processor = EmbeddingsProcessor(model_name)
# Process templates
template_files = [os.path.join(templates_folder, f) for f in os.listdir(templates_folder)]
template_texts = [pdf_processor.extract_text_from_pdfs([f])[0] for f in template_files if pdf_processor.extract_text_from_pdfs([f])]
template_embeddings = embedding_processor.get_embeddings(template_texts)
# Process contracts
contract_files = [os.path.join(contracts_folder, f) for f in os.listdir(contracts_folder)]
contract_texts = [pdf_processor.extract_text_from_pdfs([f])[0] for f in contract_files if pdf_processor.extract_text_from_pdfs([f])]
contract_embeddings = embedding_processor.get_embeddings(contract_texts)
# Compute similarities
similarities = SimilarityCalculator.compute_similarity(template_embeddings, contract_embeddings)
# Display results in a table format
similarity_data = []
for i, contract_file in enumerate(contract_files):
row = [i + 1, os.path.basename(contract_file)] # SI No and contract file name
for j in range(len(template_files)):
if j < similarities.shape[1] and i < similarities.shape[0]: # Check if indices are within bounds
row.append(f"{similarities[i, j] * 100:.2f}%") # Format as percentage
else:
row.append("N/A") # Handle out-of-bounds indices gracefully
similarity_data.append(row)
# Create a DataFrame for the table
columns = ["SI No", "Contract"] + [os.path.basename(template_files[j]) for j in range(len(template_files))]
similarity_df = pd.DataFrame(similarity_data, columns=columns)
if similarity_df.empty:
st.write("No similarities computed.")
else:
with confirmationEdit.container():
st.write("Similarity Scores Table:")
st.table(similarity_df.style.hide(axis="index"))
if st.button('Close Window'):
confirmationEdit.close()
submitted = st.button("Show Result")
if submitted:
confirmationEdit.open()
if confirmationEdit.is_open():
with confirmationEdit.container():
st.write("Similarity Scores Table:")
st.table(similarity_df.style.hide(axis="index"))
if st.button('Close Result'):
confirmationEdit.close()