juancopi81 commited on
Commit
043f26d
β€’
1 Parent(s): 380b34f

First commit

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +240 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Multilingual Stable Diffusion
3
- emoji: 🐒
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
 
1
  ---
2
  title: Multilingual Stable Diffusion
3
+ emoji: πŸ’₯
4
  colorFrom: red
5
  colorTo: pink
6
  sdk: gradio
app.py ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import DiffusionPipeline
4
+ from transformers import (
5
+ pipeline,
6
+ MBart50TokenizerFast,
7
+ MBartForConditionalGeneration,
8
+ )
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ device_dict = {"cuda": 0, "cpu": -1}
12
+
13
+ # Add language detection pipeline
14
+ language_detection_model_ckpt = "papluca/xlm-roberta-base-language-detection"
15
+ language_detection_pipeline = pipeline("text-classification",
16
+ model=language_detection_model_ckpt,
17
+ device=device_dict[device])
18
+
19
+ # Add model for language translation
20
+ trans_tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-one-mmt")
21
+ trans_model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-one-mmt").to(device)
22
+
23
+ model_id = "CompVis/stable-diffusion-v1-4"
24
+
25
+ pipe = DiffusionPipeline.from_pretrained(
26
+ "CompVis/stable-diffusion-v1-4",
27
+ custom_pipeline="multilingual_stable_diffusion",
28
+ detection_pipeline=language_detection_pipeline,
29
+ translation_model=trans_model,
30
+ translation_tokenizer=trans_tokenizer,
31
+ revision="fp16",
32
+ torch_dtype=torch.float16,
33
+ )
34
+
35
+ pipe.enable_attention_slicing()
36
+ pipe = pipe.to(device)
37
+
38
+ #torch.backends.cudnn.benchmark = True
39
+ num_samples = 2
40
+
41
+ def infer(prompt):
42
+ output = pipe([prompt] * num_samples)
43
+ return output.images
44
+
45
+ css = """
46
+ .gradio-container {
47
+ font-family: 'IBM Plex Sans', sans-serif;
48
+ }
49
+ .gr-button {
50
+ color: white;
51
+ border-color: black;
52
+ background: black;
53
+ }
54
+ input[type='range'] {
55
+ accent-color: black;
56
+ }
57
+ .dark input[type='range'] {
58
+ accent-color: #dfdfdf;
59
+ }
60
+ .container {
61
+ max-width: 730px;
62
+ margin: auto;
63
+ padding-top: 1.5rem;
64
+ }
65
+ #gallery {
66
+ min-height: 22rem;
67
+ margin-bottom: 15px;
68
+ margin-left: auto;
69
+ margin-right: auto;
70
+ border-bottom-right-radius: .5rem !important;
71
+ border-bottom-left-radius: .5rem !important;
72
+ }
73
+ #gallery>div>.h-full {
74
+ min-height: 20rem;
75
+ }
76
+ .details:hover {
77
+ text-decoration: underline;
78
+ }
79
+ .gr-button {
80
+ white-space: nowrap;
81
+ }
82
+ .gr-button:focus {
83
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
84
+ outline: none;
85
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
86
+ --tw-border-opacity: 1;
87
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
88
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
89
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
90
+ --tw-ring-opacity: .5;
91
+ }
92
+ #advanced-btn {
93
+ font-size: .7rem !important;
94
+ line-height: 19px;
95
+ margin-top: 12px;
96
+ margin-bottom: 12px;
97
+ padding: 2px 8px;
98
+ border-radius: 14px !important;
99
+ }
100
+ #advanced-options {
101
+ display: none;
102
+ margin-bottom: 20px;
103
+ }
104
+ .footer {
105
+ margin-bottom: 45px;
106
+ margin-top: 35px;
107
+ text-align: center;
108
+ border-bottom: 1px solid #e5e5e5;
109
+ }
110
+ .footer>p {
111
+ font-size: .8rem;
112
+ display: inline-block;
113
+ padding: 0 10px;
114
+ transform: translateY(10px);
115
+ background: white;
116
+ }
117
+ .dark .footer {
118
+ border-color: #303030;
119
+ }
120
+ .dark .footer>p {
121
+ background: #0b0f19;
122
+ }
123
+ .acknowledgments h4{
124
+ margin: 1.25em 0 .25em 0;
125
+ font-weight: bold;
126
+ font-size: 115%;
127
+ }
128
+ #container-advanced-btns{
129
+ display: flex;
130
+ flex-wrap: wrap;
131
+ justify-content: space-between;
132
+ align-items: center;
133
+ }
134
+ .animate-spin {
135
+ animation: spin 1s linear infinite;
136
+ }
137
+ @keyframes spin {
138
+ from {
139
+ transform: rotate(0deg);
140
+ }
141
+ to {
142
+ transform: rotate(360deg);
143
+ }
144
+ }
145
+ #share-btn-container {
146
+ 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;
147
+ }
148
+ #share-btn {
149
+ 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;
150
+ }
151
+ #share-btn * {
152
+ all: unset;
153
+ }
154
+ .gr-form{
155
+ flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
156
+ }
157
+ #prompt-container{
158
+ gap: 0;
159
+ }
160
+ #generated_id{
161
+ min-height: 700px
162
+ }
163
+ """
164
+ block = gr.Blocks(css=css)
165
+
166
+ examples = [
167
+ [
168
+ 'Una casa en la playa en un atardecer lluvioso'
169
+ ],
170
+ [
171
+ 'Ein Hund, der Orange isst'
172
+ ],
173
+ [
174
+ "Photo d'un restaurant parisien"
175
+ ],
176
+ ]
177
+
178
+ with block as demo:
179
+ gr.HTML(
180
+ """
181
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
182
+ <div
183
+ style="
184
+ display: inline-flex;
185
+ align-items: center;
186
+ gap: 0.8rem;
187
+ font-size: 1.75rem;
188
+ "
189
+ >
190
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
191
+ Multilingual Stable Diffusion
192
+ </h1>
193
+ </div>
194
+ <p style="margin-bottom: 10px; font-size: 94%">
195
+ Stable Diffusion Pipeline that supports prompts in 50 different languages.
196
+ </p>
197
+ </div>
198
+ """
199
+ )
200
+ with gr.Group():
201
+ with gr.Box():
202
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
203
+
204
+ text = gr.Textbox(
205
+ label="Enter your prompt", show_label=False, max_lines=1
206
+ ).style(
207
+ border=(True, False, True, True),
208
+ rounded=(True, False, False, True),
209
+ container=False,
210
+ )
211
+ btn = gr.Button("Run").style(
212
+ margin=False,
213
+ rounded=(False, True, True, False),
214
+ )
215
+
216
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="generated_id").style(
217
+ grid=[2], height="auto"
218
+ )
219
+
220
+ ex = gr.Examples(examples=examples, fn=infer, inputs=[text], outputs=gallery, cache_examples=True)
221
+ ex.dataset.headers = [""]
222
+
223
+ text.submit(infer, inputs=[text], outputs=gallery)
224
+ btn.click(infer, inputs=[text], outputs=gallery)
225
+
226
+ gr.HTML(
227
+ """
228
+ <div class="footer">
229
+ <p>Stable Diffusion model that supports multiple languages by <a href="https://huggingface.co/juancopi81" style="text-decoration: underline;" target="_blank">juancopi81</a>
230
+ </p>
231
+ </div>
232
+ <div class="acknowledgments">
233
+ <p><h4>LICENSE</h4>
234
+ The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-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>
235
+ <p><h4>Biases and content acknowledgment</h4>
236
+ 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>
237
+ </div>
238
+ """
239
+ )
240
+ demo.queue(max_size=25).launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ diffusers==0.7
2
+ transformers==4.24
3
+ torch