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
28.73k stars 3.68k forks source link

image/api load into the flow #3797

Closed michelgirault closed 4 days ago

michelgirault commented 4 days ago

Feature Request

It would be great to be able to load images or other format into the playground or all via the api

Motivation

it would great in order to connect with other api such as stable diffusion

Your Contribution

No response

dosubot[bot] commented 4 days ago

Langflow already supports handling image data, as it is now multi-modal and can handle images, with more formats to be supported soon [1]. Additionally, Langflow is designed to be agnostic to any model, API, or database, which means it can integrate with external APIs such as Stable Diffusion [2].

To continue talking to Dosu, mention @dosu.

michelgirault commented 4 days ago

Langflow already supports handling image data, as it is now multi-modal and can handle images, with more formats to be supported soon [1]. Additionally, Langflow is designed to be agnostic to any model, API, or database, which means it can integrate with external APIs such as Stable Diffusion [2].

To continue talking to Dosu, mention @dosu.

thanks any documentation, nodes available or general information beside what you shared?

dosubot[bot] commented 4 days ago

For handling image data and integrating external APIs like Stable Diffusion in Langflow, here are some additional details:

Nodes for Handling Image Data

Langflow includes nodes specifically designed for handling image data. The ChatInput component includes a FileInput that supports image file types such as "jpg", "jpeg", "png", and "bmp" [[1]](https://github.com/langflow-ai/langflow/blob/main/src/backend/base/langflow/initial_setup/starter_projects/Vector Store RAG.json).

Example Flows and Use Cases

The documentation provides example flows that demonstrate handling image data. For instance, the "Chat Input" node includes a "files" field that supports various file types, including image formats such as "jpg", "jpeg", "png", and "bmp" [2][3].

Integrating External APIs

The documentation also provides example flows and use cases for integrating external APIs. Here are some examples:

Example Code for Generating and Handling Image Data

The documentation includes a specific example that demonstrates generating and handling image data using the OpenAI API. The example includes a function to generate an image based on a prompt and save it in a specified format:

def build(self, prompt: str, api_key: str, model: str, file_name: str, output_format: str, width: int, height: int):
    client = OpenAI(api_key=api_key)
    cache_dir = Path(user_cache_dir("langflow"))
    images_dir = cache_dir / "images"
    images_dir.mkdir(parents=True, exist_ok=True)
    image_path = images_dir / f"{file_name}.{output_format}"
    response = client.images.generate(
        model=model,
        prompt=prompt,
        size=f"{height}x{width}",
        response_format="b64_json",
        n=1,
    )
    # Decode base64-encoded image string
    binary_data = base64.b64decode(response.data[0].b64_json)
    # Create PIL Image object from binary image data
    image_pil = Image.open(BytesIO(binary_data))
    image_pil.save(image_path, format=output_format.upper())
    return ""

This example demonstrates how to generate an image using a prompt, decode the base64-encoded image string, and save it as a file in the specified format [2][3].

For more detailed information, you can refer to the Langflow Workspace documentation which provides comprehensive guidance on creating and managing flows, as well as integrating them with external APIs.

To continue talking to Dosu, mention @dosu.

michelgirault commented 4 days ago

thats fine and there is not issue for me, back to my question is how to you display the image in the flow, playground

dosubot[bot] commented 4 days ago

To display an image in the flow or playground in Langflow, you can use the ZoomableImage component. Here is the relevant code snippet:

<ZoomableImage
  alt="Langflow canvas image"
  sources={{
    light: "img/langflow_canvas.png",
    dark: "img/langflow_canvas.png",
  }}
  style={{ width: "100%", maxWidth: "800px", margin: "0 auto" }}
/>

This code will display an image with the specified sources for light and dark themes, and it will be zoomable [1][2].

To continue talking to Dosu, mention @dosu.

michelgirault commented 4 days ago

not relevant answer, I'm asking about displaying the image previously generated with an api for example

dosubot[bot] commented 4 days ago

To display an image generated by an API within a Langflow node, you can use the FileInput field in the Chat Input node, which supports image file types. Here is an example of how to configure the Chat Input node to handle and display images:

from langflow.base.io.chat import ChatComponent
from langflow.io import FileInput, MultilineInput, Output
from langflow.schema.message import Message

class ChatInput(ChatComponent):
    display_name = "Chat Input"
    description = "Get chat inputs from the Playground."
    icon = "ChatInput"

    inputs = [
        MultilineInput(
            name="input_value",
            display_name="Text",
            value="",
            info="Message to be passed as input.",
        ),
        FileInput(
            name="files",
            display_name="Files",
            file_types=["jpg", "jpeg", "png", "bmp", "image"],
            info="Files to be sent with the message.",
            advanced=True,
            is_list=True,
        ),
    ]
    outputs = [
        Output(display_name="Message", name="message", method="message_response"),
    ]

    def message_response(self) -> Message:
        message = Message(
            text=self.input_value,
            files=self.files,
        )
        return message

This configuration allows the Chat Input node to accept and display image files. When an image is generated by an API, you can pass it to this node, and it will be displayed in the flow or playground [1].

For rendering the image in the UI, you can use the ZoomableImage component as follows:

import ZoomableImage from "/src/theme/ZoomableImage.js";

<ZoomableImage
  alt="Generated image"
  sources={{
    light: "path/to/generated_image.png",
    dark: "path/to/generated_image.png"
  }}
  style={{ width: "80%", margin: "20px auto" }}
/>

This will display the generated image with zoom functionality, enhancing the user experience [2].

To continue talking to Dosu, mention @dosu.