peterpeter8585 commited on
Commit
7a9ab3f
1 Parent(s): c3a115d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -2
app.py CHANGED
@@ -1,6 +1,41 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import requests
5
  from bs4 import BeautifulSoup
6
  import urllib
@@ -109,11 +144,115 @@ def respond(
109
  token="".join([response.token.text for response in stream if response.token.text != "</s>"])
110
  response += token
111
  yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  """
114
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
115
  """
116
- demo = gr.ChatInterface(
117
  respond,
118
  additional_inputs=[
119
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
@@ -131,4 +270,6 @@ demo = gr.ChatInterface(
131
 
132
 
133
  if __name__ == "__main__":
134
- demo.launch()
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import random
4
+ from diffusers import DiffusionPipeline
5
+ import torch
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ if torch.cuda.is_available():
10
+ torch.cuda.max_memory_allocated(device=device)
11
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
+ pipe.enable_xformers_memory_efficient_attention()
13
+ pipe = pipe.to(device)
14
+ else:
15
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
+ pipe = pipe.to(device)
17
+
18
+ MAX_SEED = np.iinfo(np.int32).max
19
+ MAX_IMAGE_SIZE = 1024
20
 
21
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
22
+
23
+ if randomize_seed:
24
+ seed = random.randint(0, MAX_SEED)
25
+
26
+ generator = torch.Generator().manual_seed(seed)
27
+
28
+ image = pipe(
29
+ prompt = prompt,
30
+ negative_prompt = negative_prompt,
31
+ guidance_scale = guidance_scale,
32
+ num_inference_steps = num_inference_steps,
33
+ width = width,
34
+ height = height,
35
+ generator = generator
36
+ ).images[0]
37
+
38
+ return image
39
  import requests
40
  from bs4 import BeautifulSoup
41
  import urllib
 
144
  token="".join([response.token.text for response in stream if response.token.text != "</s>"])
145
  response += token
146
  yield response
147
+ examples = [
148
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
149
+ "An astronaut riding a green horse",
150
+ "A delicious ceviche cheesecake slice",
151
+ ]
152
+
153
+ css="""
154
+ #col-container {
155
+ margin: 0 auto;
156
+ max-width: 520px;
157
+ }
158
+ """
159
+
160
+ if torch.cuda.is_available():
161
+ power_device = "GPU"
162
+ else:
163
+ power_device = "CPU"
164
+
165
+ with gr.Blocks(css=css) as demo2:
166
+
167
+ with gr.Column(elem_id="col-container"):
168
+ gr.Markdown(f"""
169
+ # Text-to-Image Gradio Template
170
+ Currently running on {power_device}.
171
+ """)
172
+
173
+ with gr.Row():
174
+
175
+ prompt = gr.Text(
176
+ label="Prompt",
177
+ show_label=False,
178
+ max_lines=1,
179
+ placeholder="Enter your prompt",
180
+ container=False,
181
+ )
182
+
183
+ run_button = gr.Button("Run", scale=0)
184
+
185
+ result = gr.Image(label="Result", show_label=False)
186
+
187
+ with gr.Accordion("Advanced Settings", open=False):
188
+
189
+ negative_prompt = gr.Text(
190
+ label="Negative prompt",
191
+ max_lines=1,
192
+ placeholder="Enter a negative prompt",
193
+ visible=False,
194
+ )
195
+
196
+ seed = gr.Slider(
197
+ label="Seed",
198
+ minimum=0,
199
+ maximum=MAX_SEED,
200
+ step=1,
201
+ value=0,
202
+ )
203
+
204
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
205
+
206
+ with gr.Row():
207
+
208
+ width = gr.Slider(
209
+ label="Width",
210
+ minimum=256,
211
+ maximum=MAX_IMAGE_SIZE,
212
+ step=32,
213
+ value=512,
214
+ )
215
+
216
+ height = gr.Slider(
217
+ label="Height",
218
+ minimum=256,
219
+ maximum=MAX_IMAGE_SIZE,
220
+ step=32,
221
+ value=512,
222
+ )
223
+ with gr.Row():
224
+
225
+ guidance_scale = gr.Slider(
226
+ label="Guidance scale",
227
+ minimum=0.0,
228
+ maximum=10.0,
229
+ step=0.1,
230
+ value=0.0,
231
+ )
232
+
233
+ num_inference_steps = gr.Slider(
234
+ label="Number of inference steps",
235
+ minimum=1,
236
+ maximum=12,
237
+ step=1,
238
+ value=2,
239
+ )
240
+
241
+ gr.Examples(
242
+ examples = examples,
243
+ inputs = [prompt]
244
+ )
245
+
246
+ run_button.click(
247
+ fn = infer,
248
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
249
+ outputs = [result]
250
+ )
251
 
252
  """
253
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
254
  """
255
+ aa = gr.ChatInterface(
256
  respond,
257
  additional_inputs=[
258
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
 
270
 
271
 
272
  if __name__ == "__main__":
273
+ with gr.Blocks as ai:
274
+ gr.TabbedInterface([aa, demo2], ["gpt4", "image create"])
275
+ ai.launch()