chenjian commited on
Commit
e29a76f
1 Parent(s): 8fb5db4

inference language type automatically

Browse files
Files changed (1) hide show
  1. app.py +41 -36
app.py CHANGED
@@ -5,26 +5,50 @@ import datetime
5
 
6
 
7
  model = hub.Module(name='ernie_vilg')
8
- language_model = hub.Module(name='baidu_translate')
 
9
 
10
  style_list = ['水彩','油画', '粉笔画', '卡通', '蜡笔画', '儿童画', '探索无限']
11
- language_list = ['zh', 'en', 'jp', 'kor']
12
- def inference(text_prompts, language_indx, style_indx):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  print(datetime.datetime.now())
14
  try:
15
  style = style_list[style_indx]
16
- if language_indx != 0:
17
- language = language_list[language_indx]
18
- text_prompts = language_model.translate(text_prompts, language, 'zh')
19
  results = model.generate_image(
20
  text_prompts=text_prompts, style=style, visualization=False)
21
- print(datetime.datetime.now())
22
- return 'Success', results[:6]
23
  except Exception as e:
24
  error_text = str(e)
25
  print(datetime.datetime.now())
26
- return error_text, None
27
- return
 
28
 
29
 
30
  title="ERNIE-ViLG"
@@ -109,112 +133,90 @@ block = gr.Blocks(css=css)
109
  examples = [
110
  [
111
  '戴着眼镜的猫',
112
- '中文(Chinese)',
113
  '油画(Oil painting)'
114
  ],
115
  [
116
  'A cat with glasses',
117
- '英文(English)',
118
  '油画(Oil painting)'
119
  ],
120
  [
121
  '眼鏡をかけた猫',
122
- '日文(Japanese)',
123
  '油画(Oil painting)'
124
  ],
125
  [
126
  '안경을 쓴 고양이',
127
- '韩文(Korean)',
128
  '油画(Oil painting)'
129
  ],
130
  [
131
  '日落时的城市天际线,史前遗迹风格',
132
- '中文(Chinese)',
133
  '油画(Oil painting)'
134
  ],
135
  [
136
  '一只猫坐在椅子上,戴着一副墨镜, low poly 风格',
137
- '中文(Chinese)',
138
  '卡通(Cartoon)'
139
  ],
140
  [
141
  'A cat sitting on a chair, wearing a pair of sunglasses, low poly style',
142
- '英文(English)',
143
  '油画(Oil painting)'
144
  ],
145
  [
146
  '猫が椅子に座ってサングラスをかけている、low polyスタイル',
147
- '日文(Japanese)',
148
  '油画(Oil painting)'
149
  ],
150
  [
151
  '고양이 한 마리가 의자에 앉아 선글라스를 끼고 low poly 스타일을 하고 있다',
152
- '韩文(Korean)',
153
  '油画(Oil painting)'
154
  ],
155
  [
156
  '一只猫坐在椅子上,戴着一副墨镜,秋天风格',
157
- '中文(Chinese)',
158
  '探索无限(Explore infinity)'
159
  ],
160
  [
161
  '蒙娜丽莎,赛博朋克,宝丽来,33毫米,蒸汽波艺术',
162
- '中文(Chinese)',
163
  '探索无限(Explore infinity)'
164
  ],
165
  [
166
  '一只猫坐在椅子上,戴着一副墨镜,海盗风格',
167
- '中文(Chinese)',
168
  '探索无限(Explore infinity)'
169
  ],
170
  [
171
  '一条由闪电制成的令人敬畏的龙,概念艺术',
172
- '中文(Chinese)',
173
  '探索无限(Explore infinity)'
174
  ],
175
  [
176
  'An awesome dragon made of lightning, conceptual art',
177
- '英文(English)',
178
  '油画(Oil painting)'
179
  ],
180
  [
181
  '稲妻で作られた畏敬の念を抱かせる竜、コンセプトアート',
182
- '日文(Japanese)',
183
  '油画(Oil painting)'
184
  ],
185
  [
186
  '번개로 만든 경외스러운 용, 개념 예술',
187
- '韩文(Korean)',
188
  '油画(Oil painting)'
189
  ],
190
  [
191
  '梵高猫头鹰,蒸汽波艺术',
192
- '中文(Chinese)',
193
  '探索无限(Explore infinity)'
194
  ],
195
  [
196
  '萨尔瓦多·达利描绘古代文明的超现实主义梦幻油画,写实风格',
197
- '中文(Chinese)',
198
  '探索无限(Explore infinity)'
199
  ],
200
  [
201
  '夕阳日落时,阳光落在云层上,海面波涛汹涌,风景,胶片感',
202
- '中文(Chinese)',
203
  '探索无限(Explore infinity)'
204
  ],
205
  [
206
  'Sunset, the sun falls on the clouds, the sea is rough, the scenery is filmy',
207
- '英文(English)',
208
  '油画(Oil painting)'
209
  ],
210
  [
211
  '夕日が沈むと、雲の上に太陽の光が落ち、海面は波が荒く、風景、フィルム感',
212
- '日文(Japanese)',
213
  '油画(Oil painting)'
214
  ],
215
  [
216
  '석양이 질 때 햇빛이 구름 위에 떨어지고, 해수면의 파도가 용솟음치며, 풍경, 필름감',
217
- '韩文(Korean)',
218
  '油画(Oil painting)'
219
  ],
220
  ]
@@ -273,7 +275,7 @@ with block:
273
  margin=False,
274
  rounded=(False, True, True, False),
275
  )
276
- language = gr.Dropdown(label="Prompt语言(Prompt language)", choices=['中文(Chinese)','英文(English)', '日文(Japanese)', '韩文(Korean)'], value='中文(Chinese)', type="index")
277
  styles = gr.Dropdown(label="风格(style)", choices=['水彩(Watercolor)','油画(Oil painting)', '粉笔画(Chalk drawing)', '卡通(Cartoon)', '蜡笔画(Crayon drawing)', '儿童画(Children\'s drawing)', '探索无限(Explore infinity)'], value='探索无限(Explore infinity)', type="index")
278
  gallery = gr.Gallery(
279
  label="Generated images", show_label=False, elem_id="gallery"
@@ -284,13 +286,16 @@ with block:
284
  max_lines=1,
285
  interactive=False
286
  )
 
 
287
 
288
- ex = gr.Examples(examples=examples, fn=inference, inputs=[text, language, styles], outputs=gallery, cache_examples=False)
289
  ex.dataset.headers = [""]
290
 
291
 
292
- text.submit(inference, inputs=[text, language, styles], outputs=[status_text, gallery])
293
- btn.click(inference, inputs=[text, language, styles], outputs=[status_text, gallery])
 
294
  gr.HTML(
295
  """
296
  <div class="prompt">
 
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"
 
133
  examples = [
134
  [
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)'
153
  ],
154
  [
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)'
173
  ],
174
  [
175
  '蒙娜丽莎,赛博朋克,宝丽来,33毫米,蒸汽波艺术',
 
176
  '探索无限(Explore infinity)'
177
  ],
178
  [
179
  '一只猫坐在椅子上,戴着一副墨镜,海盗风格',
 
180
  '探索无限(Explore infinity)'
181
  ],
182
  [
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)'
201
  ],
202
  [
203
  '萨尔瓦多·达利描绘古代文明的超现实主义梦幻油画,写实风格',
 
204
  '探索无限(Explore infinity)'
205
  ],
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
  ]
 
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">