Spaces:
Running
Running
File size: 38,654 Bytes
a48b255 |
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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
import re
import os
import json
import time
import fitz
import difflib
import secrets
import openai
import string
import logging
import requests
import streamlit as st
from pandas import DataFrame
from openai import AzureOpenAI
from difflib import SequenceMatcher
from tempfile import NamedTemporaryFile
from typing import Tuple, Dict, List, Union
from diff_match_patch import diff_match_patch
import base64
import pandas as pd
class FindUpdate:
@staticmethod
def clean_string(text: str, stem: str = "None") -> str:
"""Clean the input text by removing punctuation, numbers, and optionally stemming or lemmatizing the words.
Args:
text (str): The input text to be cleaned.
stem (str, optional): The stemming method to be used. Options are "None" (default), "Stem", "Lem", or "Spacy".
Returns:
str: The cleaned text.
Raises:
None
"""
try:
final_string = ""
# Replace any characters with nothing or a space
text = re.sub(r'\n', ' ', text)
text = re.sub(r' +', ' ', text)
text = re.sub(r'[^\x00-\x7f]',r'', text)
# Remove punctuation
translator = str.maketrans('', '', string.punctuation)
text = text.translate(translator)
# Remove numbers
text_filtered = [re.sub(r'\w*\d\w*\s+', '', w) for w in text]
text_filtered = [re.sub('[0-9]', '', w) for w in text_filtered]
# Stem or Lemmatize
if stem == 'Stem':
stemmer = PorterStemmer()
text_stemmed = [stemmer.stem(y) for y in text_filtered]
elif stem == 'Lem':
lem = WordNetLemmatizer()
text_stemmed = [lem.lemmatize(y) for y in text_filtered]
elif stem == 'Spacy':
text_filtered = nlp(' '.join(text_filtered))
text_stemmed = [y.lemma_ for y in text_filtered]
else:
text_stemmed = text_filtered
final_string = ''.join(text_stemmed)
except Exception as e:
# Handle the exception here
logging.error(f"An error occurred in clean_string: {e}")
return None
return final_string
@staticmethod
def extract_text_without_header_footer(page: fitz.Page, page_height: float, header_height: float, footer_height: float) -> str:
"""
Extract the text from a page of a PDF document, excluding the header and footer.
Args:
page (fitz.Page): The page object from the PyMuPDF library.
page_height (float): The height of the page.
header_height (float): The height of the header to be excluded.
footer_height (float): The height of the footer to be excluded.
Returns:
str: The extracted text.
Raises:
None
"""
try:
exclude_top = header_height
exclude_bottom = page_height - footer_height
text = ""
for text_block in page.get_text_blocks():
bbox = fitz.Rect(text_block[:4]) # Get bounding box of text block
if bbox.y0 >= exclude_top and bbox.y1 <= exclude_bottom:
text += text_block[4] + "\n" # Add text content to result
return text
except Exception as e:
# Handle the exception here
logging.error(f"An error occurred in extract_text_without_header_footer: {e}")
return ""
@staticmethod
def rect_to_dict(rect: fitz.Rect) -> Dict[str, float]:
"""
Convert a PyMuPDF Rect object to a dictionary.
Args:
rect (fitz.Rect): The Rect object from the PyMuPDF library.
Returns:
Dict[str, float]: The dictionary representation of the Rect object.
Raises:
None
"""
return {
"x1": rect[0],
"y1": rect[1],
"x2": rect[2],
"y2": rect[3]
}
@staticmethod
def extract_sections(pdf_path: str, header_height: float, footer_height: float) -> Dict[str, Dict[str, List[str]]]:
"""
Extract sections from a PDF document, excluding the header and footer.
Args:
pdf_path (str): The path to the PDF document.
header_height (float): The height of the header to be excluded.
footer_height (float): The height of the footer to be excluded.
Returns:
Dict[str, Dict[str, List[str]]]: A dictionary containing the extracted sections.
Raises:
None
"""
try:
doc = fitz.open(pdf_path)
sections = {}
current_section = None
for page_num in range(2, len(doc)):
page = doc.load_page(page_num)
page_height = page.rect.height
text = FindUpdate.extract_text_without_header_footer(page, page_height, header_height, footer_height)
lines = text.split('\n')
print("start")
for line in lines:
match = re.match(r'^(?:\d+\.\d*(?:\.\d+)*)|AT&T ALLIANCE PROGRAM AGREEMENT', line.strip())
if match:
print("line:",line)
section_num = match.group()
if section_num != current_section:
current_section = section_num
sections[current_section] = {'page_num': str(page_num + 1), 'coords': [], 'content': []}
if current_section:
sections[current_section]['content'].append(line)
bbox = page.search_for(line)
if bbox:
for cord in bbox:
sections[current_section]['coords'].append(FindUpdate.rect_to_dict(cord))
print("section-keys:",sections.keys())
for key in sections.keys():
if 'content' in sections[key]:
sections[key]['content'] = (' '.join(sections[key]['content']))[len(key):].lstrip()
print("Content:",sections)
return sections
except Exception as e:
# Handle the exception here
logging.error(f"An error occurred in extract_sections: {e}")
return {}
@staticmethod
def check_identify_changes(template_text: str, contract_text: str) -> str:
"""
Compare the template text with the contract text and identify the changes made.
Args:
template_text (str): The template text.
contract_text (str): The contract text.
Returns:
str: A sentence prompt describing the changes made.
Raises:
None
"""
try:
dmp = diff_match_patch()
diff = dmp.diff_main(template_text, contract_text)
dmp.diff_cleanupSemantic(diff)
if all([True if each[0] == 0 or each[1] == ' ' else False for each in diff]):
return('no_change')
else:
deleted = []
added = []
for each in diff:
if each[0] == -1:
deleted.append(each[1])
elif each[0] == 1:
added.append(each[1])
sentence_prompt = f"""
template text = {template_text}
contract_text = {contract_text}
deleted from template --- {deleted}
added to the actual contract -- {added}
"""
return(sentence_prompt)
except Exception as e:
# Handle the exception here
logging.error(f"An error occurred in check_identify_changes: {e}")
return ""
@staticmethod
def open_ai(prompt: str) -> str:
"""
Generate AI response using OpenAI GPT-4 model.
Args:
prompt (str): The prompt for the AI model.
Returns:
str: The generated AI response.
Raises:
None
"""
stat = 0
while stat == 0:
try:
client = AzureOpenAI(api_key="5c68e66b7eb3470987c9088b6602c6c7",
api_version="2023-07-01-preview",
azure_endpoint="https://azureadople.openai.azure.com/")
conversation = []
conversation.append({"role": "user", "content": prompt})
response = client.chat.completions.create(
model="GPT-3",
messages=conversation,
temperature=0,
max_tokens=3000,
stop=None
)
stat = 1
except openai.RateLimitError as e:
logging.error(f"Rate limit error occurred: {e}")
stat = 0
time.sleep(60)
except Exception as e:
logging.error(f"An error occurred in open_ai: {e}")
stat = 0
output = response.choices[0].message.content
return output
@staticmethod
def compare_strings(string1: str, string2: str) -> float:
"""
Compare two strings and return their similarity ratio.
Args:
string1 (str): The first string.
string2 (str): The second string.
Returns:
float: The similarity ratio between the two strings.
Raises:
None
"""
matcher = SequenceMatcher(None, string1, string2)
similarity_ratio = matcher.ratio()
return similarity_ratio
@staticmethod
def get_main_section(section_number: str) -> str:
"""
Get the main section number from a given section number.
Args:
section_number (str): The section number.
Returns:
str: The main section number.
Raises:
None+
"""
parts = section_number.split('.')
if len(parts) > 1:
parts.pop()
return ".".join(parts)
@staticmethod
def increment_section(section_number: str) -> str:
"""
Increment a section number by one.
Args:
section_number (str): The section number.
Returns:
str: The incremented section number.
Raises:
None
"""
parts = section_number.split('.')
last_part = '0' if not parts[-1] else parts[-1] # Get the last part of the section number
# Check if the last part is numeric
if last_part.isdigit():
# Convert the last part to an integer and increment it by one
incremented_last_part = str(int(last_part) + 1)
# Replace the last part in the parts list
parts[-1] = incremented_last_part
else:
# If the last part is not numeric, return the original section number
return section_number
# Join the parts back together with '.'
incremented_section = '.'.join(parts)
return incremented_section
@staticmethod
def count_subsections(dictionary: Dict[str, Union[str, Dict]]) -> Dict[str, int]:
"""
Count the number of subsections in a dictionary.
Args:
dictionary (Dict[str, Union[str, Dict]]): The dictionary containing the subsections.
Returns:
Dict[str, int]: A dictionary with the main section numbers as keys and the count of subsections as values.
Raises:
None
"""
section_counts = {}
for key in dictionary:
# Split the key by '.' and take the first part as the section number
section_number = FindUpdate.get_main_section(key)
# Check if the section number is already in the section_counts dictionary
if section_number in section_counts:
# Increment the count for the section
section_counts[section_number] += 1
else:
# Initialize the count for the section
section_counts[section_number] = 1
return section_counts
@staticmethod
def split_page_section(page_no: str, sec_no: str) -> Tuple[str, Tuple[int]]:
"""
Split the page number and section number into separate components.
Args:
page_no (str): The page number.
sec_no (str): The section number.
Returns:
Tuple[str, Tuple[int]]: A tuple containing the page number and section number.
Raises:
None
"""
try:
page_number = page_no
section_numbers = sec_no
section_numbers = [int(num) if num and num.isdigit() else 0 for num in section_numbers]
section_number = tuple(section_numbers)
return (page_number, section_number)
except Exception as e:
logging.error(f"An error occurred in split_page_section: {e}")
return ("", ())
@staticmethod
def process_comparisons(result_agreement: Dict[str, Dict], result_template: Dict[str, Dict]) -> Tuple[List[Dict], List[Dict], List[Dict]]:
"""
Process the comparisons between the agreement and template texts.
Args:
result_agreement (Dict[str, Dict]): The agreement text.
result_template (Dict[str, Dict]): The template text.
Returns:
Tuple[List[Dict], List[Dict], List[Dict]]: A tuple containing the changes list, actual list, and changes_ui list.
Raises:
None
"""
# Initialize a flag to track if there are any changes
changes = {}
changes_ui = {}
actual = {}
compared_sections = []
actual_list = []
changes_list = []
changes_ui_list = []
sections_added = False
current_section = 0
prev_section = 0
# Iterate through the keys and values of the dictionaries
for key in result_template:
current_section = FindUpdate.get_main_section(key)
if sections_added and current_section == prev_section:
contract_key = FindUpdate.increment_section(current_section)
else:
contract_key = key
if prev_section != current_section:
sections_added = False
if contract_key in result_agreement:
if FindUpdate.count_subsections(result_agreement)[current_section] != FindUpdate.count_subsections(result_template)[current_section]:
if FindUpdate.compare_strings(result_template[key]['content'], result_agreement[contract_key]['content']) < .1:
# Actual JSON
actual = {}
actual["actual"] = ""
actual["page_number"] = result_template[key]['page_num']
actual["section_number"] = key
actual["actual_coords"] = ""
actual_list.append(actual)
# Changes JSON
changes = {}
changes["changed"] = result_agreement[contract_key]['content'] + ' Analysis:New clause added' + '~!~addition'
changes["changed_page_number"] = result_agreement[contract_key]['page_num']
changes["changed_section_number"] = key
changes["changed_coords"] = result_agreement[contract_key]['coords']
changes_list.append(changes)
# changes_ui JSON
changes_ui = {}
changes_ui["changed"] = result_agreement[contract_key]['content'] + ' Analysis:New clause added' + '~!~addition'
changes_ui["changed_page_number"] = result_agreement[contract_key]['page_num']
changes_ui["changed_section_number"] = key
changes_ui["changed_coords"] = result_agreement[contract_key]['coords']
changes_ui_list.append(changes_ui)
contract_key = FindUpdate.increment_section(contract_key)
sections_added = True
compared_sections.append(contract_key)
if contract_key in result_agreement:
try:
prompt = f""""
As an attorney representing your client, your task is to compare the template text and the contract text provided and identify any changes that may impact the agreement or the involved parties. Specifically, you need to analyze the legal implications and considerations of the word 'subcontractors' when it appears in the changed text. Instead of focusing solely on the addition or removal of the letters 'or,' provide a comprehensive analysis based on the complete word 'subcontractors.' Classify the changes as either "minor_change" or "major_change."
Please provide the changed text alone as a separate paragraph under the "Changed:" subheading, and the analysis of the changes as a separate paragraph under the "Analysis:" subheading. At the end, add ~!~ and classification like ~!~minor_change or ~!~major_change.
EXAMPLES -
1.
Contract text:
Verify identification credentials including Social Security number, driver’s license, educational credentials, employment history, home address, and citizenship indicia;
In connection with providing access to a customer’s facilities or systems, comply with any additional investigation or screening requirements required by such customer as communicated in advance; test for use of illicit drugs including opiates, cocaine, cannabinoids, amphetamines, and phencyclidine.
Template text:
Verify identification credentials including Social Security number, driver’s license, educational credentials, employment history, home address, and citizenship indicia.
In connection with providing access to a customer’s facilities or systems, comply with any additional investigation or screening requirements required by such customer as communicated in advance. Test for use of illicit drugs including opiates, cocaine, cannabinoids, amphetamines, and phencyclidine.
Differences Observed -
Changed: "When (i) the customer or end user is a federal, state, or local government entity"
Analysis:
A new sentence is added: "When (i) the customer or end user is a federal, state, or local government entity" in the template text. This clause specifies a condition under which the drug testing requirement applies. In the template text, this condition is excluded, meaning that the drug testing requirement would apply regardless of whether the customer or end user is a government entity. This could potentially change the scope and applicability of the drug testing provision.~!~major_change
2.
Contract text:
Perform a criminal background check to determine, in the counties, states, and federal court districts where the candidate has lived, worked, or attended school in the previous ten years, whether the candidate has been: (1) convicted of any felony; (2) convicted of a misdemeanor involving violence, theft, computer crimes, fraud, financial crimes, drug distribution, unlawful possession or use of a dangerous weapon, or sexual misconduct; or, (3) listed on any government registry as a sex offender (together, “Conviction”); and in connection with providing access to a customer’s facilities or systems, comply with any additional investigation or screening requirements required by such customer as communicated in advance.
Template text:
Perform a criminal background check to determine, in the counties, states, and federal court districts where the candidate has lived, worked, or attended school in the previous ten years, whether the candidate has been: (1) convicted of any felony; (2) convicted of a misdemeanor involving violence, theft, computer crimes, fraud, financial crimes, drug distribution, unlawful possession or use of a dangerous weapon, or sexual misconduct; or, (3) listed on any government registry as a sex offender (together, “Conviction”). In connection with providing access to a customer’s facilities or systems, comply with any additional investigation or screening requirements required by such customer as communicated in advance.
Differences Observed -
Changed: "and"
Analysis:
The contract text includes the word "and," which isn't in the template. This could potentially change the interpretation of the agreement, as it could be read as each clause being a separate requirement, rather than a list of requirements.~!~major_change
3.
Contract text:
The service provider may not assign, delegate, or otherwise transfer its rights or obligations under this Agreement, voluntarily or involuntarily, without the prior written consent of the other party except that the service provider may delegate certain obligations hereunder to its subcontractors as contemplated by this Agreement. Any attempted assignment, delegation, or transfer not consented to in writing will be void. Notwithstanding the foregoing, with notice to the other party, either party may assign this Agreement, in whole or in part, to any affiliate, successor-in-interest or without securing the consent of the other party. Any assignment of money will be void if (i) the assignor fails to give the non-assigning party at least thirty (30) days prior written notice, or (ii) the assignment purports to impose upon the non-assigning party additional costs or obligations in addition to the payment of such money, or (iii) the assignment purports to preclude the other party from dealing solely and directly with the service provider in all matters pertaining to this Agreement. This Agreement binds and benefits both parties and their permitted successors and assigns.
Template text:
The service provider may not assign, delegate, or otherwise transfer its rights or obligations under this Agreement, voluntarily or involuntarily, without the prior written consent of the other party except that the service provider may delegate certain obligations hereunder to its subcontracts as contemplated by this Agreement. Any attempted assignment, delegation, or transfer not consented to in writing will be void. Notwithstanding the foregoing, with notice to the other party, either party may assign this Agreement, in whole or in part, to any affiliate, successor-in-interest or without securing the consent of the other party. Any assignment of money will be void if (i) the assignor fails to give the non-assigning party at least thirty (30) days prior written notice, or (ii) the assignment purports to impose upon the non-assigning party additional costs or obligations in addition to the payment of such money, or (iii) the assignment purports to preclude the other party from dealing solely and directly with the service provider in all matters pertaining to this Agreement. This Agreement binds and benefits both parties and their permitted successors and assigns.
Differences Observed -
Changed: "Subcontractors"
Analysis: The contract text includes the word "Subcontractors" instead of "Subcontracts" as in the template. This change could potentially alter the meaning of the agreement as it specifies a different entity (Subcontractors vs Subcontracts). However, given the context, it seems likely that this is a typographical error in the template text, and the intended meaning remains the same.~!~minor_change
4.
Contract text:
This AGREEMENT is entered into between [Company Name], a [State] corporation, which sometimes does business as [DBA Name], (“Company”) and [Other Party Name], a [State] LLC (“Solution Provider” or “SP”). The company and solution provider may be referred to collectively as the “Parties” or individually as a “Party.”
Template text:
ALLIANCE PROGRAM AGREEMENT This AGREEMENT is entered into between [Company Name], a [State] corporation, which sometimes does business as [DBA Name], (“Company”) and ________________________, a __________ corporation (“Solution Provider” or “SP”). The company and solution provider may be referred to collectively as the “Parties” or individually as a “Party.”
Differences Observed -
Changed:"[Other Party Name], a [State] LLC"
Analysis: The contract text includes the specific name of the corporation "[Other Party Name], a [State] LLC" instead of a placeholder as in the template. This is a minor change as it is expected that the specific name of the corporation would be filled in the actual contract.~!~minor_change
"""
res = FindUpdate.check_identify_changes(FindUpdate.clean_string(result_template[key]['content']), FindUpdate.clean_string(result_agreement[contract_key]['content']))
if res != 'no_change':
final_prompt =prompt + res
llm_res = FindUpdate.open_ai(final_prompt)
# Actual JSON
actual = {}
actual["actual"] = result_template[key]['content']
actual["page_number"] = result_template[key]['page_num']
actual["section_number"] = key
actual["actual_coords"] = result_template[key]['coords']
actual_list.append(actual)
# Changes JSON
changes = {}
changes["changed"] = llm_res
changes["changed_page_number"] = result_agreement[contract_key]['page_num']
changes["changed_section_number"] = key
changes["changed_coords"] = result_agreement[contract_key]['coords']
changes_list.append(changes)
# changes_ui JSON
changes_ui = {}
changes_ui["changed"] = llm_res
changes_ui["changed_page_number"] = result_agreement[contract_key]['page_num']
changes_ui["changed_section_number"] = key
changes_ui["changed_coords"] = result_agreement[contract_key]['coords']
changes_ui_list.append(changes_ui)
except Exception as e:
logging.error(f"An error occurred in check_identify_changes: {e}")
else:
# Actual JSON
actual = {}
actual["actual"] = result_template[key]['content']
actual["page_number"] = result_template[key]['page_num']
actual["section_number"] = key
actual["actual_coords"] = result_template[key]['coords']
actual_list.append(actual)
prev_section = current_section
for key in result_template:
if key not in result_agreement:
actual = {}
# Actual JSON
actual["actual"] = result_template[key]['content']
actual["page_number"] = result_template[key]['page_num']
actual["section_number"] = key
actual['actual_coords'] = ''
actual_list.append(actual)
#Changes JSON
changes ={}
changes["changed"] = result_template[key]['content']+' Analysis:missing'+'~!~missing'
changes["changed_page_number"] = result_template[key]['page_num']
changes["changed_section_number"] = key
changes["changed_coords"] = result_template[key]['coords']
changes_list.append(changes)
#changes_ui JSON
changes_ui = {}
changes_ui["changed"] = result_template[key]['content']+' Analysis:missing'+'~!~missing'
changes_ui["changed_page_number"] = result_template[key]['page_num']
changes_ui["changed_section_number"] = key
changes_ui["changed_coords"] = result_template[key]['coords']
changes_ui_list.append(changes_ui)
for key in result_agreement:
if key not in result_template and key not in compared_sections:
actual ={}
#Actual JSON
actual["actual"] = ""
actual["page_number"] = result_agreement[key]['page_num']
actual["section_number"] = key
actual["actual_coords"] = ""
actual_list.append(actual)
changes = {}
#Changes JSON
changes["changed"] = result_agreement[key]['content']+' Analysis:New clause added'+'~!~addition'
changes["changed_page_number"] = result_agreement[key]['page_num']
changes["changed_section_number"] = key
changes["changed_coords"] = result_agreement[key]['coords']
changes_list.append(changes)
#changes_ui JSON
changes_ui ={}
changes_ui["changed"] = result_agreement[key]['content']+' Analysis:New clause added'+'~!~addition'
changes_ui["changed_page_number"] = result_agreement[key]['page_num']
changes_ui["changed_section_number"] = key
changes_ui["changed_coords"] = result_agreement[key]['coords']
changes_ui_list.append(changes_ui)
return changes_list,actual_list,changes_ui_list
@staticmethod
def remove_analysis(text: str) -> str:
"""
Remove the analysis portion from the given text.
Args:
text (str): The text to remove the analysis from.
Returns:
str: The text with the analysis portion removed.
"""
new_text = re.sub(r"(Analysis.*)", "", text)
return new_text
@staticmethod
def json_output(actual: List[Dict[str, str]], changes_ui: List[Dict[str, str]], file: str, template_files: str, result_template: str, result_agreement: str) -> Dict[str, any]:
"""
Generate a JSON output based on the provided inputs.
Args:
actual (List[Dict[str, str]]): The list of actual changes.
changes_ui (List[Dict[str, str]]): The list of UI changes.
file (str): The file path.
template_files (str): The template path.
result_template (str): The template result.
result_agreement (str): The agreement result.
Returns:
Dict[str, any]: The generated JSON output.
"""
deletion_prompt = """
{} : This clause has been deleted from the existing contract. what is the impact? provide me short analysis in 1 single paragraph should not exceed 100 words.
"""
addition_prompt = """
{} : This clause has been added in the existing contract. what is the impact? provide me short analysis in 1 single paragraph should not exceed 100 words.
"""
json_output = {}
json_output["input_file_path"] = file
comparison_list = []
for i in range(len(actual)):
actual_changes = '{}'
actual_changes_json = json.loads(actual_changes)
analysis_final = re.sub(r'\n', ' ', changes_ui[i]['changed'].split("~!~")[0].split("Analysis:")[1])
if changes_ui[i]['changed'].split('~!~')[-1] == 'addition' or changes_ui[i]['changed'].split('~!~')[-1] == 'missing':
continue
actual_changes_json.update({"actual": actual[i]['actual']})
actual_changes_json.update({"page_number": actual[i]['page_number']})
actual_changes_json.update({"section_number": actual[i]['section_number']})
actual_changes_json.update({"actual_coords": actual[i]['actual_coords']})
actual_changes_json.update({"changed": FindUpdate.remove_analysis(changes_ui[i]["changed"])})
actual_changes_json.update({"changed_page_number": changes_ui[i]["changed_page_number"]})
actual_changes_json.update({"changed_section_number": changes_ui[i]["changed_section_number"]})
actual_changes_json.update({"changed_coords": changes_ui[i]["changed_coords"]})
actual_changes_json.update({"analysis": analysis_final})
actual_changes_json.update({"type_of_change": changes_ui[i]['changed'].split("~!~")[1]})
comparison_list.append(actual_changes_json)
for i in range(len(changes_ui)):
actual_changes = '{}'
actual_changes_json = json.loads(actual_changes)
if changes_ui[i]['changed'].split('~!~')[-1] == 'missing':
actual_changes_json.update({"actual": actual[i]['actual']})
actual_changes_json.update({"page_number": actual[i]['page_number']})
actual_changes_json.update({"section_number": actual[i]['section_number']})
actual_changes_json.update({"actual_coords": actual[i]['actual_coords']})
actual_changes_json.update({"changed": FindUpdate.remove_analysis(changes_ui[i]["changed"])})
actual_changes_json.update({"changed_page_number": changes_ui[i]["changed_page_number"]})
actual_changes_json.update({"changed_section_number": changes_ui[i]["changed_section_number"]})
actual_changes_json.update({"changed_coords": changes_ui[i]["changed_coords"]})
final_deletion_prompt = deletion_prompt.format(actual[i]['actual'])
actual_changes_json.update({"analysis": FindUpdate.open_ai(final_deletion_prompt)})
actual_changes_json.update({"type_of_change": "missing"})
comparison_list.append(actual_changes_json)
if changes_ui[i]['changed'].split('~!~')[-1] == 'addition':
actual_changes_json.update({"actual": actual[i]['actual']})
actual_changes_json.update({"page_number": actual[i]['page_number']})
actual_changes_json.update({"section_number": actual[i]['section_number']})
actual_changes_json.update({"actual_coords": actual[i]['actual_coords']})
actual_changes_json.update({"changed": FindUpdate.remove_analysis(changes_ui[i]["changed"])})
actual_changes_json.update({"changed_page_number": changes_ui[i]["changed_page_number"]})
actual_changes_json.update({"changed_section_number": changes_ui[i]["changed_section_number"]})
actual_changes_json.update({"changed_coords": changes_ui[i]["changed_coords"]})
final_addition_prompt =addition_prompt.format(changes_ui[i]["changed"])
actual_changes_json.update({"analysis": FindUpdate.open_ai(final_addition_prompt)})
actual_changes_json.update({"type_of_change": "addition"})
comparison_list.append(actual_changes_json)
json_output["changes"] = comparison_list
# Sort the data based on the "actual" key in ascending order
sorted_data = sorted(json_output["changes"], key=lambda x: FindUpdate.split_page_section(x["page_number"], x["section_number"]))
# Update the JSON data with the sorted_data
json_output["changes"] = sorted_data
return json_output
@staticmethod
def process_files_template(file: str) -> List[str]:
"""
Process the template file and extract sections.
Args:
file (str): The path of the template file.
Returns:
List[str]: The extracted sections from the template file.
"""
try:
# Set the header and footer heights
header_height = 50
footer_height = 80
# Extract sections from the file
sections = FindUpdate.extract_sections(file, header_height, footer_height)
print("Template:",sections)
return sections
except Exception as e:
# Create a logger
logger = logging.getLogger(__name__)
logger.error(f"Error processing template file: {str(e)}")
raise
@staticmethod
def process_files_original(file: str) -> List[str]:
"""
Process the original file and extract sections.
Args:
file (str): The path of the original file.
Returns:
List[str]: The extracted sections from the original file.
"""
try:
# Set the header and footer heights
header_height = 50
footer_height = 80
# Extract sections from the file
sections = FindUpdate.extract_sections(file, header_height, footer_height)
print("Original:",sections)
return sections
except Exception as e:
# Create a logger
logger = logging.getLogger(__name__)
logger.error(f"Error processing original file: {str(e)}")
raise
@staticmethod
def main_processing_function(file_path: str, template_path: str):
"""
Processes the provided PDF file and its template to identify and report changes.
Args:
file_path (str): Path to the PDF file to be processed.
template_path (str): Path to the corresponding template PDF file.
Returns:
dict: A dictionary containing the processed results and change analysis.
"""
logger = logging.getLogger(__name__)
try:
# Process the original PDF file
result_agreement = FindUpdate.process_files_original(file_path)
# Process the template PDF file
result_template = FindUpdate.process_files_template(template_path)
# Compare the sections extracted from the original and template files
changes, actual, changes_ui = FindUpdate.process_comparisons(result_agreement, result_template)
# Generate a JSON output summarizing the changes
final_output = FindUpdate.json_output(actual, changes_ui, file_path, template_path, result_template, result_agreement)
return final_output
except Exception as e:
logger.error(f"Error processing files: {e}")
raise Exception(f"Error during file processing: {str(e)}") |