Azure99 commited on
Commit
690d6e4
1 Parent(s): 84c2899

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import uuid
4
+
5
+ import gradio as gr
6
+ import numpy as np
7
+ import spaces
8
+ import torch
9
+ from diffusers import DiffusionPipeline
10
+
11
+ MAX_SEED = np.iinfo(np.int32).max
12
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
13
+ MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1536"))
14
+
15
+ device = torch.device("cuda:0")
16
+
17
+ pipe = DiffusionPipeline.from_pretrained(
18
+ "playgroundai/playground-v2.5-1024px-aesthetic",
19
+ torch_dtype=torch.float16,
20
+ use_safetensors=True,
21
+ add_watermarker=False,
22
+ variant="fp16"
23
+ )
24
+ pipe.to(device)
25
+ print("Loaded on Device!")
26
+
27
+
28
+ def save_image(img):
29
+ unique_name = str(uuid.uuid4()) + ".png"
30
+ img.save(unique_name)
31
+ return unique_name
32
+
33
+
34
+ @spaces.GPU(enable_queue=True)
35
+ def generate(
36
+ prompt: str,
37
+ progress=gr.Progress(track_tqdm=True),
38
+ ):
39
+ seed = random.randint(0, 2147483647)
40
+ pipe.to(device)
41
+ generator = torch.Generator().manual_seed(seed)
42
+
43
+ images = pipe(
44
+ prompt=prompt,
45
+ negative_prompt=None,
46
+ width=1024,
47
+ height=1024,
48
+ guidance_scale=3,
49
+ num_inference_steps=25,
50
+ generator=generator,
51
+ num_images_per_prompt=1,
52
+ use_resolution_binning=True,
53
+ output_type="pil",
54
+ ).images
55
+
56
+ image_paths = [save_image(img) for img in images]
57
+ return image_paths
58
+
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("# Blossom Playground v2.5")
61
+ with gr.Group():
62
+ with gr.Row():
63
+ prompt = gr.Text(
64
+ label="Prompt",
65
+ show_label=False,
66
+ max_lines=1,
67
+ placeholder="Enter your prompt",
68
+ container=False,
69
+ )
70
+ run_button = gr.Button("Run", scale=0)
71
+ result = gr.Gallery(label="Result", columns=1, show_label=False)
72
+
73
+ gr.on(
74
+ triggers=[
75
+ prompt.submit,
76
+ run_button.click,
77
+ ],
78
+ fn=generate,
79
+ inputs=[
80
+ prompt,
81
+ ],
82
+ outputs=[result],
83
+ api_name="run",
84
+ )
85
+
86
+ if __name__ == "__main__":
87
+ demo.queue(max_size=20).launch()