import openai import json import logging import sys import os import re from settings import gpt_model, RPFAAP2, RPFAAP1, TDRP, TDRP_COORDS from helpers import remove_na, filter_tables, merge_strings, Logger logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') sys.stdout = Logger("output.log") def chat_gpt_image(content, context): openai.api_key = os.environ['GPT_API_KEY'] prompt = "You are an expert at identifying OCR errors and correcting them with the help of context, intuition and logic." document = "The following text was scanned using OCR, your goal is to return a corrected version of the text" prefix = "Additionally" if context == "": sequence = (document, content) else: sequence = (prefix, context) additional = (" ".join(sequence)) sequence = (additional, content) final_content = (" ".join(sequence)) logging.info(final_content) print(final_content) completion = openai.ChatCompletion.create( model=gpt_model, user="1", messages=[ {"role": "system", "content": prompt}, {"role": "user", "content": final_content} ] ) logging.info(completion.choices[0].message.content) print(completion.choices[0].message.content) return(completion.choices[0].message.content) def chat_gpt_document(content, document_type, context): openai.api_key = os.environ['GPT_API_KEY'] prompt = "You are an expert at identifying OCR errors and correcting them with the help of context, intuition and logic." document_prefix = "The following text was scanned using OCR, your goal is to extract the important entities from the text and correct them with the help of the restrictions placed in the desired format. Remember to not make any changes on the labels and do not populate fields that don't have the , simply extract the text, correct it and return only the desired format. Leave the field blank if it cannot be found in the text. Text:" additional_prefix = "Additionally the text" text_content = content[0] pair_content = content[1] document_content = content[2] table_content = content[3] content_name = content[4] if document_type == "RPFAA Building P1": document = "./templates/RPFAAP1.json" desired_format = RPFAAP1 tables = [3] input_coords = TDRP_COORDS elif document_type == "RPFAA Building P2": document = "./templates/RPFAAP2.json" desired_format = RPFAAP2 tables = [] input_coords = TDRP_COORDS elif document_type == "TDRP": document = "./templates/TDRP.json" desired_format = TDRP tables = [0] input_coords = TDRP_COORDS else: property_info = ["Please Select a Document Type"] return json.dumps(property_info, indent=4) validity = check_valid(text_content, document_type) logging.info(validity) print(validity) if validity < 50: return f"Document type and document provided do not match for {content_name}" if context == "": sequence = (document_prefix, pair_content, desired_format) else: sequence = (document_prefix, pair_content, desired_format, additional_prefix, context) gpt_content = (" ".join(sequence)) logging.info(gpt_content) print(gpt_content) completion = openai.ChatCompletion.create( model=gpt_model, user="1", messages=[ {"role": "system", "content": prompt}, {"role": "user", "content": gpt_content} ] ) logging.info(completion.choices[0].message.content) print(completion.choices[0].message.content) input_string = remove_na(completion.choices[0].message.content) input_string = merge_strings(input_string,input_coords,document_content) with open(document) as f: property_info = json.load(f) #Adds the name of the file property_info["File Name"] = content_name #Fills in the information for line in input_string.split('\n'): if ':' in line: key, value = line.split(':', 1) key = key.strip() for category in property_info: if key in property_info[category]: property_info[category][key] = value.strip() break else: if key == "File Name": property_info[key] = value.strip() json.dumps(property_info, indent=4) table_string = filter_tables(table_content, tables) table_dict = json.loads(table_string) property_info.update(table_dict) json_string = json.dumps(property_info, indent=4) return json_string def check_valid(text_content, document_type): openai.api_key = os.environ['GPT_API_KEY'] if document_type == "RPFAA Building P1" or document_type == "RPFAA Building P2": document = "Real Property Field Appraisal and Assessment Sheet for Buildings and Other Structures" elif document_type == "TDRP": document = "Tax Declaration Form for Real Property" else: document = "None" prompt = "You are an expert at identifying Document Types based on their content." content = f"The document attached was scanned through OCR, from a scale of 0-100 how possible for it to have the Document Type: {document}. Return only the probability as a numerical value. Document Content: {text_content}" completion = openai.ChatCompletion.create( model=gpt_model, user="1", messages=[ {"role": "system", "content": prompt}, {"role": "user", "content": content} ] ) match = re.search(r'\d+', completion.choices[0].message.content) if match: return int(match.group()) else: return 100