用多了其他网站的文生图,有的还需求冲金币,是不是很想自己也建立个?那么办法来了。
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>
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 注意
- 别生成坏模型啊,有检测模型检测。
- 当然,你能够跳过。
本文正在参加「金石方案」