File size: 5,793 Bytes
3dd785b 18626e5 3dd785b ca92e41 18626e5 3dd785b 18626e5 3dd785b ca92e41 3dd785b 18626e5 3dd785b 18626e5 3dd785b ca92e41 3dd785b 18626e5 3dd785b 18626e5 3dd785b 18626e5 3dd785b ca92e41 3dd785b ca92e41 3dd785b ca92e41 3dd785b ca92e41 3dd785b ca92e41 3dd785b ca92e41 3dd785b ca92e41 3dd785b ca92e41 |
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 |
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 |