chenjian commited on
Commit
b194067
1 Parent(s): 738729f
Files changed (1) hide show
  1. app.py +197 -7
app.py CHANGED
@@ -6,17 +6,207 @@ import paddlehub as hub
6
  model = hub.Module(name='ernie_vilg')
7
 
8
 
9
- def inference(text_prompts):
10
  results = model.generate_image(
11
- text_prompts=text_prompts)
12
- return np.array(results[0])
13
 
14
- outputs = [
15
- gr.outputs.Image(type="numpy",label="result")
16
- ]
17
 
18
  title="ERNIE-ViLG"
19
 
20
  description="ERNIE-ViLG model, which supports text-to-image task."
21
 
22
- gr.Interface(inference, gr.inputs.Textbox(),outputs,title=title,description=description).launch(enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  model = hub.Module(name='ernie_vilg')
7
 
8
 
9
+ def inference(text_prompts, style):
10
  results = model.generate_image(
11
+ text_prompts=text_prompts, style=style)
12
+ return results[:6]
13
 
 
 
 
14
 
15
  title="ERNIE-ViLG"
16
 
17
  description="ERNIE-ViLG model, which supports text-to-image task."
18
 
19
+ css = """
20
+ .gradio-container {
21
+ font-family: 'IBM Plex Sans', sans-serif;
22
+ }
23
+ .gr-button {
24
+ color: white;
25
+ border-color: black;
26
+ background: black;
27
+ }
28
+ input[type='range'] {
29
+ accent-color: black;
30
+ }
31
+ .dark input[type='range'] {
32
+ accent-color: #dfdfdf;
33
+ }
34
+ .container {
35
+ max-width: 730px;
36
+ margin: auto;
37
+ padding-top: 1.5rem;
38
+ }
39
+ #gallery {
40
+ min-height: 22rem;
41
+ margin-bottom: 15px;
42
+ margin-left: auto;
43
+ margin-right: auto;
44
+ border-bottom-right-radius: .5rem !important;
45
+ border-bottom-left-radius: .5rem !important;
46
+ }
47
+ #gallery>div>.h-full {
48
+ min-height: 20rem;
49
+ }
50
+ .details:hover {
51
+ text-decoration: underline;
52
+ }
53
+ .gr-button {
54
+ white-space: nowrap;
55
+ }
56
+ .gr-button:focus {
57
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
58
+ outline: none;
59
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
60
+ --tw-border-opacity: 1;
61
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
62
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
63
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
64
+ --tw-ring-opacity: .5;
65
+ }
66
+ .footer {
67
+ margin-bottom: 45px;
68
+ margin-top: 35px;
69
+ text-align: center;
70
+ border-bottom: 1px solid #e5e5e5;
71
+ }
72
+ .footer>p {
73
+ font-size: .8rem;
74
+ display: inline-block;
75
+ padding: 0 10px;
76
+ transform: translateY(10px);
77
+ background: white;
78
+ }
79
+ .dark .footer {
80
+ border-color: #303030;
81
+ }
82
+ .dark .footer>p {
83
+ background: #0b0f19;
84
+ }
85
+ .prompt h4{
86
+ margin: 1.25em 0 .25em 0;
87
+ font-weight: bold;
88
+ font-size: 115%;
89
+ }
90
+ """
91
+
92
+ block = gr.Blocks(css=css)
93
+
94
+ examples = [
95
+ [
96
+ '戴着眼镜的猫',
97
+ '油画'
98
+ ],
99
+ [
100
+ '日落时的城市天际线,史前遗迹风格',
101
+ '油画'
102
+ ],
103
+ [
104
+ '一只猫坐在椅子上,戴着一副墨镜, low poly 风格',
105
+ '卡通'
106
+ ],
107
+ ]
108
+
109
+ with block:
110
+ gr.HTML(
111
+ """
112
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
113
+ <div
114
+ style="
115
+ display: inline-flex;
116
+ align-items: center;
117
+ gap: 0.8rem;
118
+ font-size: 1.75rem;
119
+ "
120
+ >
121
+ <svg
122
+ width="0.65em"
123
+ height="0.65em"
124
+ viewBox="0 0 115 115"
125
+ fill="none"
126
+ xmlns="http://www.w3.org/2000/svg"
127
+ >
128
+ <rect width="23" height="23" fill="white"></rect>
129
+ <rect y="69" width="23" height="23" fill="white"></rect>
130
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
131
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
132
+ <rect x="46" width="23" height="23" fill="white"></rect>
133
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
134
+ <rect x="69" width="23" height="23" fill="black"></rect>
135
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
136
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
137
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
138
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
139
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
140
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
141
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
142
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
143
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
144
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
145
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
146
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
147
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
148
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
149
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
150
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
151
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
152
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
153
+ </svg>
154
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
155
+ ERNIE-ViLG Demo
156
+ </h1>
157
+ </div>
158
+ <p style="margin-bottom: 10px; font-size: 94%">
159
+ ERNIE-ViLG is a state-of-the-art text-to-image model that generates
160
+ images from text.
161
+ </p>
162
+ </div>
163
+ """
164
+ )
165
+ with gr.Group():
166
+ with gr.Box():
167
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
168
+ text = gr.Textbox(
169
+ label="Prompt (Chinese)",
170
+ show_label=False,
171
+ max_lines=1,
172
+ placeholder="Enter your prompt",
173
+ ).style(
174
+ border=(True, False, True, True),
175
+ rounded=(True, False, False, True),
176
+ container=False,
177
+ )
178
+ btn = gr.Button("Generate image").style(
179
+ margin=False,
180
+ rounded=(False, True, True, False),
181
+ )
182
+ styles = gr.Dropdown(label="style", choices=['水彩','油画', '粉笔画', '卡通', '蜡笔画', '儿童画'], value='油画')
183
+ gallery = gr.Gallery(
184
+ label="Generated images", show_label=False, elem_id="gallery"
185
+ ).style(grid=[2, 3], height="auto")
186
+
187
+
188
+
189
+
190
+ ex = gr.Examples(examples=examples, fn=inference, inputs=[text, styles], outputs=gallery, cache_examples=True)
191
+ ex.dataset.headers = [""]
192
+
193
+
194
+ text.submit(inference, inputs=[text, styles], outputs=gallery)
195
+ btn.click(inference, inputs=[text, styles], outputs=gallery)
196
+ gr.HTML(
197
+ """
198
+ <div class="prompt">
199
+ <p><h4>Prompt公式</h4>
200
+ <span> Prompt = [形容词] [主语] ,[细节设定], [修饰语或者艺术家]。 </span>
201
+ 关于各部分的构造方式和效果,可以参考<a href="https://github.com/OleNet/YouPromptMe/blob/gh-pages/you-prompt-me/README.md" style="text-decoration: underline;" target="_blank">YouPromptMe指南</a>。
202
+ </p>
203
+ </div>
204
+ <div class="footer">
205
+ <p>Model by 文心大模型<a href="https://wenxin.baidu.com" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://github.com/PaddlePaddle/PaddleHub" style="text-decoration: underline;" target="_blank">PaddleHub</a> - Gradio Demo by 🤗 Hugging Face
206
+ </p>
207
+ </div>
208
+
209
+ """
210
+ )
211
+
212
+ block.queue(max_size=10).launch()