Update index.js
Browse files
index.js
CHANGED
|
@@ -45,20 +45,62 @@ async function initializeSessions() {
|
|
| 45 |
status.textContent = 'Ready';
|
| 46 |
}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
async function parse(img, txt) {
|
| 64 |
imageContainer.innerHTML = '';
|
|
@@ -68,7 +110,6 @@ async function parse(img, txt) {
|
|
| 68 |
status.textContent = output;
|
| 69 |
}
|
| 70 |
|
| 71 |
-
|
| 72 |
export async function imageTextToText(
|
| 73 |
imagePath,
|
| 74 |
query,
|
|
@@ -282,4 +323,19 @@ export async function imageTextToText(
|
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
-
await initializeSessions();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
status.textContent = 'Ready';
|
| 46 |
}
|
| 47 |
|
| 48 |
+
export function int64ToFloat16(int64Value) {
|
| 49 |
+
// Convert BigInt to Number (float64)
|
| 50 |
+
const float64Value = Number(int64Value);
|
| 51 |
+
|
| 52 |
+
// Handle special cases
|
| 53 |
+
if (!isFinite(float64Value)) return float64Value > 0 ? 0x7c00 : 0xfc00; // +/- infinity
|
| 54 |
+
if (float64Value === 0) return 0; // Zero is represented as 0
|
| 55 |
+
|
| 56 |
+
// Get sign, exponent, and mantissa from float64
|
| 57 |
+
const sign = float64Value < 0 ? 1 : 0;
|
| 58 |
+
const absValue = Math.abs(float64Value);
|
| 59 |
+
const exponent = Math.floor(Math.log2(absValue));
|
| 60 |
+
const mantissa = absValue / Math.pow(2, exponent) - 1;
|
| 61 |
+
|
| 62 |
+
// Convert exponent and mantissa to float16 format
|
| 63 |
+
const float16Exponent = exponent + 15; // Offset exponent by 15 (float16 bias)
|
| 64 |
+
const float16Mantissa = Math.round(mantissa * 1024); // 10-bit mantissa for float16
|
| 65 |
+
|
| 66 |
+
// Handle overflow/underflow
|
| 67 |
+
if (float16Exponent <= 0) {
|
| 68 |
+
// Subnormal numbers (exponent <= 0)
|
| 69 |
+
return (sign << 15) | (float16Mantissa >> 1);
|
| 70 |
+
} else if (float16Exponent >= 31) {
|
| 71 |
+
// Overflow, set to infinity
|
| 72 |
+
return (sign << 15) | 0x7c00;
|
| 73 |
+
} else {
|
| 74 |
+
// Normalized numbers
|
| 75 |
+
return (sign << 15) | (float16Exponent << 10) | (float16Mantissa & 0x3ff);
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
|
| 79 |
+
export function float16ToInt64(float16Value: number): bigint {
|
| 80 |
+
// Extract components from float16
|
| 81 |
+
const sign = (float16Value & 0x8000) >> 15;
|
| 82 |
+
const exponent = (float16Value & 0x7c00) >> 10;
|
| 83 |
+
const mantissa = float16Value & 0x03ff;
|
| 84 |
+
|
| 85 |
+
// Handle special cases
|
| 86 |
+
if (exponent === 0 && mantissa === 0) return BigInt(0); // Zero
|
| 87 |
+
if (exponent === 0x1f) return sign ? BigInt("-Infinity") : BigInt("Infinity"); // Infinity
|
| 88 |
+
|
| 89 |
+
// Convert back to number
|
| 90 |
+
let value;
|
| 91 |
+
if (exponent === 0) {
|
| 92 |
+
// Subnormal numbers
|
| 93 |
+
value = Math.pow(2, -14) * (mantissa / 1024);
|
| 94 |
+
} else {
|
| 95 |
+
// Normalized numbers
|
| 96 |
+
value = Math.pow(2, exponent - 15) * (1 + mantissa / 1024);
|
| 97 |
+
}
|
| 98 |
|
| 99 |
+
// Apply sign
|
| 100 |
+
value = sign ? -value : value;
|
| 101 |
+
|
| 102 |
+
return BigInt(Math.round(value));
|
| 103 |
+
}
|
| 104 |
|
| 105 |
async function parse(img, txt) {
|
| 106 |
imageContainer.innerHTML = '';
|
|
|
|
| 110 |
status.textContent = output;
|
| 111 |
}
|
| 112 |
|
|
|
|
| 113 |
export async function imageTextToText(
|
| 114 |
imagePath,
|
| 115 |
query,
|
|
|
|
| 323 |
}
|
| 324 |
}
|
| 325 |
|
| 326 |
+
await initializeSessions();
|
| 327 |
+
|
| 328 |
+
// UI Event Handlers
|
| 329 |
+
example.addEventListener('click', (e) => {
|
| 330 |
+
e.preventDefault();
|
| 331 |
+
parse(EXAMPLE_URL, 'Describe this image.');
|
| 332 |
+
});
|
| 333 |
+
|
| 334 |
+
fileUpload.addEventListener('change', function(e) {
|
| 335 |
+
const file = e.target.files[0];
|
| 336 |
+
if (!file) return;
|
| 337 |
+
|
| 338 |
+
const reader = new FileReader();
|
| 339 |
+
reader.onload = e2 => parse(e2.target.result, '');
|
| 340 |
+
reader.readAsDataURL(file);
|
| 341 |
+
});
|