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