File size: 3,090 Bytes
f1f2c2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

def output_from_text(text):
    #extract the number in the last line of text
    try:
        lines = text.strip().split("\n")
        lines = list(filter(None, lines))
        last_line = list(filter(None, lines[-1].split(" ")))
        #last_line = [x.lower() for x in last_line]
        answer = []
        for x in last_line:
            word = re.sub(r'[^a-zA-Z]', '', x.lower())
            if word == 'blue' or word == 'green':
                answer.append(word)

        if answer != []:
            return {
                "OUTPUT": answer,
                "ERROR": None
            }
        else:
            return{
                "OUTPUT": None,
                "ERROR": 'Does not give answer in the correct format'
            }
    except Exception as e:
        return {
            "OUTPUT": None,
            "ERROR": str(e)
        }

"""import re
import json
import os

def output_from_text(response):
    # Define a regular expression pattern to match 'Row Number: Color'
    try:
        pattern = r"ANSWER:\s*(.*)"
        
        # Find all matches in the response
        match = re.search(pattern, response)
        
        if match:
            row_colors = match.group(1).strip(" \n\t*").split(" ")

        # strip whitespacce and stars from all elements in the list
            row_colors = [color.strip("*") for color in row_colors]

        # # Convert the matches into a dictionary
        # row_colors = [color for row, color in matches]
            return {
                "OUTPUT":{
                    "Gold_output": row_colors,
                    "num_objects": len(row_colors)
                },
                "ERROR": None
            }
        else:
            return {
                "OUTPUT": None,
                "ERROR": "Output not found"
            }
    except Exception as e:
        return {
            "OUTPUT": None,
            "ERROR": str(e)
        }
    
def gold_to_output(i):
    gold_output = None
    try:
         #get directory of this file
        dir_of_file = os.path.dirname(os.path.realpath(__file__))
        #open the data file
        with open(dir_of_file + "\data.json") as f:
            data = json.load(f)
        row = data[i]
        gold_output = row['Gold_output']
        output_prompt = "ANSWER: "
        for i , col in enumerate(gold_output):
            output_prompt += f"{col} "
    except Exception as e:
        return {
            "OUTPUT": None,
            "ERROR": str(e)
        }
    
    return {
        "OUTPUT": output_prompt,
        "ERROR": None
    }
"""    
# import json
# with open("./answer_vertexai.json", "r") as f:
#     data = json.load(f)
#     # print(data[1])
#     for i in data:
#         output_text = i["gpt_response"]
#         i["Output"] = output_from_text(output_text)["OUTPUT"]
#         i["ERROR"] = output_from_text(output_text)["ERROR"]
#         # remove OUTPU/ key
#         # i.pop("OUTPUT")
    
#     with open("./answer_vertexai.json", "w") as f:
#         json.dump(data, f, indent=4)
#         print("Output saved to answer.json")