用多了其他网站的文生图,有的还需要冲金币,是不是很想自己也搭建个?那么方法来了。
1.环境设置
requirements.txt 如下所示:
paddlenlp>=2.4.1
ftfy
regex
Pillow
再来一下 pip install --upgrade ppdiffusers
即可,环境简单吧。
注意:需要GPU环境,需要提前安装好PaddlePaddle-gpu包
2.代码
# 引入 StableDiffusion包
from ppdiffusers import StableDiffusionPipeline
# 引入gradio包,怎么用可以看我的介绍
import gradio as gr
# 加载模型,其实就是下载,需要特别注意的是包3、4个gb,很大,手机的话注意流量
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe.enable_attention_slicing()
# 设置块
block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
# 设置生成图片熟练
num_samples = 8
# 开始推理
def infer(prompt):
images = pipe([prompt] * num_samples).images
return images
# 设置样式
with block as demo:
gr.Markdown(
"<h1><center>PaddleNLP version of Stable Diffusion</center></h1>")
with gr.Group():
with gr.Box():
with gr.Row().style(mobile_collapse=False, equal_height=True):
text = gr.Textbox(label="Enter your prompt",
show_label=False,
max_lines=1).style(
border=(True, False, True, True),
rounded=(True, False, False, True),
container=False,
)
btn = gr.Button("Run").style(
margin=False,
rounded=(False, True, True, False),
)
gallery = gr.Gallery(label="Generated images",
show_label=False).style(grid=[2], height="auto")
text.submit(infer, inputs=[text], outputs=gallery)
btn.click(infer, inputs=[text], outputs=gallery)
gr.Markdown("""___
<p style='text-align: center'>
Created by https://huggingface.co/CompVis/stable-diffusion-v1-4
<br/>
</p>""")
# 启动
demo.launch(debug=True, server_port=8235, server_name="0.0.0.0", share=True)
2.1 gradio使用
gradio是机器学习推理分享的最佳工具,quickstart可参考我的:
Gradio机器学习模型快速部署工具【quickstart】翻译1 – 掘金
Gradio机器学习模型快速部署工具【quickstart】翻译1 – 掘金
2.1 ppdifusion 使用
这里使用PaddleNLP实现,直接安装下载即可。
需要设置的有模型地址、生成图片数量,可以根据源码改,或者换成你想要的进行加载(比如换成你自己的模型)。
3.运行
运行过程中会自动下载模型,此处浪费我流量好多好多。
运行脚本后,输出如下:
Running on local URL: http://0.0.0.0:8235
Running on public URL: https://e633ad4625553a6c67.gradio.live
This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces
给出了2个url,可以通过互联网、以及本地局域网使用
- 其中http://0.0.0.0:8235 应该换成本地ip即可,或者localhost。
- 后面的可以全球访问,特别便于访问,提示3天有效时间。
需要注意的是要量力而行,一次2张照片,我显卡扛得住,多的话显存不够会崩。
3.1 效果
3.1来一张:
- many cats run on grass
- 许多小猫在草地上奔跑,赛博朋克风格
3.2再来一张
- jean grey, young female, x-men, (((full body visible))), looking at viewer, portrait, photography, detailed skin, realistic, photo-realistic, 8k, highly detailed, full length frame, High detail RAW color art, piercing, diffused soft lighting, shallow depth of field, sharp focus, hyperrealism, cinematic lighting
3.3 注意
- 别生成坏模型啊,有检测模型检测。
- 当然,你可以跳过。
本文正在参加「金石计划」