import numpy as np
import gradio as gr
import paddlehub as hub
model = hub.Module(name='ernie_vilg')
language_translation_model = hub.Module(name='baidu_translate')
language_recognition_model = hub.Module(name='baidu_language_recognition')
style_list = ['水彩','油画', '粉笔画', '卡通', '蜡笔画', '儿童画', '探索无限']
tips = {"en": "Tips: The input text will be translated into Chinese for generation",
"jp": "ヒント: 入力テキストは生成のために中国語に翻訳されます",
"kor": "힌트: 입력 텍스트는 생성을 위해 중국어로 번역됩니다"}
count = 0
def translate_language(text_prompts):
global count
try:
count += 1
tips_text = None
language_code = language_recognition_model.recognize(text_prompts)
if language_code != 'zh':
text_prompts = language_translation_model.translate(text_prompts, language_code, 'zh')
except Exception as e:
error_text = str(e)
return {status_text:error_text, language_tips_text:gr.update(visible=False)}
if language_code in tips:
tips_text = tips[language_code]
else:
tips_text = tips['en']
if language_code == 'zh':
return {language_tips_text:gr.update(visible=False), translated_language:text_prompts, trigger_component: gr.update(value=count, visible=False)}
else:
return {language_tips_text:gr.update(visible=True, value=tips_text), translated_language:text_prompts, trigger_component: gr.update(value=count, visible=False)}
def inference(text_prompts, style_indx):
try:
style = style_list[style_indx]
results = model.generate_image(
text_prompts=text_prompts, style=style, visualization=False)
except Exception as e:
error_text = str(e)
return {status_text:error_text, gallery:None}
return {status_text:'Success', gallery:results[:6]}
title="ERNIE-ViLG"
description="ERNIE-ViLG model, which supports text-to-image task."
css = """
.gradio-container {
font-family: 'IBM Plex Sans', sans-serif;
}
.gr-button {
color: white;
border-color: black;
background: black;
}
input[type='range'] {
accent-color: black;
}
.dark input[type='range'] {
accent-color: #dfdfdf;
}
.container {
max-width: 730px;
margin: auto;
padding-top: 1.5rem;
}
#gallery {
min-height: 22rem;
margin-bottom: 15px;
margin-left: auto;
margin-right: auto;
border-bottom-right-radius: .5rem !important;
border-bottom-left-radius: .5rem !important;
}
#gallery>div>.h-full {
min-height: 20rem;
}
.details:hover {
text-decoration: underline;
}
.gr-button {
white-space: nowrap;
}
.gr-button:focus {
border-color: rgb(147 197 253 / var(--tw-border-opacity));
outline: none;
box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
--tw-border-opacity: 1;
--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
--tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
--tw-ring-opacity: .5;
}
.footer {
margin-bottom: 45px;
margin-top: 35px;
text-align: center;
border-bottom: 1px solid #e5e5e5;
}
.footer>p {
font-size: .8rem;
display: inline-block;
padding: 0 10px;
transform: translateY(10px);
background: white;
}
.dark .footer {
border-color: #303030;
}
.dark .footer>p {
background: #0b0f19;
}
.prompt h4{
margin: 1.25em 0 .25em 0;
font-weight: bold;
font-size: 115%;
}
"""
block = gr.Blocks(css=css)
examples = [
[
'戴着眼镜的猫',
'油画(Oil painting)'
],
[
'A cat with glasses',
'油画(Oil painting)'
],
[
'眼鏡をかけた猫',
'油画(Oil painting)'
],
[
'안경을 쓴 고양이',
'油画(Oil painting)'
],
[
'日落时的城市天际线,史前遗迹风格',
'油画(Oil painting)'
],
[
'一只猫坐在椅子上,戴着一副墨镜, low poly 风格',
'卡通(Cartoon)'
],
[
'A cat sitting on a chair, wearing a pair of sunglasses, low poly style',
'油画(Oil painting)'
],
[
'猫が椅子に座ってサングラスをかけている、low polyスタイル',
'油画(Oil painting)'
],
[
'고양이 한 마리가 의자에 앉아 선글라스를 끼고 low poly 스타일을 하고 있다',
'油画(Oil painting)'
],
[
'一只猫坐在椅子上,戴着一副墨镜,秋天风格',
'探索无限(Explore infinity)'
],
[
'蒙娜丽莎,赛博朋克,宝丽来,33毫米,蒸汽波艺术',
'探索无限(Explore infinity)'
],
[
'一只猫坐在椅子上,戴着一副墨镜,海盗风格',
'探索无限(Explore infinity)'
],
[
'一条由闪电制成的令人敬畏的龙,概念艺术',
'探索无限(Explore infinity)'
],
[
'An awesome dragon made of lightning, conceptual art',
'油画(Oil painting)'
],
[
'稲妻で作られた畏敬の念を抱かせる竜、コンセプトアート',
'油画(Oil painting)'
],
[
'번개로 만든 경외스러운 용, 개념 예술',
'油画(Oil painting)'
],
[
'梵高猫头鹰,蒸汽波艺术',
'探索无限(Explore infinity)'
],
[
'萨尔瓦多·达利描绘古代文明的超现实主义梦幻油画,写实风格',
'探索无限(Explore infinity)'
],
[
'夕阳日落时,阳光落在云层上,海面波涛汹涌,风景,胶片感',
'探索无限(Explore infinity)'
],
[
'Sunset, the sun falls on the clouds, the sea is rough, the scenery is filmy',
'油画(Oil painting)'
],
[
'夕日が沈むと、雲の上に太陽の光が落ち、海面は波が荒く、風景、フィルム感',
'油画(Oil painting)'
],
[
'석양이 질 때 햇빛이 구름 위에 떨어지고, 해수면의 파도가 용솟음치며, 풍경, 필름감',
'油画(Oil painting)'
],
]
with block:
gr.HTML(
"""
ERNIE-ViLG Demo
ERNIE-ViLG is a state-of-the-art text-to-image model that generates
images from Chinese text.
"""
)
with gr.Group():
with gr.Box():
with gr.Row().style(mobile_collapse=False, equal_height=True):
text = gr.Textbox(
label="Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt, multiple languages are supported now.",
).style(
border=(True, False, True, True),
rounded=(True, False, False, True),
container=False,
)
btn = gr.Button("Generate image").style(
margin=False,
rounded=(False, True, True, False),
)
language_tips_text = gr.Textbox(label="language tips", show_label=False, visible=False, max_lines=1)
styles = gr.Dropdown(label="风格(style)", choices=['水彩(Watercolor)','油画(Oil painting)', '粉笔画(Chalk drawing)', '卡通(Cartoon)', '蜡笔画(Crayon drawing)', '儿童画(Children\'s drawing)', '探索无限(Explore infinity)'], value='探索无限(Explore infinity)', type="index")
gallery = gr.Gallery(
label="Generated images", show_label=False, elem_id="gallery"
).style(grid=[2, 3], height="auto")
status_text = gr.Textbox(
label="处理状态(Process status)",
show_label=True,
max_lines=1,
interactive=False
)
trigger_component = gr.Textbox(vaule="", visible=False) # This component is used for triggering inference funtion.
translated_language = gr.Textbox(vaule="", visible=False)
ex = gr.Examples(examples=examples, fn=translate_language, inputs=[text], outputs=[language_tips_text, status_text, trigger_component, translated_language], cache_examples=False)
ex.dataset.headers = [""]
text.submit(translate_language, inputs=[text], outputs=[language_tips_text, status_text, trigger_component, translated_language])
btn.click(translate_language, inputs=[text], outputs=[language_tips_text, status_text, trigger_component, translated_language])
trigger_component.change(fn=inference, inputs=[translated_language, styles], outputs=[status_text, gallery])
gr.HTML(
"""
Prompt format
Prompt = [adjective] [object], [details], [styles or artists].
For more details, please refer to
YouPromptMe Guide .
There are more interesting models in PaddleHub, if you think it's great, welcome to star
PaddleHub .
Stars Stars 8.4k 8.4k
Besides, you can use free GPU resourses in
aistudio to enjoy more cases, have fun.
"""
)
gr.Markdown(
"""
在"探索无限"的风格模式下,画作的真实风格完全可以由你的prompt来决定。下面是一些参考案例:
In "Explore infinity" style mode, how the image looks like is totally up to your prompt. Below are some cases:
### 复古未来主义风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,复古未来主义风格 | 日落时的城市天际线,复古未来主义风格 |
### 粉彩朋克风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,粉彩朋克风格 | 日落时的城市天际线,粉彩朋克风格 |
### 史前遗迹风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,史前遗迹风格 | 日落时的城市天际线,史前遗迹风格 |
### 波普艺术风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,波普艺术风格 | 日落时的城市天际线,后世界末日风格 |
### 迷幻风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,迷幻风格 | 日落时的城市天际线,迷幻风格 |
### 赛博朋克风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,赛博朋克风格 | 日落时的城市天际线,赛博朋克风格 |
### 纸箱风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,纸箱风格 | 日落时的城市天际线,纸箱风格 |
### 未来主义风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,未来主义风格 | 一只猫坐在椅子上,戴着一副墨镜,未来主义风格 |
### 抽象技术风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,抽象技术风格 | 日落时的城市天际线,抽象技术风格 |
### 海滩兔风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,海滩兔风格 | 日落时的城市天际线,海滩兔风格 |
### 粉红公主风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,粉红公主风格 | 日落时的城市天际线,粉红公主风格 |
### 嬉皮士风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,嬉皮士风格 | 日落时的城市天际线,嬉皮士风格 |
### 幻象之城风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,幻象之城风格 | 日落时的城市天际线,幻象之城风格 |
### 美人鱼风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,美人鱼风格 | 日落时的城市天际线,美人鱼风格 |
### 迷宫物语风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,迷宫物语风格 | 日落时的城市天际线,迷宫物语风格 |
### 仙女风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,仙女风格 | 日落时的城市天际线,仙女风格 |
### Low Poly 风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜, low poly 风格 | 日落时的城市天际线, low-poly |
### 浮世绘风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,浮世绘风格 | 日落时的城市天际线,浮世绘风格 |
### 矢量心风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,矢量心风格 | 日落时的城市天际线,矢量心风格 |
### 摩托车手风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,摩托车手风格 | 日落时的城市天际线,摩托车手风格 |
### 孟菲斯公司风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,孟菲斯公司风格 | 日落时的城市天际线,孟菲斯公司风格 |
### 泥塑风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜, 泥塑风格 | 日落时的城市天际线, 泥塑风格 |
### 苔藓风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,苔藓风格 | 日落时的城市天际线,苔藓风格 |
### 新浪潮风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,新浪潮风格 | 日落时的城市天际线,新浪潮风格 |
### 嘻哈风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,嘻哈风格 | 日落时的城市天际线,嘻哈风格 |
### 矢量图
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜, 矢量图 | 日落时的城市天际线, 矢量图 |
### 铅笔艺术
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜, 铅笔艺术 | 日落时的城市天际线, 铅笔艺术 |
### 女巫店风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,女巫店风格 | 日落时的城市天际线,女巫店风格 |
### 4D 建模
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜, 4D 建模 | 日落时的城市天际线, 4D 建模 |
### 水彩墨风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜, 水彩墨风格 | 日落时的城市天际线, 水彩墨风格 |
### 酸性精灵风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,酸性精灵风格 | 日落时的城市天际线,酸性精灵风格 |
### 海盗风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 日落时的城市天际线,海盗风格 | 一只猫坐在椅子上,戴着一副墨镜,海盗风格 |
### 古埃及风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,古埃及风格 | 日落时的城市天际线,古埃及风格 |
### 风帽风格
|  |  |
| --------------------------------------------------------- | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,风帽风格 | 日落时的城市天际线,风帽风格 |
### 装饰艺术风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,装饰艺术风格 | 日落时的城市天际线,装饰艺术风格 |
### 极光风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,极光风格 | 日落时的城市天际线,极光风格 |
### 秋天风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 日落时的城市天际线,秋天风格 | 一只猫坐在椅子上,戴着一副墨镜,秋天风格 |
### 巴洛克风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,巴洛克风格 | 日落时的城市天际线,巴洛克风格 |
### 立体主义风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,立体主义风格 | 日落时的城市天际线,立体主义风格 |
### 黑暗自然主义风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,黑暗自然主义风格 | 日落时的城市天际线,黑暗自然主义风格 |
### 表现主义风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,表现主义风格 | 日落时的城市天际线,表现主义风格 |
### 野兽派风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,野兽派风格 | 日落时的城市天际线,野兽派风格 |
### 鬼魂风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,鬼魂风格 | 日落时的城市天际线,鬼魂风格 |
### 印象主义风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,印象主义风格 | 日落时的城市天际线,印象主义风格 |
### 卡瓦伊风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,卡瓦伊风格 | 日落时的城市天际线,卡瓦伊风格 |
### 极简主义风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,极简主义风格 | 日落时的城市天际线,极简主义风格 |
### 水井惠郎风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,水井惠郎风格 | 日落时的城市天际线,水井惠郎风格 |
### 照片写实风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,照片写实风格 | 日落时的城市天际线,照片写实风格 |
### 像素可爱风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,像素可爱风格 | 日落时的城市天际线,像素可爱风格 |
### 雨天风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 日落时的城市天际线,雨天风格 | 一只猫坐在椅子上,戴着一副墨镜,雨天风格 |
### 湿漉漉的风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,湿漉漉的风格 | 日落时的城市天际线,湿漉漉的风格 |
### 维京人风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,维京人风格 | 日落时的城市天际线,维京人风格 |
### 后印象主义
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,风格:后印象主义 | 日落时的城市天际线, 风格:后印象主义-v2 |
### 素人主义
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,风格:素人主义 | 日落时的城市天际线, 风格:素人艺术 |
### 碎核风格
|  |  |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| 一只猫坐在椅子上,戴着一副墨镜,碎核风格 | 日落时的城市天际线,碎核风格 |
"""
)
gr.HTML('''
''')
block.queue(concurrency_count=80).launch()