Spaces:
Runtime error
Runtime error
Commit
·
f1cb0a2
1
Parent(s):
88898ec
feat: added app
Browse files- app.py +27 -0
- requirements.txt +7 -0
- service.py +30 -0
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from service import Service
|
4 |
+
|
5 |
+
prompt = ""
|
6 |
+
scale = 10
|
7 |
+
n_samples = 4
|
8 |
+
rows = 2
|
9 |
+
cols = 2
|
10 |
+
|
11 |
+
service = Service()
|
12 |
+
|
13 |
+
|
14 |
+
def generate_pkm(prompt):
|
15 |
+
return service.generate(prompt, n_samples=n_samples, rows=rows, cols=cols)
|
16 |
+
|
17 |
+
|
18 |
+
with gr.Blocks() as demo:
|
19 |
+
gr.Markdown("Write a prompt to generate a Pokemon!")
|
20 |
+
prompt = gr.Textbox(label="Prompt")
|
21 |
+
image_output = gr.Image()
|
22 |
+
generate = gr.Button("Generate")
|
23 |
+
generate.click(generate_pkm, inputs=prompt, outputs=image_output)
|
24 |
+
gr.Markdown("Models finetuned by: lambdalabs")
|
25 |
+
|
26 |
+
if __name__ == "__main__":
|
27 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
diffusers==0.8.0
|
3 |
+
transformers #==4.24.0
|
4 |
+
scipy
|
5 |
+
ftfy
|
6 |
+
torch
|
7 |
+
accelerate
|
service.py
CHANGED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
from torch import autocast
|
5 |
+
|
6 |
+
pipe = StableDiffusionPipeline.from_pretrained("lambdalabs/sd-pokemon-diffusers", torch_dtype=torch.float16)
|
7 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
+
pipe = pipe.to(device)
|
9 |
+
|
10 |
+
|
11 |
+
class Service():
|
12 |
+
def __init__(self):
|
13 |
+
self.scale = 10
|
14 |
+
|
15 |
+
def image_grid(imgs, rows, cols):
|
16 |
+
assert len(imgs) == rows * cols
|
17 |
+
w, h = imgs[0].size
|
18 |
+
grid = Image.new('RGB', size=(cols * w, rows * h))
|
19 |
+
for i, img in enumerate(imgs):
|
20 |
+
grid.paste(img, box=(i % cols * w, i // cols * h))
|
21 |
+
return grid
|
22 |
+
|
23 |
+
def generate(self,
|
24 |
+
prompt,
|
25 |
+
n_samples,
|
26 |
+
rows,
|
27 |
+
cols):
|
28 |
+
with autocast("device"):
|
29 |
+
images = pipe(n_samples * [prompt], guidance_scale=self.scale).images
|
30 |
+
return self.image_grid(images, rows=rows, cols=cols)
|