import random
import gradio as gr
from sample import (arg_parse,
sampling,
load_fontdiffuer_pipeline)
def run_fontdiffuer(source_image,
character,
reference_image,
sampling_step,
guidance_scale,
batch_size):
args.character_input = False if source_image is not None else True
args.content_character = character
args.sampling_step = sampling_step
args.guidance_scale = guidance_scale
args.batch_size = batch_size
args.seed = random.randint(0, 10000)
out_image = sampling(
args=args,
pipe=pipe,
content_image=source_image,
style_image=reference_image)
return out_image
if __name__ == '__main__':
args = arg_parse()
args.demo = True
args.ckpt_dir = 'ckpt'
args.ttf_path = 'ttf/KaiXinSongA.ttf'
args.device = 'cpu'
# load fontdiffuer pipeline
pipe = load_fontdiffuer_pipeline(args=args)
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=1):
gr.HTML("""
FontDiffuser
South China University of Technology, Alibaba DAMO Academy
1.We propose FontDiffuser, which is capable to generate unseen characters and styles, and it can be extended to the cross-lingual generation, such as Chinese to Korean.
2. FontDiffuser excels in generating complex character and handling large style variation. And it achieves state-of-the-art performance.
""")
gr.Image('figures/result_vis.png')
gr.Image('figures/demo_tips.png')
with gr.Column(scale=1):
with gr.Row():
source_image = gr.Image(width=320, label='[Option 1] Source Image', image_mode='RGB', type='pil')
reference_image = gr.Image(width=320, label='Reference Image', image_mode='RGB', type='pil')
with gr.Row():
character = gr.Textbox(value='隆', label='[Option 2] Source Character')
with gr.Row():
fontdiffuer_output_image = gr.Image(label="FontDiffuser Output Image", image_mode='RGB', type='pil')
sampling_step = gr.Slider(20, 50, value=20, step=10,
label="Sampling Step", info="The sampling step by FontDiffuser.")
guidance_scale = gr.Slider(1, 12, value=7.5, step=0.5,
label="Scale of Classifier-free Guidance",
info="The scale used for classifier-free guidance sampling")
batch_size = gr.Slider(1, 4, value=1, step=1,
label="Batch Size", info="The number of images to be sampled.")
FontDiffuser = gr.Button('Run FontDiffuser')
gr.Markdown("## Examples that You Can Choose Below⬇️")
with gr.Row():
gr.Markdown("## Examples")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## Example 1️⃣: Source Image and Reference Image")
gr.Markdown("### In this mode, we provide both the source image and \
the reference image for you to try our demo!")
gr.Examples(
examples=[['figures/source_imgs/source_灨.jpg', 'figures/ref_imgs/ref_籍.jpg'],
['figures/source_imgs/source_鑻.jpg', 'figures/ref_imgs/ref_鹰.jpg'],
['figures/source_imgs/source_鑫.jpg', 'figures/ref_imgs/ref_壤.jpg'],
['figures/source_imgs/source_釅.jpg', 'figures/ref_imgs/ref_雕.jpg']],
inputs=[source_image, reference_image]
)
with gr.Column(scale=1):
gr.Markdown("## Example 2️⃣: Character and Reference Image")
gr.Markdown("### In this mode, we provide the content character and the reference image \
for you to try our demo!")
gr.Examples(
examples=[['龍', 'figures/ref_imgs/ref_鷢.jpg'],
['轉', 'figures/ref_imgs/ref_鲸.jpg'],
['懭', 'figures/ref_imgs/ref_籍_1.jpg'],
['識', 'figures/ref_imgs/ref_鞣.jpg']],
inputs=[character, reference_image]
)
with gr.Column(scale=1):
gr.Markdown("## Example 3️⃣: Reference Image")
gr.Markdown("### In this mode, we provide only the reference image, \
you can upload your own source image or you choose the character above \
to try our demo!")
gr.Examples(
examples=['figures/ref_imgs/ref_闡.jpg',
'figures/ref_imgs/ref_雕.jpg',
'figures/ref_imgs/ref_豄.jpg',
'figures/ref_imgs/ref_馨.jpg',
'figures/ref_imgs/ref_鲸.jpg',
'figures/ref_imgs/ref_檀.jpg',
'figures/ref_imgs/ref_鞣.jpg',
'figures/ref_imgs/ref_穗.jpg',
'figures/ref_imgs/ref_欟.jpg',
'figures/ref_imgs/ref_籍_1.jpg',
'figures/ref_imgs/ref_鷢.jpg',
'figures/ref_imgs/ref_媚.jpg',
'figures/ref_imgs/ref_籍.jpg',
'figures/ref_imgs/ref_壤.jpg',
'figures/ref_imgs/ref_蜓.jpg',
'figures/ref_imgs/ref_鹰.jpg'],
examples_per_page=20,
inputs=reference_image
)
FontDiffuser.click(
fn=run_fontdiffuer,
inputs=[source_image,
character,
reference_image,
sampling_step,
guidance_scale,
batch_size],
outputs=fontdiffuer_output_image)
demo.launch(debug=True)