Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up function update_new_output by 922% in src/backend/base/langflow/initial_setup/setup.py #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 40 additions & 51 deletions src/backend/base/langflow/initial_setup/setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import copy
import json
import shutil
from collections import defaultdict
Expand Down Expand Up @@ -158,16 +157,13 @@ def scape_json_parse(json_string: str) -> dict:


def update_new_output(data):
nodes = copy.deepcopy(data["nodes"])
edges = copy.deepcopy(data["edges"])

for edge in edges:
nodes_dict = {node["id"]: node for node in data["nodes"]}
for edge in data["edges"]:
if "sourceHandle" in edge and "targetHandle" in edge:
new_source_handle = scape_json_parse(edge["sourceHandle"])
new_target_handle = scape_json_parse(edge["targetHandle"])
id_ = new_source_handle["id"]
source_node_index = next((index for (index, d) in enumerate(nodes) if d["id"] == id_), -1)
source_node = nodes[source_node_index] if source_node_index != -1 else None
source_node = nodes_dict.get(id_)

if "baseClasses" in new_source_handle:
if "output_types" not in new_source_handle:
Expand All @@ -191,54 +187,47 @@ def update_new_output(data):
new_source_handle["name"] = " | ".join(new_source_handle["output_types"])
new_source_handle["output_types"] = [selected] if selected else []

if source_node and not source_node["data"]["node"].get("outputs"):
if "outputs" not in source_node["data"]["node"]:
source_node["data"]["node"]["outputs"] = []
types = source_node["data"]["node"].get(
"output_types", source_node["data"]["node"].get("base_classes", [])
)
if not any(output.get("selected") == selected for output in source_node["data"]["node"]["outputs"]):
source_node["data"]["node"]["outputs"].append(
{
"types": types,
"selected": selected,
"name": " | ".join(types),
"display_name": " | ".join(types),
}
)
deduplicated_outputs = []
if source_node is None:
source_node = {"data": {"node": {"outputs": []}}}

for output in source_node["data"]["node"]["outputs"]:
if output["name"] not in [d["name"] for d in deduplicated_outputs]:
deduplicated_outputs.append(output)
source_node["data"]["node"]["outputs"] = deduplicated_outputs
if source_node:
node_data = source_node["data"]["node"]
if not node_data.get("outputs"):
if "outputs" not in node_data:
node_data["outputs"] = []
types = node_data.get("output_types", node_data.get("base_classes", []))
if not any(output.get("selected") == selected for output in node_data["outputs"]):
node_data["outputs"].append(
{
"types": types,
"selected": selected,
"name": " | ".join(types),
"display_name": " | ".join(types),
}
)
deduplicated_outputs = []
outputs_set = set()
for output in node_data["outputs"]:
name = output["name"]
if name not in outputs_set:
deduplicated_outputs.append(output)
outputs_set.add(name)
node_data["outputs"] = deduplicated_outputs

edge["sourceHandle"] = escape_json_dump(new_source_handle)
edge["data"]["sourceHandle"] = new_source_handle
edge["data"]["targetHandle"] = new_target_handle
# The above sets the edges but some of the sourceHandles do not have valid name
# which can be found in the nodes. We need to update the sourceHandle with the
# name from node['data']['node']['outputs']
for node in nodes:
if "outputs" in node["data"]["node"]:
for output in node["data"]["node"]["outputs"]:
for edge in edges:
if node["id"] != edge["source"] or output.get("method") is None:
continue
source_handle = scape_json_parse(edge["sourceHandle"])
if source_handle["output_types"] == output.get("types") and source_handle["name"] != output["name"]:
source_handle["name"] = output["name"]
if isinstance(source_handle, str):
source_handle = scape_json_parse(source_handle)
edge["sourceHandle"] = escape_json_dump(source_handle)
edge["data"]["sourceHandle"] = source_handle

data_copy = copy.deepcopy(data)
data_copy["nodes"] = nodes
data_copy["edges"] = edges
return data_copy

for node in data["nodes"]:
node_outputs = node["data"]["node"].get("outputs", [])
for output in node_outputs:
for edge in data["edges"]:
if node["id"] != edge["source"] or output.get("method") is None:
continue
source_handle = scape_json_parse(edge["sourceHandle"])
if source_handle["output_types"] == output.get("types") and source_handle["name"] != output["name"]:
source_handle["name"] = output["name"]
edge["sourceHandle"] = escape_json_dump(source_handle)
edge["data"]["sourceHandle"] = source_handle

return data


def update_edges_with_latest_component_versions(project_data):
Expand Down