-
Notifications
You must be signed in to change notification settings - Fork 868
/
Copy pathgradio_helper.py
124 lines (96 loc) · 3.97 KB
/
gradio_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import gradio as gr
import numpy as np
from tqdm.auto import tqdm
import sys
import openvino_genai as ov_genai
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 832
examples = [
"Cyberpunk cityscape like Tokyo New York with tall buildings at dusk golden hour cinematic lighting",
"Curly-haired unicorn in the forest, anime, line",
"Snime, masterpiece, high quality, a green snowman with a happy smiling face in the snows",
"A panda reading a book in a lush forest.",
"Pirate ship sailing on a sea with the milky way galaxy in the sky and purple glow lights",
]
css = """
#col-container {
margin: 0 auto;
max-width: 580px;
}
"""
def make_demo(pipeline, generator_cls, adapter_config, device):
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps, use_lora, progress=gr.Progress(track_tqdm=True)):
if randomize_seed:
seed = np.random.randint(0, MAX_SEED)
generator = generator_cls(seed)
use_negative_prompt = pipeline.get_generation_config().guidance_scale > 1
pbar = tqdm(total=num_inference_steps)
def callback(step, num_steps, latent):
pbar.update(1)
sys.stdout.flush()
return False
generate_args = {
"prompt": prompt,
"num_inference_steps": num_inference_steps,
"generator": generator,
"callback": callback,
"height": height,
"width": width,
}
if use_negative_prompt:
generate_args["negative_prompt"] = negative_prompt
if device != "NPU":
generate_args["adapters"] = adapter_config if use_lora else ov_genai.AdapterConfig()
image_tensor = pipeline.generate(**generate_args)
return image_tensor.data[0], seed
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(
"""
# Demo Text to Image with OpenVINO with Generate API
"""
)
with gr.Row():
prompt = gr.Text(
label="Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
container=False,
)
run_button = gr.Button("Run", scale=0)
result = gr.Image(label="Result", show_label=False)
use_lora = gr.Checkbox(label="Use LoRA", value=False, visible=device != "NPU")
with gr.Accordion("Advanced Settings", open=False):
negative_prompt = gr.Text(
label="Negative prompt",
max_lines=1,
placeholder="Enter a negative prompt",
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=64, value=512, visible=device != "NPU")
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=64, value=512, visible=device != "NPU")
with gr.Row():
num_inference_steps = gr.Slider(
label="Number of inference steps",
minimum=1,
maximum=50,
step=1,
value=20,
)
gr.Examples(examples=examples, inputs=[prompt])
gr.on(
triggers=[run_button.click, prompt.submit, negative_prompt.submit],
fn=infer,
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps, use_lora],
outputs=[result, seed],
)
return demo