Script Nodes
Script Nodes allow users to integrate custom Python scripts into aiXplain pipelines. They are particularly useful when:
Modifying or transforming data between pipeline nodes. Implementing custom logic not covered by built-in nodes. Formatting outputs to meet specific requirements. This guide demonstrates how to create a pipeline with a Script Node, modify the output of a speech recognition model, and execute a custom Python script.
Creating a Pipeline with a Script Node
A Script Node allows users to insert a Python script within an aiXplain pipeline. Below, we create a pipeline with the following flow:
input_node -> asset_node (speech recognition) -> script_node (custom processing) -> output_node
Initialize the Pipeline
from aixplain.factories import PipelineFactory
pipeline = PipelineFactory.init("Example Scripted Pipeline")
input_node = pipeline.input()
asset_node = pipeline.speech_recognition("60ddefab8d38c51c5885ee38")
Writing a Custom Script for the Script Node
In this example, we write a simple script that modifies the output of the speech recognition node by formatting it into JSON.
with open("example_script.py", "w") as f:
f.write("""
def main(speakers):
# build the response
response = []
for i, speaker in enumerate(speakers):
print(f"Processing speaker at index={i}")
data = speaker["data"]
# convert data into a json formatted data
json_data = f'{{"url": "{data}"}}'
response.append(
{
"index": i,
"success": True,
"input_type": "text",
"is_url": False,
"details": {},
"data": json_data,
"input": json_data,
}
)
return response
""")
Adding the Script Node to the Pipeline
Once the script is ready, we integrate it into the pipeline using a Script Node.
script_node = pipeline.script(script_path="example_script.py")
The script above accepts an input parameter called speakers
and an output parameter called data
, we need to create them in our node specs accordingly.
script_node.inputs.create_param("speakers", data_type="text")
script_node.outputs.create_param("data", data_type="text")
Linking Nodes Together
input_node --(input-source_audio)--> asset_node --(data-speakers)--> script_node --(data-output) --> output_node
input_node.outputs.input.link(asset_node.inputs.source_audio)
asset_node.outputs.data.link(script_node.inputs.speakers)
output_node = script_node.use_output("data")
Running the Pipeline
Now that the pipeline is fully configured, save and run it with an audio input.
Save the Pipeline
pipeline.save(save_as_asset=True)
Run the Pipeline
pipeline.run("https://homepage.ntu.edu.tw/~karchung/miniconversations/mc1.mp3", **{ "version": "3.0" })
Delete the Pipeline (Optional)
If the pipeline is no longer needed, it can be deleted.
pipeline.delete()
Script Nodes provide a flexible way to incorporate custom processing within aiXplain pipelines. They allow developers to:
- Modify intermediate outputs.
- Format and structure data before passing it to the next node.
- Implement custom logic within AI-driven workflows.
By leveraging Script Nodes, you can enhance the capabilities of aiXplain pipelines for a variety of use cases.