| import os |
| from PIL import Image |
| import gradio as gr |
|
|
|
|
| def create_interface_style_transfer(runner): |
| with gr.Blocks(): |
| with gr.Row(): |
| gr.Markdown( |
| '1. 上传内容和风格图像作为输入.\n' |
| '2. (可选)根据需要定制以下配置.\n' |
| '3. 点击“运行”开始转换.' |
| ) |
| |
| with gr.Row(): |
| with gr.Column(): |
| with gr.Row(): |
| content_image = gr.Image(label='内容图像', type='pil', interactive=True, value=None) |
| style_image = gr.Image(label='风格图像', type='pil', interactive=True, value=None) |
|
|
| run_button = gr.Button(value='Run') |
|
|
| with gr.Accordion('选项', open=True): |
| seed = gr.Number(label='种子', value=2025, precision=0, minimum=0, maximum=2**31) |
| num_steps = gr.Slider(label='步数', minimum=1, maximum=1000, value=200, step=1) |
| lr = gr.Slider(label='学习率', minimum=0.01, maximum=0.5, value=0.05, step=0.01) |
| content_weight = gr.Slider(label='内容权重', minimum=0., maximum=1., value=0.25, step=0.001) |
| mixed_precision = gr.Radio(choices=['bf16', 'no'], value='bf16', label='混合精度') |
| model_path=gr.Textbox(label='模型路径', value=r'D:\learn_torch\Exploration_Platform\model',placeholder='Path to your local model') |
| base_model_list = ['stable-diffusion-v1-5/stable-diffusion-v1-5', ] |
| model = gr.Radio(choices=base_model_list, label='选择基础模型', value='stable-diffusion-v1-5/stable-diffusion-v1-5') |
|
|
| with gr.Column(): |
| gr.Markdown('#### 输出图片:\n') |
| result_gallery = gr.Gallery(label='Output', elem_id='gallery', columns=2, height='auto', preview=True) |
| gr.Markdown( |
| 'Notes:\n' |
| '* 如果你发现风格效果不够,你可以尝试增加“步数”或减少“内容权重”`\n' |
| '* 我们通常建议使用“内容权重”为“0.25”' |
| ) |
|
|
| |
| ips = [content_image, style_image, seed, num_steps, lr, content_weight, mixed_precision, model_path,model] |
|
|
| run_button.click(fn=runner.run_style_transfer, inputs=ips, outputs=[result_gallery]) |
|
|