chenjian commited on
Commit
12c7dc4
1 Parent(s): 03aa3f4
Files changed (1) hide show
  1. app.py +95 -17
app.py CHANGED
@@ -1,27 +1,54 @@
1
  import numpy as np
2
  import gradio as gr
3
  import paddlehub as hub
 
4
 
5
 
6
  model = hub.Module(name='ernie_vilg')
7
- language_model = hub.Module(name='baidu_translate')
 
8
 
9
  style_list = ['水彩','油画', '粉笔画', '卡通', '蜡笔画', '儿童画', '探索无限']
10
- language_list = ['zh', 'en', 'jp', 'kor']
11
- def inference(text_prompts, language_indx, style_indx):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
  style = style_list[style_indx]
14
- if language_indx != 0:
15
- language = language_list[language_indx]
16
- text_prompts = language_model.translate(text_prompts, language, 'zh')
17
- print(text_prompts)
18
  results = model.generate_image(
19
  text_prompts=text_prompts, style=style, visualization=False)
20
- return 'Success', results[:6]
21
  except Exception as e:
22
  error_text = str(e)
23
- return error_text, None
24
- return
 
 
25
 
26
 
27
  title="ERNIE-ViLG"
@@ -108,6 +135,18 @@ examples = [
108
  '戴着眼镜的猫',
109
  '油画(Oil painting)'
110
  ],
 
 
 
 
 
 
 
 
 
 
 
 
111
  [
112
  '日落时的城市天际线,史前遗迹风格',
113
  '油画(Oil painting)'
@@ -116,6 +155,18 @@ examples = [
116
  '一只猫坐在椅子上,戴着一副墨镜, low poly 风格',
117
  '卡通(Cartoon)'
118
  ],
 
 
 
 
 
 
 
 
 
 
 
 
119
  [
120
  '一只猫坐在椅子上,戴着一副墨镜,秋天风格',
121
  '探索无限(Explore infinity)'
@@ -132,6 +183,18 @@ examples = [
132
  '一条由闪电制成的令人敬畏的龙,概念艺术',
133
  '探索无限(Explore infinity)'
134
  ],
 
 
 
 
 
 
 
 
 
 
 
 
135
  [
136
  '梵高猫头鹰,蒸汽波艺术',
137
  '探索无限(Explore infinity)'
@@ -143,7 +206,19 @@ examples = [
143
  [
144
  '夕阳日落时,阳光落在云层上,海面波涛汹涌,风景,胶片感',
145
  '探索无限(Explore infinity)'
146
- ]
 
 
 
 
 
 
 
 
 
 
 
 
147
  ]
148
 
149
  with block:
@@ -189,7 +264,7 @@ with block:
189
  label="Prompt",
190
  show_label=False,
191
  max_lines=1,
192
- placeholder="Enter your prompt",
193
  ).style(
194
  border=(True, False, True, True),
195
  rounded=(True, False, False, True),
@@ -200,7 +275,7 @@ with block:
200
  margin=False,
201
  rounded=(False, True, True, False),
202
  )
203
- language = gr.Dropdown(label="语言(language)", choices=['中文(Chineses)','英文(English)', '日文(Japanese)', '韩文(Korean)'], value='中文(Chineses)', type="index")
204
  styles = gr.Dropdown(label="风格(style)", choices=['水彩(Watercolor)','油画(Oil painting)', '粉笔画(Chalk drawing)', '卡通(Cartoon)', '蜡笔画(Crayon drawing)', '儿童画(Children\'s drawing)', '探索无限(Explore infinity)'], value='探索无限(Explore infinity)', type="index")
205
  gallery = gr.Gallery(
206
  label="Generated images", show_label=False, elem_id="gallery"
@@ -211,13 +286,16 @@ with block:
211
  max_lines=1,
212
  interactive=False
213
  )
 
 
214
 
215
- ex = gr.Examples(examples=examples, fn=inference, inputs=[text, language, styles], outputs=gallery, cache_examples=False)
216
  ex.dataset.headers = [""]
217
 
218
 
219
- text.submit(inference, inputs=[text, language, styles], outputs=[status_text, gallery])
220
- btn.click(inference, inputs=[text, language, styles], outputs=[status_text, gallery])
 
221
  gr.HTML(
222
  """
223
  <div class="prompt">
@@ -656,4 +734,4 @@ In "Explore infinity" style mode, how the image looks like is totally up to your
656
  </div>
657
  ''')
658
 
659
- block.queue(concurrency_count=500).launch()
 
1
  import numpy as np
2
  import gradio as gr
3
  import paddlehub as hub
4
+ import datetime
5
 
6
 
7
  model = hub.Module(name='ernie_vilg')
8
+ language_translation_model = hub.Module(name='baidu_translate')
9
+ language_recognition_model = hub.Module(name='baidu_language_recognition')
10
 
11
  style_list = ['水彩','油画', '粉笔画', '卡通', '蜡笔画', '儿童画', '探索无限']
12
+
13
+ tips = {"en": "Tips: The input text will be translated into Chinese for generation",
14
+ "jp": "ヒント: 入力テキストは生成のために中国語に翻訳されます",
15
+ "kor": "힌트: 입력 텍스트는 생성을 위해 중국어로 번역됩니다"}
16
+
17
+ count = 0
18
+
19
+ def translate_language(text_prompts):
20
+ global count
21
+ try:
22
+ count += 1
23
+ tips_text = None
24
+ language_code = language_recognition_model.recognize(text_prompts)
25
+ if language_code != 'zh':
26
+ text_prompts = language_translation_model.translate(text_prompts, language_code, 'zh')
27
+ except Exception as e:
28
+ error_text = str(e)
29
+ return {status_text:error_text, language_tips_text:gr.update(visible=False)}
30
+ if language_code in tips:
31
+ tips_text = tips[language_code]
32
+ else:
33
+ tips_text = tips['en']
34
+ if language_code == 'zh':
35
+ return {language_tips_text:gr.update(visible=False), translated_language:text_prompts, trigger_component: gr.update(value=count, visible=False)}
36
+ else:
37
+ return {language_tips_text:gr.update(visible=True, value=tips_text), translated_language:text_prompts, trigger_component: gr.update(value=count, visible=False)}
38
+
39
+
40
+ def inference(text_prompts, style_indx):
41
+ print(datetime.datetime.now())
42
  try:
43
  style = style_list[style_indx]
 
 
 
 
44
  results = model.generate_image(
45
  text_prompts=text_prompts, style=style, visualization=False)
 
46
  except Exception as e:
47
  error_text = str(e)
48
+ print(datetime.datetime.now())
49
+ return {status_text:error_text, gallery:None}
50
+ print(datetime.datetime.now())
51
+ return {status_text:'Success', gallery:results[:6]}
52
 
53
 
54
  title="ERNIE-ViLG"
 
135
  '戴着眼镜的猫',
136
  '油画(Oil painting)'
137
  ],
138
+ [
139
+ 'A cat with glasses',
140
+ '油画(Oil painting)'
141
+ ],
142
+ [
143
+ '眼鏡をかけた猫',
144
+ '油画(Oil painting)'
145
+ ],
146
+ [
147
+ '안경을 쓴 고양이',
148
+ '油画(Oil painting)'
149
+ ],
150
  [
151
  '日落时的城市天际线,史前遗迹风格',
152
  '油画(Oil painting)'
 
155
  '一只猫坐在椅子上,戴着一副墨镜, low poly 风格',
156
  '卡通(Cartoon)'
157
  ],
158
+ [
159
+ 'A cat sitting on a chair, wearing a pair of sunglasses, low poly style',
160
+ '油画(Oil painting)'
161
+ ],
162
+ [
163
+ '猫が椅子に座ってサングラスをかけている、low polyスタイル',
164
+ '油画(Oil painting)'
165
+ ],
166
+ [
167
+ '고양이 한 마리가 의자에 앉아 선글라스를 끼고 low poly 스타일을 하고 있다',
168
+ '油画(Oil painting)'
169
+ ],
170
  [
171
  '一只猫坐在椅子上,戴着一副墨镜,秋天风格',
172
  '探索无限(Explore infinity)'
 
183
  '一条由闪电制成的令人敬畏的龙,概念艺术',
184
  '探索无限(Explore infinity)'
185
  ],
186
+ [
187
+ 'An awesome dragon made of lightning, conceptual art',
188
+ '油画(Oil painting)'
189
+ ],
190
+ [
191
+ '稲妻で作られた畏敬の念を抱かせる竜、コンセプトアート',
192
+ '油画(Oil painting)'
193
+ ],
194
+ [
195
+ '번개로 만든 경외스러운 용, 개념 예술',
196
+ '油画(Oil painting)'
197
+ ],
198
  [
199
  '梵高猫头鹰,蒸汽波艺术',
200
  '探索无限(Explore infinity)'
 
206
  [
207
  '夕阳日落时,阳光落在云层上,海面波涛汹涌,风景,胶片感',
208
  '探索无限(Explore infinity)'
209
+ ],
210
+ [
211
+ 'Sunset, the sun falls on the clouds, the sea is rough, the scenery is filmy',
212
+ '油画(Oil painting)'
213
+ ],
214
+ [
215
+ '夕日が沈むと、雲の上に太陽の光が落ち、海面は波が荒く、風景、フィルム感',
216
+ '油画(Oil painting)'
217
+ ],
218
+ [
219
+ '석양이 질 때 햇빛이 구름 위에 떨어지고, 해수면의 파도가 용솟음치며, 풍경, 필름감',
220
+ '油画(Oil painting)'
221
+ ],
222
  ]
223
 
224
  with block:
 
264
  label="Prompt",
265
  show_label=False,
266
  max_lines=1,
267
+ placeholder="Enter your prompt, multiple languages are supported now.",
268
  ).style(
269
  border=(True, False, True, True),
270
  rounded=(True, False, False, True),
 
275
  margin=False,
276
  rounded=(False, True, True, False),
277
  )
278
+ language_tips_text = gr.Textbox(label="language tips", show_label=False, visible=False, max_lines=1)
279
  styles = gr.Dropdown(label="风格(style)", choices=['水彩(Watercolor)','油画(Oil painting)', '粉笔画(Chalk drawing)', '卡通(Cartoon)', '蜡笔画(Crayon drawing)', '儿童画(Children\'s drawing)', '探索无限(Explore infinity)'], value='探索无限(Explore infinity)', type="index")
280
  gallery = gr.Gallery(
281
  label="Generated images", show_label=False, elem_id="gallery"
 
286
  max_lines=1,
287
  interactive=False
288
  )
289
+ trigger_component = gr.Textbox(vaule="", visible=False) # This component is used for triggering inference funtion.
290
+ translated_language = gr.Textbox(vaule="", visible=False)
291
 
292
+ ex = gr.Examples(examples=examples, fn=translate_language, inputs=[text], outputs=[language_tips_text, status_text, trigger_component, translated_language], cache_examples=False)
293
  ex.dataset.headers = [""]
294
 
295
 
296
+ text.submit(translate_language, inputs=[text], outputs=[language_tips_text, status_text, trigger_component, translated_language])
297
+ btn.click(translate_language, inputs=[text], outputs=[language_tips_text, status_text, trigger_component, translated_language])
298
+ trigger_component.change(fn=inference, inputs=[translated_language, styles], outputs=[status_text, gallery])
299
  gr.HTML(
300
  """
301
  <div class="prompt">
 
734
  </div>
735
  ''')
736
 
737
+ block.queue(concurrency_count=80).launch()