File size: 1,004 Bytes
bafe111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from smolagents.tools import Tool

from PIL.Image import Image


class ImageGeneratorTool(Tool):
    name = "image_generator"
    description = "Generates an image based on your query."
    inputs = {
        "query": {
            "type": "string",
            "description": "The query to generate an image for.",
        }
    }
    output_type = "any"

    def __init__(self, **kwargs):
        super().__init__()
        try:
            from huggingface_hub import InferenceClient
        except ImportError as e:
            raise ImportError(
                "You must install package `huggingface_hub` to run this tool: for instance run `pip install huggingface_hub`."
            ) from e
        self.client = InferenceClient("black-forest-labs/FLUX.1-dev")

    def forward(self, query: str) -> Image:
        image: Image = self.client.text_to_image(query)
        if image is None:
            raise Exception("No results found! Try a less restrictive/shorter query.")
        return image