Spaces:
Runtime error
Runtime error
| import openai | |
| from PyPDF2 import PdfReader | |
| import logging | |
| # Configure logging | |
| logging.basicConfig( | |
| filename='contract_missing_clausses.log', # You can adjust the log file name here | |
| filemode='a', | |
| format='[%(asctime)s] [%(levelname)s] [%(filename)s] [%(lineno)s:%(funcName)s()] %(message)s', | |
| datefmt='%Y-%b-%d %H:%M:%S' | |
| ) | |
| LOGGER = logging.getLogger(__name__) | |
| log_level_env = 'INFO' # You can adjust the log level here | |
| log_level_dict = { | |
| 'DEBUG': logging.DEBUG, | |
| 'INFO': logging.INFO, | |
| 'WARNING': logging.WARNING, | |
| 'ERROR': logging.ERROR, | |
| 'CRITICAL': logging.CRITICAL | |
| } | |
| if log_level_env in log_level_dict: | |
| log_level = log_level_dict[log_level_env] | |
| else: | |
| log_level = log_level_dict['INFO'] | |
| LOGGER.setLevel(log_level) | |
| class ContractMissingClauses: | |
| """ | |
| Class for identifying missing clauses, sub-clauses, and terms in a contract. | |
| """ | |
| def __init__(self): | |
| """ | |
| Initialize the ContractMissingClauses class and set up the OpenAI API client. | |
| """ | |
| # openai.api_type = os.getenv['api_type'] | |
| # openai.api_base = os.getenv['api_base'] | |
| # openai.api_version = os.getenv['api_version'] | |
| # openai.api_key = os.getenv['api_key'] | |
| pass | |
| def get_missing_clauses(self, contract: str): | |
| """ | |
| Generate and print missing clauses, sub-clauses, and terms in the given contract. | |
| Args: | |
| contract (str): The text of the contract. | |
| """ | |
| try: | |
| LOGGER.info("Analyzing contract and extracting missing clauses...") | |
| # Generate text using the OpenAI GPT-3 model | |
| conversation = [ | |
| {"role": "system", "content": "You are a helpful incomplete sentences finder"}, | |
| {"role": "user", "content": f"""list out the incomplete sentences in the following text: {text}"""} | |
| ] | |
| # Call OpenAI GPT-3.5-turbo | |
| chat_completion = openai.ChatCompletion.create( | |
| engine="ChatGPT", | |
| messages = conversation, | |
| temperature=0.7, | |
| max_tokens=800, | |
| top_p=0.95, | |
| frequency_penalty=0, | |
| presence_penalty=0, | |
| stop=None | |
| ) | |
| response = chat_completion.choices[0].message.content | |
| return response | |
| except Exception as e: | |
| # If an error occurs during the key-value extraction process, log the error | |
| LOGGER.error(f"Error occurred while extracting missing clauses: {str(e)}") | |
| def iterate_each_page(self,pdf_file): | |
| """ | |
| Iterate through each page of a PDF contract, extract text, and call get_missing_clauses for each page. | |
| """ | |
| try: | |
| LOGGER.info("Analyzing contract and extracting pdf page...") | |
| result = '' | |
| pdf = PdfReader(pdf_file_path.name) | |
| # Extract text from each page and pass it to the process_text function | |
| for page_number in range(len(pdf.pages)): | |
| # Extract text from the page | |
| page = pdf.pages[page_number] | |
| text = page.extract_text() | |
| result += self.get_missing_clauses(contract) | |
| return result | |
| except Exception as e: | |
| # If an error occurs during the key-value extraction process, log the error | |
| LOGGER.error(f"Error occurred while extracting pdf page: {str(e)}") | |