cppowboy commited on
Commit
2362a20
·
1 Parent(s): 6cc7fd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -336
app.py CHANGED
@@ -7,346 +7,61 @@ from PIL import Image
7
  import re
8
  import os
9
  import requests
 
 
 
10
 
11
- from share_btn import community_icon_html, loading_icon_html, share_js
12
-
13
-
14
- is_gpu_busy = False
15
  def infer(prompt, negative, scale):
16
- global is_gpu_busy
17
-
18
- images = []
19
- for _ in range(4):
20
- try:
21
- url = os.environ.get('SERVICE_URL', 'https://modelbest.cn/')
22
- resp = requests.post(url, headers={
23
- "X-Model-Best-Model": "viscpm-paint-balance",
24
- "X-Model-Best-Trace-ID": "test-trace",
25
- }, json={
26
- "question": prompt,
27
- }).json()
28
- image_base64 = resp['data']['response']
29
- images.append(image_base64)
30
- except Exception as e:
31
- print(e)
32
- return images
33
-
34
-
35
- css = """
36
- .gradio-container {
37
- font-family: 'IBM Plex Sans', sans-serif;
38
- }
39
- .gr-button {
40
- color: white;
41
- border-color: black;
42
- background: black;
43
- }
44
- input[type='range'] {
45
- accent-color: black;
46
- }
47
- .dark input[type='range'] {
48
- accent-color: #dfdfdf;
49
- }
50
- .container {
51
- max-width: 730px;
52
- margin: auto;
53
- padding-top: 1.5rem;
54
- }
55
- #gallery {
56
- min-height: 22rem;
57
- margin-bottom: 15px;
58
- margin-left: auto;
59
- margin-right: auto;
60
- border-bottom-right-radius: .5rem !important;
61
- border-bottom-left-radius: .5rem !important;
62
- }
63
- #gallery>div>.h-full {
64
- min-height: 20rem;
65
- }
66
- .details:hover {
67
- text-decoration: underline;
68
- }
69
- .gr-button {
70
- white-space: nowrap;
71
- }
72
- .gr-button:focus {
73
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
74
- outline: none;
75
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
76
- --tw-border-opacity: 1;
77
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
78
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
79
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
80
- --tw-ring-opacity: .5;
81
- }
82
- #advanced-btn {
83
- font-size: .7rem !important;
84
- line-height: 19px;
85
- margin-top: 12px;
86
- margin-bottom: 12px;
87
- padding: 2px 8px;
88
- border-radius: 14px !important;
89
- }
90
- #advanced-options {
91
- display: none;
92
- margin-bottom: 20px;
93
- }
94
- .footer {
95
- margin-bottom: 45px;
96
- margin-top: 35px;
97
- text-align: center;
98
- border-bottom: 1px solid #e5e5e5;
99
- }
100
- .footer>p {
101
- font-size: .8rem;
102
- display: inline-block;
103
- padding: 0 10px;
104
- transform: translateY(10px);
105
- background: white;
106
- }
107
- .dark .footer {
108
- border-color: #303030;
109
- }
110
- .dark .footer>p {
111
- background: #0b0f19;
112
- }
113
- .acknowledgments h4{
114
- margin: 1.25em 0 .25em 0;
115
- font-weight: bold;
116
- font-size: 115%;
117
- }
118
- .animate-spin {
119
- animation: spin 1s linear infinite;
120
- }
121
- @keyframes spin {
122
- from {
123
- transform: rotate(0deg);
124
- }
125
- to {
126
- transform: rotate(360deg);
127
- }
128
- }
129
- #share-btn-container {
130
- display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
131
- margin-top: 10px;
132
- margin-left: auto;
133
- }
134
- #share-btn {
135
- all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
136
- }
137
- #share-btn * {
138
- all: unset;
139
- }
140
- #share-btn-container div:nth-child(-n+2){
141
- width: auto !important;
142
- min-height: 0px !important;
143
- }
144
- #share-btn-container .wrap {
145
- display: none !important;
146
- }
147
-
148
- .gr-form{
149
- flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
150
- }
151
- #prompt-container{
152
- gap: 0;
153
- }
154
- #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
155
- #component-16{border-top-width: 1px!important;margin-top: 1em}
156
- .image_duplication{position: absolute; width: 100px; left: 50px}
157
- """
158
-
159
- block = gr.Blocks(css=css)
160
-
161
- examples = [
162
- [
163
- 'A high tech solarpunk utopia in the Amazon rainforest',
164
- 'low quality',
165
- 9
166
- ],
167
- [
168
- 'A pikachu fine dining with a view to the Eiffel Tower',
169
- 'low quality',
170
- 9
171
- ],
172
- [
173
- 'A mecha robot in a favela in expressionist style',
174
- 'low quality, 3d, photorealistic',
175
- 9
176
- ],
177
- [
178
- 'an insect robot preparing a delicious meal',
179
- 'low quality, illustration',
180
- 9
181
- ],
182
- [
183
- "A small cabin on top of a snowy mountain in the style of Disney, artstation",
184
- 'low quality, ugly',
185
- 9
186
- ],
187
- ]
188
-
189
-
190
- with block:
191
- gr.HTML(
192
- """
193
- <div style="text-align: center; margin: 0 auto;">
194
- <div
195
- style="
196
- display: inline-flex;
197
- align-items: center;
198
- gap: 0.8rem;
199
- font-size: 1.75rem;
200
- "
201
- >
202
- <svg
203
- width="0.65em"
204
- height="0.65em"
205
- viewBox="0 0 115 115"
206
- fill="none"
207
- xmlns="http://www.w3.org/2000/svg"
208
- >
209
- <rect width="23" height="23" fill="white"></rect>
210
- <rect y="69" width="23" height="23" fill="white"></rect>
211
- <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
212
- <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
213
- <rect x="46" width="23" height="23" fill="white"></rect>
214
- <rect x="46" y="69" width="23" height="23" fill="white"></rect>
215
- <rect x="69" width="23" height="23" fill="black"></rect>
216
- <rect x="69" y="69" width="23" height="23" fill="black"></rect>
217
- <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
218
- <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
219
- <rect x="115" y="46" width="23" height="23" fill="white"></rect>
220
- <rect x="115" y="115" width="23" height="23" fill="white"></rect>
221
- <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
222
- <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
223
- <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
224
- <rect x="92" y="69" width="23" height="23" fill="white"></rect>
225
- <rect x="69" y="46" width="23" height="23" fill="white"></rect>
226
- <rect x="69" y="115" width="23" height="23" fill="white"></rect>
227
- <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
228
- <rect x="46" y="46" width="23" height="23" fill="black"></rect>
229
- <rect x="46" y="115" width="23" height="23" fill="black"></rect>
230
- <rect x="46" y="69" width="23" height="23" fill="black"></rect>
231
- <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
232
- <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
233
- <rect x="23" y="69" width="23" height="23" fill="black"></rect>
234
- </svg>
235
- <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
236
- Stable Diffusion 2.1 Demo
237
- </h1>
238
- </div>
239
- <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
240
- Stable Diffusion 2.1 is the latest text-to-image model from StabilityAI. <a style="text-decoration: underline;" href="https://huggingface.co/spaces/stabilityai/stable-diffusion-1">Access Stable Diffusion 1 Space here</a><br>For faster generation and API
241
- access you can try
242
- <a
243
- href="http://beta.dreamstudio.ai/"
244
- style="text-decoration: underline;"
245
- target="_blank"
246
- >DreamStudio Beta</a
247
- >.</a>
248
- </p>
249
- </div>
250
- """
251
- )
252
- with gr.Group():
253
- with gr.Box():
254
- with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
255
- with gr.Column():
256
- text = gr.Textbox(
257
- label="Enter your prompt",
258
- show_label=False,
259
- max_lines=1,
260
- placeholder="Enter your prompt",
261
- elem_id="prompt-text-input",
262
- ).style(
263
- border=(True, False, True, True),
264
- rounded=(True, False, False, True),
265
- container=False,
266
- )
267
- negative = gr.Textbox(
268
- label="Enter your negative prompt",
269
- show_label=False,
270
- max_lines=1,
271
- placeholder="Enter a negative prompt",
272
- elem_id="negative-prompt-text-input",
273
- ).style(
274
- border=(True, False, True, True),
275
- rounded=(True, False, False, True),
276
- container=False,
277
- )
278
- btn = gr.Button("Generate image").style(
279
- margin=False,
280
- rounded=(False, True, True, False),
281
- full_width=False,
282
  )
 
 
 
 
 
 
 
 
 
283
 
284
  gallery = gr.Gallery(
285
- label="Generated images", show_label=False, elem_id="gallery"
286
- ).style(grid=[2], height="auto")
287
-
288
- with gr.Group(elem_id="container-advanced-btns"):
289
- #advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
290
- with gr.Group(elem_id="share-btn-container"):
291
- community_icon = gr.HTML(community_icon_html)
292
- loading_icon = gr.HTML(loading_icon_html)
293
- share_button = gr.Button("Share to community", elem_id="share-btn")
294
-
295
- with gr.Accordion("Advanced settings", open=False):
296
- # gr.Markdown("Advanced settings are temporarily unavailable")
297
- # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
298
- # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
299
- guidance_scale = gr.Slider(
300
- label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
301
- )
302
- # seed = gr.Slider(
303
- # label="Seed",
304
- # minimum=0,
305
- # maximum=2147483647,
306
- # step=1,
307
- # randomize=True,
308
- # )
309
-
310
- ex = gr.Examples(examples=examples, fn=infer, inputs=[text, negative, guidance_scale], outputs=[gallery, community_icon, loading_icon, share_button], cache_examples=False)
311
- ex.dataset.headers = [""]
312
- negative.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
313
- text.submit(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
314
- btn.click(infer, inputs=[text, negative, guidance_scale], outputs=[gallery], postprocess=False)
315
-
316
- #advanced_button.click(
317
- # None,
318
- # [],
319
- # text,
320
- # _js="""
321
- # () => {
322
- # const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
323
- # options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
324
- # }""",
325
- #)
326
- share_button.click(
327
- None,
328
- [],
329
- [],
330
- _js=share_js,
331
- )
332
- gr.HTML(
333
- """
334
- <div class="footer">
335
- <p>Model by <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">StabilityAI</a> - backend running JAX on TPUs due to generous support of <a href="https://sites.research.google/trc/about/" style="text-decoration: underline;" target="_blank">Google TRC program</a> - Gradio Demo by 🤗 Hugging Face
336
- </p>
337
- </div>
338
- """
339
- )
340
- with gr.Accordion(label="License", open=False):
341
- gr.HTML(
342
- """<div class="acknowledgments">
343
- <p><h4>LICENSE</h4>
344
- The model is licensed with a <a href="https://huggingface.co/stabilityai/stable-diffusion-2/blob/main/LICENSE-MODEL" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL++</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>
345
- <p><h4>Biases and content acknowledgment</h4>
346
- 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 model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and 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>
347
- </div>
348
- """
349
- )
350
 
 
351
 
352
- block.queue(concurrency_count=80, max_size=100).launch(max_threads=150)
 
 
7
  import re
8
  import os
9
  import requests
10
+ import threading
11
+ import base64
12
+ import io
13
 
14
+ sem = threading.Semaphore()
 
 
 
15
  def infer(prompt, negative, scale):
16
+ try:
17
+ url = os.environ.get('SERVICE_URL', 'https://modelbest.cn/')
18
+ sem.acquire()
19
+ resp = requests.post(url, headers={
20
+ "X-Model-Best-Model": "viscpm-paint-balance",
21
+ "X-Model-Best-Trace-ID": "test-trace",
22
+ }, json={
23
+ "question": prompt,
24
+ "negative_prompt": negative,
25
+ "num_images_per_prompt": 4,
26
+ })
27
+ sem.release()
28
+ resp = resp.json()
29
+ images = resp['data']['response']
30
+ images = [Image.open(io.BytesIO(base64.b64decode(encoded_image))).convert("RGB") for encoded_image in images]
31
+ return images
32
+ except Exception as e:
33
+ print(e)
34
+ return []
35
+
36
+
37
+ with gr.Blocks() as demo:
38
+ gr.Markdown('<div align="center"><big><b>百亿参数量中英双语多模态大模型VisCPM</b></big></div>')
39
+ with gr.Column(variant="panel"):
40
+ with gr.Row(variant="compact"):
41
+ with gr.Column(variant="compact"):
42
+ text = gr.Textbox(
43
+ label="Enter your prompt",
44
+ show_label=False,
45
+ max_lines=1,
46
+ placeholder="输入提示词",
47
+ ).style(
48
+ container=False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  )
50
+ neg_text = gr.Textbox(
51
+ label="Enter your negative prompt",
52
+ show_label=False,
53
+ max_lines=1,
54
+ placeholder="输入负向提示词(你不想生成的内容)",
55
+ ).style(
56
+ container=False,
57
+ )
58
+ btn = gr.Button("生成图像").style(full_width=False)
59
 
60
  gallery = gr.Gallery(
61
+ label="Generated images", show_label=False, elem_id="gallery", height="1024"
62
+ ).style(columns=[2], rows=[2], object_fit="contain")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ btn.click(infer, [text, neg_text], gallery)
65
 
66
+ if __name__ == "__main__":
67
+ demo.launch(server_port=8888, server_name="0.0.0.0")