Spaces:
Build error
Build error
new gradio app
Browse files- .gitignore +1 -0
- app.py +11 -0
- placeholders.py +36 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
app.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from placeholders import PlaceholderImageTool
|
| 3 |
+
|
| 4 |
+
image_generator = PlaceholderImageTool()
|
| 5 |
+
|
| 6 |
+
iface = gr.Interface(
|
| 7 |
+
fn=image_generator,
|
| 8 |
+
inputs=gr.Text(placeholder="camel", label="prompt"),
|
| 9 |
+
outputs=gr.Image(shape=(64,64))
|
| 10 |
+
)
|
| 11 |
+
iface.launch()
|
placeholders.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageDraw, ImageFilter, ImageFont
|
| 2 |
+
import numpy as np
|
| 3 |
+
from transformers.tools import Tool
|
| 4 |
+
|
| 5 |
+
class PlaceholderImageTool(Tool):
|
| 6 |
+
"""Replacement 'image_generator'
|
| 7 |
+
- transformers HfAgent likes to use the image_generator tool.
|
| 8 |
+
- I don't have the disk space for that.
|
| 9 |
+
- This replacement provides a random placeholder image instead.
|
| 10 |
+
Install it thusly: `agent.toolbox['image_generator']=PlaceholderImageTool()`
|
| 11 |
+
"""
|
| 12 |
+
name = "image_generator"
|
| 13 |
+
description = "I will generate an image based on a prompt."
|
| 14 |
+
inputs = ["text"]
|
| 15 |
+
outputs = ["image"]
|
| 16 |
+
def sepia(self, dim:int=64) -> Image:
|
| 17 |
+
"produce a sepia image, size=(dim,dim,3)"
|
| 18 |
+
data = np.random.randint(0,255,(dim,dim,3), dtype=np.uint8)
|
| 19 |
+
image = Image.fromarray(data)
|
| 20 |
+
sepia_matrix = [
|
| 21 |
+
0.393, 0.769, 0.189, 0,
|
| 22 |
+
0.349, 0.686, 0.168, 0,
|
| 23 |
+
0.272, 0.534, 0.131, 0
|
| 24 |
+
]
|
| 25 |
+
applied = image.convert('RGB', sepia_matrix)
|
| 26 |
+
return applied
|
| 27 |
+
def __call__(self, prompt):
|
| 28 |
+
"returns a sepia image overlaid with a random digit (1,6 inclusive)"
|
| 29 |
+
DIM=64
|
| 30 |
+
image = self.sepia(DIM)
|
| 31 |
+
#image = image.filter(ImageFilter.SMOOTH)
|
| 32 |
+
draw = ImageDraw.Draw(image)
|
| 33 |
+
digit = str(np.random.randint(1,7))
|
| 34 |
+
font = ImageFont.truetype("arial.ttf", size=48)
|
| 35 |
+
draw.text((DIM//2,DIM//2), digit, fill=(0,0,0), font=font, anchor="mm")
|
| 36 |
+
return image
|