File size: 4,469 Bytes
3dd785b
 
 
18626e5
3dd785b
18626e5
 
 
3dd785b
18626e5
3dd785b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18626e5
3dd785b
 
 
 
 
 
 
 
 
18626e5
3dd785b
 
 
 
 
 
 
 
 
 
 
 
 
 
18626e5
3dd785b
 
 
 
18626e5
3dd785b
 
 
 
18626e5
3dd785b
 
 
 
 
 
 
 
 
 
 
 
 
 
18626e5
3dd785b
 
 
 
 
 
 
 
 
 
18626e5
3dd785b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import json
import logging
import sys
import os
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_1 = (prefix, context)
        additional = (" ".join(sequence_1))
        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"

    pair_content = content[0]
    document_content = content[1]
    table_content = content[2]
    content_name = content[3]

    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)
    
    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)
    print(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)
    print(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