Spaces:
Runtime error
Runtime error
chenjian
commited on
Commit
·
4cd14af
1
Parent(s):
7c4ee27
first commit
Browse files- README.md +5 -4
- app.py +199 -0
- ernie_vilg/README.md +795 -0
- ernie_vilg/__pycache__/module.cpython-37.pyc +0 -0
- ernie_vilg/module.py +244 -0
- ernie_vilg/requirements.txt +2 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.1.7
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: ERNIE-ViLG
|
3 |
+
emoji: 📉
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.1.7
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
import paddlehub as hub
|
4 |
+
|
5 |
+
|
6 |
+
model = hub.Module(directory='ernie_vilg')
|
7 |
+
|
8 |
+
def inference(text_prompts, style):
|
9 |
+
try:
|
10 |
+
results = model.generate_image(
|
11 |
+
text_prompts=text_prompts, style=style, visualization=False)
|
12 |
+
return 'Success', results[:6]
|
13 |
+
except Exception as e:
|
14 |
+
error_text = str(e)
|
15 |
+
return error_text, None
|
16 |
+
return
|
17 |
+
|
18 |
+
|
19 |
+
title="ERNIE-ViLG"
|
20 |
+
|
21 |
+
description="ERNIE-ViLG model, which supports text-to-image task."
|
22 |
+
|
23 |
+
css = """
|
24 |
+
.gradio-container {
|
25 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
26 |
+
}
|
27 |
+
.gr-button {
|
28 |
+
color: white;
|
29 |
+
border-color: black;
|
30 |
+
background: black;
|
31 |
+
}
|
32 |
+
input[type='range'] {
|
33 |
+
accent-color: black;
|
34 |
+
}
|
35 |
+
.dark input[type='range'] {
|
36 |
+
accent-color: #dfdfdf;
|
37 |
+
}
|
38 |
+
.container {
|
39 |
+
max-width: 730px;
|
40 |
+
margin: auto;
|
41 |
+
padding-top: 1.5rem;
|
42 |
+
}
|
43 |
+
#gallery {
|
44 |
+
min-height: 22rem;
|
45 |
+
margin-bottom: 15px;
|
46 |
+
margin-left: auto;
|
47 |
+
margin-right: auto;
|
48 |
+
border-bottom-right-radius: .5rem !important;
|
49 |
+
border-bottom-left-radius: .5rem !important;
|
50 |
+
}
|
51 |
+
#gallery>div>.h-full {
|
52 |
+
min-height: 20rem;
|
53 |
+
}
|
54 |
+
.details:hover {
|
55 |
+
text-decoration: underline;
|
56 |
+
}
|
57 |
+
.gr-button {
|
58 |
+
white-space: nowrap;
|
59 |
+
}
|
60 |
+
.gr-button:focus {
|
61 |
+
border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
62 |
+
outline: none;
|
63 |
+
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
64 |
+
--tw-border-opacity: 1;
|
65 |
+
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
66 |
+
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
67 |
+
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
68 |
+
--tw-ring-opacity: .5;
|
69 |
+
}
|
70 |
+
.footer {
|
71 |
+
margin-bottom: 45px;
|
72 |
+
margin-top: 35px;
|
73 |
+
text-align: center;
|
74 |
+
border-bottom: 1px solid #e5e5e5;
|
75 |
+
}
|
76 |
+
.footer>p {
|
77 |
+
font-size: .8rem;
|
78 |
+
display: inline-block;
|
79 |
+
padding: 0 10px;
|
80 |
+
transform: translateY(10px);
|
81 |
+
background: white;
|
82 |
+
}
|
83 |
+
.dark .footer {
|
84 |
+
border-color: #303030;
|
85 |
+
}
|
86 |
+
.dark .footer>p {
|
87 |
+
background: #0b0f19;
|
88 |
+
}
|
89 |
+
.prompt h4{
|
90 |
+
margin: 1.25em 0 .25em 0;
|
91 |
+
font-weight: bold;
|
92 |
+
font-size: 115%;
|
93 |
+
}
|
94 |
+
"""
|
95 |
+
|
96 |
+
block = gr.Blocks(css=css)
|
97 |
+
|
98 |
+
examples = [
|
99 |
+
[
|
100 |
+
'戴着眼镜的猫',
|
101 |
+
'油画'
|
102 |
+
],
|
103 |
+
[
|
104 |
+
'日落时的城市天际线,史前遗迹风格',
|
105 |
+
'油画'
|
106 |
+
],
|
107 |
+
[
|
108 |
+
'一只猫坐在椅子上,戴着一副墨镜, low poly 风格',
|
109 |
+
'卡通'
|
110 |
+
],
|
111 |
+
]
|
112 |
+
|
113 |
+
with block:
|
114 |
+
gr.HTML(
|
115 |
+
"""
|
116 |
+
<div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
117 |
+
<div
|
118 |
+
style="
|
119 |
+
display: inline-flex;
|
120 |
+
align-items: center;
|
121 |
+
gap: 0.8rem;
|
122 |
+
font-size: 1.75rem;
|
123 |
+
margin-bottom: 10px;
|
124 |
+
justify-content: center;
|
125 |
+
"
|
126 |
+
>
|
127 |
+
<img src="https://user-images.githubusercontent.com/22424850/187387422-f6c9ccab-7fda-416e-a24d-7d6084c46f67.jpg" alt="Paddlehub" width="40%">
|
128 |
+
</div>
|
129 |
+
<div
|
130 |
+
style="
|
131 |
+
display: inline-flex;
|
132 |
+
align-items: center;
|
133 |
+
gap: 0.8rem;
|
134 |
+
font-size: 1.75rem;
|
135 |
+
margin-bottom: 10px;
|
136 |
+
justify-content: center;
|
137 |
+
">
|
138 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;">
|
139 |
+
ERNIE-ViLG Demo
|
140 |
+
</h1>
|
141 |
+
</div>
|
142 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
143 |
+
ERNIE-ViLG is a state-of-the-art text-to-image model that generates
|
144 |
+
images from Chinese text.
|
145 |
+
</p>
|
146 |
+
</div>
|
147 |
+
"""
|
148 |
+
)
|
149 |
+
with gr.Group():
|
150 |
+
with gr.Box():
|
151 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
152 |
+
text = gr.Textbox(
|
153 |
+
label="Prompt (Chinese)",
|
154 |
+
show_label=False,
|
155 |
+
max_lines=1,
|
156 |
+
placeholder="Enter your Chinese prompt",
|
157 |
+
).style(
|
158 |
+
border=(True, False, True, True),
|
159 |
+
rounded=(True, False, False, True),
|
160 |
+
container=False,
|
161 |
+
)
|
162 |
+
btn = gr.Button("Generate image").style(
|
163 |
+
margin=False,
|
164 |
+
rounded=(False, True, True, False),
|
165 |
+
)
|
166 |
+
styles = gr.Dropdown(label="style(风格)", choices=['水彩','油画', '粉笔画', '卡通', '蜡笔画', '儿童画', '探索无限'], value='油画')
|
167 |
+
gallery = gr.Gallery(
|
168 |
+
label="Generated images", show_label=False, elem_id="gallery"
|
169 |
+
).style(grid=[2, 3], height="auto")
|
170 |
+
status_text = gr.Textbox(
|
171 |
+
label="Process status(处理状态)",
|
172 |
+
show_label=True,
|
173 |
+
max_lines=1,
|
174 |
+
interactive=False
|
175 |
+
)
|
176 |
+
|
177 |
+
ex = gr.Examples(examples=examples, fn=inference, inputs=[text, styles], outputs=gallery, cache_examples=False)
|
178 |
+
ex.dataset.headers = [""]
|
179 |
+
|
180 |
+
|
181 |
+
text.submit(inference, inputs=[text, styles], outputs=[status_text, gallery])
|
182 |
+
btn.click(inference, inputs=[text, styles], outputs=[status_text, gallery])
|
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(concurrency_count=100).launch()
|
ernie_vilg/README.md
ADDED
@@ -0,0 +1,795 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<p align="center">
|
2 |
+
<img src="https://user-images.githubusercontent.com/22424850/187387422-f6c9ccab-7fda-416e-a24d-7d6084c46f67.jpg" width = "80%" hspace='10'/>
|
3 |
+
|
4 |
+
# PaddleHub ERNIE-ViLG
|
5 |
+
|
6 |
+
# 目录
|
7 |
+
1. [模型基本信息](#一模型基本信息)
|
8 |
+
2. [安装](#二安装)
|
9 |
+
3. [模型API预测](#三模型api预测)
|
10 |
+
4. [服务部署](#四服务部署)
|
11 |
+
5. [更新历史](#五更新历史)
|
12 |
+
6. [Prompt 指南](#六-prompt-指南)
|
13 |
+
|
14 |
+
## 一、模型基本信息
|
15 |
+
|
16 |
+
|模型名称|ernie_vilg|
|
17 |
+
| :--- | :---: |
|
18 |
+
|类别|图像-文图生成|
|
19 |
+
|网络|ERNIE-ViLG|
|
20 |
+
|数据集|-|
|
21 |
+
|是否支持Fine-tuning|否|
|
22 |
+
|模型大小|-|
|
23 |
+
|最新更新日期|2022-08-02|
|
24 |
+
|数据指标|-|
|
25 |
+
|
26 |
+
### 应用效果展示
|
27 |
+
|
28 |
+
- 输入文本 "戴眼镜的猫" 风格 "油画"
|
29 |
+
|
30 |
+
- 输出图像
|
31 |
+
<p align="center">
|
32 |
+
<img src="https://user-images.githubusercontent.com/22424850/187395109-8ab830bb-4559-41a2-97a1-0a45d217abd6.png" width = "80%" hspace='10'/>
|
33 |
+
<br />
|
34 |
+
|
35 |
+
|
36 |
+
### 模型介绍
|
37 |
+
|
38 |
+
文心ERNIE-ViLG参数规模达到100亿,是目前为止全球最大规模中文跨模态生成模型,在文本生成图像、图像描述等跨模态生成任务上效果全球领先,在图文生成领域MS-COCO、COCO-CN、AIC-ICC等数据集上取得最好效果。你可以输入一段文本描述以及生成风格,模型就会根据输入的内容自动创作出符合要求的图像。
|
39 |
+
|
40 |
+
## 二、安装
|
41 |
+
|
42 |
+
- ### 1、环境依赖
|
43 |
+
|
44 |
+
- paddlepaddle >= 2.0.0
|
45 |
+
|
46 |
+
- paddlehub >= 2.2.0 | [如何安装PaddleHub](../../../../docs/docs_ch/get_start/installation.rst)
|
47 |
+
|
48 |
+
- ### 2、安装
|
49 |
+
|
50 |
+
- ```shell
|
51 |
+
$ hub install ernie_vilg
|
52 |
+
```
|
53 |
+
- 如您安装时遇到问题,可参考:[零基础windows安装](../../../../docs/docs_ch/get_start/windows_quickstart.md)
|
54 |
+
| [零基础Linux安装](../../../../docs/docs_ch/get_start/linux_quickstart.md) | [零基础MacOS安装](../../../../docs/docs_ch/get_start/mac_quickstart.md)
|
55 |
+
|
56 |
+
|
57 |
+
## 三、模型API预测
|
58 |
+
|
59 |
+
- ### 1、命令行预测
|
60 |
+
|
61 |
+
- ```shell
|
62 |
+
$ hub run ernie_vilg --text_prompts "宁静的小镇" --style "油画" --output_dir ernie_vilg_out
|
63 |
+
```
|
64 |
+
|
65 |
+
- ### 2、预测代码示例
|
66 |
+
|
67 |
+
- ```python
|
68 |
+
import paddlehub as hub
|
69 |
+
|
70 |
+
module = hub.Module(name="ernie_vilg")
|
71 |
+
text_prompts = ["宁静的小镇"]
|
72 |
+
images = module.generate_image(text_prompts=text_prompts, style='油画', output_dir='./ernie_vilg_out/')
|
73 |
+
```
|
74 |
+
|
75 |
+
- ### 3、API
|
76 |
+
|
77 |
+
- ```python
|
78 |
+
def generate_image(
|
79 |
+
text_prompts:str,
|
80 |
+
style: Optional[str] = "油画",
|
81 |
+
topk: Optional[int] = 6,
|
82 |
+
output_dir: Optional[str] = 'ernievilg_output')
|
83 |
+
```
|
84 |
+
|
85 |
+
- 文图生成API,生成文本描述内容的图像。
|
86 |
+
|
87 |
+
- **参数**
|
88 |
+
|
89 |
+
- text_prompts(str): 输入的语句,描述想要生成的图像的内容。
|
90 |
+
- style(Optional[str]): 生成图像的风格,当前支持'油画','水彩','粉笔画','卡通','儿童画','蜡笔画','探索无限'。
|
91 |
+
- topk(Optional[int]): 保存前多少张图,最多保存6张。
|
92 |
+
- output_dir(Optional[str]): 保存输出图像的目录,默认为"ernievilg_output"。
|
93 |
+
|
94 |
+
|
95 |
+
- **返回**
|
96 |
+
- images(List(PIL.Image)): 返回生成的所有图像列表,PIL的Image格式。
|
97 |
+
|
98 |
+
## 四、服务部署
|
99 |
+
|
100 |
+
- PaddleHub Serving可以部署一个在线文图生成服务。
|
101 |
+
|
102 |
+
- ### 第一步:启动PaddleHub Serving
|
103 |
+
|
104 |
+
- 运行启动命令:
|
105 |
+
- ```shell
|
106 |
+
$ hub serving start -m ernie_vilg
|
107 |
+
```
|
108 |
+
|
109 |
+
- 这样就完成了一个文图生成的在线服务API的部署,默认端口号为8866。
|
110 |
+
|
111 |
+
- **NOTE:** 如使用GPU预测,则需要在启动服务之前,请设置CUDA\_VISIBLE\_DEVICES环境变量,否则不用设置。
|
112 |
+
|
113 |
+
- ### 第二步:发送预测请求
|
114 |
+
|
115 |
+
- 配置好服务端,以下数行代码即可实现发送预测请求,获取预测结果。
|
116 |
+
|
117 |
+
- ```python
|
118 |
+
import requests
|
119 |
+
import json
|
120 |
+
import cv2
|
121 |
+
import base64
|
122 |
+
from io import BytesIO
|
123 |
+
from PIL import Image
|
124 |
+
|
125 |
+
# 发送HTTP请求
|
126 |
+
data = {'text_prompts': '巨大的白色城堡'}
|
127 |
+
headers = {"Content-type": "application/json"}
|
128 |
+
url = "http://127.0.0.1:8866/predict/ernie_vilg"
|
129 |
+
r = requests.post(url=url, headers=headers, data=json.dumps(data))
|
130 |
+
|
131 |
+
# 获取返回结果
|
132 |
+
for i, result in enumerate(r.json()["results"]):
|
133 |
+
image = Image.open(BytesIO(base64.b64decode(result)))
|
134 |
+
image.save('result_{}.png'.format(i))
|
135 |
+
|
136 |
+
|
137 |
+
## 五、更新历史
|
138 |
+
|
139 |
+
* 1.0.0
|
140 |
+
|
141 |
+
初始发布
|
142 |
+
|
143 |
+
```shell
|
144 |
+
$ hub install ernie_vilg == 1.0.0
|
145 |
+
```
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
|
150 |
+
## 六、 Prompt 指南
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
这是一份如何调整 Prompt 得到更漂亮的图片的经验性文档。我们的结果和经验都来源于[文心 ERNIE-ViLG Demo](https://wenxin.baidu.com/moduleApi/ernieVilg) 和[社区的资料](#related-work)。
|
155 |
+
|
156 |
+
什么是 Prompt?Prompt 是输入到 Demo 中的文字,可以是一个实体,例如猫;也可以是一串富含想象力的文字,例如:『夕阳日落时,天边有巨大的云朵,海面波涛汹涌,风景,胶片感』。不同的 Prompt 对于生成的图像质量影响非常大。所以也就有了下面所有的 Prompt 的一些经验性技巧。
|
157 |
+
|
158 |
+
|  |
|
159 |
+
| :----------------------------------------------------------: |
|
160 |
+
| 蒙娜丽莎,赛博朋克,宝丽来,33毫米,蒸汽波艺术 |
|
161 |
+
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
+
## 前言
|
166 |
+
|
167 |
+
Prompt 的重要性如此重要,以至于我们需要构造一个示例来进行一次说明。
|
168 |
+
|
169 |
+
如下图,[文心 ERNIE-ViLG Demo](https://wenxin.baidu.com/moduleApi/ernieVilg) 中,『卡通』模式下,输入的 Prompt 为『橘猫』,以及 『卡通』模型式下『极乐迪斯科里的猫, 故障艺术』两个示例,能够看出来后者的细节更多,呈现的图片也更加的风格化。
|
170 |
+
|
171 |
+
开放风格限制(本质上就是在 Prompt 中不加入风格控制词),即下图图3,得到的图片细节更多、也更加真实,同时还保留了比较强烈的风格元素。所以后面的所有内容,都将围绕着如何构造更好的 Prompt 进行资料的整理。
|
172 |
+
|
173 |
+
|  |  |  |
|
174 |
+
| :----------------------------------------------------------: | :----------------------------------------------------------: | ------------------------------------------------------------ |
|
175 |
+
| “橘猫”(卡通) | “极乐迪斯科里的猫, 故障艺术”(卡通) | “极乐迪斯科里的猫, 故障艺术” (探索无限) |
|
176 |
+
|
177 |
+
|  |
|
178 |
+
| :----------------------------: |
|
179 |
+
| 极乐迪斯科里的猫,故障艺术 |
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
## 呼吁与准则
|
184 |
+
|
185 |
+
机器生成图片的最终目的还是便捷地为人类创造美的作品。而技术不是十全十美的,不能保证每次生成的图像都能够尽善尽美。因此呼吁所有相关玩家,如果想分享作品,那就分享那些美感爆棚的作品!
|
186 |
+
|
187 |
+
算法生成的图片难免会受到数据的影响,从而导致生成的图片是有数据偏见的。因此在分享机器生成图片到社交媒体之前,请三思当前的图片是不是含有:令人不适的、暴力的、色情的内容。如果有以上的内容请自行承担法律后果。
|
188 |
+
|
189 |
+
|
190 |
+
<span id = "p-design"> </span>
|
191 |
+
## Prompt 的设计
|
192 |
+
|
193 |
+
如何设计 Prompt,下文大概会通过4个方面来说明:[Prompt 公式](#p-eq),[Prompt 原则](#p-principle),[Prompt 主体](#p-entity)、[Prompt 修饰词](#p-modifier)。
|
194 |
+
|
195 |
+
需要注意的是,这里的 Prompt 公式仅仅是个入门级别的参考,是经验的简单总结,在熟悉了 Prompt 的原理之后,可以尽情的发挥脑洞修改 Prompt。
|
196 |
+
|
197 |
+
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
<span id = "p-eq"> </span>
|
202 |
+
## Prompt 公式
|
203 |
+
|
204 |
+
$$
|
205 |
+
Prompt = [形容词] [主语] ,[细节设定], [修饰语或者艺术家]
|
206 |
+
$$
|
207 |
+
|
208 |
+
按照这个公式,我们首先构造一个形容词加主语的案例。 这里我构造的是 戴着眼镜的猫, 风格我选择的是油画风格,然后我再添加一些细节设定,这里我给的是 漂浮在宇宙中, 可以看到 ,猫猫的后面出现了很多天体。
|
209 |
+
|
210 |
+
|  |  |  |
|
211 |
+
| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
|
212 |
+
| “戴着眼镜的猫”(油画) | “戴着眼镜的猫,漂浮在宇宙中”(油画) | “戴着眼镜的猫,漂浮在宇宙中,高更风格”(油画) |
|
213 |
+
|
214 |
+
最后我们想让我们的照片风格更加有艺术性的效果, 我们选择的艺术家是高更, 可以看到图像的画风有了更强的艺术风格。
|
215 |
+
|
216 |
+
|
217 |
+
|
218 |
+
<span id = "p-principle"> </span>
|
219 |
+
## Prompt 设计原则
|
220 |
+
|
221 |
+
### Prompt 简单原则: 清楚地陈述
|
222 |
+
|
223 |
+
除了公式之外,也有一些简单的 Prompt设计原则分享给大家:即**清楚的陈述**。
|
224 |
+
|
225 |
+
例如我们如果是简单的输入风景的话,往往模型不知道我们想要的风景是什么样子的(下图1)。我们要去尽量的幻想风景的样子,然后变成语言描述。 例如我想像的是日落时,海边的风景, ��我就构造了 Prompt 『夕阳日落时,阳光落在云层上,海面波光粼粼,风景』(下图2)。 进一步的,我想风格化我的图像,所以我在结尾的部分,增加了『胶片感』来让图片的色彩更加好看一些(下图3)。但是云彩的细节丢失了一些,进一步的我再增加天边巨大云朵这一个细节,让我的图片朝着我想要的样子靠的更进一步(下图4)。
|
226 |
+
|
227 |
+
|  |  |  |  |
|
228 |
+
| :------------------------: | :----------------------------------------------: | :------------------------------------------------------: | -------------------------------------------------------- |
|
229 |
+
| “风景” | “夕阳日落时,阳光落在云层上,海面波光粼粼,风景” | “夕阳日落时,阳光落在云层上,海面波涛汹涌,风景,胶片感” | 夕阳日落时,天边有巨大的云朵,海面波涛汹涌,风景,胶片感 |
|
230 |
+
|
231 |
+
|
232 |
+
|
233 |
+
<span id = "p-entity"> </span>
|
234 |
+
## Prompt 主体的选择
|
235 |
+
|
236 |
+
Prompt 的主体可以是千奇百怪、各种各样的。这里我挑了几个简单的容易出效果的主体示例和一些能够营造特殊氛围的氛围词来激发大家的灵感。
|
237 |
+
|
238 |
+
|
239 |
+
|
240 |
+
|  |  |  |  |
|
241 |
+
| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
|
242 |
+
| 宇航员 | 孤岛 | 白色城堡 | 机器人 |
|
243 |
+
|  |  |  |  |
|
244 |
+
| 巫师 | 罗马城 | 海鸥 | 气球 |
|
245 |
+
|
246 |
+
|
247 |
+
|
248 |
+
|
249 |
+
|
250 |
+
|  |  |  |
|
251 |
+
| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
|
252 |
+
| …日落,霓虹灯…薄雾 | …烟… | …燃烧漩涡, …烟雾和碎片 |
|
253 |
+
|  |  |  |
|
254 |
+
| …废墟… | 光之… | 巨大的… |
|
255 |
+
|
256 |
+
|
257 |
+
|
258 |
+
<span id = "p-modifier"> </span>
|
259 |
+
## Prompt 修饰词
|
260 |
+
|
261 |
+
如果想让生成的图片更加的艺术化、风格话,可以考虑在 Prompt 中添加艺术修饰词。艺术修饰词可以是一些美术风格(例如表现主义、抽象主义等),也可以是一些美学词汇(蒸汽波艺术、故障艺术等),也可以是一些摄影术语(80mm摄像头、浅景深等),也可以是一些绘图软件(虚幻引擎、C4D等��。
|
262 |
+
|
263 |
+
按照这样的规律,我们在两个输入基准上 :
|
264 |
+
|
265 |
+
> 一只猫坐在椅子上,戴着一副墨镜
|
266 |
+
>
|
267 |
+
> 日落时的城市天际线
|
268 |
+
>
|
269 |
+
|
270 |
+
通过构造『输入 + Prompt 修饰词』来展示不同修饰词的效果 (这里的策略参考了[资料](https://docs.google.com/document/d/11WlzjBT0xRpQhP9tFMtxzd0q6ANIdHPUBkMV-YB043U/edit))。
|
271 |
+
|
272 |
+
需要注意的是,不是所有的 Prompt 对于所有的修饰词都会发生反应。所以查阅 Prompt 修饰词的过程中,会发现部分的 Prompt 修饰词只能对两个基准中的一个生效。这是很正常的,因为 Prompt 的调优是一个反复的试错的过程。接下来,大家结合如下的 Prompt 修饰词, Happy Prompting 吧!
|
273 |
+
|
274 |
+
|
275 |
+
|
276 |
+
### 复古未来主义风格
|
277 |
+
|
278 |
+
|  |  |
|
279 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
280 |
+
| 一只猫坐在椅子上,戴着一副墨镜,复古未来主义风格 | 日落时的城市天际线,复古未来主义风格 |
|
281 |
+
|
282 |
+
|
283 |
+
|
284 |
+
### 粉彩朋克风格
|
285 |
+
|
286 |
+
|  |  |
|
287 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
288 |
+
| 一只猫坐在椅子上,戴着一副墨镜,粉彩朋克风格 | 日落时的城市天际线,粉彩朋克风格 |
|
289 |
+
|
290 |
+
### 史前遗迹风格
|
291 |
+
|
292 |
+
|  |  |
|
293 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
294 |
+
| 一只猫坐在椅子上,戴着一副墨镜,史前遗迹风格 | 日落时的城市天际线,史前遗迹风格 |
|
295 |
+
|
296 |
+
|
297 |
+
|
298 |
+
|
299 |
+
### 波普艺术风格
|
300 |
+
|
301 |
+
|  |  |
|
302 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
303 |
+
| 一只猫坐在椅子上,戴着一副墨镜,波普艺术风格 | 日落时的城市天际线,后世界末日风格 |
|
304 |
+
|
305 |
+
|
306 |
+
|
307 |
+
### 迷幻风格
|
308 |
+
|
309 |
+
|  |  |
|
310 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
311 |
+
| 一只猫坐在椅子上,戴着一副墨镜,迷幻风格 | 日落时的城市天际线,迷幻风格 |
|
312 |
+
|
313 |
+
|
314 |
+
### 赛博朋克风格
|
315 |
+
|
316 |
+
|  |  |
|
317 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
318 |
+
| 一只猫坐在椅子上,戴着一副墨镜,赛博朋克风格 | 日落时的城市天际线,赛博朋克风格 |
|
319 |
+
|
320 |
+
|
321 |
+
### 纸箱风格
|
322 |
+
|
323 |
+
|
324 |
+
|  |  |
|
325 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
326 |
+
| 一只猫坐在椅子上,戴着一副墨镜,纸箱风格 | 日落时的城市天际线,纸箱风格 |
|
327 |
+
|
328 |
+
### 未来主义风格
|
329 |
+
|
330 |
+
|  |  |
|
331 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
332 |
+
| 一只猫坐在椅子上,戴着一副墨镜,未来主义风格 | 一只猫坐在椅子上,戴着一副墨镜,未来主义风格 |
|
333 |
+
|
334 |
+
|
335 |
+
|
336 |
+
### 抽象技术风格
|
337 |
+
|
338 |
+
|  |  |
|
339 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
340 |
+
| 一只猫坐在椅子上,戴着一副墨镜,抽象技术风格 | 日落时的城市天际线,抽象技术风格 |
|
341 |
+
|
342 |
+
|
343 |
+
|
344 |
+
|
345 |
+
### 海滩兔风格
|
346 |
+
|
347 |
+
|
348 |
+
|  |  |
|
349 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
350 |
+
| 一只猫坐在椅子上,戴着一副墨镜,海滩兔风格 | 日落时的城市天际线,海滩兔风格 |
|
351 |
+
|
352 |
+
|
353 |
+
### 粉红公主风格
|
354 |
+
|
355 |
+
|  |  |
|
356 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
357 |
+
| 一只猫坐在椅子上,戴着一副墨镜,粉红公主风格 | 日落时的城市天际线,粉红公主风格 |
|
358 |
+
|
359 |
+
|
360 |
+
### 嬉皮士风格
|
361 |
+
|
362 |
+
|  |  |
|
363 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
364 |
+
| 一只猫坐在椅子上,戴着一副墨镜,嬉皮士风格 | 日落时的城市天际线,嬉皮士风格 |
|
365 |
+
|
366 |
+
### 幻象之城风格
|
367 |
+
|
368 |
+
|  |  |
|
369 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
370 |
+
| 一只猫坐在椅子上,戴着一副墨镜,幻象之城风格 | 日落时的城市天际线,幻象之城风格 |
|
371 |
+
|
372 |
+
|
373 |
+
### 美人鱼风格
|
374 |
+
|
375 |
+
|  |  |
|
376 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
377 |
+
| 一只猫坐在椅子上,戴着一副墨镜,美人鱼风格 | 日落时的城市天际线,美人鱼风格 |
|
378 |
+
|
379 |
+
|
380 |
+
### 迷宫物语风格
|
381 |
+
|
382 |
+
|
383 |
+
|  |  |
|
384 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
385 |
+
| 一只猫坐在椅子上,戴着一副墨镜,迷宫物语风格 | 日落时的城市天际线,迷宫物语风格 |
|
386 |
+
|
387 |
+
### 仙女风格
|
388 |
+
|
389 |
+
|
390 |
+
|  |  |
|
391 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
392 |
+
| 一只猫坐在椅子上,戴着一副墨镜,仙女风格 | 日落时的城市天际线,仙女风格 |
|
393 |
+
|
394 |
+
|
395 |
+
|
396 |
+
|
397 |
+
|
398 |
+
### Low Poly 风格
|
399 |
+
|
400 |
+
|  |  |
|
401 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
402 |
+
| 一只猫坐在椅子上,戴着一副墨镜, low poly 风格 | 日落时的城市天际线, low-poly |
|
403 |
+
|
404 |
+
|
405 |
+
|
406 |
+
|
407 |
+
### 浮世绘风格
|
408 |
+
|
409 |
+
|  |  |
|
410 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
411 |
+
| 一只猫坐在椅子上,戴着一副墨镜,浮世绘风格 | 日落时的城市天际线,浮世绘风格 |
|
412 |
+
|
413 |
+
### 矢量心风格
|
414 |
+
|
415 |
+
|  |  |
|
416 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
417 |
+
| 一只猫坐在椅子上,戴着一副墨镜,矢量心风格 | 日落时的城市天际线,矢量心风格 |
|
418 |
+
|
419 |
+
|
420 |
+
### 摩托车手风格
|
421 |
+
|
422 |
+
|
423 |
+
|  |  |
|
424 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
425 |
+
| 一只猫坐在椅子上,戴着一副墨镜,摩托车手风格 | 日落时的城市天际线,摩托车手风格 |
|
426 |
+
|
427 |
+
|
428 |
+
|
429 |
+
### 孟菲斯公司风格
|
430 |
+
|
431 |
+
|
432 |
+
|  |  |
|
433 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
434 |
+
| 一只猫坐在椅子上,戴着一副墨镜,孟菲斯公司风格 | 日落时的城市天际线,孟菲斯公司风格 |
|
435 |
+
|
436 |
+
|
437 |
+
### 泥塑风格
|
438 |
+
|
439 |
+
|
440 |
+
|  |  |
|
441 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
442 |
+
| 一只猫坐在椅子上,戴着一副墨镜, 泥塑风格 | 日落时的城市天际线, 泥塑风格 |
|
443 |
+
|
444 |
+
|
445 |
+
|
446 |
+
|
447 |
+
### 苔藓风格
|
448 |
+
|
449 |
+
|  |  |
|
450 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
451 |
+
| 一只猫坐在椅子上,戴着一副墨镜,苔藓风格 | 日落时的城市天际线,苔藓风格 |
|
452 |
+
|
453 |
+
|
454 |
+
|
455 |
+
### 新浪潮风格
|
456 |
+
|
457 |
+
|  |  |
|
458 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
459 |
+
| 一只猫坐在椅子上,戴着一副墨镜,新浪潮风格 | 日落时的城市天际线,新浪潮风格 |
|
460 |
+
|
461 |
+
### 嘻哈风格
|
462 |
+
|
463 |
+
|  |  |
|
464 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
465 |
+
| 一只猫坐在椅子上,戴着一副墨镜,嘻哈风格 | 日落时的城市天际线,嘻哈风格 |
|
466 |
+
|
467 |
+
### 矢量图
|
468 |
+
|
469 |
+
|  |  |
|
470 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
471 |
+
| 一只猫坐在椅子上,戴着一副墨镜, 矢量图 | 日落时的城市天际线, 矢量图 |
|
472 |
+
|
473 |
+
### 铅笔艺术
|
474 |
+
|
475 |
+
|
476 |
+
|  |  |
|
477 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
478 |
+
| 一只猫坐在椅子上,戴着一副墨镜, 铅笔艺术 | 日落时的城市天际线, 铅笔艺术 |
|
479 |
+
|
480 |
+
|
481 |
+
### 女巫店风格
|
482 |
+
|
483 |
+
|  |  |
|
484 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
485 |
+
| 一只猫坐在椅子上,戴着一副墨镜,女巫店风格 | 日落时的城市天际线,女巫店风格 |
|
486 |
+
|
487 |
+
|
488 |
+
|
489 |
+
### 4D 建模
|
490 |
+
|
491 |
+
|
492 |
+
|  |  |
|
493 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
494 |
+
| 一只猫坐在椅子上,戴着一副墨镜, 4D 建模 | 日落时的城市天际线, 4D 建模 |
|
495 |
+
|
496 |
+
|
497 |
+
|
498 |
+
### 水彩墨风格
|
499 |
+
|
500 |
+
|
501 |
+
|  |  |
|
502 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
503 |
+
| 一只猫坐在椅子上,戴着一副墨镜, 水彩墨风格 | 日落时的城市天际线, 水彩墨风格 |
|
504 |
+
|
505 |
+
|
506 |
+
|
507 |
+
### 酸性精灵风格
|
508 |
+
|
509 |
+
|  |  |
|
510 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
511 |
+
| 一只猫坐在椅子上,戴着一副墨镜,酸性精灵风格 | 日落时的城市天际线,酸性精灵风格 |
|
512 |
+
|
513 |
+
|
514 |
+
### 海盗风格
|
515 |
+
|
516 |
+
|  |  |
|
517 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
518 |
+
| 日落时的城市天际线,海盗风格 | 一只猫坐在椅子上,戴着一副墨镜,海盗风格 |
|
519 |
+
|
520 |
+
|
521 |
+
|
522 |
+
### 古埃及风格
|
523 |
+
|
524 |
+
|
525 |
+
|  |  |
|
526 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
527 |
+
| 一只猫坐在椅子上,戴着一副墨镜,古埃及风格 | 日落时的城市天际线,古埃及风格 |
|
528 |
+
|
529 |
+
### 风帽风格
|
530 |
+
|
531 |
+
|
532 |
+
|  |  |
|
533 |
+
| --------------------------------------------------------- | ------------------------------------------------------------ |
|
534 |
+
| 一只猫坐在椅子上,戴着一副墨镜,风帽风格 | 日落时的城市天际线,风帽风格 |
|
535 |
+
|
536 |
+
### 装饰艺术风格
|
537 |
+
|
538 |
+
|
539 |
+
|  |  |
|
540 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
541 |
+
| 一只猫坐在椅子上,戴着一副墨镜,装饰艺术风格 | 日落时的城市天际线,装饰艺术风格 |
|
542 |
+
|
543 |
+
### 极光风格
|
544 |
+
|
545 |
+
|
546 |
+
|  |  |
|
547 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
548 |
+
| 一只猫坐在椅子上,戴着一副墨镜,极光风格 | 日落时的城市天际线,极光风格 |
|
549 |
+
|
550 |
+
### 秋天风格
|
551 |
+
|
552 |
+
|
553 |
+
|  |  |
|
554 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
555 |
+
| 日落时的城市天际线,秋天风格 | 一只猫坐在椅子上,戴着一副墨镜,秋天风格 |
|
556 |
+
|
557 |
+
### 巴洛克风格
|
558 |
+
|
559 |
+
|
560 |
+
|  |  |
|
561 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
562 |
+
| 一只猫坐在椅子上,戴着一副墨镜,巴洛克风格 | 日落时的城市天际线,巴洛克风格 |
|
563 |
+
|
564 |
+
### 立体主义风格
|
565 |
+
|
566 |
+
|  |  |
|
567 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
568 |
+
| 一只猫坐在椅子上,戴着一副墨镜,立体主义风格 | 日落时的城市天际线,立体主义风格 |
|
569 |
+
|
570 |
+
|
571 |
+
### 黑暗自然主义风格
|
572 |
+
|
573 |
+
|  |  |
|
574 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
575 |
+
| 一只猫坐在椅子上,戴着一副墨镜,黑暗自然主义风格 | 日落时的城市天际线,黑暗自然主义风格 |
|
576 |
+
|
577 |
+
### 表现主义风格
|
578 |
+
|
579 |
+
|  |  |
|
580 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
581 |
+
| 一只猫坐在椅子上,戴着一副墨镜,表现主义风格 | 日落时的城市天际线,表现主义风格 |
|
582 |
+
|
583 |
+
### 野兽派风格
|
584 |
+
|
585 |
+
|  |  |
|
586 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
587 |
+
| 一只猫坐在椅子上,戴着一副墨镜,野兽派风格 | 日落时的城市天际线,野兽派风格 |
|
588 |
+
|
589 |
+
### 鬼魂风格
|
590 |
+
|
591 |
+
|  |  |
|
592 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
593 |
+
| 一只猫坐在椅子上,戴着一副墨镜,鬼魂风格 | 日落时的城市天际线,鬼魂风格 |
|
594 |
+
|
595 |
+
### 印象主义风格
|
596 |
+
|
597 |
+
|  |  |
|
598 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
599 |
+
| 一只猫坐在椅子上,戴着一副墨镜,印象主义风格 | 日落时的城市天际线,印象主义风格 |
|
600 |
+
|
601 |
+
### 卡瓦伊风格
|
602 |
+
|
603 |
+
|  |  |
|
604 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
605 |
+
| 一只猫坐在椅子上,戴着一副墨镜,卡瓦伊风格 | 日落时的城市天际线,卡瓦伊风格 |
|
606 |
+
|
607 |
+
### 极简主义风格
|
608 |
+
|
609 |
+
|  |  |
|
610 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
611 |
+
| 一只猫坐在椅子上,戴着一副墨镜,极简主义风格 | 日落时的城市天际线,极简主义风格 |
|
612 |
+
|
613 |
+
### 水井惠郎风格
|
614 |
+
|
615 |
+
|  |  |
|
616 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
617 |
+
| 一只猫坐在椅子上,戴着一副墨镜,水井惠郎风格 | 日落时的城市天际线,水井惠郎风格 |
|
618 |
+
|
619 |
+
### 照片写实风格
|
620 |
+
|
621 |
+
|  |  |
|
622 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
623 |
+
| 一只猫坐在椅子上,戴着一副墨镜,照片写实风格 | 日落时的城市天际线,照片写实风格 |
|
624 |
+
|
625 |
+
|
626 |
+
### 像素可爱风格
|
627 |
+
|
628 |
+
|  |  |
|
629 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
630 |
+
| 一只猫坐在椅子上,戴着一副墨镜,像素可爱风格 | 日落时的城市天际线,像素可爱风格 |
|
631 |
+
|
632 |
+
|
633 |
+
|
634 |
+
### 雨天风格
|
635 |
+
|
636 |
+
|  |  |
|
637 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
638 |
+
| 日落时的城市天际线,雨天风格 | 一只猫坐在椅子上,戴着一副墨镜,雨天风格 |
|
639 |
+
|
640 |
+
### 湿漉漉的风格
|
641 |
+
|
642 |
+
|  |  |
|
643 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
644 |
+
| 一只猫坐在椅子上,戴着一副墨镜,湿漉漉的风格 | 日落时的城市天际线,湿漉漉的风格 |
|
645 |
+
|
646 |
+
|
647 |
+
### 维京人风格
|
648 |
+
|
649 |
+
|  |  |
|
650 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
651 |
+
| 一只猫坐在椅子上,戴着一副墨镜,维京人风格 | 日落时的城市天际线,维京人风格 |
|
652 |
+
|
653 |
+
### 后印象主义
|
654 |
+
|
655 |
+
|
656 |
+
|  |  |
|
657 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
658 |
+
| 一只猫坐在椅子上,戴着一副墨镜,风格:后印象主义 | 日落时的城市天际线, 风格:后印象主义-v2 |
|
659 |
+
|
660 |
+
### 素人主义
|
661 |
+
|
662 |
+
|
663 |
+
|  |  |
|
664 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
665 |
+
| 一只猫坐在椅子上,戴着一副墨镜,风格:素人主义 | 日落时的城市天际线, 风格:素人艺术 |
|
666 |
+
|
667 |
+
|
668 |
+
|
669 |
+
### 碎核风格
|
670 |
+
|
671 |
+
|
672 |
+
|  |  |
|
673 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
674 |
+
| 一只猫坐在椅子上,戴着一副墨镜,碎核风格 | 日落时的城市天际线,碎核风格 |
|
675 |
+
|
676 |
+
|
677 |
+
|
678 |
+
|
679 |
+
|
680 |
+
|
681 |
+
|
682 |
+
## Prompt 更多信息
|
683 |
+
|
684 |
+
### 概念组合
|
685 |
+
|
686 |
+

|
687 |
+
|
688 |
+
## ShowCase
|
689 |
+
|
690 |
+
更多 ShowCase 和创意 Prompt,可以参考我的[社交账号](#关注我) 或者是 http://youpromptme.cn/#/gallery/ (建设中)
|
691 |
+
|
692 |
+
### 故障艺术
|
693 |
+
|
694 |
+
|  |  |  |  |
|
695 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
|
696 |
+
| _时钟故障,时间故障,概念艺术,艺术站总部,pixiv趋势,cgsociety,蒸汽波艺术 | 巨大的纯白色城堡-油画,故障艺术 | Yggdrasil,世界树和地球融合在一起,故障艺术 | 在百货公司和工厂的高商业需��中,未来复古科幻幻想对象或设备的专业概念艺术,故障艺术 |
|
697 |
+
|
698 |
+
|
699 |
+
|
700 |
+
### 蒸汽波艺术
|
701 |
+
|
702 |
+
|  |  |  |
|
703 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
|
704 |
+
| 荒岛,蒸汽波艺术 | Christoph-Vacher和Kevin-sloan创作的广阔幻想景观,蒸汽波艺术 | 戴着眼镜的猫,蒸汽波艺术 |
|
705 |
+
|
706 |
+
|
707 |
+
### 包豪斯艺术
|
708 |
+
|
709 |
+
|  |  |
|
710 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
711 |
+
| 一只海鸥和史蒂文·西格正在进行一场凝视比赛,绘画,包豪斯 | 梵高猫头鹰,包豪斯 |
|
712 |
+
|
713 |
+
|
714 |
+
|
715 |
+
|
716 |
+
|
717 |
+
### 概念艺术
|
718 |
+
|
719 |
+
|  |  |
|
720 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
721 |
+
| 4k专业HDR-DnD幻想概念艺术一条由闪电制成的令人敬畏的龙,概念艺术 | 4k专业HDR-DnD奇幻概念艺术小鸡施展幻觉咒语,概念艺术 |
|
722 |
+
|
723 |
+
|
724 |
+
|
725 |
+
### 像素艺术
|
726 |
+
|
727 |
+
|  |  |  |  |
|
728 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
|
729 |
+
|
730 |
+
|
731 |
+
|
732 |
+
### 艺术家
|
733 |
+
|
734 |
+
|  |  |
|
735 |
+
| ------------------------------------------------------------ | ------------------------------------------------------------ |
|
736 |
+
| 萨尔瓦多·达利描绘古代文明的超现实主义梦幻油画,写实风格 | 梵高猫头鹰,蒸汽波艺术 |
|
737 |
+
|
738 |
+
|
739 |
+
|
740 |
+
|
741 |
+
## 附录
|
742 |
+
|
743 |
+
### 常见的艺术家和艺术风格整理
|
744 |
+
|
745 |
+
| 艺术类型 | 艺术家 | 常用艺术风格 |
|
746 |
+
| ---------- | ---------------------- | ---------------------- |
|
747 |
+
| 肖像画 | 文森特·梵高 | 印象主义 |
|
748 |
+
| 风景画 | 尼古拉斯·罗伊里奇 | 现实主义 |
|
749 |
+
| 风俗画 | 皮埃尔-奥古斯特·雷诺阿 | 浪漫主义 |
|
750 |
+
| 宗教绘画 | 克劳德·莫内 | 表现主义 |
|
751 |
+
| 抽象画 | 彼得·孔查洛夫斯基 | 后印象主义 |
|
752 |
+
| 都市风景画 | 卡米尔·毕沙罗 | 象征主义 |
|
753 |
+
| 素描与草图 | 约翰·辛格·萨金特 | 新艺术主义 |
|
754 |
+
| 静物 | 伦勃朗 | 巴洛克风格 |
|
755 |
+
| 裸体画 | 马克·夏加尔 | 抽象表现主义 |
|
756 |
+
| 插画 | 巴勃罗·毕加索 | 北欧文艺复兴 |
|
757 |
+
| | 古斯塔夫·多雷 | 素人艺术,原始主义 |
|
758 |
+
| | 阿尔布雷特·丢勒 | 立体主义 |
|
759 |
+
| | 鲍里斯·库斯妥基耶夫 | 洛可可 |
|
760 |
+
| | 埃德加·德加 | 色域绘画 |
|
761 |
+
| | | 波普艺术 |
|
762 |
+
| | | 文艺复兴开端 |
|
763 |
+
| | | 文艺复兴全盛期 |
|
764 |
+
| | | 极简主义 |
|
765 |
+
| | | 矫饰主义,文艺复兴晚期 |
|
766 |
+
|
767 |
+
|
768 |
+
|
769 |
+
### 常见的摄影风格词整理
|
770 |
+
|
771 |
+
| 可以加入到 Prompt 中的摄影词 | |
|
772 |
+
| ---------------------------- | ------------ |
|
773 |
+
| 浅景深 | 仰拍 |
|
774 |
+
| 负像 | 动态模糊 |
|
775 |
+
| 微距 | 高反差 |
|
776 |
+
| 双色版 | 中心构图 |
|
777 |
+
| 角度 | 逆光 |
|
778 |
+
| 三分法 | 长曝光 |
|
779 |
+
| 抓拍 | 禅宗摄影 |
|
780 |
+
| 软焦点 | 抽象微距镜头 |
|
781 |
+
| 黑白 | 暗色调 |
|
782 |
+
| 无镜反射 | 长时间曝光 |
|
783 |
+
| 双色调 | 框架,取景 |
|
784 |
+
| 颗粒图像 | |
|
785 |
+
|
786 |
+
|
787 |
+
|
788 |
+
<span id = "related-work"> </span>
|
789 |
+
### 相关链接
|
790 |
+
|
791 |
+
美学相关的词汇: https://aesthetics.fandom.com/wiki/List_of_Aesthetics
|
792 |
+
|
793 |
+
DALL-E 2 的 Prompt 技巧资料:https://docs.google.com/document/d/11WlzjBT0xRpQhP9tFMtxzd0q6ANIdHPUBkMV-YB043U/edit
|
794 |
+
|
795 |
+
DiscoDiffusion Prompt 技巧资料:https://docs.google.com/document/d/1l8s7uS2dGqjztYSjPpzlmXLjl5PM3IGkRWI3IiCuK7g/edit
|
ernie_vilg/__pycache__/module.cpython-37.pyc
ADDED
Binary file (7.61 kB). View file
|
|
ernie_vilg/module.py
ADDED
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
import argparse
|
3 |
+
import os
|
4 |
+
from functools import partial
|
5 |
+
from typing import List, Optional
|
6 |
+
import sys
|
7 |
+
import base64
|
8 |
+
from io import BytesIO
|
9 |
+
|
10 |
+
import requests
|
11 |
+
from tqdm.auto import tqdm
|
12 |
+
import time
|
13 |
+
from PIL import Image
|
14 |
+
import re
|
15 |
+
|
16 |
+
|
17 |
+
import paddlehub as hub
|
18 |
+
from paddlehub.module.module import moduleinfo
|
19 |
+
from paddlehub.module.module import runnable
|
20 |
+
from paddlehub.module.module import serving
|
21 |
+
|
22 |
+
|
23 |
+
@moduleinfo(
|
24 |
+
name="ernie_vilg",
|
25 |
+
version="1.0.0",
|
26 |
+
type="image/text_to_image",
|
27 |
+
summary="",
|
28 |
+
author="baidu-nlp",
|
29 |
+
author_email="[email protected]")
|
30 |
+
class ErnieVilG:
|
31 |
+
def __init__(self, ak=None, sk=None):
|
32 |
+
"""
|
33 |
+
:param ak: ak for applying token to request wenxin api.
|
34 |
+
:param sk: sk for applying token to request wenxin api.
|
35 |
+
"""
|
36 |
+
if ak is None or sk is None:
|
37 |
+
self.ak = 'G26BfAOLpGIRBN5XrOV2eyPA25CE01lE'
|
38 |
+
self.sk = 'txLZOWIjEqXYMU3lSm05ViW4p9DWGOWs'
|
39 |
+
else:
|
40 |
+
self.ak = ak
|
41 |
+
self.sk = sk
|
42 |
+
self.token_host = 'https://wenxin.baidu.com/younger/portal/api/oauth/token'
|
43 |
+
self.token = self._apply_token(self.ak, self.sk)
|
44 |
+
|
45 |
+
def _apply_token(self, ak, sk):
|
46 |
+
if ak is None or sk is None:
|
47 |
+
ak = self.ak
|
48 |
+
sk = self.sk
|
49 |
+
response = requests.get(self.token_host, params={'grant_type':'client_credentials', 'client_id':ak, 'client_secret':sk})
|
50 |
+
if response:
|
51 |
+
res = response.json()
|
52 |
+
if res['code'] != 0:
|
53 |
+
print('Request access token error.')
|
54 |
+
raise RuntimeError("Request access token error.")
|
55 |
+
else:
|
56 |
+
print('Request access token error.')
|
57 |
+
raise RuntimeError("Request access token error.")
|
58 |
+
return res['data']
|
59 |
+
|
60 |
+
|
61 |
+
def generate_image(self,
|
62 |
+
text_prompts,
|
63 |
+
style: Optional[str] = "油画",
|
64 |
+
topk: Optional[int] = 6,
|
65 |
+
visualization: Optional[bool] = True,
|
66 |
+
output_dir: Optional[str] = 'ernievilg_output'):
|
67 |
+
"""
|
68 |
+
Create image by text prompts using ErnieVilG model.
|
69 |
+
|
70 |
+
:param text_prompts: Phrase, sentence, or string of words and phrases describing what the image should look like.
|
71 |
+
:param style: Image stype, currently supported 油画、水彩、粉笔画、卡通、儿童画、蜡笔画、探索无限。
|
72 |
+
:param topk: Top k images to save.
|
73 |
+
:param visualization: Whether to save images or not.
|
74 |
+
:output_dir: Output directory
|
75 |
+
"""
|
76 |
+
if not os.path.exists(output_dir):
|
77 |
+
os.makedirs(output_dir, exist_ok=True)
|
78 |
+
token = self.token
|
79 |
+
create_url = 'https://wenxin.baidu.com/younger/portal/api/rest/1.0/ernievilg/v1/txt2img?from=paddlehub'
|
80 |
+
get_url = 'https://wenxin.baidu.com/younger/portal/api/rest/1.0/ernievilg/v1/getImg?from=paddlehub'
|
81 |
+
if isinstance(text_prompts, str):
|
82 |
+
text_prompts = [text_prompts]
|
83 |
+
taskids = []
|
84 |
+
for text_prompt in text_prompts:
|
85 |
+
res = requests.post(create_url, headers={'Content-Type':'application/x-www-form-urlencoded'}, data={'access_token': token,"text": text_prompt, "style": style})
|
86 |
+
res = res.json()
|
87 |
+
if res['code'] == 4001:
|
88 |
+
print('请求参数错误')
|
89 |
+
raise RuntimeError("请求参数错误")
|
90 |
+
elif res['code'] == 4002:
|
91 |
+
print('请求参数格式错误,请检查必传参数是否齐全,参数类型等')
|
92 |
+
raise RuntimeError("请求参数格式错误,请检查必传参数是否齐全,参数类型等")
|
93 |
+
elif res['code'] == 4003:
|
94 |
+
print('请求参数中,图片风格不在可选范围内')
|
95 |
+
raise RuntimeError("请求参数中,图片风格不在可选范围内")
|
96 |
+
elif res['code'] == 4004:
|
97 |
+
print('API服务内部错误,可能引起原因有请求超时、模型推理错误等')
|
98 |
+
raise RuntimeError("API服务内部错误,可能引起原因有请求超时、模型推理错误等")
|
99 |
+
elif res['code'] == 100 or res['code'] == 110 or res['code'] == 111:
|
100 |
+
token = self._apply_token(self.ak, self.sk)
|
101 |
+
res = requests.post(create_url, headers={'Content-Type':'application/x-www-form-urlencoded'}, data={'access_token': token,"text": text_prompt, "style": style})
|
102 |
+
res = res.json()
|
103 |
+
if res['code'] != 0:
|
104 |
+
print("Token失效重新请求后依然发生错误,请检查输入的参数")
|
105 |
+
raise RuntimeError("Token失效重新请求后依然发生错误,请检查输入的参数")
|
106 |
+
if res['msg'] == 'success':
|
107 |
+
taskids.append(res['data']["taskId"])
|
108 |
+
else:
|
109 |
+
print(res['msg'])
|
110 |
+
raise RuntimeError(res['msg'])
|
111 |
+
|
112 |
+
start_time = time.time()
|
113 |
+
process_bar = tqdm(total=100, unit='%')
|
114 |
+
results = {}
|
115 |
+
first_iter = True
|
116 |
+
while True:
|
117 |
+
if not taskids:
|
118 |
+
break
|
119 |
+
total_time = 0
|
120 |
+
has_done = []
|
121 |
+
for taskid in taskids:
|
122 |
+
res = requests.post(get_url, headers={'Content-Type':'application/x-www-form-urlencoded'}, data={'access_token': token,'taskId': {taskid}})
|
123 |
+
res = res.json()
|
124 |
+
if res['code'] == 4001:
|
125 |
+
print('请求参数错误')
|
126 |
+
raise RuntimeError("请求参数错误")
|
127 |
+
elif res['code'] == 4002:
|
128 |
+
print('请求参数格式错误,请检查必传参数是否齐全,参数类型等')
|
129 |
+
raise RuntimeError("请求参数格式错误,请检查必传参数是否齐全,参数类型等")
|
130 |
+
elif res['code'] == 4003:
|
131 |
+
print('请求参数中,图片风格不在可选范围内')
|
132 |
+
raise RuntimeError("请求参数中,图片风格不在可选范围内")
|
133 |
+
elif res['code'] == 4004:
|
134 |
+
print('API服务内部错误,可能引起原因有请求超时、模型推理错误等')
|
135 |
+
raise RuntimeError("API服务内部错误,可能引起原因有请求超时、模型推理错误等")
|
136 |
+
elif res['code'] == 100 or res['code'] == 110 or res['code'] == 111:
|
137 |
+
token = self._apply_token(self.ak, self.sk)
|
138 |
+
res = requests.post(get_url, headers={'Content-Type':'application/x-www-form-urlencoded'}, data={'access_token': token,'taskId': {taskid}})
|
139 |
+
res = res.json()
|
140 |
+
if res['code'] != 0:
|
141 |
+
print("Token失效重新请求后依然发生错误,请检查输入的参数")
|
142 |
+
raise RuntimeError("Token失效重新请求后依然发生错误,请检查输入的参数")
|
143 |
+
if res['msg'] == 'success':
|
144 |
+
if res['data']['status'] == 1:
|
145 |
+
has_done.append(res['data']['taskId'])
|
146 |
+
results[res['data']['text']] = {
|
147 |
+
'imgUrls': res['data']['imgUrls'],
|
148 |
+
'waiting': res['data']['waiting'],
|
149 |
+
'taskId': res['data']['taskId']
|
150 |
+
}
|
151 |
+
else:
|
152 |
+
print(res['msg'])
|
153 |
+
raise RuntimeError(res['msg'])
|
154 |
+
total_time = int(re.match('[0-9]+', str(res['data']['waiting'])).group(0)) * 60
|
155 |
+
end_time = time.time()
|
156 |
+
progress_rate = int(((end_time - start_time) / total_time*100)) if total_time != 0 else 100
|
157 |
+
if progress_rate > process_bar.n:
|
158 |
+
increase_rate = progress_rate - process_bar.n
|
159 |
+
if progress_rate >= 100:
|
160 |
+
increase_rate = 100 - process_bar.n
|
161 |
+
else:
|
162 |
+
increase_rate = 0
|
163 |
+
process_bar.update(increase_rate)
|
164 |
+
time.sleep(5)
|
165 |
+
for taskid in has_done:
|
166 |
+
taskids.remove(taskid)
|
167 |
+
end_time = time.time() #debug code
|
168 |
+
print('request task time', end_time-start_time) #debug code
|
169 |
+
start_save_time = time.time() #debug code
|
170 |
+
print('Saving Images...')
|
171 |
+
result_images = []
|
172 |
+
for text, data in results.items():
|
173 |
+
for idx, imgdata in enumerate(data['imgUrls']):
|
174 |
+
try:
|
175 |
+
image = Image.open(BytesIO(requests.get(imgdata['image']).content))
|
176 |
+
except Exception as e:
|
177 |
+
print('Download generated images error, retry one time')
|
178 |
+
try:
|
179 |
+
image = Image.open(BytesIO(requests.get(imgdata['image']).content))
|
180 |
+
except Exception:
|
181 |
+
raise RuntimeError('Download generated images failed.')
|
182 |
+
if visualization:
|
183 |
+
image.save(os.path.join(output_dir, '{}_{}.png'.format(text, idx)))
|
184 |
+
result_images.append(image)
|
185 |
+
if idx + 1 >= topk:
|
186 |
+
break
|
187 |
+
print('Done')
|
188 |
+
end_save_time = time.time() #debug code
|
189 |
+
print('Download and decode images time', end_save_time-start_save_time)
|
190 |
+
return result_images
|
191 |
+
|
192 |
+
@runnable
|
193 |
+
def run_cmd(self, argvs):
|
194 |
+
"""
|
195 |
+
Run as a command.
|
196 |
+
"""
|
197 |
+
self.parser = argparse.ArgumentParser(
|
198 |
+
description="Run the {} module.".format(self.name),
|
199 |
+
prog='hub run {}'.format(self.name),
|
200 |
+
usage='%(prog)s',
|
201 |
+
add_help=True)
|
202 |
+
self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
|
203 |
+
self.add_module_input_arg()
|
204 |
+
args = self.parser.parse_args(argvs)
|
205 |
+
if args.ak is not None and args.sk is not None:
|
206 |
+
self.ak = args.ak
|
207 |
+
self.sk = args.sk
|
208 |
+
self.token = self._apply_token(self.ak, self.sk)
|
209 |
+
results = self.generate_image(
|
210 |
+
text_prompts=args.text_prompts,
|
211 |
+
style=args.style,
|
212 |
+
topk=args.topk,
|
213 |
+
visualization=args.visualization,
|
214 |
+
output_dir=args.output_dir
|
215 |
+
)
|
216 |
+
return results
|
217 |
+
|
218 |
+
@serving
|
219 |
+
def serving_method(self, text_prompts, **kwargs):
|
220 |
+
"""
|
221 |
+
Run as a service.
|
222 |
+
"""
|
223 |
+
results_base64encoded = []
|
224 |
+
results = self.generate_image(text_prompts=text_prompts, **kwargs)
|
225 |
+
for result in results:
|
226 |
+
buffered = BytesIO()
|
227 |
+
result.save(buffered, format="png")
|
228 |
+
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
229 |
+
results_base64encoded.append(img_str)
|
230 |
+
return results_base64encoded
|
231 |
+
|
232 |
+
|
233 |
+
def add_module_input_arg(self):
|
234 |
+
"""
|
235 |
+
Add the command input options.
|
236 |
+
"""
|
237 |
+
self.arg_input_group.add_argument('--text_prompts', type=str)
|
238 |
+
self.arg_input_group.add_argument('--style', type=str, default='油画', choices=['油画','水彩','粉笔画','卡通','儿童画','蜡笔画','探索无限'], help="绘画风格")
|
239 |
+
self.arg_input_group.add_argument('--topk', type=int, default=6, help="选取保存前多少张图,最多10张")
|
240 |
+
self.arg_input_group.add_argument('--ak', type=str, default=None, help="申请文心api使用token的ak")
|
241 |
+
self.arg_input_group.add_argument('--sk', type=str, default=None, help="申请文心api使用token的sk")
|
242 |
+
self.arg_input_group.add_argument('--visualization', type=bool, default=True, help="是否保存生成的图片")
|
243 |
+
self.arg_input_group.add_argument('--output_dir', type=str, default='ernievilg_output')
|
244 |
+
|
ernie_vilg/requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
tqdm
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
paddlepaddle
|
2 |
+
paddlehub
|
3 |
+
requests
|