前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 gradio 创建 图文任务 App

使用 gradio 创建 图文任务 App

作者头像
Michael阿明
发布2023-07-31 13:39:07
5950
发布2023-07-31 13:39:07
举报

1. 给图片起标题

代码语言:javascript
复制
import pathlib
from PIL import Image
from transformers import pipeline
import gradio as gr

get_completion = pipe = pipeline("image-to-text", model="D:\huggingface\hub\Maciel\Muge-Image-Caption")


def summarize(input):
    output = get_completion(input)
    return output[0]['generated_text']




if __name__ == '__main__':
    image = pathlib.Path(__file__).parent / 'Andrew.jpg'
    img = Image.open(image)
    # img.show()
    print(get_completion(img))
    # [{'generated_text': '高颜值电脑桌,让你的生活更有情调'}]

    gr.close_all()
    demo = gr.Interface(fn=summarize,
                        inputs=[gr.Image(label="上传图片", type="pil")],
                        outputs=[gr.Textbox(label="标题")],
                        title="为图像添加标题",
                        description="使用 BLIP 模型为图像添加标题",
                        allow_flagging="never",
                        examples=[image])

    demo.launch(share=True)
在这里插入图片描述
在这里插入图片描述

2. 文生图

以下,本地电脑资源不够,就直接在 官网上操作了

代码语言:javascript
复制
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")

def get_completion(prompt):
    return pipeline(prompt).images[0] 
代码语言:javascript
复制
prompt = "a train in hill"

result = get_completion(prompt)
IPython.display.HTML(f'<img src="data:image/png;base64,{result}" />')
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
import gradio as gr 

#A helper function to convert the PIL image to base64
#so you can send it to the API
def base64_to_pil(img_base64):
    base64_decoded = base64.b64decode(img_base64)
    byte_stream = io.BytesIO(base64_decoded)
    pil_image = Image.open(byte_stream)
    return pil_image

def generate(prompt):
    output = get_completion(prompt)
    result_image = base64_to_pil(output)
    return result_image

gr.close_all()
demo = gr.Interface(fn=generate,
                    inputs=[gr.Textbox(label="提示词")],
                    outputs=[gr.Image(label="输出图片")],
                    title="文本生成图片",
                    description="输入提示词,使用SD模型生成图片",
                    allow_flagging="never",
                    examples=["中国古风建筑","a mecha robot in a favela"])

demo.launch(share=True, server_port=int(os.environ['PORT1']))
在这里插入图片描述
在这里插入图片描述

添加高级选项

代码语言:javascript
复制
import gradio as gr 

#A helper function to convert the PIL image to base64 
# so you can send it to the API
def base64_to_pil(img_base64):
    base64_decoded = base64.b64decode(img_base64)
    byte_stream = io.BytesIO(base64_decoded)
    pil_image = Image.open(byte_stream)
    return pil_image

def generate(prompt, negative_prompt, steps, guidance, width, height):
    params = {
        "negative_prompt": negative_prompt,
        "num_inference_steps": steps,
        "guidance_scale": guidance,
        "width": width,
        "height": height
    }
    
    output = get_completion(prompt, params)
    pil_image = base64_to_pil(output)
    return pil_image

gr.close_all()
demo = gr.Interface(fn=generate,
                    inputs=[
                        gr.Textbox(label="提示词"),
                        gr.Textbox(label="反向提示词"),
                        gr.Slider(label="推理步数", minimum=1, maximum=100, value=25,
                                 info="模型迭代多少步生成图片"),
                        gr.Slider(label="提示指导程度", minimum=1, maximum=20, value=7, 
                                  info="提示词影响结果的重要程度"),
                        gr.Slider(label="宽", minimum=64, maximum=512, step=64, value=512),
                        gr.Slider(label="高", minimum=64, maximum=512, step=64, value=512),
                    ],
                    outputs=[gr.Image(label="输出图片")],
                    title="文本生成图片",
                    description="输入提示词,使用SD模型生成图片",
                    allow_flagging="never"
                    )

demo.launch(share=True, server_port=int(os.environ['PORT2']))
在这里插入图片描述
在这里插入图片描述

gr.Blocks

使用这个模块写起来更优雅,可以自由的排版

代码语言:javascript
复制
with gr.Blocks() as demo:
    gr.Markdown("# 文本生成图片")
    prompt = gr.Textbox(label="提示词")
    with gr.Row():
        with gr.Column():
            negative_prompt = gr.Textbox(label="负向提示词")
            steps = gr.Slider(label="推理步数", minimum=1, maximum=100, value=25,
                      info="模型迭代多少步生成图片")
            guidance = gr.Slider(label="提示指导程度", minimum=1, maximum=20, value=7,
                      info="提示词影响结果的重要程度")
            width = gr.Slider(label="宽", minimum=64, maximum=512, step=64, value=512)
            height = gr.Slider(label="高", minimum=64, maximum=512, step=64, value=512)
            btn = gr.Button("提交")
        with gr.Column():
            output = gr.Image(label="输出")

    btn.click(fn=generate, inputs=[prompt,negative_prompt,steps,guidance,width,height], outputs=[output])
gr.close_all()
demo.launch(share=True, server_port=36790)
在这里插入图片描述
在这里插入图片描述
  • 调整布局
代码语言:javascript
复制
with gr.Blocks() as demo:
    gr.Markdown("# 文本生成图片")
    with gr.Row():
        with gr.Column(scale=4):
            prompt = gr.Textbox(label="提示词") #Give prompt some real estate
        with gr.Column(scale=1, min_width=50):
            btn = gr.Button("提交") #Submit button side by side!
    with gr.Accordion("高级选项", open=False): #Let's hide the advanced options!
            negative_prompt = gr.Textbox(label="负向提示词")
            with gr.Row():
                with gr.Column():
                    steps = gr.Slider(label="推理步数", minimum=1, maximum=100, value=25,
                      info="模型迭代多少步生成图片")
                    guidance = gr.Slider(label="提示指导程度", minimum=1, maximum=20, value=7,
                      info="提示词影响结果的重要程度")
                with gr.Column():
                    width = gr.Slider(label="宽", minimum=64, maximum=512, step=64, value=512)
                    height = gr.Slider(label="高", minimum=64, maximum=512, step=64, value=512)
    output = gr.Image(label="输出") #Move the output up too
            
    btn.click(fn=generate, inputs=[prompt,negative_prompt,steps,guidance,width,height], outputs=[output])

gr.close_all()
demo.launch(share=True, server_port=41010)

提交按钮放在上面,减少鼠标移动距离

在这里插入图片描述
在这里插入图片描述

高级选项默认折叠,看起来简洁

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-07-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 给图片起标题
    • 2. 文生图
      • 添加高级选项
      • gr.Blocks
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档