File size: 2,717 Bytes
59a7259
da9fcee
decddfb
 
b2010da
 
 
f3dd2c8
4cd6fae
9d7ed12
0fccc5c
4cd6fae
c945f59
 
cd30d51
f8a3ff8
 
 
59a7259
 
aa80c98
59a7259
 
 
 
c62b997
f21ded6
 
c62b997
affcc27
59a7259
 
aa80c98
 
59a7259
 
b2010da
c945f59
e88f8b4
57bf9b3
 
 
 
8c3ddf5
 
c5a577b
 
18410c3
 
 
b2010da
18410c3
 
57bf9b3
18410c3
 
a65f5bc
18410c3
 
 
 
c945f59
 
b2010da
c945f59
 
90c9483
c945f59
 
2d54ed1
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
import base64, cv2
import pandas as pd
from openai import OpenAI

def get_questions(file_path):
    df = pd.read_json(file_path, lines=True)
    
    result=[]
    
    for index, row in df.iterrows():
        result.append([row["Level"], row["Question"], row["file_name"], row["Final answer"]])

    return result

def get_img_b64(file_path):
    with open(file_path, "rb") as file:
        return base64.b64encode(file.read()).decode("utf-8")

def get_imgs_b64(file_path):
    video = cv2.VideoCapture(file_path)
    
    result = []
    
    while video.isOpened():
        success, frame = video.read()

        if not success:
            break
        
        _, buffer = cv2.imencode(".png", frame)
        result.append(base64.b64encode(buffer).decode("utf-8"))

    video.release()
    
    return result

def get_final_answer(model, question, answer):
    prompt_template = """
        You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
        Your final answer must be a number and/or string OR as few words as possible OR a comma-separated list of numbers and/or strings.
        If you are asked for a number, don't use comma to write your number neither use units such as $ or % unless specified otherwise.
        If you are asked for a string, don't use articles, neither abbreviations, and write the digits in plain text unless specified otherwise.
        If you are asked for a comma-separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
        If the final answer is a number, use a number not a word.
        If the final answer is a word, start with an uppercase character.
        If the final answer is a comma-separated list of numbers, use a space character after each comma.
        If the final answer is a comma-separated list of strings, start with a lowercase character.

        **Question:** """ + question + """
        
        **Initial answer:** """ + answer + """
        
        **Example 1:** How many 'r's are in strawberry? 3
        **Example 2:** What is the opposite of black? White
        **Example 3:** What is the biggest city in California? Los Angeles
        **Example 4:** What is the superlative of good? Best
        **Example 5:** What are the first 10 numbers in the Fibonacci sequence? 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
        
        **Final answer:** 

        """

    client = OpenAI()
    
    completion = client.chat.completions.create(
        messages=[{"role": "user", "content": [{"type": "text", "text": prompt_template}]}],
        model=model
    )

    return completion.choices[0].message.content