File size: 10,575 Bytes
77df4ca
 
a5a59c1
 
 
 
 
77df4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5a59c1
77df4ca
 
a5a59c1
5ea4cc2
77df4ca
a5a59c1
 
 
 
 
 
 
 
5ea4cc2
 
77df4ca
a5a59c1
77df4ca
 
 
a5a59c1
77df4ca
68f056c
77df4ca
 
 
 
68f056c
77df4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68f056c
77df4ca
68f056c
77df4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68f056c
77df4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5a59c1
77df4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5a59c1
 
77df4ca
 
 
a5a59c1
de92875
77df4ca
 
 
 
 
 
5ea4cc2
 
 
 
68f056c
 
 
 
 
 
77df4ca
 
5ea4cc2
 
 
77df4ca
 
 
 
de92875
77df4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c7cd02
77df4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c7cd02
77df4ca
 
 
 
a5a59c1
68f056c
77df4ca
 
a5a59c1
77df4ca
a5a59c1
 
de92875
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import time
import io
import gradio as gr
import cv2
import base64
import openai

from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.schema import StrOutputParser
from PIL import Image

global_dict = {}


def failure():
    raise gr.Error("This should fail!")

def validate_api_key(api_key):
    client = openai.OpenAI(api_key=api_key)

    try:
        # Make your OpenAI API request here
        response = client.completions.create(
            prompt="Hello world",
            model="gpt-3.5-turbo-instruct"
        )
    except openai.RateLimitError as e:
        # Handle rate limit error (we recommend using exponential backoff)
        print(f"OpenAI API request exceeded rate limit: {e}")
        response = None
        pass
    except openai.APIConnectionError as e:
        # Handle connection error here
        print(f"Failed to connect to OpenAI API: {e}")
        response = None
        pass
    except openai.APIError as e:
        # Handle API error here, e.g. retry or log
        print(f"OpenAI API returned an API Error: {e}")
        response = None
        pass

    if response:
        return True
    else:
        raise gr.Error(f"OpenAI API returned an API Error")


def _process_video(image_file):
    # Read and process the video file
    video = cv2.VideoCapture(image_file.name)

    base64Frames = []
    while video.isOpened():
        success, frame = video.read()
        if not success:
            break
        _, buffer = cv2.imencode(".jpg", frame)
        base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
    video.release()
    if len(base64Frames) > 700:
        raise gr.Warning(f"Video's play time is too long. (>20s)")
    print(len(base64Frames), "frames read.")

    if not base64Frames:
        raise gr.Error(f"Cannot open the video.")
    return base64Frames


def _make_video_batch(image_file, batch_size, total_batch_percent):

    frames = _process_video(image_file)

    TOTAL_FRAME_COUNT = len(frames)
    BATCH_SIZE = int(batch_size)
    TOTAL_BATCH_SIZE = int(TOTAL_FRAME_COUNT * total_batch_percent / 100)
    BATCH_STEP = int(TOTAL_FRAME_COUNT / TOTAL_BATCH_SIZE)
    
    base64FramesBatch = []

    for idx in range(0, TOTAL_FRAME_COUNT, BATCH_STEP * BATCH_SIZE):
        # print(f'## {idx}')
        temp = []
        for i in range(BATCH_SIZE):
            # print(f'# {idx + BATCH_STEP * i}')
            if (idx + BATCH_STEP * i) < TOTAL_FRAME_COUNT:
                temp.append(frames[idx + BATCH_STEP * i])
            else:
                continue
        base64FramesBatch.append(temp)
    
    for idx, batch in enumerate(base64FramesBatch):
        # assert len(batch) <= BATCH_SIZE
        print(f'##{idx} - batch_size: {len(batch)}')

    global_dict.setdefault('batched_frames', base64FramesBatch)
    return base64FramesBatch


def show_batches(image_file, batch_size, total_batch_percent):
    
    batched_frames = _make_video_batch(image_file, batch_size, total_batch_percent)
    
    images = []
    for i, l in enumerate(batched_frames):
        print(f"#### Batch_{i+1}")
        for j, img in enumerate(l):
            print(f'## Image_{j+1}')
            image_bytes = base64.b64decode(img.encode("utf-8"))
            # Convert the bytes to a stream (file-like object)
            image_stream = io.BytesIO(image_bytes)
            # Open the image as a PIL image
            image = Image.open(image_stream)
            images.append((image, f"batch {i+1}"))
        print("-"*100)
    
    return images


def call_gpt_vision(api_key, instruction):
    frames = global_dict.get('batched_frames')
    openai.api_key = api_key

    full_result = []

    for idx, batch in enumerate(frames):
        PROMPT_MESSAGES = [
            {
                "role": "system",
                "content": "You will evaluate the behavior of the person in the sequences of images. They show discrete parts of the whole continuous behavior. You should only evaluate the parts you can rate based on the given images. Remember, you're evaluating the given parts to evaluate the whole continuous behavior, and you'll connect them later to evaluate the whole. Never add your own judgment. Evlaute only in the contents of images themselves. If you can't evaluate it, just answer '(Unevaluable)'"
            },
            {
                "role": "user",
                "content": [
                    "Evaluate the behavior's actions based on the <CRITERIA> provided.\n\n" + instruction,
                    *map(lambda x: {"image": x, "resize": 300}, batch),
                ],
            },
        ]
        
        params = {
        "model": "gpt-4-vision-preview",
        "messages": PROMPT_MESSAGES,
        "max_tokens": 1024,
        }

        try:
            result = openai.chat.completions.create(**params)
            print(result.choices[0].message.content)
            full_result.append(result)
        except Exception as e:
            print(f"Error: {e}")
            yield f'### BATCH_{idx+1}\n' + "-"*50 + "\n" + f"Error: {e}" +  "\n" + "-"*50
        
        if 'full_result' not in global_dict:
            global_dict.setdefault('full_result', full_result)
        else:
            global_dict['full_result'] = full_result
        
        print(f'### BATCH_{idx+1}')
        print('-'*100)
        time.sleep(2)

        yield f'### BATCH_{idx+1}\n' + "-"*50 + "\n" + result.choices[0].message.content +  "\n" + "-"*50


def get_full_result():
    full_result = global_dict.get('full_result')
    
    result_text = ""

    for idx, res in enumerate(full_result):
        result_text += f'<Evaluation_{idx+1}>\n'
        result_text += res.choices[0].message.content
        result_text += "\n"
        result_text += "-"*5
        result_text += "\n"
    
    global_dict.setdefault('result_text', result_text)

    return result_text


def get_final_anser(api_key, result_text):
    chain = ChatOpenAI(model="gpt-4", max_tokens=1024, temperature=0, api_key=api_key)
    prompt = PromptTemplate.from_template(
    """
    You see the following list of texts that evaluate forward roll:
    {evals}
    Write an full text that synthesizes and summarizes the contents of all the text above.
    Each evaluates a specific part, and you should combine them based on what was evaluated in each part.
    The way to combine them is 'or', not 'and', which means you only need to evaluate the parts of a post that are rated based on that.
    Concatenate based on what was evaluated, if anything.

    Example:
    an overview of evaluations
    1. Specific assessments for each item
    2.
    3.
    ....
    Overall opinion

    Total score : 1~10 / 10

    Output:
    """
    )
    runnable = prompt | chain | StrOutputParser()

    final_eval = runnable.invoke({"evals": result_text})
    return final_eval


# Define the Gradio app
def main():
    with gr.Blocks() as demo:
        gr.Markdown("# GPT-4 Vision for Evaluation")
        gr.Markdown("## 1st STEP. Make Batched Snapshots")
        with gr.Row():
            with gr.Column(scale=1):
                api_key_input = gr.Textbox(
                    label="Enter your OpenAI API Key",
                    info="Your API Key must be allowed to use GPT-4 Vision",
                    placeholder="sk-*********...",
                    lines=1
                )
                video_upload = gr.File(
                    label="Upload your video (under 10 second video is the best..!)",
                    file_types=["video"],
                )
                batch_size = gr.Number(
                    label="Number of images in one batch",
                    value=2,
                    minimum=2,
                    maximum=5
                )
                total_batch_percent = gr.Number(
                    label="Percentage(%) of batched image frames to total frames",
                    value=5,
                    minimum=5,
                    maximum=20,
                    step=5
                )
                process_button = gr.Button("Process")
            
            with gr.Column(scale=1):
                gallery = gr.Gallery(
                    label="Batched Snapshots of Video",
                    columns=[5],
                    rows=[1],
                    object_fit="contain",
                    height="auto"
                )
        gr.Markdown("## 2nd STEP. Set Evaluation Criteria")
        with gr.Row():
            with gr.Column(scale=1):
                instruction_input = gr.Textbox(
                    label="Evaluation Criteria",
                    info="Enter your evaluation criteria here...",
                    placeholder="<CRITERIA>\nThe correct way to do a forward roll is as follows:\n1. From standing, bend your knees and straighten your arms in front of you.\n2. Place your hands on the floor, shoulder width apart with fingers pointing forward and your chin on your chest.\n3. Rock forward, straighten legs and transfer body weight onto shoulders.\n4. Rock forward on a rounded back placing both feet on the floor.\n5. Stand using arms for balance, without hands touching the floor.",
                    lines=7)
                submit_button = gr.Button("Evaluate")

            with gr.Column(scale=1):
                output_box = gr.Textbox(
                    label="Batched Generated Response...(Streaming)",
                    lines=10,
                    interactive=False
                )
        gr.Markdown("## 3rd STEP. Summarize and Get Result")
        with gr.Row():
            with gr.Column(scale=1):
                output_box_fin = gr.Textbox(
                    label="FULL Response",
                    info="You can edit partial evaluation in here...",
                    lines=10,
                    interactive=True)
                submit_button_2 = gr.Button("Summarize")

            with gr.Column(scale=1):
                output_box_fin_fin = gr.Textbox(label="FINAL EVALUATION", lines=10, interactive=True)


        process_button.click(fn=validate_api_key, inputs=api_key_input, outputs=None).success(fn=show_batches, inputs=[video_upload, batch_size, total_batch_percent], outputs=gallery)
        submit_button.click(fn=call_gpt_vision, inputs=[api_key_input, instruction_input], outputs=output_box).then(get_full_result, None, output_box_fin)
        submit_button_2.click(fn=get_final_anser, inputs=[api_key_input, output_box_fin], outputs=output_box_fin_fin)

    demo.launch()

if __name__ == "__main__":
    main()