RamAnanth1 commited on
Commit
629deb2
·
1 Parent(s): 546037a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from stability_sdk import client
3
+ import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
4
+
5
+
6
+ theme = gr.themes.Monochrome(
7
+ primary_hue="indigo",
8
+ secondary_hue="blue",
9
+ neutral_hue="slate",
10
+ radius_size=gr.themes.sizes.radius_sm,
11
+ font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
12
+ )
13
+
14
+ def infer(prompt, api_key):
15
+ stability_api = client.StabilityInference(
16
+ key=api_key, # API Key reference.
17
+ verbose=True, # Print debug messages.
18
+ engine="stable-diffusion-xl-beta-v2-2-2", # Set the engine to use for generation.
19
+ # 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
20
+ )
21
+ answers = stability_api.generate(
22
+ prompt=prompt,
23
+ seed=992446758, # If a seed is provided, the resulting generated image will be deterministic.
24
+ # 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.
25
+ # Note: This isn't quite the case for Clip Guided generations, which we'll tackle in a future example notebook.
26
+ steps=30, # Amount of inference steps performed on image generation. Defaults to 30.
27
+ cfg_scale=8.0, # Influences how strongly your generation is guided to match your prompt.
28
+ # Setting this value higher increases the strength in which it tries to match your prompt.
29
+ # Defaults to 7.0 if not specified.
30
+ width=512, # Generation width, defaults to 512 if not included.
31
+ height=512, # Generation height, defaults to 512 if not included.
32
+ samples=1, # Number of images to generate, defaults to 1 if not included.
33
+ sampler=generation.SAMPLER_K_DPMPP_2M # Choose which sampler we want to denoise our generation with.
34
+ # Defaults to k_dpmpp_2m if not specified. Clip Guidance only supports ancestral samplers.
35
+ # (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)
36
+ )
37
+ for resp in answers:
38
+ for artifact in resp.artifacts:
39
+ if artifact.finish_reason == generation.FILTER:
40
+ warnings.warn(
41
+ "Your request activated the API's safety filters and could not be processed."
42
+ "Please modify the prompt and try again.")
43
+ if artifact.type == generation.ARTIFACT_IMAGE:
44
+ img = Image.open(io.BytesIO(artifact.binary))
45
+
46
+ return img
47
+
48
+ with gr.Blocks(theme = theme) as demo:
49
+ gr.Markdown("# Stable Diffusion XL")
50
+ #gr.Markdown('<p> For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. <a href="https://huggingface.co/spaces/RamAnanth1/stable-diffusion-xl?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>')
51
+ api_key_input = gr.Textbox(type = "password", label = "Enter your StabilityAI API key here")
52
+ text = gr.Textbox(label="Enter your prompt",
53
+ show_label=True,
54
+ max_lines=1,
55
+ placeholder="Enter your prompt",
56
+ elem_id="prompt-text-input",
57
+ ).style(
58
+ border=(True, False, True, True),
59
+ rounded=(True, False, False, True),
60
+ container=False,
61
+ )
62
+ btn = gr.Button("Generate image").style(
63
+ margin=False,
64
+ rounded=(False, True, True, False),
65
+ full_width=False,
66
+ )
67
+
68
+ gallery = gr.Gallery(
69
+ label="Generated images", show_label=False, elem_id="gallery"
70
+ ).style(grid=[2], height="auto")
71
+
72
+ btn.click(infer, inputs=[text, api_key_input], outputs=[gallery])
73
+
74
+
75
+
76
+ demo.launch()