sayakpaul HF staff commited on
Commit
2ac0472
1 Parent(s): 2eec5f5

simplify the Space.

Browse files
Files changed (1) hide show
  1. app.py +25 -125
app.py CHANGED
@@ -33,6 +33,9 @@ _ = pokemon_model.text_to_image("Teddy bear", batch_size=num_images_to_gen)
33
 
34
  def generate_image_fn(prompt: str, unconditional_guidance_scale: int) -> list:
35
  start_time = time.time()
 
 
 
36
  images = pokemon_model.text_to_image(
37
  prompt,
38
  batch_size=num_images_to_gen,
@@ -40,128 +43,25 @@ def generate_image_fn(prompt: str, unconditional_guidance_scale: int) -> list:
40
  )
41
  end_time = time.time()
42
  print(f"Time taken: {end_time - start_time} seconds.")
43
- return images
44
-
45
-
46
- block = gr.Blocks(css=css)
47
-
48
-
49
- with block:
50
- gr.HTML(
51
- """
52
- <div style="text-align: center; margin: 0 auto;">
53
- <div
54
- style="
55
- display: inline-flex;
56
- align-items: center;
57
- gap: 0.8rem;
58
- font-size: 1.75rem;
59
- "
60
- >
61
- <svg
62
- width="0.65em"
63
- height="0.65em"
64
- viewBox="0 0 115 115"
65
- fill="none"
66
- xmlns="http://www.w3.org/2000/svg"
67
- >
68
- <rect width="23" height="23" fill="white"></rect>
69
- <rect y="69" width="23" height="23" fill="white"></rect>
70
- <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
71
- <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
72
- <rect x="46" width="23" height="23" fill="white"></rect>
73
- <rect x="46" y="69" width="23" height="23" fill="white"></rect>
74
- <rect x="69" width="23" height="23" fill="black"></rect>
75
- <rect x="69" y="69" width="23" height="23" fill="black"></rect>
76
- <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
77
- <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
78
- <rect x="115" y="46" width="23" height="23" fill="white"></rect>
79
- <rect x="115" y="115" width="23" height="23" fill="white"></rect>
80
- <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
81
- <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
82
- <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
83
- <rect x="92" y="69" width="23" height="23" fill="white"></rect>
84
- <rect x="69" y="46" width="23" height="23" fill="white"></rect>
85
- <rect x="69" y="115" width="23" height="23" fill="white"></rect>
86
- <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
87
- <rect x="46" y="46" width="23" height="23" fill="black"></rect>
88
- <rect x="46" y="115" width="23" height="23" fill="black"></rect>
89
- <rect x="46" y="69" width="23" height="23" fill="black"></rect>
90
- <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
91
- <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
92
- <rect x="23" y="69" width="23" height="23" fill="black"></rect>
93
- </svg>
94
- <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
95
- Stable Diffusion fine-tuned Pokemons 🐾
96
- </h1>
97
- </div>
98
- <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
99
- This Space demonstrates a fine-tuned version of the <a style="text-decoration: underline;" href="https://github.com/keras-team/keras-cv/tree/master/keras_cv/models/stable_diffusion">Stable Diffusion (v1) model shipped by KerasCV</a>. You can use the model to generate custom Pokemon-like characters. To get started either enter a text prompt or try one from the ones given below.
100
- </p>
101
- </div>
102
- """
103
- )
104
- with gr.Group():
105
- with gr.Box():
106
- with gr.Row(elem_id="prompt-container").style(
107
- mobile_collapse=False, equal_height=True
108
- ):
109
- with gr.Column():
110
- text = gr.Textbox(
111
- label="Enter your prompt",
112
- show_label=False,
113
- max_lines=1,
114
- placeholder="Enter your prompt",
115
- elem_id="prompt-text-input",
116
- ).style(
117
- border=(True, False, True, True),
118
- rounded=(True, False, False, True),
119
- container=False,
120
- )
121
- guidance_scale = gr.Slider(
122
- label="Guidance Scale", minimum=10, maximum=50, value=40, step=1.0
123
- )
124
- btn = gr.Button("Generate image").style(
125
- margin=False, rounded=(False, True, True, False), full_width=False
126
- )
127
-
128
- gallery = gr.Gallery(
129
- label="Generated images", show_label=False, elem_id="gallery"
130
- ).style(grid=[2], height="auto")
131
-
132
- with gr.Group(elem_id="container-advanced-btns"):
133
- with gr.Group(elem_id="share-btn-container"):
134
- community_icon = gr.HTML(community_icon_html)
135
- loading_icon = gr.HTML(loading_icon_html)
136
- share_button = gr.Button("Share to community", elem_id="share-btn")
137
-
138
- ex = gr.Examples(
139
- examples=examples,
140
- fn=generate_image_fn,
141
- inputs=[text, guidance_scale],
142
- outputs=[gallery, community_icon, loading_icon, share_button],
143
- cache_examples=False,
144
- )
145
- ex.dataset.headers = [""]
146
- btn.click(
147
- generate_image_fn,
148
- inputs=[text, guidance_scale],
149
- outputs=[gallery],
150
- postprocess=False,
151
- )
152
- share_button.click(None, [], [], _js=share_js)
153
- gr.HTML(
154
- """
155
- <div class="footer">
156
- <p>For details on the fine-tuning procedure, check out the <a style="text-decoration: underline;" href="https://github.com/sayakpaul/stable-diffusion-keras-ft/">corresponding repository</a>. The generated images may not be very impressive as the underlying model is still being fine-tuned as of this writing. Since this Space is running on a Tesla T4 GPU, we took advantage of mixed-precision for optimized inference. Additionally, we leveraged XLA to carve out maximum performance from TensorFlow. The front-end of this Space is adapted from the <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion">original Stable Diffusion 2.1 Space</a>.</p>
157
- </div>
158
- <div class="acknowledgments">
159
- <p><h4>LICENSE</h4>
160
- The original model is licensed with a <a href="https://raw.githubusercontent.com/CompVis/stable-diffusion/main/LICENSE" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a>.</p>
161
- <p><h4>Biases and content acknowledgment</h4>
162
- Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The oiginal model is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a>.</p>
163
- </div>
164
- """
165
- )
166
-
167
- block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)
 
33
 
34
  def generate_image_fn(prompt: str, unconditional_guidance_scale: int) -> list:
35
  start_time = time.time()
36
+ # `images is an `np.ndarray`. So we convert it to a list of ndarrays.
37
+ # Each ndarray represents a generated image.
38
+ # Reference: https://gradio.app/docs/#gallery
39
  images = pokemon_model.text_to_image(
40
  prompt,
41
  batch_size=num_images_to_gen,
 
43
  )
44
  end_time = time.time()
45
  print(f"Time taken: {end_time - start_time} seconds.")
46
+ return [image for image in images]
47
+
48
+
49
+ description = "This Space demonstrates a fine-tuned Stable Diffusion model. You can use it for generating custom pokemons. To get started, either use a prompt and pick one from the examples below. For details on the fine-tuning procedure refer to [this repository](https://github.com/sayakpaul/stable-diffusion-keras-ft/)."
50
+ article = "This Space leverages a T4 GPU to run the predictions. We use mixed-precision to speed the inference latency. We further use XLA to carve out maximum performance from TensorFlow."
51
+ gr.Interface(
52
+ generate_image_fn,
53
+ inputs=[
54
+ gr.Textbox(
55
+ label="Enter your prompt",
56
+ max_lines=1,
57
+ placeholder="cure Sundar Pichai creature",
58
+ ),
59
+ gr.Slider(value=40, minimum=8, maximum=50, step=1),
60
+ ],
61
+ outputs=gr.Gallery().style(grid=[2], height="auto"),
62
+ title="Generate custom pokemons",
63
+ description=description,
64
+ article=article,
65
+ examples=[["cute Sundar Pichai creature", 40], ["Hello kitty", 40]],
66
+ allow_flagging=False,
67
+ ).launch(enable_queue=True, cache_examples=True)