Clean Versions
Browse files- InterpreterAtomicFlow.py +16 -20
- InterpreterAtomicFlow.yaml +2 -1
- run.py +1 -0
InterpreterAtomicFlow.py
CHANGED
|
@@ -42,26 +42,13 @@ class InterpreterAtomicFlow(AtomicFlow):
|
|
| 42 |
is not compatible with that of the current version of aiflows(v.0.1.7).
|
| 43 |
"""
|
| 44 |
def __init__(self,
|
| 45 |
-
max_output=2000,
|
| 46 |
**kwargs):
|
| 47 |
super().__init__(**kwargs)
|
| 48 |
-
self.max_output = max_output
|
| 49 |
self._code_interpreters = {}
|
| 50 |
|
| 51 |
-
@classmethod
|
| 52 |
-
def instantiate_from_config(cls, config):
|
| 53 |
-
flow_config = deepcopy(config)
|
| 54 |
-
|
| 55 |
-
kwargs = {"flow_config": flow_config}
|
| 56 |
-
|
| 57 |
-
# ~~~ Instantiate flow ~~~
|
| 58 |
-
return cls(**kwargs)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
def set_up_flow_state(self):
|
| 62 |
-
"""
|
| 63 |
-
class-specific flow state: language and code,
|
| 64 |
-
which describes the programming language and the code to run.
|
| 65 |
"""
|
| 66 |
super().set_up_flow_state()
|
| 67 |
self.flow_state["language"] = None
|
|
@@ -73,13 +60,16 @@ class InterpreterAtomicFlow(AtomicFlow):
|
|
| 73 |
"""
|
| 74 |
updates the language and code passed from _process_input_data
|
| 75 |
to the flow state
|
|
|
|
|
|
|
| 76 |
"""
|
| 77 |
self.flow_state["language"] = language
|
| 78 |
self.flow_state["code"] = code
|
| 79 |
|
| 80 |
def _check_input(self, input_data: Dict[str, Any]):
|
| 81 |
-
"""
|
| 82 |
-
|
|
|
|
| 83 |
"""
|
| 84 |
# ~~~ Sanity check of input_data ~~~
|
| 85 |
assert "language" in input_data, "attribute 'language' not in input data."
|
|
@@ -87,8 +77,9 @@ class InterpreterAtomicFlow(AtomicFlow):
|
|
| 87 |
|
| 88 |
|
| 89 |
def _process_input_data(self, input_data: Dict[str, Any]):
|
| 90 |
-
"""
|
| 91 |
-
|
|
|
|
| 92 |
"""
|
| 93 |
# code in Jupyter notebook that starts with '!' is actually shell command.
|
| 94 |
if input_data["language"] == "python" and input_data["code"].startswith("!"):
|
|
@@ -109,6 +100,8 @@ class InterpreterAtomicFlow(AtomicFlow):
|
|
| 109 |
)
|
| 110 |
|
| 111 |
def _call(self):
|
|
|
|
|
|
|
| 112 |
output = ""
|
| 113 |
try:
|
| 114 |
code_interpreter = self._code_interpreters[self.flow_state["language"]]
|
|
@@ -127,7 +120,10 @@ class InterpreterAtomicFlow(AtomicFlow):
|
|
| 127 |
def run(
|
| 128 |
self,
|
| 129 |
input_message: FlowMessage):
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
input_data = input_message.data
|
| 132 |
self._check_input(input_data)
|
| 133 |
self._process_input_data(input_data)
|
|
|
|
| 42 |
is not compatible with that of the current version of aiflows(v.0.1.7).
|
| 43 |
"""
|
| 44 |
def __init__(self,
|
|
|
|
| 45 |
**kwargs):
|
| 46 |
super().__init__(**kwargs)
|
| 47 |
+
self.max_output = self.flow_config["max_output"]
|
| 48 |
self._code_interpreters = {}
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def set_up_flow_state(self):
|
| 51 |
+
""" class-specific flow state: language and code, which describes the programming language and the code to run.
|
|
|
|
|
|
|
| 52 |
"""
|
| 53 |
super().set_up_flow_state()
|
| 54 |
self.flow_state["language"] = None
|
|
|
|
| 60 |
"""
|
| 61 |
updates the language and code passed from _process_input_data
|
| 62 |
to the flow state
|
| 63 |
+
:param language: the programming language
|
| 64 |
+
:param code: the code to run
|
| 65 |
"""
|
| 66 |
self.flow_state["language"] = language
|
| 67 |
self.flow_state["code"] = code
|
| 68 |
|
| 69 |
def _check_input(self, input_data: Dict[str, Any]):
|
| 70 |
+
""" Sanity check of input data
|
| 71 |
+
:param input_data: input data
|
| 72 |
+
:type input_data: Dict[str, Any]
|
| 73 |
"""
|
| 74 |
# ~~~ Sanity check of input_data ~~~
|
| 75 |
assert "language" in input_data, "attribute 'language' not in input data."
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
def _process_input_data(self, input_data: Dict[str, Any]):
|
| 80 |
+
""" Allocate interpreter if any, pass input data into flow state
|
| 81 |
+
:param input_data: input data
|
| 82 |
+
:type input_data: Dict[str, Any]
|
| 83 |
"""
|
| 84 |
# code in Jupyter notebook that starts with '!' is actually shell command.
|
| 85 |
if input_data["language"] == "python" and input_data["code"].startswith("!"):
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
def _call(self):
|
| 103 |
+
""" This method runs the code interpreter and returns the output. (runs the code interpreter and returns the output.)
|
| 104 |
+
"""
|
| 105 |
output = ""
|
| 106 |
try:
|
| 107 |
code_interpreter = self._code_interpreters[self.flow_state["language"]]
|
|
|
|
| 120 |
def run(
|
| 121 |
self,
|
| 122 |
input_message: FlowMessage):
|
| 123 |
+
""" Run the code interpreter and return the output.
|
| 124 |
+
:param input_message: The input message of the flow.
|
| 125 |
+
:type input_message: FlowMessage
|
| 126 |
+
"""
|
| 127 |
input_data = input_message.data
|
| 128 |
self._check_input(input_data)
|
| 129 |
self._process_input_data(input_data)
|
InterpreterAtomicFlow.yaml
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
name: "InterpreterAtomicFlow"
|
| 2 |
description: "A flow that compiles and runs codes"
|
| 3 |
-
_target_: flow_modules.aiflows.InterpreterFlowModule.InterpreterAtomicFlow.instantiate_from_default_config
|
|
|
|
|
|
| 1 |
name: "InterpreterAtomicFlow"
|
| 2 |
description: "A flow that compiles and runs codes"
|
| 3 |
+
_target_: flow_modules.aiflows.InterpreterFlowModule.InterpreterAtomicFlow.instantiate_from_default_config
|
| 4 |
+
max_output: 2000
|
run.py
CHANGED
|
@@ -59,6 +59,7 @@ if __name__ == "__main__":
|
|
| 59 |
cl=cl,
|
| 60 |
flow_endpoint="InterpreterAtomicFlow",
|
| 61 |
user_id="local",
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
#6. ~~~ Get the data ~~~
|
|
|
|
| 59 |
cl=cl,
|
| 60 |
flow_endpoint="InterpreterAtomicFlow",
|
| 61 |
user_id="local",
|
| 62 |
+
config_overrides= cfg
|
| 63 |
)
|
| 64 |
|
| 65 |
#6. ~~~ Get the data ~~~
|