Towhee Documentation

website·Indexed 34 days ago

https://docs.towhee.io/

Documentation for Towhee, a framework for building AI pipelines. It includes guides on pipeline programming using nodes such as map, flat-map, filter, and window, as well as API references for AutoConfig (LocalCPUConfig, LocalGPUConfig, TritonCPUConfig, TritonGPUConfig) and integration with Triton inference server.

Tokens
10.1K
Snippets
49
Records
74
Agent score
59%

What's inside Towhee

  1. Overview of Towhee Layer building blocks

    Towhee provides a library of reusable neural network building blocks called layers. These layers act as modular components (similar to "legos") that allow developers and researchers to construct machine learning models quickly without reimplementing common architectures. The framework supports classical, modern, and hypermodern blocks, which can be optimized for different computation platforms.
  2. Overview of Towhee for unstructured data ETL

    Towhee is an open-source framework providing ETL (Extract, Transform, Load) capabilities for unstructured data (such as images, video, and text). It allows developers to build application-centric pipelines by wrapping machine learning models and functions from libraries like torch, timm, and transformers into standard Towhee operators. These operators can be chained together in a Directed Acyclic Graph (DAG) to create end-to-end ML applications.
  3. Understand Towhee node types and their functions

    Towhee pipelines are constructed using nodes, which are building blocks that perform specific data transformations. These nodes are combined to create directed acyclic graphs (DAGs). There are nine primary node types categorized by their role in the pipeline: input/output definition and data processing/analytics.
  4. Understand the Towhee flat_map operator

    The flat_map node applies a function to every row of input and flattens the resulting list into individual rows. Unlike the map operator, which always returns the same number of rows as the input, flat_map can return the same number of rows or more, depending on the size of the list returned by the transformation function.
  5. Install Towhee using pip

    It is highly recommended to use a virtual environment to avoid system-level dependency issues. On Windows, ensure Python >= 3.7 is installed before proceeding.
    # Linux/macOS
    $ python3 -m venv towhee_env
    $ source towhee_env/bin/activate
    $ pip3 install -U pip
    $ pip3 install -U towhee
    $ pip3 install -U towhee.models
    
    # Windows
    c:\> python -m venv towhee_env
    c:\> source towhee_env/Scripts/Activate
    c:\> pip3 install -U towhee
    c:\> pip3 install -U towhee.models
  6. Aggregate all items in a pipeline using window_all

    The window_all node creates a window that includes all items from the input list, applies a specified function (fn) to those items, and appends the result to the output list. It is used for global aggregations across the entire input stream.
    from towhee import pipe
        
    p = (pipe.input('n1', 'n2')
             .flat_map(('n1', 'n2'), ('n1', 'n2'), lambda x, y: [(a, b) for a, b in zip(x, y)])
             .window_all(('n1', 'n2'), ('s1', 's2'), lambda x, y: (sum(x), sum(y)))
             .output('s1', 's2'))
    
    p([1, 2, 3, 4], [2, 3, 4, 5]).get() # returns [10, 14]
  7. Install Towhee from source

    Install Towhee by cloning the official GitHub repository. It is recommended to perform this installation within a virtual environment. Note that the main branch is subject to ongoing testing and may contain bugs.
    $ python3 -m venv towhee_env
    $ source towhee_env/bin/activate
    $ git clone https://github.com/towhee-io/towhee.git
    $ cd towhee
    $ python setup.py install
  8. Install Towhee using Conda

    Install Towhee by creating a dedicated conda environment and installing the package from the towhee-io channel.
    $ conda create -n towhee_env python=3.7  # create a conda environment for Towhee
    $ conda activate towhee_env  # activate your newly created conda environment
    $ conda install -c towhee-io towhee   # install Towhee via the `towhee-io` channel
  9. Implement a map node in a Towhee pipeline

    Use the map(input_schema, output_schema, fn, config=None) interface to create a map node. The function fn must accept inputs matching the input_schema and return outputs matching the output_schema. Multiple input/output pairs correspond to multiple columns in the source and target tables.
    from towhee import pipe, ops
    
    # Define a pipeline for text feature extraction
    text_embedding = (
        pipe.input('text')
        .map('text', 'embedding', ops.sentence_embedding.transformers(model_name='all-MiniLM-L6-v2'))
        .output('text', 'embedding')
    )
    
    # Process multiple rows of data using .batch()
    data = ['Hello, world.', 'How are you?']
    res = text_embedding.batch(data)
  10. Create a sliding window node in a Towhee pipeline

    The window node iterates over an input list using a specified step size. In each iteration, it creates a window of a given size, applies a function (fn) to the items within that window, and appends the result to the output list. This is useful for processing sequences of data in overlapping or non-overlapping chunks.
    from towhee import pipe
    
    # Example: Create windows of size 2 with a step of 1
    # and calculate the sum of each window for two input streams
    p = (pipe.input('n1', 'n2')
            .flat_map(('n1', 'n2'), ('n1', 'n2'), lambda x, y: [(a, b) for a, b in zip(x, y)])
            .window(('n1', 'n2'), ('s1', 's2'), 2, 1, lambda x, y: (sum(x), sum(y)))
            .output('s1', 's2'))
    
    res = p([1, 2, 3, 4], [2, 3, 4, 5])
    res.to_list() # returns [[3, 5], [5, 7], [7, 9], [4, 5]]
  11. Create a basic Towhee pipeline with custom functions

    A Towhee pipeline connects function nodes to process data. A basic pipeline consists of an input node to define the input schema, map nodes to apply transformations (via lambda functions or named functions), and an output node to define the final schema. Use .get() on the pipeline call to retrieve the result.
    from towhee import pipe
    
    # Using a lambda function
    add_one = (
        pipe.input('x')
            .map('x', 'y', lambda x: x + 1)
            .output('y')
    )
    
    # Using a named function
    def my_func(x):
       return x + 1
    
    add_one_func = (
        pipe.input('x')
            .map('x', 'y', my_func)
            .output('y')
    )
    
    res = add_one(0).get() # Returns 1