截至 2022 年 6 月,最热门的🔥型号之一是DALL-E mini。如此处、此处和此处所示,有许多使用此模型的项目和示例。甚至有主流新闻文章报道过这种模式。
本文介绍了构建网页摘要然后生成该摘要图像的工作流程。虽然文本到图像模型有许多潜在用例,但此示例侧重于展示工作流程的强大功能,并提供有关 DALL-E mini 如何“看”世界的有趣见解。
安装依赖
安装txtai和所有依赖项。
DALL-E 迷你模型和代码的所有功劳都归功于https://github.com/borisdayma/dalle-mini和https://github.com/kuprel/min-dalle。
pip install txtai tika min-dalle ipyplot
构建 DALL-E 管道
让我们首先构建一个使用 DALL-E mini 生成图像的 txtai 管道。
from min_dalle import MinDalle
from txtai.pipeline import Pipeline
class Dalle(Pipeline):
def init(self):
self.model = MinDalle(is_mega=False, is_verbose=False)
def call(self, texts, seed, prefix):
results = []
for text in texts:
text = prefix + text
results.append(self.model.generate_image(text, seed))
return results
构建 DALL-E 工作流程
接下来我们将定义一个 txtai 工作流作为 YAML。此工作流提取指定 URL 处的文本,构建摘要,然后为摘要文本生成图像。
此工作流可以从 Python 运行,如下所示或作为API 服务运行。
from txtai.app import Application
app = Application(“”"
main.Dalle:
summary:
path: sshleifer/distilbart-cnn-12-6
textractor:
join: true
lines: false
minlength: 100
paragraphs: true
sentences: false
workflow:
draw:
tasks:
- action: textractor
task: url
- action: summary
args: [0, 60, 0]
- action: main.Dalle
args: [1024, "Illustration of “]
“””)
生成网页摘要图像
现在工作流程已经启动,让我们生成一些图像!以下示例为一组维基百科文章生成图像。尝试一下文章、食谱或任何其他描述性网页。
使用此 url 为随机维基百科页面生成图像也很有趣:https://en.wikipedia.org/wiki/Special:Random
import ipyplot
Build list of Wikipedia article URLs
captions = [“Catholic Church”, “Magic Kingdom”, “Epcot”, “Mountain”, “Tundra”, “War of 1812”, “Chinese Cuisine”, “Indian Cuisine”, “Italian Cuisine”,
“Romanian cuisine”, “Football”, “Artificial Intelligence”, “Galaxy”, “Fjord”, “Island”]
urls = [f"https://en.wikipedia.org/wiki/{url.replace(’ ', ‘_’)}" for url in captions]
Run workflow to generate images with DALL-E mini
images = list(app.workflow(“draw”, urls))
Plot images
ipyplot.plot_images(images, captions, img_width=256)
包起来
本文通过一个示例介绍了如何构建 txtai 工作流来生成网页摘要图像。DALL-E mini 是一款引人入胜的模型,“了解”该模型的工作原理非常有趣。自己试一试!