File size: 3,172 Bytes
12d535c
 
 
 
 
 
 
c94c99e
12d535c
 
 
 
 
 
 
 
 
 
 
 
c94c99e
12d535c
 
 
 
c94c99e
12d535c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c94c99e
12d535c
 
 
 
 
 
c94c99e
 
 
 
 
 
 
 
12d535c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from openai import OpenAI


def ask_gpt(prompt, raw):
    client = OpenAI()

    prompt = (
        "this is a python code :\n"
        + "```python\n"
        + raw
        + "```\n"
        + prompt
        + "Format your response by: Showing the whole modified code. No explanation is required. Only code."
    )

    response = client.chat.completions.create(
        model="gpt-4-1106-preview",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt},
        ],
    )

    answer = response.choices[0].message.content
    return prompt, answer


def extract_command(gptCommand):
    blocks = []
    temp = ""
    writing = False

    for line in gptCommand.splitlines():
        if line == "```":
            writing = False
            blocks.append(temp)
            temp = ""

        if writing:
            temp += line
            temp += "\n"

        if line == "```python":
            writing = True

    return blocks


def save_as(content, path):
    # use at the end of replace_2 as save_as(end_result, "file_path")
    with open(path, "w") as file:
        file.write(content)


import pyarrow as pa

from dora import DoraStatus


class Operator:
    """
    Infering object from images
    """

    def on_event(
        self,
        dora_event,
        send_output,
    ) -> DoraStatus:
        # todo: remove this
        return DoraStatus.CONTINUE

        if dora_event["type"] == "INPUT":
            input = dora_event["value"][0].as_py()
            with open(input["path"], "r", encoding="utf8") as f:
                raw = f.read()
            print("--- Asking chatGPT ", flush=True)
            prompt, response = ask_gpt(input["query"], raw)
            blocks = extract_command(response)
            print(response, flush=True)
            print(blocks[0], input["path"], flush=True)
            send_output(
                "output_file",
                pa.array(
                    [
                        {
                            "raw": blocks[0],
                            "path": input["path"],
                            "response": response,
                            "prompt": prompt,
                        }
                    ]
                ),
                dora_event["metadata"],
            )

        return DoraStatus.CONTINUE


if __name__ == "__main__":
    op = Operator()

    # Path to the current file
    current_file_path = __file__

    # Directory of the current file
    current_directory = os.path.dirname(current_file_path)

    path = current_directory + "/planning_op.py"
    with open(path, "r", encoding="utf8") as f:
        raw = f.read()

    op.on_event(
        {
            "type": "INPUT",
            "id": "tick",
            "value": pa.array(
                [
                    {
                        "raw": raw,
                        "path": path,
                        "query": "Can you change the RGB to change according to the object distances",
                    }
                ]
            ),
            "metadata": [],
        },
        print,
    )