Spaces:
Runtime error
Runtime error
Commit
·
da30eb6
1
Parent(s):
fad3848
Initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pathlib import Path
|
3 |
+
from huggingface_hub import notebook_login
|
4 |
+
from diffusers import StableDiffusionPipeline
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
|
8 |
+
import torch, logging
|
9 |
+
logging.disable(logging.WARNING)
|
10 |
+
torch.cuda.empty_cache()
|
11 |
+
torch.manual_seed(3407)
|
12 |
+
from torch import autocast
|
13 |
+
from contextlib import nullcontext
|
14 |
+
from diffusers import StableDiffusionPipeline
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
+
context = autocast if device == "cuda" else nullcontext
|
21 |
+
|
22 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, revision="fp16", torch_dtype=torch.float16).to(device)
|
23 |
+
|
24 |
+
|
25 |
+
def infer(prompt,samples):
|
26 |
+
with context(device):
|
27 |
+
images = pipe(samples*[prompt], guidance_scale=7.5).images
|
28 |
+
return images
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
demo = gr.Blocks()
|
33 |
+
|
34 |
+
with demo:
|
35 |
+
text = gr.Textbox(lines=7,placeholder="Enter your prompt to generate a background image... something like - Photorealistic scenery of bookshelf in a room")
|
36 |
+
samples = gr.Slider(label="Number of Images", minimum=1, maximum=5, value=2, step=1)
|
37 |
+
btn = gr.Button("Generate images",variant="primary").style(
|
38 |
+
margin=False,
|
39 |
+
rounded=(False, True, True, False),
|
40 |
+
)
|
41 |
+
gallery = gr.Gallery(label="Generated images", show_label=True).style(grid=(1, 3), height="auto")
|
42 |
+
|
43 |
+
text.submit(infer, inputs=[text, samples], outputs=gallery)
|
44 |
+
btn.click(infer, inputs=[text, samples], outputs=gallery, show_progress=True, status_tracker=None)
|
45 |
+
|
46 |
+
demo.launch()
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|