ehristoforu commited on
Commit
d968f38
1 Parent(s): cc337e2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +216 -0
app.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import io
4
+ import random
5
+ import os
6
+ from PIL import Image
7
+
8
+ API_URL = os.getenv("HF_MODEL_API_URL")
9
+ API_TOKEN = os.getenv("HF_READ_TOKEN")
10
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
11
+
12
+ def query(prompt, is_negative=False, steps=50, cfg_scale=7, seed=None):
13
+ payload = {
14
+ "inputs": prompt+"8k",
15
+ "is_negative": is_negative,
16
+ "steps": steps,
17
+ "cfg_scale": cfg_scale,
18
+ "seed": seed if seed is not None else random.randint(-1, 2147483647)
19
+ }
20
+ # Вставьте ваш код для запроса к модели и получения изображения
21
+ image_bytes = requests.post(API_URL, headers=headers, json=payload).content
22
+ image = Image.open(io.BytesIO(image_bytes))
23
+ return image
24
+
25
+
26
+ css = """
27
+ .gradio-container {
28
+ font-family: 'IBM Plex Sans', sans-serif;
29
+ }
30
+ .gr-button {
31
+ color: white;
32
+ border-color: black;
33
+ background: black;
34
+ }
35
+ input[type='range'] {
36
+ accent-color: black;
37
+ }
38
+ .dark input[type='range'] {
39
+ accent-color: #dfdfdf;
40
+ }
41
+ .gradio-container {
42
+ max-width: 730px !important;
43
+ margin: auto;
44
+ padding-top: 1.5rem;
45
+ }
46
+ #gallery {
47
+ min-height: 22rem;
48
+ margin-bottom: 15px;
49
+ margin-left: auto;
50
+ margin-right: auto;
51
+ border-bottom-right-radius: .5rem !important;
52
+ border-bottom-left-radius: .5rem !important;
53
+ }
54
+ #gallery>div>.h-full {
55
+ min-height: 20rem;
56
+ }
57
+ .details:hover {
58
+ text-decoration: underline;
59
+ }
60
+ .gr-button {
61
+ white-space: nowrap;
62
+ }
63
+ .gr-button:focus {
64
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
65
+ outline: none;
66
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
67
+ --tw-border-opacity: 1;
68
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
69
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
70
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
71
+ --tw-ring-opacity: .5;
72
+ }
73
+ #advanced-btn {
74
+ font-size: .7rem !important;
75
+ line-height: 19px;
76
+ margin-top: 12px;
77
+ margin-bottom: 12px;
78
+ padding: 2px 8px;
79
+ border-radius: 14px !important;
80
+ }
81
+ #advanced-options {
82
+ display: none;
83
+ margin-bottom: 20px;
84
+ }
85
+ .footer {
86
+ margin-bottom: 45px;
87
+ margin-top: 35px;
88
+ text-align: center;
89
+ border-bottom: 1px solid #e5e5e5;
90
+ }
91
+ .footer>p {
92
+ font-size: .8rem;
93
+ display: inline-block;
94
+ padding: 0 10px;
95
+ transform: translateY(10px);
96
+ background: white;
97
+ }
98
+ .dark .footer {
99
+ border-color: #303030;
100
+ }
101
+ .dark .footer>p {
102
+ background: #0b0f19;
103
+ }
104
+ .acknowledgments h4{
105
+ margin: 1.25em 0 .25em 0;
106
+ font-weight: bold;
107
+ font-size: 115%;
108
+ }
109
+ .animate-spin {
110
+ animation: spin 1s linear infinite;
111
+ }
112
+ @keyframes spin {
113
+ from {
114
+ transform: rotate(0deg);
115
+ }
116
+ to {
117
+ transform: rotate(360deg);
118
+ }
119
+ }
120
+ #share-btn-container {padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; max-width: 13rem; margin-left: auto;}
121
+ div#share-btn-container > div {flex-direction: row;background: black;align-items: center}
122
+ #share-btn-container:hover {background-color: #060606}
123
+ #share-btn {all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.5rem !important; padding-bottom: 0.5rem !important;right:0;}
124
+ #share-btn * {all: unset}
125
+ #share-btn-container div:nth-child(-n+2){width: auto !important;min-height: 0px !important;}
126
+ #share-btn-container .wrap {display: none !important}
127
+ #share-btn-container.hidden {display: none!important}
128
+
129
+ .gr-form{
130
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
131
+ }
132
+ #prompt-container{
133
+ gap: 0;
134
+ }
135
+ #prompt-container .form{
136
+ border-top-right-radius: 0;
137
+ border-bottom-right-radius: 0;
138
+ }
139
+ #gen-button{
140
+ border-top-left-radius:0;
141
+ border-bottom-left-radius:0;
142
+ }
143
+ #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
144
+ #component-16{border-top-width: 1px!important;margin-top: 1em}
145
+ .image_duplication{position: absolute; width: 100px; left: 50px}
146
+ .tabitem{border: 0 !important}
147
+ """
148
+
149
+ with gr.Blocks(css=css) as sdxl:
150
+ gr.HTML(
151
+ """
152
+ <div style="text-align: center; margin: 0 auto;">
153
+ <div
154
+ style="
155
+ display: inline-flex;
156
+ align-items: center;
157
+ gap: 0.8rem;
158
+ font-size: 1.75rem;
159
+ "
160
+ >
161
+ <svg
162
+ width="0.65em"
163
+ height="0.65em"
164
+ viewBox="0 0 115 115"
165
+ fill="none"
166
+ xmlns="http://www.w3.org/2000/svg"
167
+ >
168
+ <rect width="23" height="23" fill="white"></rect>
169
+ <rect y="69" width="23" height="23" fill="white"></rect>
170
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
171
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
172
+ <rect x="46" width="23" height="23" fill="white"></rect>
173
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
174
+ <rect x="69" width="23" height="23" fill="black"></rect>
175
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
176
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
177
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
178
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
179
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
180
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
181
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
182
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
183
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
184
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
185
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
186
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
187
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
188
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
189
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
190
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
191
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
192
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
193
+ </svg>
194
+ <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
195
+ 🎨 New Super-Fast SDXL
196
+ </h1>
197
+ </div>
198
+ <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
199
+ SDXL is a high quality text-to-image model from Stability AI. This demo is running on <a style="text-decoration: underline;" href="https://huggingface.co/docs/api-inference/quicktour">HuggingFace Inference API ⚡</a>, to achieve efficient and cost-effective inference of 1024×1024 images. <a href="https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0" target="_blank">What model?</a>
200
+ </p>
201
+ </div>
202
+ """
203
+ )
204
+
205
+ with gr.Row():
206
+ with gr.Column(scale=1):
207
+ text_prompt = gr.Textbox(label="Prompt", placeholder="a cute cat", lines=1, elem_id="prompt-text-input")
208
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1, elem_id="negative-prompt-text-input")
209
+ text_button = gr.Button("Generate", variant='primary', elem_id="gen-button")
210
+ with gr.Row():
211
+ with gr.Column(scale=1):
212
+ image_output = gr.Image(type="pil", label="Output Image", elem_id="gallery")
213
+
214
+ text_button.click(query, inputs=[text_prompt, negative_prompt], outputs=image_output)
215
+
216
+ sdxl.launch(show_api=False)