Spaces:
Sleeping
Sleeping
File size: 24,251 Bytes
caa2ac3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
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://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftheindustryspread.com%2Fwp-content%2Fuploads%2F2019%2F05%2FBroadridge-1.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(
"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Quicksand:[email protected]&display=swap');
body {
font-family: 'Quicksand', sans-serif;
}
.st-emotion-cache-13ln4jf.ea3mdgi5 {
position: -webkit-sticky;
position: sticky;
top: 10;
z-index: 100;
padding-top: 42px;
padding-bottom: 10px;
border-bottom: 1px solid #e6e6e6;
}
header.st-emotion-cache-12fmjuu.ezrtsby2 {
background-color: #f0f2f6 !important;
}
.st-emotion-cache-12fmjuu.ezrtsby2{
background:url('https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftheindustryspread.com%2Fwp-content%2Fuploads%2F2019%2F05%2FBroadridge-1.png') no-repeat;
background-size: 250px 50px;
background-position: center;
padding-left: 50px;
}
.st-emotion-cache-1vt4y43.ef3psqc13{
}
</style>
""",
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", "Update Find", "Comparizer"],
icons=['list', 'binoculars-fill', 'patch-check-fill'],
menu_icon="cast",
default_index=0,
orientation="horizontal",
)
st.markdown('</div>', unsafe_allow_html=True)
uploaded_file=''
# Display different content based on the menu selection
if selected_main_option == "Dashboard":
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Quicksand:[email protected]&display=swap');
body {
font-family: "Quicksand", sans-serif;
font-optical-sizing: auto;
font-weight: <weight>;
font-style: normal;
}
.topic-box {
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
margin-bottom: 10px;
}
.topic-box:hover{
background-color: #000080;
box-shadow: 6px 1px 12px gray;
color:#fff;
}
.topic-title {
font-weight: bold;
}
.st-emotion-cache-ocqkz7 {
gap: 1.5rem;
}
.st-emotion-cache-1vt4y43.ef3psqc13{
background-color: #f0f2f6 !important;
padding: 10px;
color:black;
font-weight: bold;
width: 200px;
border-style: none;
border-radius: 15px;
margin-bottom: 10px;
}
.st-emotion-cache-1vt4y43.ef3psqc13:hover{
background-color: #00578E !important;
box-shadow: 6px 1px 12px gray;
color:white;
font-weight: bold;
border-style: none;
width: 200px;
}
</style>
""", unsafe_allow_html=True)
button_value=''
with st.sidebar:
button_value = option_menu(
"CONTRACT AI",
[
'Home', 'Tags', 'Clauses', 'Summarizer', 'Headings', 'Extract Date',
'Pdf to Json', 'Key Values', 'Incorrect Sentences',
'Incomplete Sentences', 'Aggressive Content', "Contract Generator"
],
icons=[
'arrow-right-circle-fill'] * 12, # repeated icon list
menu_icon="house-gear-fill",
default_index=0
)
uploaded_file=st.file_uploader("Upload Document")
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.write("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.write("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.write("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.write("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.write("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.write("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.write("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.write("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 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.write("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':
confirmationEdit = Modal("Contract Generator", key= "contractgen")
get_response=''
contract_info=st.text_input("Enter Contract info")
if contract_info!='':
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 != '':
with confirmationEdit.container():
st.write("Contract Template")
st.write(get_response)
if st.button('Exit'):
confirmationEdit.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 = st.selectbox("Select Model", ['sentence-transformers/multi-qa-mpnet-base-dot-v1','sentence-transformers/multi-qa-MiniLM-L6-cos-v1',], index=0)
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()
|