File size: 3,316 Bytes
8b2bba8
 
 
3831aa7
db1d11f
 
 
 
3628bde
 
 
8b2bba8
 
f4b71e7
 
 
 
 
8b2bba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4b71e7
 
 
 
 
8b2bba8
 
3831aa7
8b2bba8
 
 
 
 
 
 
 
f4b71e7
 
 
 
 
8b2bba8
 
 
 
 
 
 
f4b71e7
 
 
 
 
8b2bba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4b71e7
 
 
 
 
8b2bba8
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Hugging Face Spaces Tool Template for Agents

- a quick tribute to [Aymeric](https://huggingface.co/m-ric) for this simple but effective exapmle of how to build and serve agents using Hugging Face spaces
- the one thing I've noticed with some of the reusable tools created by the HF team is that they appear to still use a number of potentially older transformer libraries
- just be aware that there are differences between the [smolagents](https://huggingface.co/docs/smolagents/main/en/reference/tools#smolagents.launch_gradio_demo) and [transformers](https://huggingface.co/docs/transformers/v4.48.0/en/main_classes/agent#transformers.launch_gradio_demo) implementations for some of these methods and you'll be fine

## [m-ric/text-to-image repo](https://huggingface.co/spaces/m-ric/text-to-image)

- the structure and content below is a full extract of the entire repo
- helps to understand the config and code required to publish and launch a reusable tool
- refer to the comment under `app.py` for an example of where the older transformers import is still used

### File: README.md
- Type: txt
- Size: 247 bytes
- Created: 2025-01-22 19:08:45 UTC
- Modified: 2025-01-22 19:08:45 UTC

#### Content:
```
---
title: Text To Image
emoji: πŸ‘€
colorFrom: blue
colorTo: indigo
sdk: gradio
sdk_version: 4.29.0
app_file: app.py
pinned: false
tags:
- tool
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference

```

### File: app.py
- Type: python
- Size: 114 bytes
- Created: 2025-01-22 19:08:45 UTC
- Modified: 2025-01-22 19:08:45 UTC

#### Content:
```
# the launch_gradio_demo import should likely be from smolagents
from transformers import launch_gradio_demo
from tool import TextToImageTool

launch_gradio_demo(TextToImageTool)

```

### File: requirements.txt
- Type: txt
- Size: 26 bytes
- Created: 2025-01-22 19:08:45 UTC
- Modified: 2025-01-22 19:08:45 UTC

#### Content:
```
huggingface_hub
smolagents
```

### File: tool.py
- Type: python
- Size: 635 bytes
- Created: 2025-01-22 19:08:45 UTC
- Modified: 2025-01-22 19:08:45 UTC

#### Content:
```
from smolagents import Tool
from huggingface_hub import InferenceClient


class TextToImageTool(Tool):
    description = "This tool creates an image according to a prompt, which is a text description."
    name = "image_generator"
    inputs = {"prompt": {"type": "string", "description": "The image generator prompt. Don't hesitate to add details in the prompt to make the image look better, like 'high-res, photorealistic', etc."}}
    output_type = "image"
    model_sdxl = "black-forest-labs/FLUX.1-schnell"
    client = InferenceClient(model_sdxl)


    def forward(self, prompt):
        return self.client.text_to_image(prompt)

```

### File: tool_config.json
- Type: json
- Size: 414 bytes
- Created: 2025-01-22 19:08:45 UTC
- Modified: 2025-01-22 19:08:45 UTC

#### Content:
```
{
  "description": "This is a tool that creates an image according to a prompt, which is a text description.",
  "inputs": "{'prompt': {'type': 'string', 'description': \"The image generator prompt. Don't hesitate to add details in the prompt to make the image look better, like 'high-res, photorealistic', etc.\"}}",
  "name": "image_generator",
  "output_type": "image",
  "tool_class": "tool.TextToImageTool"
}

```