Sage
commited on
Commit
•
ca92e41
1
Parent(s):
18626e5
added verifier
Browse files- ai_functions.py +52 -17
- app.py +2 -1
- ocr_functions.py +5 -1
ai_functions.py
CHANGED
@@ -3,6 +3,7 @@ import json
|
|
3 |
import logging
|
4 |
import sys
|
5 |
import os
|
|
|
6 |
from settings import gpt_model, RPFAAP2, RPFAAP1, TDRP, TDRP_COORDS
|
7 |
from helpers import remove_na, filter_tables, merge_strings, Logger
|
8 |
|
@@ -17,8 +18,8 @@ def chat_gpt_image(content, context):
|
|
17 |
if context == "":
|
18 |
sequence = (document, content)
|
19 |
else:
|
20 |
-
|
21 |
-
additional = (" ".join(
|
22 |
sequence = (additional, content)
|
23 |
|
24 |
final_content = (" ".join(sequence))
|
@@ -42,10 +43,11 @@ def chat_gpt_document(content, document_type, context):
|
|
42 |
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:"
|
43 |
additional_prefix = "Additionally the text"
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
if document_type == "RPFAA Building P1":
|
51 |
document = "./templates/RPFAAP1.json"
|
@@ -66,26 +68,33 @@ def chat_gpt_document(content, document_type, context):
|
|
66 |
property_info = ["Please Select a Document Type"]
|
67 |
return json.dumps(property_info, indent=4)
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
if context == "":
|
70 |
-
|
71 |
else:
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
logging.info(
|
76 |
-
print(
|
77 |
|
78 |
-
|
79 |
model=gpt_model,
|
80 |
user="1",
|
81 |
messages=[
|
82 |
{"role": "system", "content": prompt},
|
83 |
-
{"role": "user", "content":
|
84 |
]
|
85 |
)
|
86 |
-
logging.info(
|
87 |
-
print(
|
88 |
-
input_string = remove_na(
|
89 |
input_string = merge_strings(input_string,input_coords,document_content)
|
90 |
|
91 |
with open(document) as f:
|
@@ -110,4 +119,30 @@ def chat_gpt_document(content, document_type, context):
|
|
110 |
table_dict = json.loads(table_string)
|
111 |
property_info.update(table_dict)
|
112 |
json_string = json.dumps(property_info, indent=4)
|
113 |
-
return json_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import logging
|
4 |
import sys
|
5 |
import os
|
6 |
+
import re
|
7 |
from settings import gpt_model, RPFAAP2, RPFAAP1, TDRP, TDRP_COORDS
|
8 |
from helpers import remove_na, filter_tables, merge_strings, Logger
|
9 |
|
|
|
18 |
if context == "":
|
19 |
sequence = (document, content)
|
20 |
else:
|
21 |
+
sequence = (prefix, context)
|
22 |
+
additional = (" ".join(sequence))
|
23 |
sequence = (additional, content)
|
24 |
|
25 |
final_content = (" ".join(sequence))
|
|
|
43 |
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:"
|
44 |
additional_prefix = "Additionally the text"
|
45 |
|
46 |
+
text_content = content[0]
|
47 |
+
pair_content = content[1]
|
48 |
+
document_content = content[2]
|
49 |
+
table_content = content[3]
|
50 |
+
content_name = content[4]
|
51 |
|
52 |
if document_type == "RPFAA Building P1":
|
53 |
document = "./templates/RPFAAP1.json"
|
|
|
68 |
property_info = ["Please Select a Document Type"]
|
69 |
return json.dumps(property_info, indent=4)
|
70 |
|
71 |
+
validity = check_valid(text_content, document_type)
|
72 |
+
logging.info(validity)
|
73 |
+
print(validity)
|
74 |
+
|
75 |
+
if validity < 50:
|
76 |
+
return f"Document type and document provided do not match for {content_name}"
|
77 |
+
|
78 |
if context == "":
|
79 |
+
sequence = (document_prefix, pair_content, desired_format)
|
80 |
else:
|
81 |
+
sequence = (document_prefix, pair_content, desired_format, additional_prefix, context)
|
82 |
|
83 |
+
gpt_content = (" ".join(sequence))
|
84 |
+
logging.info(gpt_content)
|
85 |
+
print(gpt_content)
|
86 |
|
87 |
+
completion = openai.ChatCompletion.create(
|
88 |
model=gpt_model,
|
89 |
user="1",
|
90 |
messages=[
|
91 |
{"role": "system", "content": prompt},
|
92 |
+
{"role": "user", "content": gpt_content}
|
93 |
]
|
94 |
)
|
95 |
+
logging.info(completion.choices[0].message.content)
|
96 |
+
print(completion.choices[0].message.content)
|
97 |
+
input_string = remove_na(completion.choices[0].message.content)
|
98 |
input_string = merge_strings(input_string,input_coords,document_content)
|
99 |
|
100 |
with open(document) as f:
|
|
|
119 |
table_dict = json.loads(table_string)
|
120 |
property_info.update(table_dict)
|
121 |
json_string = json.dumps(property_info, indent=4)
|
122 |
+
return json_string
|
123 |
+
|
124 |
+
def check_valid(text_content, document_type):
|
125 |
+
openai.api_key = os.environ['GPT_API_KEY']
|
126 |
+
|
127 |
+
if document_type == "RPFAA Building P1" or document_type == "RPFAA Building P2":
|
128 |
+
document = "Real Property Field Appraisal and Assessment Sheet for Buildings and Other Structures"
|
129 |
+
elif document_type == "TDRP":
|
130 |
+
document = "Tax Declaration Form for Real Property"
|
131 |
+
else:
|
132 |
+
document = "None"
|
133 |
+
|
134 |
+
prompt = "You are an expert at identifying Document Types based on their content."
|
135 |
+
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}"
|
136 |
+
completion = openai.ChatCompletion.create(
|
137 |
+
model=gpt_model,
|
138 |
+
user="1",
|
139 |
+
messages=[
|
140 |
+
{"role": "system", "content": prompt},
|
141 |
+
{"role": "user", "content": content}
|
142 |
+
]
|
143 |
+
)
|
144 |
+
match = re.search(r'\d+', completion.choices[0].message.content)
|
145 |
+
if match:
|
146 |
+
return int(match.group())
|
147 |
+
else:
|
148 |
+
return 100
|
app.py
CHANGED
@@ -5,7 +5,6 @@ import time
|
|
5 |
import logging
|
6 |
import requests
|
7 |
import sys
|
8 |
-
from tqdm import tqdm
|
9 |
from azure.core.exceptions import HttpResponseError
|
10 |
from ocr_functions import detect_document, detect_image
|
11 |
from ai_functions import chat_gpt_document, chat_gpt_image
|
@@ -162,10 +161,12 @@ def document(content, document_type, context):
|
|
162 |
time.sleep(timeout)
|
163 |
i += 1
|
164 |
else:
|
|
|
165 |
break
|
166 |
except Exception as e: # catch any other exceptions
|
167 |
logging.error(f'Unexpected error {e}')
|
168 |
print(f'Unexpected error {e}')
|
|
|
169 |
break
|
170 |
return data
|
171 |
|
|
|
5 |
import logging
|
6 |
import requests
|
7 |
import sys
|
|
|
8 |
from azure.core.exceptions import HttpResponseError
|
9 |
from ocr_functions import detect_document, detect_image
|
10 |
from ai_functions import chat_gpt_document, chat_gpt_image
|
|
|
161 |
time.sleep(timeout)
|
162 |
i += 1
|
163 |
else:
|
164 |
+
data = f"Error: {e}, Please check document configuration or document type"
|
165 |
break
|
166 |
except Exception as e: # catch any other exceptions
|
167 |
logging.error(f'Unexpected error {e}')
|
168 |
print(f'Unexpected error {e}')
|
169 |
+
data = f"Error: {e}, Please check document configuration or document type"
|
170 |
break
|
171 |
return data
|
172 |
|
ocr_functions.py
CHANGED
@@ -23,6 +23,10 @@ def detect_document(content):
|
|
23 |
)
|
24 |
result = poller.result()
|
25 |
|
|
|
|
|
|
|
|
|
26 |
pair_content = "----Key-value pairs found in document----\n"
|
27 |
for kv_pair in result.key_value_pairs:
|
28 |
if kv_pair.key and kv_pair.value:
|
@@ -58,7 +62,7 @@ def detect_document(content):
|
|
58 |
name = content.name.split('\\')[-1]
|
59 |
name = name.split("/")[-1]
|
60 |
name = name.split('.')[0]
|
61 |
-
return (pair_content, document_content, table_content, name)
|
62 |
|
63 |
def detect_image(content):
|
64 |
document_analysis_client = DocumentAnalysisClient(
|
|
|
23 |
)
|
24 |
result = poller.result()
|
25 |
|
26 |
+
text_content = result.content
|
27 |
+
logging.info(result.content)
|
28 |
+
print(text_content)
|
29 |
+
|
30 |
pair_content = "----Key-value pairs found in document----\n"
|
31 |
for kv_pair in result.key_value_pairs:
|
32 |
if kv_pair.key and kv_pair.value:
|
|
|
62 |
name = content.name.split('\\')[-1]
|
63 |
name = name.split("/")[-1]
|
64 |
name = name.split('.')[0]
|
65 |
+
return (text_content, pair_content, document_content, table_content, name)
|
66 |
|
67 |
def detect_image(content):
|
68 |
document_analysis_client = DocumentAnalysisClient(
|