Spaces:
Runtime error
Runtime error
Commit
•
958a329
0
Parent(s):
Duplicate from imseldrith/text2image
Browse filesCo-authored-by: Ashiq Hussain <imseldrith@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +14 -0
- app.py +103 -0
- requirements.txt +5 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Text2image
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.12.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: openrail
|
11 |
+
duplicated_from: imseldrith/text2image
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from torch import autocast
|
4 |
+
from diffusers import StableDiffusionPipeline, PNDMScheduler
|
5 |
+
import requests
|
6 |
+
import PIL
|
7 |
+
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
+
from io import BytesIO
|
10 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
11 |
+
|
12 |
+
def preprocess(image):
|
13 |
+
w, h = image.size
|
14 |
+
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
|
15 |
+
image = image.resize((w, h), resample=PIL.Image.LANCZOS)
|
16 |
+
image = np.array(image).astype(np.float32) / 255.0
|
17 |
+
image = image[None].transpose(0, 3, 1, 2)
|
18 |
+
image = torch.from_numpy(image)
|
19 |
+
return 2.0 * image - 1.0
|
20 |
+
|
21 |
+
scheduler_ = PNDMScheduler(
|
22 |
+
beta_start=0.005,
|
23 |
+
beta_end=0.12,
|
24 |
+
beta_schedule="scaled_linear"
|
25 |
+
)
|
26 |
+
|
27 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
28 |
+
"CompVis/stable-diffusion-v1-4",
|
29 |
+
scheduler=scheduler_,
|
30 |
+
revision="fp16",
|
31 |
+
use_auth_token=True
|
32 |
+
).to("cuda")
|
33 |
+
|
34 |
+
pipeimg = StableDiffusionImg2ImgPipeline.from_pretrained(
|
35 |
+
"CompVis/stable-diffusion-v1-4",
|
36 |
+
revision="fp16",
|
37 |
+
torch_dtype=torch.float16,
|
38 |
+
use_auth_token=True
|
39 |
+
).to("cuda")
|
40 |
+
|
41 |
+
block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
|
42 |
+
|
43 |
+
num_samples = 2
|
44 |
+
|
45 |
+
def infer(prompt, init_image, strength):
|
46 |
+
if init_image != None:
|
47 |
+
init_image = init_image.resize((512, 512))
|
48 |
+
init_image = preprocess(init_image)
|
49 |
+
with autocast("cuda"):
|
50 |
+
images = pipeimg([prompt] * num_samples, init_image=init_image, strength=strength, guidance_scale=7.5)["sample"]
|
51 |
+
else:
|
52 |
+
with autocast("cuda"):
|
53 |
+
images = pipe([prompt] * num_samples, guidance_scale=7.5)["sample"]
|
54 |
+
|
55 |
+
return images
|
56 |
+
|
57 |
+
|
58 |
+
with block as demo:
|
59 |
+
gr.Markdown("<h1><center>Stable Diffusion</center></h1>")
|
60 |
+
gr.Markdown(
|
61 |
+
"Stable Diffusion is an AI model that generates images from any prompt you give!"
|
62 |
+
)
|
63 |
+
with gr.Group():
|
64 |
+
with gr.Box():
|
65 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
66 |
+
|
67 |
+
text = gr.Textbox(
|
68 |
+
label="Enter your prompt", show_label=False, max_lines=1
|
69 |
+
).style(
|
70 |
+
border=(True, False, True, True),
|
71 |
+
rounded=(True, False, False, True),
|
72 |
+
container=False,
|
73 |
+
)
|
74 |
+
btn = gr.Button("Run").style(
|
75 |
+
margin=False,
|
76 |
+
rounded=(False, True, True, False),
|
77 |
+
)
|
78 |
+
strength_slider = gr.Slider(
|
79 |
+
label="Strength",
|
80 |
+
maximum = 1,
|
81 |
+
value = 0.75
|
82 |
+
)
|
83 |
+
image = gr.Image(
|
84 |
+
label="Intial Image",
|
85 |
+
type="pil"
|
86 |
+
)
|
87 |
+
|
88 |
+
gallery = gr.Gallery(label="Generated images", show_label=False).style(
|
89 |
+
grid=[2], height="auto"
|
90 |
+
)
|
91 |
+
text.submit(infer, inputs=[text,image,strength_slider], outputs=gallery)
|
92 |
+
btn.click(infer, inputs=[text,image,strength_slider], outputs=gallery)
|
93 |
+
|
94 |
+
gr.Markdown(
|
95 |
+
"""___
|
96 |
+
<p style='text-align: center'>
|
97 |
+
Created by CompVis and Stability AI
|
98 |
+
<br/>
|
99 |
+
</p>"""
|
100 |
+
)
|
101 |
+
|
102 |
+
|
103 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
diffusers
|
4 |
+
requests
|
5 |
+
numpy
|