Public
Edited
Mar 6, 2023
1 fork
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
payload = ({
"cfg_scale": 7,
// "clip_guidance_preset": "FAST_BLUE",
"height": 512,
"width": 512,
"sampler": "K_EULER_ANCESTRAL",
// "samples": form.samples,
"samples": 2,
"steps": form.steps,
"text_prompts": [
{
"text": form.pos,
"weight": 1
},
{
"text": form.neg,
"weight": -1
}
]
})
Insert cell
Insert cell
result = {
// Observable cells can yield results via Generators, so by default we are loading
yield "loading"

// then we can yield the promise of our fetch
yield fetch(`https://api.stability.ai/v1beta/generation/${engine}/text-to-image`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-type': 'application/json',
'Accept': "application/json",
},
body: JSON.stringify(payload),
// This cancels the request if the form is submitted again before the results return
signal: toSignal(invalidation)
})
.then(response => {
if(response.status !== 200) {
return response.text()
}
return response.json()
})
}
Insert cell
Insert cell
{
result; // trigger this cell to rerun and check our balance whenever result changes
return stability("v1beta/user/balance")
}
Insert cell
function stability(endpoint, params) {
let url = `https://api.stability.ai/${endpoint}`
return fetch(url, {
headers: {
"Authorization": `Bearer ${apiKey}`
}
}).then(response => {
if(response.status !== 200) {
return response.text()
}
return response.json()
})
}
Insert cell
engine = selectedEngine.id
Insert cell
engines = stability("v1beta/engines/list")
Insert cell
Insert cell
import { loading as spinner } from "@mateh/loading"
Insert cell
import {textcolor} from "@observablehq/text-color-annotations-in-markdown"
Insert cell
// this lets us avoid resubmitting our request everytime our form changes
import {guard} from "@mootari/inputs-submit"
Insert cell
// https://talk.observablehq.com/t/when-do-event-listeners-need-to-be-manually-removed/7160
// this lets us cancel a fetch request if our cell's recalculate before the fetch is complete
function toSignal(invalidation) {
const controller = new AbortController;
invalidation.then(() => controller.abort());
return controller.signal;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more