filio/animate
9
1from towhee import pipeline, FileManagerConfig, FileManager2import gradio3import numpy4from PIL import Image5 6title = 'Towhee AnimeGanV2 Pipeline'7description = 'An end to end pipeline for AnimeGanV2 using the Towhee framework. Take a look at `app.py` to see how little steps it takes and try it for yourself using `pip install towhee`.\nQuick Note: First run after reboot may be slow due to caching pipeline operators.'8article = '<a href="https://github.com/towhee-io/towhee" style="text-align:center" target="_blank">Check out the Towhee Github</a>'9 10 11size = (512, 512)12 13# Configuring the caching location14fmc = FileManagerConfig()15fmc.update_default_cache('./')16 17# All pipelines loaded in at start. These pipelines all share operators for reduced memory overhead.18celeba = pipeline('filip-halt/style-transfer-animegan', tag = 'celeba')19facepaintv1 = pipeline('filip-halt/style-transfer-animegan', tag = 'facepaintv1')20facepaintv2 = pipeline('filip-halt/style-transfer-animegan', tag = 'facepaintv2')21hayao = pipeline('filip-halt/style-transfer-animegan', tag = 'hayao')22paprika = pipeline('filip-halt/style-transfer-animegan', tag = 'paprika')23shinkai = pipeline('filip-halt/style-transfer-animegan', tag = 'shinkai')24 25def operation(Input, Version):26 # Resizing the image while keeping aspect ratio.27 Input.thumbnail(size, Image.ANTIALIAS)28 # Saving image to file for input. Very low chance of concurrent file saves during the time29 # between saving and taking first step of pipeline, so avoiding locks for now. In addition,30 # current gradio is set to queue so there will never be parallel runs for this. 31 Input.save('./test.jpg')32 33 if Version == 'celeba':34 x = celeba('./test.jpg')35 elif Version == 'facepaintv1':36 x = facepaintv1('./test.jpg')37 elif Version == 'facepaintv2':38 x = facepaintv2('./test.jpg')39 elif Version == 'hayao':40 x = hayao('./test.jpg')41 elif Version == 'paprika':42 x = paprika('./test.jpg')43 elif Version == 'shinkai':44 x = shinkai('./test.jpg')45 46 # Converting from channel-first, [0,1] value RGB, numpy array to PIL image.47 x = numpy.transpose(x[0][0], (1,2,0))48 x = Image.fromarray((x * 255).astype(numpy.uint8))49 return x50 51gradio.Interface(operation, [gradio.inputs.Image(type="pil"), gradio.inputs.Radio(["celeba", "facepaintv1", "facepaintv2", "hayao", "paprika", 'shinkai'])], gradio.outputs.Image(type="pil"), allow_flagging=False,allow_screenshot=False, title=title, article=article, description=description).launch(enable_queue=True)52 