langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
34.2k stars 4.13k forks source link

Can't import Spacy in custom components #2952

Closed c0pper closed 2 months ago

c0pper commented 3 months ago

Bug Description

Hi, I was wondering if it's possible at all to import other python libraries such as spacy to build custom components that use them, for example to get the nlp analysis of a text.

So far I have tried the following:

# from langflow.field_typing import Data
from langflow.custom import Component
from langflow.io import MessageTextInput, Output
from langflow.schema import Data
import spacy

print(spacy.__version__)
nlp = spacy.load("en_core_web_sm")

class CustomComponent(Component):
    display_name = "Custom Component"
    description = "Use as a template to create your own component."
    documentation: str = "http://docs.langflow.org/components/custom"
    icon = "custom_components"
    name = "CustomComponent"

    inputs = [
        MessageTextInput(name="input_value", display_name="Input Value", value="Hello, World!"),
    ]

    outputs = [
        Output(display_name="Output", name="output", method="build_output"),
    ]

    def build_output(self) -> Data:
        doc = nlp(self.input_value)
        tokens = []

        for token in doc:
            tokens.append(f"({token.text}, {token.pos_})")
        data = Data(value="; ".join(tokens))
        self.status = data
        return data

but on execution I'm getting

Error Building Component

    Error building Component Custom Component: name 'nlp' is not defined

Reproduction

Create custom component Copy the code I've tried

Expected behavior

Spacy loads and the nlp analysis is provided in output

Who can help?

No response

Operating System

Windows 11

Langflow Version

1.0.12

Python Version

3.11

Screenshot

No response

Flow File

No response

dosubot[bot] commented 3 months ago

To fix the error "name 'nlp' is not defined" when trying to import and use Spacy in your custom component, you need to ensure that the nlp object is properly defined and initialized within the class methods. Here is an updated version of your code:

from langflow.custom import Component
from langflow.io import MessageTextInput, Output
from langflow.schema import Data
import spacy

class CustomComponent(Component):
    display_name = "Custom Component"
    description = "Use as a template to create your own component."
    documentation: str = "http://docs.langflow.org/components/custom"
    icon = "custom_components"
    name = "CustomComponent"

    inputs = [
        MessageTextInput(name="input_value", display_name="Input Value", value="Hello, World!"),
    ]

    outputs = [
        Output(display_name="Output", name="output", method="build_output"),
    ]

    def __init__(self):
        super().__init__()
        self.nlp = spacy.load("en_core_web_sm")

    def build_output(self) -> Data:
        doc = self.nlp(self.input_value)
        tokens = []

        for token in doc:
            tokens.append(f"({token.text}, {token.pos_})")
        data = Data(value="; ".join(tokens))
        self.status = data
        return data

In this updated code, the Spacy model is loaded in the __init__ method of the CustomComponent class, ensuring that the nlp object is available when the build_output method is called. This should resolve the "name 'nlp' is not defined" error [1].

To continue talking to Dosu, mention @dosu.

c0pper commented 3 months ago

That code doesn't work, it won't event let me check and save the component. I just click the button to save and it does nothing.

dosubot[bot] commented 3 months ago

To resolve the issue with your custom Langflow component using the Spacy library, ensure that your component follows the rules and structure outlined in the documentation. Here is an example of how you can create a custom component that uses Spacy for Named Entity Recognition (NER):

from langflow.custom import CustomComponent
from langflow.field_typing import Data
from langchain.schema import Document
import spacy

class SpacyNERComponent(CustomComponent):
    display_name = "Spacy NER Processor"
    description = "This component uses Spacy to perform Named Entity Recognition on a document"

    def build_config(self) -> dict:
        return {
            "model": {
                "options": ["en_core_web_sm", "en_core_web_md", "en_core_web_lg"],
                "value": "en_core_web_sm",
                "display_name": "Spacy Model"
            },
            "document": {
                "display_name": "Document"
            }
        }

    def build(self, document: Document, model: str) -> Document:
        if isinstance(document, list):
            document = document[0]
        nlp = spacy.load(model)
        doc = nlp(document.page_content)
        entities = [(ent.text, ent.label_) for ent in doc.ents]
        self.repr_value = f"Entities: {entities}"
        return Document(page_content=str(entities))

Ensure you have the Spacy library and the required models installed:

pip install spacy
python -m spacy download en_core_web_sm
python -m spacy download en_core_web_md
python -m spacy download en_core_web_lg

Save this script in your custom_components directory, and load it using the Langflow CLI as described in the documentation:

export LANGFLOW_COMPONENTS_PATH='["/path/to/components"]'
langflow run

This will allow you to save and use your custom Spacy component in Langflow. For more detailed information on creating and loading custom components, refer to the Langflow documentation [1][2][3][4].

To continue talking to Dosu, mention @dosu.

c0pper commented 3 months ago

@dosu Where am i supposed to find my custom components from your script in the gui? I only see the normal custom component

dosubot[bot] commented 3 months ago

You should find your custom components in Langflow's sidebar once they have been loaded successfully. If your custom component is not appearing, ensure that:

  1. The component code is error-free.
  2. The LANGFLOW_COMPONENTS_PATH environment variable is correctly set to the path of your custom components directory.
  3. You have restarted Langflow after setting the environment variable.

Here is a quick checklist:

  1. Set the Environment Variable:

    export LANGFLOW_COMPONENTS_PATH='["/path/to/components"]'
    langflow run
  2. Check for Errors: Ensure there are no syntax or runtime errors in your custom component code.

  3. Restart Langflow: After setting the environment variable and ensuring your code is error-free, restart Langflow.

Once these steps are completed, your custom components should appear in the sidebar of the Langflow GUI. If there are any errors in the component's code, they will not be available for addition to the canvas [1][2].

To continue talking to Dosu, mention @dosu.

carlosrcoelho commented 2 months ago

Hey!

You can try to create your custom component with the help of: https://chatgpt.com/g/g-rCodu5oaW-langflow-component-helper