Spaces:
Running
Running
Update imgen.js
Browse files
imgen.js
CHANGED
@@ -10,12 +10,18 @@ class ImageGenerator {
|
|
10 |
this.sampler = 'DPM++ 2M';
|
11 |
this.strength = 0.7;
|
12 |
this.seed = -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
}
|
20 |
|
21 |
// Return parameters as JSON for debugging
|
@@ -29,22 +35,22 @@ class ImageGenerator {
|
|
29 |
cfgs: this.cfgs,
|
30 |
sampler: this.sampler,
|
31 |
strength: this.strength,
|
32 |
-
seed: this.seed
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
});
|
34 |
}
|
35 |
|
36 |
-
//
|
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,7 +65,7 @@ class ImageGenerator {
|
|
59 |
});
|
60 |
}
|
61 |
} catch (error) {
|
62 |
-
console.error(
|
63 |
throw new Error('Error generating image: ' + error.message);
|
64 |
}
|
65 |
}
|
@@ -78,6 +84,12 @@ class ImageGenerator {
|
|
78 |
{ opcode: 'setSampler', blockType: Scratch.BlockType.COMMAND, text: 'set sampler to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: 'DPM++ 2M' } } },
|
79 |
{ opcode: 'setStrength', blockType: Scratch.BlockType.COMMAND, text: 'set strength to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 0.7 } } },
|
80 |
{ opcode: 'setSeed', blockType: Scratch.BlockType.COMMAND, text: 'set seed to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: -1 } } },
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
{ opcode: 'setReturnType', blockType: Scratch.BlockType.COMMAND, text: 'set return type to [TYPE]', arguments: { TYPE: { type: Scratch.ArgumentType.STRING, menu: 'returnTypes', defaultValue: 'dataURL' } } },
|
82 |
{ opcode: 'generateImage', blockType: Scratch.BlockType.REPORTER, text: 'generate image' },
|
83 |
{ opcode: 'getFetchURL', blockType: Scratch.BlockType.REPORTER, text: 'get fetch URL' },
|
@@ -99,6 +111,12 @@ class ImageGenerator {
|
|
99 |
setSampler(args) { this.sampler = args.TEXT; }
|
100 |
setStrength(args) { this.strength = args.NUM; }
|
101 |
setSeed(args) { this.seed = args.NUM; }
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
setReturnType(args) { this.returnType = args.TYPE; }
|
103 |
|
104 |
async generateImage() { return this.fetchImage(); }
|
|
|
10 |
this.sampler = 'DPM++ 2M';
|
11 |
this.strength = 0.7;
|
12 |
this.seed = -1;
|
13 |
+
this.guidanceScale = 7.5;
|
14 |
+
this.numReturnSequences = 1;
|
15 |
+
this.temperature = 1.0;
|
16 |
+
this.topK = 50;
|
17 |
+
this.topP = 0.9;
|
18 |
+
this.eta = 0.1;
|
19 |
this.returnType = 'dataURL'; // dataURL or blobURL
|
20 |
}
|
21 |
|
22 |
+
// Generate URL for fetching
|
23 |
generateFetchURL() {
|
24 |
+
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}&guidance_scale=${this.guidanceScale}&num_return_sequences=${this.numReturnSequences}&temperature=${this.temperature}&top_k=${this.topK}&top_p=${this.topP}&eta=${this.eta}`;
|
25 |
}
|
26 |
|
27 |
// Return parameters as JSON for debugging
|
|
|
35 |
cfgs: this.cfgs,
|
36 |
sampler: this.sampler,
|
37 |
strength: this.strength,
|
38 |
+
seed: this.seed,
|
39 |
+
guidanceScale: this.guidanceScale,
|
40 |
+
numReturnSequences: this.numReturnSequences,
|
41 |
+
temperature: this.temperature,
|
42 |
+
topK: this.topK,
|
43 |
+
topP: this.topP,
|
44 |
+
eta: this.eta
|
45 |
});
|
46 |
}
|
47 |
|
48 |
+
// Fetch function to generate the image
|
49 |
async fetchImage() {
|
50 |
const url = this.generateFetchURL();
|
51 |
try {
|
|
|
52 |
const response = await fetch(url);
|
53 |
+
if (!response.ok) throw new Error('Failed to generate image');
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
const blob = await response.blob();
|
56 |
if (this.returnType === 'blobURL') {
|
|
|
65 |
});
|
66 |
}
|
67 |
} catch (error) {
|
68 |
+
console.error(error);
|
69 |
throw new Error('Error generating image: ' + error.message);
|
70 |
}
|
71 |
}
|
|
|
84 |
{ opcode: 'setSampler', blockType: Scratch.BlockType.COMMAND, text: 'set sampler to [TEXT]', arguments: { TEXT: { type: Scratch.ArgumentType.STRING, defaultValue: 'DPM++ 2M' } } },
|
85 |
{ opcode: 'setStrength', blockType: Scratch.BlockType.COMMAND, text: 'set strength to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 0.7 } } },
|
86 |
{ opcode: 'setSeed', blockType: Scratch.BlockType.COMMAND, text: 'set seed to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: -1 } } },
|
87 |
+
{ opcode: 'setGuidanceScale', blockType: Scratch.BlockType.COMMAND, text: 'set guidance scale to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 7.5 } } },
|
88 |
+
{ opcode: 'setNumReturnSequences', blockType: Scratch.BlockType.COMMAND, text: 'set number of return sequences to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 1 } } },
|
89 |
+
{ opcode: 'setTemperature', blockType: Scratch.BlockType.COMMAND, text: 'set temperature to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 1.0 } } },
|
90 |
+
{ opcode: 'setTopK', blockType: Scratch.BlockType.COMMAND, text: 'set top K to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 50 } } },
|
91 |
+
{ opcode: 'setTopP', blockType: Scratch.BlockType.COMMAND, text: 'set top P to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 0.9 } } },
|
92 |
+
{ opcode: 'setEta', blockType: Scratch.BlockType.COMMAND, text: 'set eta to [NUM]', arguments: { NUM: { type: Scratch.ArgumentType.NUMBER, defaultValue: 0.1 } } },
|
93 |
{ opcode: 'setReturnType', blockType: Scratch.BlockType.COMMAND, text: 'set return type to [TYPE]', arguments: { TYPE: { type: Scratch.ArgumentType.STRING, menu: 'returnTypes', defaultValue: 'dataURL' } } },
|
94 |
{ opcode: 'generateImage', blockType: Scratch.BlockType.REPORTER, text: 'generate image' },
|
95 |
{ opcode: 'getFetchURL', blockType: Scratch.BlockType.REPORTER, text: 'get fetch URL' },
|
|
|
111 |
setSampler(args) { this.sampler = args.TEXT; }
|
112 |
setStrength(args) { this.strength = args.NUM; }
|
113 |
setSeed(args) { this.seed = args.NUM; }
|
114 |
+
setGuidanceScale(args) { this.guidanceScale = args.NUM; }
|
115 |
+
setNumReturnSequences(args) { this.numReturnSequences = args.NUM; }
|
116 |
+
setTemperature(args) { this.temperature = args.NUM; }
|
117 |
+
setTopK(args) { this.topK = args.NUM; }
|
118 |
+
setTopP(args) { this.topP = args.NUM; }
|
119 |
+
setEta(args) { this.eta = args.NUM; }
|
120 |
setReturnType(args) { this.returnType = args.TYPE; }
|
121 |
|
122 |
async generateImage() { return this.fetchImage(); }
|