fixed prot!
Browse files- prototype_index.html +49 -78
prototype_index.html
CHANGED
@@ -372,92 +372,63 @@
|
|
372 |
captionResultDiv.innerHTML = "Generating caption…";
|
373 |
|
374 |
try {
|
375 |
-
console.log("===
|
376 |
-
console.log("imageToCaption:",
|
377 |
-
name: imageToCaption.name,
|
378 |
-
size: imageToCaption.size,
|
379 |
-
type: imageToCaption.type,
|
380 |
-
constructor: imageToCaption.constructor.name,
|
381 |
-
isFile: imageToCaption instanceof File
|
382 |
-
});
|
383 |
-
console.log("captionType:", captionType);
|
384 |
-
console.log("captionLength:", captionLength);
|
385 |
|
386 |
-
//
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
}
|
394 |
|
|
|
|
|
|
|
|
|
395 |
console.log("Calling joyCaption.predict...");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
396 |
|
397 |
-
|
398 |
-
let output;
|
399 |
-
let attempt = 1;
|
400 |
|
401 |
-
//
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
captionLength,
|
408 |
-
[], // extra_options
|
409 |
-
"", // name_input
|
410 |
-
"" // custom_prompt
|
411 |
-
]);
|
412 |
-
console.log("Success with direct File!");
|
413 |
-
} catch (err1) {
|
414 |
-
console.warn(`Attempt ${attempt} failed:`, err1);
|
415 |
-
attempt++;
|
416 |
-
|
417 |
-
// Attempt 2: Convert to blob URL
|
418 |
-
try {
|
419 |
-
console.log(`Attempt ${attempt}: Blob URL`);
|
420 |
-
const blobUrl = URL.createObjectURL(imageToCaption);
|
421 |
-
output = await joyCaption.predict("/stream_chat", [
|
422 |
-
blobUrl,
|
423 |
-
captionType,
|
424 |
-
captionLength,
|
425 |
-
[], // extra_options
|
426 |
-
"", // name_input
|
427 |
-
"" // custom_prompt
|
428 |
-
]);
|
429 |
-
console.log("Success with blob URL!");
|
430 |
-
URL.revokeObjectURL(blobUrl);
|
431 |
-
} catch (err2) {
|
432 |
-
console.warn(`Attempt ${attempt} failed:`, err2);
|
433 |
-
attempt++;
|
434 |
-
|
435 |
-
// Attempt 3: Convert to data URL
|
436 |
-
try {
|
437 |
-
console.log(`Attempt ${attempt}: Data URL`);
|
438 |
-
const dataUrl = await new Promise((resolve) => {
|
439 |
-
const reader = new FileReader();
|
440 |
-
reader.onload = () => resolve(reader.result);
|
441 |
-
reader.readAsDataURL(imageToCaption);
|
442 |
-
});
|
443 |
-
output = await joyCaption.predict("/stream_chat", [
|
444 |
-
dataUrl,
|
445 |
-
captionType,
|
446 |
-
captionLength,
|
447 |
-
[], // extra_options
|
448 |
-
"", // name_input
|
449 |
-
"" // custom_prompt
|
450 |
-
]);
|
451 |
-
console.log("Success with data URL!");
|
452 |
-
} catch (err3) {
|
453 |
-
console.error(`All ${attempt} attempts failed. Last error:`, err3);
|
454 |
-
throw err3;
|
455 |
-
}
|
456 |
-
}
|
457 |
}
|
458 |
|
459 |
-
console.log("Success!
|
460 |
-
const [prompt, caption] = output.data;
|
461 |
captionResultDiv.innerHTML = `
|
462 |
<div style="background:#f8f9fa; padding:1rem; border-radius:6px; margin-top:1rem;">
|
463 |
<h4>Generated Caption</h4>
|
|
|
372 |
captionResultDiv.innerHTML = "Generating caption…";
|
373 |
|
374 |
try {
|
375 |
+
console.log("=== CALLING JOY CAPTION ===");
|
376 |
+
console.log("imageToCaption:", imageToCaption.name, imageToCaption.size, imageToCaption.type);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
|
378 |
+
// Build the prompt using the helper function logic from Joy Caption
|
379 |
+
let promptTemplate;
|
380 |
+
if (captionLength === "any") {
|
381 |
+
promptTemplate = "Write a detailed description for this image.";
|
382 |
+
} else if (captionLength.match(/^\d+$/)) {
|
383 |
+
promptTemplate = `Write a detailed description for this image in ${captionLength} words or less.`;
|
384 |
+
} else {
|
385 |
+
promptTemplate = `Write a ${captionLength} detailed description for this image.`;
|
386 |
+
}
|
387 |
+
|
388 |
+
// Adjust template based on caption type
|
389 |
+
if (captionType === "Descriptive (Informal)") {
|
390 |
+
promptTemplate = promptTemplate.replace("detailed description", "descriptive caption in a casual tone");
|
391 |
+
} else if (captionType === "Training Prompt") {
|
392 |
+
promptTemplate = "Output a stable diffusion prompt that is indistinguishable from a real stable diffusion prompt.";
|
393 |
+
} else if (captionType === "MidJourney") {
|
394 |
+
promptTemplate = "Write a MidJourney prompt for this image.";
|
395 |
+
} else if (captionType === "Booru tag list") {
|
396 |
+
promptTemplate = "Generate only comma-separated Danbooru tags (lowercase_underscores). Strict order: `artist:`, `copyright:`, `character:`, `meta:`, then general tags.";
|
397 |
+
} else if (captionType === "Booru-like tag list") {
|
398 |
+
promptTemplate = "Write a list of Booru-like tags for this image.";
|
399 |
+
} else if (captionType === "Art Critic") {
|
400 |
+
promptTemplate = "Analyze this image like an art critic would with information about its composition, style, symbolism, the use of color, light, any artistic movement it might belong to, etc.";
|
401 |
+
} else if (captionType === "Product Listing") {
|
402 |
+
promptTemplate = "Write a caption for this image as though it were a product listing.";
|
403 |
+
} else if (captionType === "Social Media Post") {
|
404 |
+
promptTemplate = "Write a caption for this image as if it were being used for a social media post.";
|
405 |
}
|
406 |
|
407 |
+
console.log("Using prompt:", promptTemplate);
|
408 |
+
|
409 |
+
// Call the correct endpoint with correct parameters
|
410 |
+
// chat_joycaption expects: input_image, prompt, temperature, top_p, max_new_tokens, log_prompt
|
411 |
console.log("Calling joyCaption.predict...");
|
412 |
+
const result = await joyCaption.predict("/chat_joycaption", [
|
413 |
+
imageToCaption, // input_image
|
414 |
+
promptTemplate, // prompt
|
415 |
+
0.6, // temperature
|
416 |
+
0.9, // top_p
|
417 |
+
512, // max_new_tokens
|
418 |
+
false // log_prompt
|
419 |
+
]);
|
420 |
|
421 |
+
console.log("Raw result:", result);
|
|
|
|
|
422 |
|
423 |
+
// For generator endpoints, the result might be different
|
424 |
+
let caption;
|
425 |
+
if (result && result.data) {
|
426 |
+
caption = result.data[0] || result.data;
|
427 |
+
} else {
|
428 |
+
caption = result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
429 |
}
|
430 |
|
431 |
+
console.log("Success! Caption:", caption);
|
|
|
432 |
captionResultDiv.innerHTML = `
|
433 |
<div style="background:#f8f9fa; padding:1rem; border-radius:6px; margin-top:1rem;">
|
434 |
<h4>Generated Caption</h4>
|