RamAnanth1 commited on
Commit
d9a4e74
·
1 Parent(s): 25fe7da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -40
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import gradio as gr
2
- from stability_sdk import client
3
- import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
4
  from PIL import Image
5
  import io
6
  import os
7
  import warnings
8
-
 
 
9
 
10
  # theme = gr.themes.Monochrome(
11
  # primary_hue="indigo",
@@ -15,41 +15,17 @@ import warnings
15
  # font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
16
  # )
17
 
18
- def infer(prompt, api_key):
19
- stability_api = client.StabilityInference(
20
- key=api_key, # API Key reference.
21
- verbose=True, # Print debug messages.
22
- engine="stable-diffusion-xl-beta-v2-2-2", # Set the engine to use for generation.
23
- # Available engines: stable-diffusion-v1 stable-diffusion-v1-5 stable-diffusion-512-v2-0 stable-diffusion-768-v2-0 stable-inpainting-v1-0 stable-inpainting-512-v2-0
24
- )
25
- answers = stability_api.generate(
26
- prompt=prompt,
27
- #seed=992446758, # If a seed is provided, the resulting generated image will be deterministic.
28
- # What this means is that as long as all generation parameters remain the same, you can always recall the same image simply by generating it again.
29
- # Note: This isn't quite the case for Clip Guided generations, which we'll tackle in a future example notebook.
30
- steps=50, # Amount of inference steps performed on image generation. Defaults to 30.
31
- cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt.
32
- # Setting this value higher increases the strength in which it tries to match your prompt.
33
- # Defaults to 7.0 if not specified.
34
- width=512, # Generation width, defaults to 512 if not included.
35
- height=512, # Generation height, defaults to 512 if not included.
36
- samples=2, # Number of images to generate, defaults to 1 if not included.
37
- sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with.
38
- # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers.
39
- # (Available Samplers: ddim, plms, k_euler, k_euler_ancestral, k_heun, k_dpm_2, k_dpm_2_ancestral, k_dpmpp_2s_ancestral, k_lms, k_dpmpp_2m)
40
- )
41
- results = []
42
- for resp in answers:
43
- for artifact in resp.artifacts:
44
- if artifact.finish_reason == generation.FILTER:
45
- warnings.warn(
46
- "Your request activated the API's safety filters and could not be processed."
47
- "Please modify the prompt and try again.")
48
- if artifact.type == generation.ARTIFACT_IMAGE:
49
- img = Image.open(io.BytesIO(artifact.binary))
50
- results.append(img)
51
 
52
- return results
53
 
54
  css = """
55
  .gradio-container {
@@ -238,8 +214,7 @@ with gr.Blocks(css = css) as demo:
238
  )
239
  gr.HTML("<p>You can duplicate this Space to run it privately without a queue for shorter queue times : <a style='display:inline-block' href='https://huggingface.co/spaces/RamAnanth1/stable-diffusion-xl?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a> </p>")
240
 
241
- api_key_input = gr.Textbox(type = "password", label = "Enter your StabilityAI API key here")
242
-
243
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
244
  with gr.Column():
245
  text = gr.Textbox(
@@ -263,7 +238,7 @@ with gr.Blocks(css = css) as demo:
263
  label="Generated images", show_label=False, elem_id="gallery"
264
  ).style(grid=[2], height="auto")
265
 
266
- btn.click(infer, inputs=[text, api_key_input], outputs=[gallery])
267
  examples = [
268
  ["Vintage hot rod with custom flame paint job"],
269
  ["Ancient, mysterious temple in a mountain range, surrounded by misty clouds and tall peaks"],
 
1
  import gradio as gr
 
 
2
  from PIL import Image
3
  import io
4
  import os
5
  import warnings
6
+ import torch
7
+ import numpy as np
8
+ from diffusers import DiffusionPipeline
9
 
10
  # theme = gr.themes.Monochrome(
11
  # primary_hue="indigo",
 
15
  # font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
16
  # )
17
 
18
+ def infer(prompt):
19
+ torch.cuda.max_memory_allocated(device='cpu')
20
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
21
+ pipe = pipe.to(device)
22
+ pipe.enable_xformers_memory_efficient_attention()
23
+ torch.cuda.empty_cache()
24
+ generator = torch.Generator(device=device).manual_seed(seed)
25
+ int_image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=steps, height=height, width=width, guidance_scale=scale, num_images_per_prompt=1, generator=generator).images
26
+ torch.cuda.empty_cache()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
+ return int_image
29
 
30
  css = """
31
  .gradio-container {
 
214
  )
215
  gr.HTML("<p>You can duplicate this Space to run it privately without a queue for shorter queue times : <a style='display:inline-block' href='https://huggingface.co/spaces/RamAnanth1/stable-diffusion-xl?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a> </p>")
216
 
217
+
 
218
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
219
  with gr.Column():
220
  text = gr.Textbox(
 
238
  label="Generated images", show_label=False, elem_id="gallery"
239
  ).style(grid=[2], height="auto")
240
 
241
+ btn.click(infer, inputs=[text], outputs=[gallery])
242
  examples = [
243
  ["Vintage hot rod with custom flame paint job"],
244
  ["Ancient, mysterious temple in a mountain range, surrounded by misty clouds and tall peaks"],