1import inputHeader from "https://esm.town/v/substrate/inputHeader";
2import { ComputeJSON, ComputeText, GenerateImage, sb, Substrate } from "npm:substrate";
3import { z } from "npm:zod";
4import { zodToJsonSchema } from "npm:zod-to-json-schema";
21 });
22 const prompt1 = new ComputeText({
23 prompt: sb.interpolate`generate a description of an image of ${
24 c1.future.json_object.get("concepts").at(0)
25 }. Be creative depicting abstract topics using detailed abstract imagery. Include details on the background, angle & framing, and style.`,
26 });
27 const prompt2 = new ComputeText({
28 prompt: sb.interpolate`generate a description of an image of ${
29 c1.future.json_object.get("concepts").at(1)
30 }. Be creative depicting abstract topics using detailed abstract imagery. Include details on the background, angle & framing, and style.`,
31 });
32 const caption1 = new ComputeText({
39 ${caption1.future.text}`,
40 }, { cache_age: 800 });
41 const image1 = new GenerateImage({ prompt: prompt1.future.text });
42 const image2 = new GenerateImage({ prompt: prompt2.future.text });
43 const c2 = new ComputeJSON({
44 prompt: sb.interpolate`List advanced concepts related to: ${c1.future.json_object.get("concepts").at(0)}
49 });
50 const prompt3 = new ComputeText({
51 prompt: sb.interpolate`generate a description of an image of ${
52 c2.future.json_object.get("concepts").at(0)
53 }. Be creative depicting abstract topics using detailed abstract imagery. Include details on the background, angle & framing, and style.`,
54 });
55 const prompt4 = new ComputeText({
56 prompt: sb.interpolate`generate a description of an image of ${
57 c2.future.json_object.get("concepts").at(1)
58 }. Be creative depicting abstract topics using detailed abstract imagery. Include details on the background, angle & framing, and style.`,
59 });
60 const caption3 = new ComputeText({
73 ${caption3.future.text}`,
74 });
75 const image3 = new GenerateImage({ prompt: prompt3.future.text });
76 const image4 = new GenerateImage({ prompt: prompt4.future.text });
77 const nodes = [image1, caption1, image2, caption2, image3, caption3, image4, caption4];
78 const stream = await substrate.stream(...nodes);
79
84 async start(controller) {
85 const pairs = [
86 { image: null, caption: null, title: null },
87 { image: null, caption: null, title: null },
88 { image: null, caption: null, title: null },
89 { image: null, caption: null, title: null },
90 ];
91
92 const outputPair = (pair, index) => {
93 if (pair.image && pair.caption) {
94 controller.enqueue(new TextEncoder().encode(
95 `<div style="display:flex;justify-content:center;align-items:center;margin-bottom:20px;">
96 <div style="margin:0 10px;">${pair.image}</div>
97 <div style="margin:0 10px;font-size:1.2rem;">
98 <a href="/?input=${pair.title}"><b>${capitalize(pair.title)}</b></a><br/>${pair.caption}</div>
99</div>`,
100 ));
101 pairs[index] = { image: null, caption: null, title: null }; // Reset after output
102 }
103 };
119 if (index !== -1) {
120 const pairIndex = Math.floor(index / 2);
121 const isImage = index % 2 === 0;
122 const content = event.data.image_uri
123 ? `<img src="${event.data.image_uri}" width=400/>`
124 : `<div style="font-family:system-ui,sans-serif;">${event.data.text}</div>`;
125
126 if (isImage) {
127 pairs[pairIndex].image = content;
128 } else {
129 pairs[pairIndex].caption = content;