Spaces:
Running
Running
Update imgen.js
Browse files
imgen.js
CHANGED
@@ -13,7 +13,7 @@ class ImageGenerator {
|
|
13 |
this.returnType = 'dataURL'; // dataURL or blobURL
|
14 |
}
|
15 |
|
16 |
-
// Generate URL for fetching
|
17 |
generateFetchURL() {
|
18 |
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}`;
|
19 |
}
|
@@ -33,12 +33,18 @@ class ImageGenerator {
|
|
33 |
});
|
34 |
}
|
35 |
|
36 |
-
//
|
37 |
async fetchImage() {
|
38 |
const url = this.generateFetchURL();
|
39 |
try {
|
|
|
40 |
const response = await fetch(url);
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
const blob = await response.blob();
|
44 |
if (this.returnType === 'blobURL') {
|
@@ -53,7 +59,7 @@ class ImageGenerator {
|
|
53 |
});
|
54 |
}
|
55 |
} catch (error) {
|
56 |
-
console.error(error);
|
57 |
throw new Error('Error generating image: ' + error.message);
|
58 |
}
|
59 |
}
|
@@ -63,8 +69,8 @@ class ImageGenerator {
|
|
63 |
id: 'imageGenerator',
|
64 |
name: 'Image Generator',
|
65 |
blocks: [
|
66 |
-
{ opcode: 'setPrompt', blockType: Scratch.BlockType.COMMAND, text: 'set prompt to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: '1girl,
|
67 |
-
{ opcode: 'setNegativePrompt', blockType: Scratch.BlockType.COMMAND, text: 'set negative prompt to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: '
|
68 |
{ opcode: 'setWidth', blockType: Scratch.BlockType.COMMAND, text: 'set width to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 512 } } },
|
69 |
{ opcode: 'setHeight', blockType: Scratch.BlockType.COMMAND, text: 'set height to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 512 } } },
|
70 |
{ opcode: 'setSteps', blockType: Scratch.BlockType.COMMAND, text: 'set steps to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 25 } } },
|
@@ -101,4 +107,4 @@ class ImageGenerator {
|
|
101 |
}
|
102 |
|
103 |
// Register the extension
|
104 |
-
Scratch.extensions.register(new ImageGenerator());
|
|
|
13 |
this.returnType = 'dataURL'; // dataURL or blobURL
|
14 |
}
|
15 |
|
16 |
+
// Generate URL for fetching (no num_return_sequences)
|
17 |
generateFetchURL() {
|
18 |
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}`;
|
19 |
}
|
|
|
33 |
});
|
34 |
}
|
35 |
|
36 |
+
// Improved fetch function with detailed error handling
|
37 |
async fetchImage() {
|
38 |
const url = this.generateFetchURL();
|
39 |
try {
|
40 |
+
console.log("Fetch URL:", url); // Debugging URL
|
41 |
const response = await fetch(url);
|
42 |
+
|
43 |
+
if (!response.ok) {
|
44 |
+
const errorMessage = await response.text();
|
45 |
+
console.error('Error response:', errorMessage); // Log detailed error message
|
46 |
+
throw new Error(`Failed to generate image: ${errorMessage}`);
|
47 |
+
}
|
48 |
|
49 |
const blob = await response.blob();
|
50 |
if (this.returnType === 'blobURL') {
|
|
|
59 |
});
|
60 |
}
|
61 |
} catch (error) {
|
62 |
+
console.error('Error generating image:', error); // Log full error object for debugging
|
63 |
throw new Error('Error generating image: ' + error.message);
|
64 |
}
|
65 |
}
|
|
|
69 |
id: 'imageGenerator',
|
70 |
name: 'Image Generator',
|
71 |
blocks: [
|
72 |
+
{ opcode: 'setPrompt', blockType: Scratch.BlockType.COMMAND, text: 'set prompt to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: '1girl, halo, white wings, white sundress, angel, blue sky, blonde hair, blue eyes, long hair, dynamic angle' } } },
|
73 |
+
{ opcode: 'setNegativePrompt', blockType: Scratch.BlockType.COMMAND, text: 'set negative prompt to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: 'anatomically bad, low quality, old, 1900s, 1800s, bad anatomically, many fingers, deformed feet, deformed fingers, deformed face , deformed eyes, blurry eyes, blurry, old quality' } } },
|
74 |
{ opcode: 'setWidth', blockType: Scratch.BlockType.COMMAND, text: 'set width to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 512 } } },
|
75 |
{ opcode: 'setHeight', blockType: Scratch.BlockType.COMMAND, text: 'set height to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 512 } } },
|
76 |
{ opcode: 'setSteps', blockType: Scratch.BlockType.COMMAND, text: 'set steps to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 25 } } },
|
|
|
107 |
}
|
108 |
|
109 |
// Register the extension
|
110 |
+
Scratch.extensions.register(new ImageGenerator());
|