Grandediw commited on
Commit
00c2577
·
2 Parent(s): 1db5cfd 3190832
Files changed (1) hide show
  1. app.py +154 -0
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
@@ -59,6 +60,159 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
+ <<<<<<< HEAD
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
 
60
  ],
61
  )
62
 
63
+ =======
64
+ import gradio as gr
65
+ import numpy as np
66
+ import random
67
+
68
+ # import spaces #[uncomment to use ZeroGPU]
69
+ from diffusers import DiffusionPipeline
70
+ import torch
71
+
72
+ device = "cuda" if torch.cuda.is_available() else "cpu"
73
+ model_repo_id = "Grandediw/lora_model" # Replace to the model you would like to use
74
+
75
+ if torch.cuda.is_available():
76
+ torch_dtype = torch.float16
77
+ else:
78
+ torch_dtype = torch.float32
79
+
80
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
81
+ pipe = pipe.to(device)
82
+
83
+ MAX_SEED = np.iinfo(np.int32).max
84
+ MAX_IMAGE_SIZE = 1024
85
+
86
+
87
+ # @spaces.GPU #[uncomment to use ZeroGPU]
88
+ def infer(
89
+ prompt,
90
+ negative_prompt,
91
+ seed,
92
+ randomize_seed,
93
+ width,
94
+ height,
95
+ guidance_scale,
96
+ num_inference_steps,
97
+ progress=gr.Progress(track_tqdm=True),
98
+ ):
99
+ if randomize_seed:
100
+ seed = random.randint(0, MAX_SEED)
101
+
102
+ generator = torch.Generator().manual_seed(seed)
103
+
104
+ image = pipe(
105
+ prompt=prompt,
106
+ negative_prompt=negative_prompt,
107
+ guidance_scale=guidance_scale,
108
+ num_inference_steps=num_inference_steps,
109
+ width=width,
110
+ height=height,
111
+ generator=generator,
112
+ ).images[0]
113
+
114
+ return image, seed
115
+
116
+
117
+ examples = [
118
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
119
+ "An astronaut riding a green horse",
120
+ "A delicious ceviche cheesecake slice",
121
+ ]
122
+
123
+ css = """
124
+ #col-container {
125
+ margin: 0 auto;
126
+ max-width: 640px;
127
+ }
128
+ """
129
+
130
+ with gr.Blocks(css=css) as demo:
131
+ with gr.Column(elem_id="col-container"):
132
+ gr.Markdown(" # Text-to-Image Gradio Template")
133
+
134
+ with gr.Row():
135
+ prompt = gr.Text(
136
+ label="Prompt",
137
+ show_label=False,
138
+ max_lines=1,
139
+ placeholder="Enter your prompt",
140
+ container=False,
141
+ )
142
+
143
+ run_button = gr.Button("Run", scale=0, variant="primary")
144
+
145
+ result = gr.Image(label="Result", show_label=False)
146
+
147
+ with gr.Accordion("Advanced Settings", open=False):
148
+ negative_prompt = gr.Text(
149
+ label="Negative prompt",
150
+ max_lines=1,
151
+ placeholder="Enter a negative prompt",
152
+ visible=False,
153
+ )
154
+
155
+ seed = gr.Slider(
156
+ label="Seed",
157
+ minimum=0,
158
+ maximum=MAX_SEED,
159
+ step=1,
160
+ value=0,
161
+ )
162
+
163
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
164
+
165
+ with gr.Row():
166
+ width = gr.Slider(
167
+ label="Width",
168
+ minimum=256,
169
+ maximum=MAX_IMAGE_SIZE,
170
+ step=32,
171
+ value=1024, # Replace with defaults that work for your model
172
+ )
173
+
174
+ height = gr.Slider(
175
+ label="Height",
176
+ minimum=256,
177
+ maximum=MAX_IMAGE_SIZE,
178
+ step=32,
179
+ value=1024, # Replace with defaults that work for your model
180
+ )
181
+
182
+ with gr.Row():
183
+ guidance_scale = gr.Slider(
184
+ label="Guidance scale",
185
+ minimum=0.0,
186
+ maximum=10.0,
187
+ step=0.1,
188
+ value=0.0, # Replace with defaults that work for your model
189
+ )
190
+
191
+ num_inference_steps = gr.Slider(
192
+ label="Number of inference steps",
193
+ minimum=1,
194
+ maximum=50,
195
+ step=1,
196
+ value=2, # Replace with defaults that work for your model
197
+ )
198
+
199
+ gr.Examples(examples=examples, inputs=[prompt])
200
+ gr.on(
201
+ triggers=[run_button.click, prompt.submit],
202
+ fn=infer,
203
+ inputs=[
204
+ prompt,
205
+ negative_prompt,
206
+ seed,
207
+ randomize_seed,
208
+ width,
209
+ height,
210
+ guidance_scale,
211
+ num_inference_steps,
212
+ ],
213
+ outputs=[result, seed],
214
+ )
215
+ >>>>>>> 31908329aa8aa1d9432af8899421971abe08af4f
216
 
217
  if __name__ == "__main__":
218
  demo.launch()