Spaces:
Runtime error
Runtime error
Upload tool
Browse files- app.py +5 -0
- requirements.txt +3 -0
- tool.py +35 -0
app.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from tool import SimpleTool
|
| 3 |
+
|
| 4 |
+
tool = SimpleTool()
|
| 5 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
PIL
|
| 2 |
+
requests
|
| 3 |
+
smolagents
|
tool.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from typing import Any, Optional
|
| 3 |
+
|
| 4 |
+
class SimpleTool(Tool):
|
| 5 |
+
name = "pollinations_generate"
|
| 6 |
+
description = "Generate an image from a prompt"
|
| 7 |
+
inputs = {'prompt': {'type': 'string', 'description': 'The prompt to generate the image from'}, 'width': {'type': 'integer', 'nullable': True, 'description': 'image width'}, 'height': {'type': 'integer', 'nullable': True, 'description': 'image height'}, 'seed': {'type': 'integer', 'nullable': True, 'description': 'A decimal integer Each seed generates a new image variation Returns generated image as a PILL image'}}
|
| 8 |
+
output_type = "object"
|
| 9 |
+
|
| 10 |
+
def forward(self, prompt: str, width: int = 1024, height: int = 1024, seed: int = 0) -> PIL.Image.Image:
|
| 11 |
+
"""
|
| 12 |
+
Generate an image from a prompt
|
| 13 |
+
Args:
|
| 14 |
+
prompt : The prompt to generate the image from
|
| 15 |
+
width : image width
|
| 16 |
+
height : image height
|
| 17 |
+
seed : A decimal integer Each seed generates a new image variation
|
| 18 |
+
Returns
|
| 19 |
+
generated image as a PILL image
|
| 20 |
+
|
| 21 |
+
"""
|
| 22 |
+
import PIL
|
| 23 |
+
from PIL import Image
|
| 24 |
+
import requests
|
| 25 |
+
import random
|
| 26 |
+
from io import BytesIO
|
| 27 |
+
if seed ==0:
|
| 28 |
+
seed=random.randint(1,8*1024**2)
|
| 29 |
+
else:
|
| 30 |
+
random.seed(seed)
|
| 31 |
+
prompt="A beautiful landscape"
|
| 32 |
+
url = f"https://pollinations.ai/p/{prompt}"
|
| 33 |
+
params={"width":width,"height":height,"seed":seed}
|
| 34 |
+
response = requests.get(url)
|
| 35 |
+
return Image.open(requests.get(url, stream=True,params=params).raw)
|