from settings import gpt_model, RPFAAP2, RPFAAP1, TDRP, TDRP_COORDS import openai import json import logging from helpers import remove_na, filter_tables, merge_strings import os logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') 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_1 = (prefix, context) additional = (" ".join(sequence_1)) sequence = (additional, content) final_content = (" ".join(sequence)) logging.info(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) 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" pair_content = content[0] document_content = content[1] table_content = content[2] content_name = content[3] if document_type == "RPFAA Building P1": document = "RPFAAP1.json" desired_format = RPFAAP1 tables = [3] input_coords = TDRP_COORDS elif document_type == "RPFAA Building P2": document = "RPFAAP2.json" desired_format = RPFAAP2 tables = [] input_coords = TDRP_COORDS elif document_type == "TDRP": document = "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) if context == "": sequence_1 = (document_prefix, pair_content, desired_format) else: sequence_1 = (document_prefix, pair_content, desired_format, additional_prefix, context) content_1 = (" ".join(sequence_1)) logging.info(content_1) completion_1 = openai.ChatCompletion.create( model=gpt_model, user="1", messages=[ {"role": "system", "content": prompt}, {"role": "user", "content": content_1} ] ) logging.info(completion_1.choices[0].message.content) input_string = remove_na(completion_1.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