Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,46 +2,101 @@ import gradio as gr
|
|
| 2 |
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
def
|
| 6 |
# Body
|
| 7 |
draw.ellipse((170, 240, 340, 380), outline="white", width=6)
|
| 8 |
# Head
|
| 9 |
draw.ellipse((120, 260, 210, 330), outline="white", width=6)
|
| 10 |
# Ears
|
| 11 |
-
draw.
|
| 12 |
-
draw.
|
| 13 |
# Tail
|
| 14 |
draw.line((340, 320, 450, 260), fill="white", width=6)
|
| 15 |
# Eyes
|
| 16 |
draw.ellipse((155, 285, 165, 295), fill="white")
|
| 17 |
draw.ellipse((185, 285, 195, 295), fill="white")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def generate_image(prompt):
|
| 20 |
img = Image.new("RGB", (512, 512), (20, 20, 30))
|
| 21 |
draw = ImageDraw.Draw(img)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
for i in range(512):
|
| 25 |
-
color = (
|
| 26 |
draw.line([(0, i), (512, i)], fill=color)
|
| 27 |
|
| 28 |
-
# Basic prompt detection
|
| 29 |
prompt_lower = prompt.lower()
|
|
|
|
| 30 |
if "rat" in prompt_lower:
|
| 31 |
draw_rat(draw)
|
| 32 |
elif "cat" in prompt_lower:
|
| 33 |
-
draw
|
| 34 |
-
|
| 35 |
-
draw
|
| 36 |
-
|
| 37 |
-
draw
|
| 38 |
-
|
| 39 |
-
draw
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
-
draw.text((
|
| 42 |
|
| 43 |
draw.rectangle([0, 470, 512, 512], fill=(0, 0, 0))
|
| 44 |
-
draw.text((10, 472), f"Prompt: {prompt[:
|
| 45 |
|
| 46 |
return img
|
| 47 |
|
|
|
|
| 2 |
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
def draw_cat(draw):
|
| 6 |
# Body
|
| 7 |
draw.ellipse((170, 240, 340, 380), outline="white", width=6)
|
| 8 |
# Head
|
| 9 |
draw.ellipse((120, 260, 210, 330), outline="white", width=6)
|
| 10 |
# Ears
|
| 11 |
+
draw.polygon([(130, 260), (150, 230), (170, 260)], outline="white", fill=None)
|
| 12 |
+
draw.polygon([(180, 260), (200, 230), (220, 260)], outline="white", fill=None)
|
| 13 |
# Tail
|
| 14 |
draw.line((340, 320, 450, 260), fill="white", width=6)
|
| 15 |
# Eyes
|
| 16 |
draw.ellipse((155, 285, 165, 295), fill="white")
|
| 17 |
draw.ellipse((185, 285, 195, 295), fill="white")
|
| 18 |
|
| 19 |
+
def draw_rat(draw):
|
| 20 |
+
draw.ellipse((170, 240, 340, 380), outline="white", width=6)
|
| 21 |
+
draw.ellipse((120, 260, 210, 330), outline="white", width=6)
|
| 22 |
+
draw.ellipse((130, 240, 160, 270), outline="white", width=6)
|
| 23 |
+
draw.ellipse((180, 240, 210, 270), outline="white", width=6)
|
| 24 |
+
draw.line((340, 320, 460, 260), fill="white", width=6)
|
| 25 |
+
draw.ellipse((155, 285, 165, 295), fill="white")
|
| 26 |
+
draw.ellipse((185, 285, 195, 295), fill="white")
|
| 27 |
+
|
| 28 |
+
def draw_dog(draw):
|
| 29 |
+
draw.ellipse((170, 240, 340, 380), outline="white", width=6)
|
| 30 |
+
draw.ellipse((120, 260, 210, 330), outline="white", width=6)
|
| 31 |
+
draw.ellipse((110, 250, 140, 290), outline="white", width=6)
|
| 32 |
+
draw.ellipse((200, 250, 230, 290), outline="white", width=6)
|
| 33 |
+
draw.line((340, 320, 460, 260), fill="white", width=6)
|
| 34 |
+
draw.ellipse((155, 285, 165, 295), fill="white")
|
| 35 |
+
draw.ellipse((185, 285, 195, 295), fill="white")
|
| 36 |
+
|
| 37 |
+
def draw_lion(draw):
|
| 38 |
+
draw.ellipse((170, 240, 340, 380), outline="white", width=6)
|
| 39 |
+
draw.ellipse((120, 260, 210, 330), outline="white", width=6)
|
| 40 |
+
# Mane
|
| 41 |
+
for i in range(0, 360, 20):
|
| 42 |
+
x1 = 165 + 90 * random.random()
|
| 43 |
+
y1 = 260 + 90 * random.random()
|
| 44 |
+
draw.arc((x1, y1, x1 + 120, y1 + 120), start=i, end=i+10, fill="white", width=6)
|
| 45 |
+
draw.ellipse((155, 285, 165, 295), fill="white")
|
| 46 |
+
draw.ellipse((185, 285, 195, 295), fill="white")
|
| 47 |
+
|
| 48 |
+
def draw_bird(draw):
|
| 49 |
+
draw.ellipse((170, 240, 340, 380), outline="white", width=6)
|
| 50 |
+
draw.polygon([(120, 300), (170, 250), (170, 350)], outline="white", fill=None)
|
| 51 |
+
draw.polygon([(340, 300), (290, 250), (290, 350)], outline="white", fill=None)
|
| 52 |
+
draw.line((250, 380, 250, 450), fill="white", width=6)
|
| 53 |
+
draw.ellipse((155, 285, 165, 295), fill="white")
|
| 54 |
+
draw.ellipse((185, 285, 195, 295), fill="white")
|
| 55 |
+
|
| 56 |
+
def draw_fish(draw):
|
| 57 |
+
draw.ellipse((170, 250, 340, 350), outline="white", width=6)
|
| 58 |
+
draw.polygon([(340, 300), (410, 270), (410, 330)], outline="white", fill=None)
|
| 59 |
+
draw.line((200, 300, 140, 270), fill="white", width=6)
|
| 60 |
+
draw.ellipse((240, 285, 250, 295), fill="white")
|
| 61 |
+
|
| 62 |
+
def draw_turtle(draw):
|
| 63 |
+
draw.ellipse((170, 250, 340, 380), outline="white", width=6)
|
| 64 |
+
draw.ellipse((120, 270, 190, 330), outline="white", width=6)
|
| 65 |
+
draw.ellipse((340, 270, 410, 330), outline="white", width=6)
|
| 66 |
+
draw.ellipse((200, 380, 240, 430), outline="white", width=6)
|
| 67 |
+
draw.ellipse((270, 380, 310, 430), outline="white", width=6)
|
| 68 |
+
draw.ellipse((155, 285, 165, 295), fill="white")
|
| 69 |
+
|
| 70 |
def generate_image(prompt):
|
| 71 |
img = Image.new("RGB", (512, 512), (20, 20, 30))
|
| 72 |
draw = ImageDraw.Draw(img)
|
| 73 |
|
| 74 |
+
# Gradient background
|
| 75 |
for i in range(512):
|
| 76 |
+
color = (30 + i // 18, 20 + i // 30, 50 + i // 20)
|
| 77 |
draw.line([(0, i), (512, i)], fill=color)
|
| 78 |
|
|
|
|
| 79 |
prompt_lower = prompt.lower()
|
| 80 |
+
|
| 81 |
if "rat" in prompt_lower:
|
| 82 |
draw_rat(draw)
|
| 83 |
elif "cat" in prompt_lower:
|
| 84 |
+
draw_cat(draw)
|
| 85 |
+
elif "dog" in prompt_lower:
|
| 86 |
+
draw_dog(draw)
|
| 87 |
+
elif "lion" in prompt_lower:
|
| 88 |
+
draw_lion(draw)
|
| 89 |
+
elif "bird" in prompt_lower:
|
| 90 |
+
draw_bird(draw)
|
| 91 |
+
elif "fish" in prompt_lower:
|
| 92 |
+
draw_fish(draw)
|
| 93 |
+
elif "turtle" in prompt_lower:
|
| 94 |
+
draw_turtle(draw)
|
| 95 |
else:
|
| 96 |
+
draw.text((30, 250), "I can draw: cat, rat, dog, lion, bird, fish, turtle", fill="white")
|
| 97 |
|
| 98 |
draw.rectangle([0, 470, 512, 512], fill=(0, 0, 0))
|
| 99 |
+
draw.text((10, 472), f"Prompt: {prompt[:40]}", fill="white")
|
| 100 |
|
| 101 |
return img
|
| 102 |
|