Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 |
|
| 103 |
demo = gr.Interface(
|
| 104 |
-
fn=
|
| 105 |
inputs=gr.Textbox(label="Describe the image"),
|
| 106 |
outputs=gr.Image(label="Generated Image"),
|
| 107 |
-
title="
|
| 108 |
-
description="
|
| 109 |
)
|
| 110 |
|
| 111 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
def generate(prompt):
|
| 6 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 7 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 8 |
+
model_id,
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
revision="fp16",
|
| 11 |
+
use_auth_token=False
|
| 12 |
+
)
|
| 13 |
+
pipe = pipe.to("cpu")
|
| 14 |
+
|
| 15 |
+
image = pipe(prompt, num_inference_steps=20).images[0]
|
| 16 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
demo = gr.Interface(
|
| 19 |
+
fn=generate,
|
| 20 |
inputs=gr.Textbox(label="Describe the image"),
|
| 21 |
outputs=gr.Image(label="Generated Image"),
|
| 22 |
+
title="Realistic AI Generator (CPU)",
|
| 23 |
+
description="Slow but realistic. CPU only. No token."
|
| 24 |
)
|
| 25 |
|
| 26 |
demo.launch()
|