Spaces:
Sleeping
Sleeping
T1ckbase
commited on
Commit
·
4a797ab
1
Parent(s):
0abf878
new
Browse files
main.ts
CHANGED
|
@@ -7,6 +7,20 @@ import OpenAI from '@openai/openai';
|
|
| 7 |
import { encodeBase64 } from '@std/encoding/base64';
|
| 8 |
import { ensureDir } from '@std/fs';
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
// https://api-inference.huggingface.co/v1
|
| 11 |
const HF_API_URL = 'https://api-inference.huggingface.co';
|
| 12 |
const JINA_API_URL = 'https://deepsearch.jina.ai';
|
|
@@ -176,43 +190,69 @@ app.post('/v1/images/generations', async (c) => {
|
|
| 176 |
// Create the appropriate data format based on the response_format
|
| 177 |
if (params.response_format === 'b64_json') {
|
| 178 |
return {
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
| 180 |
};
|
| 181 |
} else {
|
| 182 |
return {
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
| 184 |
};
|
| 185 |
}
|
| 186 |
} catch (error) {
|
| 187 |
console.error(`Error generating image ${i + 1}:`, error);
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
}
|
| 190 |
});
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
const imageResults = await Promise.all(imagePromises);
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
| 205 |
}
|
| 206 |
|
| 207 |
-
//
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
-
|
| 216 |
});
|
| 217 |
|
| 218 |
app.post('*', async (c) => {
|
|
|
|
| 7 |
import { encodeBase64 } from '@std/encoding/base64';
|
| 8 |
import { ensureDir } from '@std/fs';
|
| 9 |
|
| 10 |
+
interface Payload {
|
| 11 |
+
model: string;
|
| 12 |
+
inputs: string;
|
| 13 |
+
parameters?: {
|
| 14 |
+
guidance_scale?: number;
|
| 15 |
+
negative_prompt?: string;
|
| 16 |
+
num_inference_steps?: number;
|
| 17 |
+
width?: number;
|
| 18 |
+
height?: number;
|
| 19 |
+
scheduler?: string;
|
| 20 |
+
seed?: number;
|
| 21 |
+
};
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
// https://api-inference.huggingface.co/v1
|
| 25 |
const HF_API_URL = 'https://api-inference.huggingface.co';
|
| 26 |
const JINA_API_URL = 'https://deepsearch.jina.ai';
|
|
|
|
| 190 |
// Create the appropriate data format based on the response_format
|
| 191 |
if (params.response_format === 'b64_json') {
|
| 192 |
return {
|
| 193 |
+
success: true,
|
| 194 |
+
data: {
|
| 195 |
+
b64_json: encodeBase64(imageArrayBuffer),
|
| 196 |
+
},
|
| 197 |
};
|
| 198 |
} else {
|
| 199 |
return {
|
| 200 |
+
success: true,
|
| 201 |
+
data: {
|
| 202 |
+
url,
|
| 203 |
+
},
|
| 204 |
};
|
| 205 |
}
|
| 206 |
} catch (error) {
|
| 207 |
console.error(`Error generating image ${i + 1}:`, error);
|
| 208 |
+
// Return failure object instead of throwing
|
| 209 |
+
return {
|
| 210 |
+
success: false,
|
| 211 |
+
error: error instanceof Error ? error.message : 'Unknown error',
|
| 212 |
+
};
|
| 213 |
}
|
| 214 |
});
|
| 215 |
|
| 216 |
+
// Wait for all image generation attempts to complete (regardless of success/failure)
|
| 217 |
+
const results = await Promise.all(imagePromises);
|
|
|
|
| 218 |
|
| 219 |
+
// Filter out the successful results
|
| 220 |
+
const successfulImages = results
|
| 221 |
+
.filter((result) => result.success)
|
| 222 |
+
.map((result) => result.data);
|
| 223 |
|
| 224 |
+
// Collect errors for logging/reporting
|
| 225 |
+
const errors = results
|
| 226 |
+
.filter((result) => !result.success)
|
| 227 |
+
.map((result) => result.error);
|
| 228 |
+
|
| 229 |
+
if (errors.length > 0) {
|
| 230 |
+
console.warn(`${errors.length} of ${numImages} images failed to generate:`, errors);
|
| 231 |
}
|
| 232 |
|
| 233 |
+
// Return successful images even if some failed
|
| 234 |
+
const responseBody = {
|
| 235 |
+
created: Math.floor(Date.now() / 1000),
|
| 236 |
+
data: successfulImages,
|
| 237 |
+
// Include error information if any images failed
|
| 238 |
+
...(errors.length > 0
|
| 239 |
+
? {
|
| 240 |
+
partial_failure: true,
|
| 241 |
+
error_count: errors.length,
|
| 242 |
+
success_count: successfulImages.length,
|
| 243 |
+
}
|
| 244 |
+
: {}),
|
| 245 |
+
};
|
| 246 |
+
|
| 247 |
+
// If all images failed, return 500 status
|
| 248 |
+
if (successfulImages.length === 0) {
|
| 249 |
+
return c.json({
|
| 250 |
+
error: 'Failed to generate any images',
|
| 251 |
+
errors: errors,
|
| 252 |
+
}, 500);
|
| 253 |
+
}
|
| 254 |
|
| 255 |
+
return c.json(responseBody);
|
| 256 |
});
|
| 257 |
|
| 258 |
app.post('*', async (c) => {
|