Spaces:
Running
Running
from PyPDF2 import PdfReader | |
from openai import AzureOpenAI | |
import logging | |
import os | |
# Configure logging | |
logging.basicConfig( | |
filename='extract_date.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 ExtractDateAndDuration: | |
def __init__(self): | |
""" | |
Initialize the ExtractDateAndDuration class. | |
""" | |
# 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_date_and_duration(self, contract_text: str) -> str: | |
""" | |
Extract dates and durations from the provided contract text. | |
Args: | |
contract_text (str): The text of the contract to analyze. | |
Returns: | |
str: Extracted dates and durations. | |
""" | |
try: | |
client = AzureOpenAI(api_key=os.getenv("AZURE_OPENAI_KEY"), | |
api_version="2023-07-01-preview", | |
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") | |
) | |
conversation = [ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": f"""Your task is Identify Dates and Durations Mentioned in the contract and extract that date and duration in key-value pair. | |
```contract: {contract_text}``` | |
format: | |
date:extracted date | |
Durations:extracted Durations | |
"""} | |
] | |
# Call OpenAI GPT-3.5-turbo | |
chat_completion = client.chat.completions.create( | |
model = "GPT-3", | |
messages = conversation, | |
max_tokens=1000, | |
temperature=0 | |
) | |
response = chat_completion.choices[0].message.content | |
return response | |
except Exception as e: | |
LOGGER.error(f"An error occurred during text analysis: {str(e)}") | |
def itrate_each_page(self, pdf_file_path: str): | |
""" | |
Extract text from each page of a PDF document and process it. | |
Args: | |
pdf_file_path (str): The path to the PDF document. | |
Returns: | |
str: Extracted text from the PDF pages. | |
""" | |
try: | |
# Open the multi-page PDF using PdfReaderer | |
pdf = PdfReader(pdf_file_path.name) | |
extracted_date_duration = "" | |
# 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() | |
# Pass the text to the process_text function for further processing | |
extracted_date_duration += self.get_date_and_duration(text) | |
return extracted_date_duration | |
except Exception as e: | |
LOGGER.error(f"An error occurred while processing the PDF document: {str(e)}") | |