ktrndy commited on
Commit
b34b34f
·
verified ·
1 Parent(s): 50ef0b7

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -175
app.py DELETED
@@ -1,175 +0,0 @@
1
- import gradio as gr
2
- import numpy as np
3
- import random
4
- import os
5
- import torch
6
- from diffusers import StableDiffusionPipeline
7
- from peft import PeftModel, LoraConfig
8
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_id_default = "stable-diffusion-v1-5/stable-diffusion-v1-5"
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- MAX_SEED = np.iinfo(np.int32).max
18
- MAX_IMAGE_SIZE = 1024
19
-
20
-
21
- # @spaces.GPU #[uncomment to use ZeroGPU]
22
- def infer(
23
- prompt,
24
- negative_prompt,
25
- width=512,
26
- height=512,
27
- model_id=model_id_default,
28
- seed=42,
29
- guidance_scale=7.0,
30
- lora_scale=1.0,
31
- num_inference_steps=20,
32
- progress=gr.Progress(track_tqdm=True),
33
- ):
34
- generator = torch.Generator(device).manual_seed(seed)
35
-
36
- ckpt_dir='./model_output'
37
- unet_sub_dir = os.path.join(ckpt_dir, "unet")
38
- text_encoder_sub_dir = os.path.join(ckpt_dir, "text_encoder")
39
-
40
- if model_id is None:
41
- raise ValueError("Please specify the base model name or path")
42
-
43
- pipe = StableDiffusionPipeline.from_pretrained(model_id,
44
- torch_dtype=torch_dtype,
45
- safety_checker=None).to(device)
46
- pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir)
47
- pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder, text_encoder_sub_dir)
48
-
49
- pipe.unet.load_state_dict({k: lora_scale*v for k, v in pipe.unet.state_dict().items()})
50
- pipe.text_encoder.load_state_dict({k: lora_scale*v for k, v in pipe.text_encoder.state_dict().items()})
51
-
52
- if torch_dtype in (torch.float16, torch.bfloat16):
53
- pipe.unet.half()
54
- pipe.text_encoder.half()
55
-
56
- pipe.to(device)
57
-
58
- image = pipe(
59
- prompt=prompt,
60
- negative_prompt=negative_prompt,
61
- guidance_scale=guidance_scale,
62
- num_inference_steps=num_inference_steps,
63
- width=width,
64
- height=height,
65
- generator=generator,
66
- ).images[0]
67
-
68
- return image
69
-
70
- css = """
71
- #col-container {
72
- margin: 0 auto;
73
- max-width: 640px;
74
- }
75
- """
76
-
77
- with gr.Blocks(css=css, fill_height=True) as demo:
78
- with gr.Column(elem_id="col-container"):
79
- gr.Markdown(" # Text-to-Image demo")
80
-
81
- with gr.Row():
82
- model_id = gr.Textbox(
83
- label="Model ID",
84
- max_lines=1,
85
- placeholder="Enter model id",
86
- value=model_id_default,
87
- )
88
-
89
- prompt = gr.Textbox(
90
- label="Prompt",
91
- max_lines=1,
92
- placeholder="Enter your prompt",
93
- )
94
-
95
- negative_prompt = gr.Textbox(
96
- label="Negative prompt",
97
- max_lines=1,
98
- placeholder="Enter your negative prompt",
99
- )
100
-
101
- with gr.Row():
102
- seed = gr.Number(
103
- label="Seed",
104
- minimum=0,
105
- maximum=MAX_SEED,
106
- step=1,
107
- value=42,
108
- )
109
-
110
- guidance_scale = gr.Slider(
111
- label="Guidance scale",
112
- minimum=0.0,
113
- maximum=30.0,
114
- step=0.1,
115
- value=7.0, # Replace with defaults that work for your model
116
- )
117
- with gr.Row():
118
- lora_scale = gr.Slider(
119
- label="LoRA scale",
120
- minimum=0.0,
121
- maximum=1.0,
122
- step=0.01,
123
- value=1.0,
124
- )
125
-
126
- num_inference_steps = gr.Slider(
127
- label="Number of inference steps",
128
- minimum=1,
129
- maximum=100,
130
- step=1,
131
- value=20, # Replace with defaults that work for your model
132
- )
133
-
134
- with gr.Accordion("Optional Settings", open=False):
135
-
136
- with gr.Row():
137
- width = gr.Slider(
138
- label="Width",
139
- minimum=256,
140
- maximum=MAX_IMAGE_SIZE,
141
- step=32,
142
- value=512, # Replace with defaults that work for your model
143
- )
144
-
145
- height = gr.Slider(
146
- label="Height",
147
- minimum=256,
148
- maximum=MAX_IMAGE_SIZE,
149
- step=32,
150
- value=512, # Replace with defaults that work for your model
151
- )
152
-
153
- run_button = gr.Button("Run", scale=0, variant="primary")
154
- result = gr.Image(label="Result", show_label=False)
155
-
156
- gr.on(
157
- triggers=[run_button.click],
158
- fn=infer,
159
- inputs=[
160
- prompt,
161
- negative_prompt,
162
- width,
163
- height,
164
- model_id,
165
- seed,
166
- guidance_scale,
167
- lora_scale,
168
- num_inference_steps
169
-
170
- ],
171
- outputs=[result],
172
- )
173
-
174
- if __name__ == "__main__":
175
- demo.launch()