scratch-extensions / imgen.js
soiz's picture
Create imgen.js
5fe33ce verified
raw
history blame
5.53 kB
class ImageGenerator {
constructor() {
this.baseURL = 'https://soiz-flux-1-dev-serverless.hf.space/generate';
this.prompt = '';
this.negativePrompt = '';
this.width = 512;
this.height = 512;
this.steps = 25;
this.cfgs = 7;
this.sampler = 'DPM++ 2M';
this.strength = 0.7;
this.seed = -1;
this.returnType = 'dataURL'; // dataURL or blobURL
}
// Generate URL for fetching
generateFetchURL() {
return `${this.baseURL}?prompt=${encodeURIComponent(this.prompt)}&negative_prompt=${encodeURIComponent(this.negativePrompt)}&width=${this.width}&height=${this.height}&steps=${this.steps}&cfgs=${this.cfgs}&sampler=${encodeURIComponent(this.sampler)}&strength=${this.strength}&seed=${this.seed}`;
}
// Return parameters as JSON for debugging
getParametersAsJSON() {
return JSON.stringify({
prompt: this.prompt,
negativePrompt: this.negativePrompt,
width: this.width,
height: this.height,
steps: this.steps,
cfgs: this.cfgs,
sampler: this.sampler,
strength: this.strength,
seed: this.seed
});
}
// Fetch function to generate the image
async fetchImage() {
const url = this.generateFetchURL();
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Failed to generate image');
const blob = await response.blob();
if (this.returnType === 'blobURL') {
return URL.createObjectURL(blob);
} else {
// Convert Blob to Data URL
return await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
} catch (error) {
console.error(error);
throw new Error('Error generating image: ' + error.message);
}
}
getInfo() {
return {
id: 'imageGenerator',
name: 'Image Generator',
blocks: [
{ opcode: 'setPrompt', blockType: Scratch.BlockType.COMMAND, text: 'set prompt to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: '1girl, halo, white wings, blue sky' } } },
{ opcode: 'setNegativePrompt', blockType: Scratch.BlockType.COMMAND, text: 'set negative prompt to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: 'blurry, low quality' } } },
{ opcode: 'setWidth', blockType: Scratch.BlockType.COMMAND, text: 'set width to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 512 } } },
{ opcode: 'setHeight', blockType: Scratch.BlockType.COMMAND, text: 'set height to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 512 } } },
{ opcode: 'setSteps', blockType: Scratch.BlockType.COMMAND, text: 'set steps to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 25 } } },
{ opcode: 'setCfgScale', blockType: Scratch.BlockType.COMMAND, text: 'set CFG Scale to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 7 } } },
{ opcode: 'setSampler', blockType: Scratch.BlockType.COMMAND, text: 'set sampler to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: 'DPM++ 2M' } } },
{ opcode: 'setStrength', blockType: Scratch.BlockType.COMMAND, text: 'set strength to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 0.7 } } },
{ opcode: 'setSeed', blockType: Scratch.BlockType.COMMAND, text: 'set seed to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: -1 } } },
{ opcode: 'setReturnType', blockType: Scratch.BlockType.COMMAND, text: 'set return type to [TYPE]', arguments: { TYPE: { type: Scratch.ArgumentType.STRING, menu: 'returnTypes', defaultValue: 'dataURL' } } },
{ opcode: 'generateImage', blockType: Scratch.BlockType.REPORTER, text: 'generate image' },
{ opcode: 'getFetchURL', blockType: Scratch.BlockType.REPORTER, text: 'get fetch URL' },
{ opcode: 'getParametersJSON', blockType: Scratch.BlockType.REPORTER, text: 'get parameters as JSON' }
],
menus: {
returnTypes: { acceptReporters: true, items: ['dataURL', 'blobURL'] }
}
};
}
// Block functions
setPrompt(args) { this.prompt = args.TEXT; }
setNegativePrompt(args) { this.negativePrompt = args.TEXT; }
setWidth(args) { this.width = args.NUM; }
setHeight(args) { this.height = args.NUM; }
setSteps(args) { this.steps = args.NUM; }
setCfgScale(args) { this.cfgs = args.NUM; }
setSampler(args) { this.sampler = args.TEXT; }
setStrength(args) { this.strength = args.NUM; }
setSeed(args) { this.seed = args.NUM; }
setReturnType(args) { this.returnType = args.TYPE; }
generateImage() { return this.fetchImage(); }
getFetchURL() { return this.generateFetchURL(); }
getParametersJSON() { return this.getParametersAsJSON(); }
}
// Register the extension
Scratch.extensions.register(new ImageGenerator());