pranavajay commited on
Commit
13cee83
·
verified ·
1 Parent(s): 075f274

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ from enhanceaiteam.kalpana import KalpanaPipeline
5
+ import torch
6
+ import spaces
7
+
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+
10
+ pipe = KalpanaPipeline.from_pretrained(
11
+ "enhanceaiteam/kalpana",
12
+ torch_dtype=torch.float16
13
+ ).to(device)
14
+
15
+ MAX_SEED = np.iinfo(np.int32).max
16
+ MAX_IMAGE_SIZE = 1024
17
+
18
+ @spaces.GPU
19
+ def infer(prompt, negative_prompt="", seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
20
+
21
+ if randomize_seed:
22
+ seed = random.randint(0, MAX_SEED)
23
+
24
+ generator = torch.Generator().manual_seed(seed)
25
+
26
+ image = pipe(
27
+ prompt = prompt,
28
+ negative_prompt = negative_prompt,
29
+ width=width,
30
+ height=height,
31
+ guidance_scale = guidance_scale,
32
+ num_inference_steps = num_inference_steps,
33
+ generator = generator
34
+ ).images[0]
35
+
36
+ return image, seed
37
+
38
+ examples = [
39
+ "A sunset over a mountain range",
40
+ "A futuristic cityscape with flying cars",
41
+ "A serene beach with crystal clear water",
42
+ "A robot exploring an ancient temple",
43
+ ]
44
+
45
+ css="""
46
+ #col-container {
47
+ margin: 0 auto;
48
+ max-width: 520px;
49
+ }
50
+ """
51
+
52
+ if torch.cuda.is_available():
53
+ power_device = "GPU"
54
+ else:
55
+ power_device = "CPU"
56
+
57
+ with gr.Blocks(css=css) as demo:
58
+
59
+ with gr.Column(elem_id="col-container"):
60
+ gr.Markdown(f"""
61
+ # Kalpana 1.0
62
+ Demo of the Kalpana 1.0 open source diffusion transformer model
63
+ """)
64
+
65
+ with gr.Row():
66
+
67
+ prompt = gr.Text(
68
+ label="Prompt",
69
+ show_label=False,
70
+ max_lines=1,
71
+ placeholder="Enter your prompt",
72
+ container=False,
73
+ )
74
+
75
+ run_button = gr.Button("Run", scale=0)
76
+
77
+ result = gr.Image(label="Result", show_label=False)
78
+
79
+ with gr.Accordion("Advanced Settings", open=False):
80
+
81
+ negative_prompt = gr.Text(
82
+ label="Negative prompt",
83
+ max_lines=1,
84
+ placeholder="Enter a negative prompt",
85
+ )
86
+
87
+ seed = gr.Slider(
88
+ label="Seed",
89
+ minimum=0,
90
+ maximum=MAX_SEED,
91
+ step=1,
92
+ value=0,
93
+ )
94
+
95
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
96
+
97
+ with gr.Row():
98
+
99
+ width = gr.Slider(
100
+ label="Width",
101
+ minimum=256,
102
+ maximum=MAX_IMAGE_SIZE,
103
+ step=32,
104
+ value=1024,
105
+ )
106
+
107
+ height = gr.Slider(
108
+ label="Height",
109
+ minimum=256,
110
+ maximum=MAX_IMAGE_SIZE,
111
+ step=32,
112
+ value=1024,
113
+ )
114
+
115
+ with gr.Row():
116
+
117
+ guidance_scale = gr.Slider(
118
+ label="Guidance scale",
119
+ minimum=0.0,
120
+ maximum=10.0,
121
+ step=0.1,
122
+ value=5.0,
123
+ )
124
+
125
+ num_inference_steps = gr.Slider(
126
+ label="Number of inference steps",
127
+ minimum=1,
128
+ maximum=50,
129
+ step=1,
130
+ value=28,
131
+ )
132
+
133
+ gr.Examples(
134
+ examples = examples,
135
+ fn = infer,
136
+ inputs = [prompt],
137
+ outputs = [result, seed],
138
+ cache_examples="lazy"
139
+ )
140
+
141
+ gr.on(
142
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
143
+ fn = infer,
144
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
145
+ outputs = [result, seed]
146
+ )
147
+
148
+ demo.queue().launch()