change ui style to be more modern
Browse files- index.html +382 -409
index.html
CHANGED
|
@@ -4,497 +4,470 @@
|
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
<title>Camera Interaction App</title>
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
<style>
|
| 10 |
:root {
|
| 11 |
-
--primary: #
|
| 12 |
-
--primary-
|
| 13 |
-
--
|
| 14 |
-
--
|
| 15 |
-
--
|
| 16 |
-
--
|
| 17 |
-
--
|
| 18 |
-
--
|
| 19 |
-
--
|
| 20 |
-
--
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
height: 100%;
|
| 24 |
-
margin: 0;
|
| 25 |
-
padding: 0;
|
| 26 |
}
|
|
|
|
| 27 |
body {
|
| 28 |
-
font-family: var(--font);
|
| 29 |
-
background: var(--
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
display: flex;
|
| 32 |
flex-direction: column;
|
| 33 |
align-items: center;
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
h1 {
|
| 38 |
-
color: var(--primary-dark);
|
| 39 |
-
font-size: 2.2rem;
|
| 40 |
-
font-weight: 700;
|
| 41 |
-
margin-bottom: 0;
|
| 42 |
-
text-align: center;
|
| 43 |
-
width: 100%;
|
| 44 |
-
max-width: 700px;
|
| 45 |
}
|
|
|
|
| 46 |
.container {
|
| 47 |
-
width:
|
| 48 |
-
max-width:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
display: flex;
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
-
|
|
|
|
| 54 |
position: relative;
|
| 55 |
width: 100%;
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
border-radius: var(--radius);
|
| 59 |
overflow: hidden;
|
| 60 |
-
box-shadow: var(--shadow);
|
| 61 |
-
|
| 62 |
-
background: #000;
|
| 63 |
-
margin: 0 auto;
|
| 64 |
}
|
|
|
|
| 65 |
#videoFeed {
|
| 66 |
width: 100%;
|
| 67 |
height: 100%;
|
| 68 |
object-fit: cover;
|
| 69 |
-
border-radius: var(--radius);
|
| 70 |
display: block;
|
| 71 |
}
|
|
|
|
| 72 |
#loadingOverlay {
|
| 73 |
position: absolute;
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
display: none;
|
| 76 |
justify-content: center;
|
| 77 |
align-items: center;
|
| 78 |
-
|
| 79 |
-
z-index: 10;
|
| 80 |
-
color: #fff;
|
| 81 |
-
font-size: 1.5em;
|
| 82 |
-
font-weight: bold;
|
| 83 |
-
border-radius: var(--radius);
|
| 84 |
-
text-align: right;
|
| 85 |
}
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
gap: 18px;
|
| 90 |
-
background: var(--surface);
|
| 91 |
-
border-radius: var(--radius);
|
| 92 |
-
box-shadow: var(--shadow);
|
| 93 |
-
padding: 24px 28px;
|
| 94 |
-
align-items: stretch;
|
| 95 |
-
border: 1.5px solid var(--border);
|
| 96 |
-
}
|
| 97 |
-
.io-areas > div {
|
| 98 |
-
display: flex;
|
| 99 |
-
flex-direction: column;
|
| 100 |
-
align-items: flex-start;
|
| 101 |
-
gap: 6px;
|
| 102 |
}
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
text-
|
| 108 |
-
width: 100%;
|
| 109 |
}
|
| 110 |
-
|
|
|
|
| 111 |
width: 100%;
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
min-height: 8em;
|
| 116 |
-
max-height: 8em;
|
| 117 |
-
padding: 10px 14px;
|
| 118 |
-
border: 1.5px solid var(--border);
|
| 119 |
-
border-radius: 8px;
|
| 120 |
-
font-size: 1rem;
|
| 121 |
-
background: var(--bg);
|
| 122 |
-
color: #222;
|
| 123 |
-
resize: vertical;
|
| 124 |
box-sizing: border-box;
|
|
|
|
|
|
|
|
|
|
| 125 |
text-align: right;
|
| 126 |
-
transition: border 0.2s;
|
| 127 |
-
}
|
| 128 |
-
textarea:focus {
|
| 129 |
-
border-color: var(--primary);
|
| 130 |
-
outline: none;
|
| 131 |
-
background: #f1f5f9;
|
| 132 |
}
|
|
|
|
| 133 |
.controls {
|
| 134 |
display: flex;
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
align-items: center;
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
border: 1.5px solid var(--border);
|
| 142 |
-
justify-content: flex-start;
|
| 143 |
-
}
|
| 144 |
-
.controls label {
|
| 145 |
-
margin-bottom: 0;
|
| 146 |
-
font-size: 1rem;
|
| 147 |
-
color: #334155;
|
| 148 |
-
font-weight: 500;
|
| 149 |
-
text-align: right;
|
| 150 |
-
width: auto;
|
| 151 |
}
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
border:
|
|
|
|
|
|
|
| 156 |
font-size: 1rem;
|
| 157 |
-
|
| 158 |
-
|
| 159 |
text-align: right;
|
| 160 |
-
|
| 161 |
-
transition: border 0.2s;
|
| 162 |
}
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
font-weight: 600;
|
| 171 |
-
cursor: pointer;
|
| 172 |
border: none;
|
| 173 |
-
border-radius:
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
}
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
| 180 |
}
|
| 181 |
-
|
| 182 |
-
|
|
|
|
| 183 |
}
|
|
|
|
| 184 |
.hidden {
|
| 185 |
display: none;
|
| 186 |
}
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
textarea, select, input {
|
| 192 |
-
direction: rtl;
|
| 193 |
-
}
|
| 194 |
-
/* Responsive */
|
| 195 |
-
@media (max-width: 800px) {
|
| 196 |
-
.container {
|
| 197 |
-
max-width: 98vw;
|
| 198 |
-
}
|
| 199 |
-
#videoContainer {
|
| 200 |
-
max-width: 98vw;
|
| 201 |
-
}
|
| 202 |
-
textarea {
|
| 203 |
-
min-width: 0;
|
| 204 |
-
max-width: 98vw;
|
| 205 |
-
}
|
| 206 |
}
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
}
|
| 211 |
-
h1 {
|
| 212 |
-
font-size: 1.3rem;
|
| 213 |
-
}
|
| 214 |
}
|
| 215 |
</style>
|
| 216 |
</head>
|
| 217 |
<body>
|
| 218 |
<div class="container">
|
| 219 |
-
<
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
| 221 |
<video id="videoFeed" autoplay playsinline></video>
|
| 222 |
<div id="loadingOverlay">در حال بارگذاری...</div>
|
| 223 |
</div>
|
| 224 |
<canvas id="canvas" class="hidden"></canvas>
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
<div>
|
| 235 |
-
<label for="responseText">پاسخ:</label>
|
| 236 |
-
<textarea
|
| 237 |
-
id="responseText"
|
| 238 |
-
name="Response"
|
| 239 |
-
readonly
|
| 240 |
-
placeholder="پاسخ سرور اینجا نمایش داده میشود..."
|
| 241 |
-
rows="4"
|
| 242 |
-
cols="50"
|
| 243 |
-
></textarea>
|
| 244 |
-
</div>
|
| 245 |
</div>
|
|
|
|
| 246 |
<div class="controls">
|
| 247 |
-
<
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
| 257 |
</div>
|
| 258 |
</div>
|
|
|
|
| 259 |
<script type="module">
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
-
|
| 267 |
|
| 268 |
-
|
| 269 |
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
const loadingOverlay = document.getElementById("loadingOverlay");
|
| 283 |
|
| 284 |
-
|
| 285 |
-
const CONTEXT = `
|
| 286 |
Translate the text into persian and only return the translated text without any other text.
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
}
|
| 324 |
}
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
canvas.width = video.videoWidth;
|
| 331 |
-
canvas.height = video.videoHeight;
|
| 332 |
-
const context = canvas.getContext("2d", { willReadFrequently: true });
|
| 333 |
-
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 334 |
-
const frame = context.getImageData(0, 0, canvas.width, canvas.height);
|
| 335 |
-
return new RawImage(frame.data, frame.width, frame.height, 4);
|
| 336 |
}
|
| 337 |
-
|
| 338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
{
|
| 340 |
-
|
| 341 |
-
|
| 342 |
},
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
add_generation_prompt: true,
|
| 347 |
-
});
|
| 348 |
-
|
| 349 |
-
const inputs = await processor(text, [imgElement], {
|
| 350 |
-
do_image_splitting: false,
|
| 351 |
-
});
|
| 352 |
-
|
| 353 |
-
const generatedIds = await model.generate({
|
| 354 |
-
...inputs,
|
| 355 |
-
max_new_tokens: 100,
|
| 356 |
-
});
|
| 357 |
-
|
| 358 |
-
const output = processor.batch_decode(
|
| 359 |
-
generatedIds.slice(null, [inputs.input_ids.dims.at(-1), null]),
|
| 360 |
-
{ skip_special_tokens: true }
|
| 361 |
-
);
|
| 362 |
-
return output[0].trim();
|
| 363 |
-
}
|
| 364 |
-
|
| 365 |
-
async function callExternalLLmAPI(text) {
|
| 366 |
-
let response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
| 367 |
-
method: "POST",
|
| 368 |
-
headers: {
|
| 369 |
-
"Authorization": "Bearer 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",
|
| 370 |
-
"HTTP-Referer": "<YOUR_SITE_URL>", // Optional. Site URL for rankings on openrouter.ai.
|
| 371 |
-
"X-Title": "<YOUR_SITE_NAME>", // Optional. Site title for rankings on openrouter.ai.
|
| 372 |
-
"Content-Type": "application/json"
|
| 373 |
},
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
"role": "system",
|
| 379 |
-
"content": CONTEXT
|
| 380 |
-
},
|
| 381 |
-
{
|
| 382 |
-
"role": "user",
|
| 383 |
-
"content": text
|
| 384 |
-
}
|
| 385 |
-
]
|
| 386 |
-
})
|
| 387 |
-
});
|
| 388 |
-
|
| 389 |
-
if (!response.ok) {
|
| 390 |
-
throw new Error(`HTTP error! Status: ${response.status}`);
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
const data = await response.json();
|
| 394 |
-
const generatedText = data.choices[0].message.content;
|
| 395 |
-
return generatedText;
|
| 396 |
-
}
|
| 397 |
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
const response = await openai.chat.completions.create({
|
| 401 |
-
messages: [
|
| 402 |
-
{ role: "system", content: CONTEXT },
|
| 403 |
-
{ role: "user", content: text }
|
| 404 |
-
],
|
| 405 |
-
model: "gpt-4o",
|
| 406 |
-
});
|
| 407 |
-
|
| 408 |
-
let generatedText = response.choices[0].message.content;
|
| 409 |
-
generatedText = generatedText.trim();
|
| 410 |
-
return generatedText;
|
| 411 |
}
|
| 412 |
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
}
|
| 430 |
-
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
}
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
| 440 |
}
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
isProcessing = true;
|
| 448 |
-
startButton.textContent = "توقف";
|
| 449 |
-
startButton.classList.replace("start", "stop");
|
| 450 |
-
// instructionText.disabled = true;
|
| 451 |
-
intervalSelect.disabled = true;
|
| 452 |
-
responseText.value = "Processing started...";
|
| 453 |
-
processingLoop();
|
| 454 |
}
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
}
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
}
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
stream.getTracks().forEach((track) => track.stop());
|
| 496 |
-
}
|
| 497 |
-
});
|
| 498 |
</script>
|
| 499 |
</body>
|
| 500 |
</html>
|
|
|
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
<title>Camera Interaction App</title>
|
| 7 |
+
<link
|
| 8 |
+
href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;600;700&display=swap"
|
| 9 |
+
rel="stylesheet"
|
| 10 |
+
/>
|
| 11 |
+
<link
|
| 12 |
+
rel="stylesheet"
|
| 13 |
+
href="https://fonts.googleapis.com/icon?family=Material+Icons"
|
| 14 |
+
/>
|
| 15 |
<style>
|
| 16 |
:root {
|
| 17 |
+
--primary-color: #6200ee;
|
| 18 |
+
--primary-variant: #3700b3;
|
| 19 |
+
--secondary-color: #03dac6;
|
| 20 |
+
--background-color: #fff;
|
| 21 |
+
--surface-color: #fff;
|
| 22 |
+
--error-color: #b00020;
|
| 23 |
+
--text-primary: #212121;
|
| 24 |
+
--text-secondary: #757575;
|
| 25 |
+
--disabled-color: #bdbdbd;
|
| 26 |
+
--elevation-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
| 27 |
+
--border-radius: 4px;
|
| 28 |
+
--font-family: 'Vazirmatn', 'Inter', 'Segoe UI', Arial, sans-serif;
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
+
|
| 31 |
body {
|
| 32 |
+
font-family: var(--font-family);
|
| 33 |
+
background-color: var(--background-color);
|
| 34 |
+
color: var(--text-primary);
|
| 35 |
+
margin: 0;
|
| 36 |
+
padding: 0;
|
| 37 |
display: flex;
|
| 38 |
flex-direction: column;
|
| 39 |
align-items: center;
|
| 40 |
+
justify-content: center;
|
| 41 |
+
min-height: 100vh;
|
| 42 |
+
direction: rtl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
+
|
| 45 |
.container {
|
| 46 |
+
width: 90%;
|
| 47 |
+
max-width: 800px;
|
| 48 |
+
padding: 24px;
|
| 49 |
+
box-sizing: border-box;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
.header {
|
| 53 |
display: flex;
|
| 54 |
+
align-items: center;
|
| 55 |
+
justify-content: space-between;
|
| 56 |
+
margin-bottom: 24px;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
.header h1 {
|
| 60 |
+
font-size: 1.5rem;
|
| 61 |
+
font-weight: 500;
|
| 62 |
+
color: var(--primary-color);
|
| 63 |
+
margin: 0;
|
| 64 |
}
|
| 65 |
+
|
| 66 |
+
.video-wrapper {
|
| 67 |
position: relative;
|
| 68 |
width: 100%;
|
| 69 |
+
aspect-ratio: 4 / 3;
|
| 70 |
+
border-radius: var(--border-radius);
|
|
|
|
| 71 |
overflow: hidden;
|
| 72 |
+
box-shadow: var(--elevation-shadow);
|
| 73 |
+
margin-bottom: 24px;
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
+
|
| 76 |
#videoFeed {
|
| 77 |
width: 100%;
|
| 78 |
height: 100%;
|
| 79 |
object-fit: cover;
|
|
|
|
| 80 |
display: block;
|
| 81 |
}
|
| 82 |
+
|
| 83 |
#loadingOverlay {
|
| 84 |
position: absolute;
|
| 85 |
+
top: 0;
|
| 86 |
+
left: 0;
|
| 87 |
+
width: 100%;
|
| 88 |
+
height: 100%;
|
| 89 |
+
background-color: rgba(0, 0, 0, 0.5);
|
| 90 |
+
color: white;
|
| 91 |
display: none;
|
| 92 |
justify-content: center;
|
| 93 |
align-items: center;
|
| 94 |
+
font-size: 1.2rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
+
|
| 97 |
+
.input-group {
|
| 98 |
+
margin-bottom: 16px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
+
|
| 101 |
+
.input-group label {
|
| 102 |
+
display: block;
|
| 103 |
+
margin-bottom: 8px;
|
| 104 |
+
color: var(--text-secondary);
|
|
|
|
| 105 |
}
|
| 106 |
+
|
| 107 |
+
.input-group textarea {
|
| 108 |
width: 100%;
|
| 109 |
+
padding: 12px;
|
| 110 |
+
border: 1px solid var(--disabled-color);
|
| 111 |
+
border-radius: var(--border-radius);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
box-sizing: border-box;
|
| 113 |
+
font-family: inherit;
|
| 114 |
+
font-size: 1rem;
|
| 115 |
+
resize: none;
|
| 116 |
text-align: right;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
}
|
| 118 |
+
|
| 119 |
.controls {
|
| 120 |
display: flex;
|
| 121 |
+
flex-direction: column;
|
| 122 |
+
gap: 16px;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
.select-wrapper {
|
| 126 |
+
position: relative;
|
| 127 |
+
display: flex;
|
| 128 |
align-items: center;
|
| 129 |
+
border: 1px solid var(--disabled-color);
|
| 130 |
+
border-radius: var(--border-radius);
|
| 131 |
+
overflow: hidden;
|
| 132 |
+
background-color: var(--surface-color);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
+
|
| 135 |
+
.select-wrapper select {
|
| 136 |
+
padding: 12px 40px 12px 12px;
|
| 137 |
+
border: none;
|
| 138 |
+
background-color: transparent;
|
| 139 |
+
font-family: inherit;
|
| 140 |
font-size: 1rem;
|
| 141 |
+
color: var(--text-primary);
|
| 142 |
+
appearance: none;
|
| 143 |
text-align: right;
|
| 144 |
+
flex: 1;
|
|
|
|
| 145 |
}
|
| 146 |
+
|
| 147 |
+
.select-wrapper .material-icons {
|
| 148 |
+
position: absolute;
|
| 149 |
+
left: 12px;
|
| 150 |
+
color: var(--text-secondary);
|
| 151 |
+
pointer-events: none;
|
| 152 |
}
|
| 153 |
+
|
| 154 |
+
.button {
|
| 155 |
+
padding: 12px 24px;
|
|
|
|
|
|
|
| 156 |
border: none;
|
| 157 |
+
border-radius: var(--border-radius);
|
| 158 |
+
font-family: inherit;
|
| 159 |
+
font-size: 1rem;
|
| 160 |
+
font-weight: 500;
|
| 161 |
+
text-transform: uppercase;
|
| 162 |
+
cursor: pointer;
|
| 163 |
+
box-shadow: var(--elevation-shadow);
|
| 164 |
+
transition: background-color 0.3s;
|
| 165 |
}
|
| 166 |
+
|
| 167 |
+
.button.primary {
|
| 168 |
+
background-color: var(--primary-color);
|
| 169 |
+
color: white;
|
| 170 |
}
|
| 171 |
+
|
| 172 |
+
.button.primary:hover {
|
| 173 |
+
background-color: var(--primary-variant);
|
| 174 |
}
|
| 175 |
+
|
| 176 |
.hidden {
|
| 177 |
display: none;
|
| 178 |
}
|
| 179 |
+
|
| 180 |
+
/* Utility Classes */
|
| 181 |
+
.mt-2 {
|
| 182 |
+
margin-top: 16px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
}
|
| 184 |
+
|
| 185 |
+
.mb-2 {
|
| 186 |
+
margin-bottom: 16px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
}
|
| 188 |
</style>
|
| 189 |
</head>
|
| 190 |
<body>
|
| 191 |
<div class="container">
|
| 192 |
+
<header class="header">
|
| 193 |
+
<h1>مدل زبانی-بصری فارسی</h1>
|
| 194 |
+
</header>
|
| 195 |
+
|
| 196 |
+
<div class="video-wrapper">
|
| 197 |
<video id="videoFeed" autoplay playsinline></video>
|
| 198 |
<div id="loadingOverlay">در حال بارگذاری...</div>
|
| 199 |
</div>
|
| 200 |
<canvas id="canvas" class="hidden"></canvas>
|
| 201 |
+
|
| 202 |
+
<div class="input-group">
|
| 203 |
+
<label for="responseText">پاسخ:</label>
|
| 204 |
+
<textarea
|
| 205 |
+
id="responseText"
|
| 206 |
+
rows="4"
|
| 207 |
+
readonly
|
| 208 |
+
placeholder="پاسخ سرور اینجا نمایش داده میشود..."
|
| 209 |
+
></textarea>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
</div>
|
| 211 |
+
|
| 212 |
<div class="controls">
|
| 213 |
+
<div class="select-wrapper mb-2">
|
| 214 |
+
<select id="intervalSelect">
|
| 215 |
+
<option value="0">۰ میلیثانیه</option>
|
| 216 |
+
<option value="100">۱۰۰ میلیثانیه</option>
|
| 217 |
+
<option value="250">۲۵۰ میلیثانیه</option>
|
| 218 |
+
<option value="500">۵۰۰ میلیثانیه</option>
|
| 219 |
+
<option value="1000">۱ ثانیه</option>
|
| 220 |
+
<option value="2000">۲ ثانیه</option>
|
| 221 |
+
</select>
|
| 222 |
+
<i class="material-icons">arrow_drop_down</i>
|
| 223 |
+
</div>
|
| 224 |
+
<button id="startButton" class="button primary">شروع</button>
|
| 225 |
</div>
|
| 226 |
</div>
|
| 227 |
+
|
| 228 |
<script type="module">
|
| 229 |
+
import {
|
| 230 |
+
AutoProcessor,
|
| 231 |
+
AutoModelForVision2Seq,
|
| 232 |
+
RawImage,
|
| 233 |
+
} from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers/dist/transformers.min.js';
|
| 234 |
|
| 235 |
+
import OpenAI from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
|
| 236 |
|
| 237 |
+
const baseURL = 'https://api.avalai.ir/v1';
|
| 238 |
|
| 239 |
+
const openai = new OpenAI({
|
| 240 |
+
apiKey: 'aa-H6NlUS0RP0RWYcNgh0eAIhsl0tBxJ1vgw4xG9M3HdFhXIS3h',
|
| 241 |
+
baseURL: baseURL,
|
| 242 |
+
dangerouslyAllowBrowser: true,
|
| 243 |
+
});
|
| 244 |
|
| 245 |
+
const video = document.getElementById('videoFeed');
|
| 246 |
+
const canvas = document.getElementById('canvas');
|
| 247 |
+
const responseText = document.getElementById('responseText');
|
| 248 |
+
const intervalSelect = document.getElementById('intervalSelect');
|
| 249 |
+
const startButton = document.getElementById('startButton');
|
| 250 |
+
const loadingOverlay = document.getElementById('loadingOverlay');
|
|
|
|
| 251 |
|
| 252 |
+
const CONTEXT = `
|
|
|
|
| 253 |
Translate the text into persian and only return the translated text without any other text.
|
| 254 |
+
`;
|
| 255 |
+
|
| 256 |
+
let stream;
|
| 257 |
+
let isProcessing = false;
|
| 258 |
+
let processor, model;
|
| 259 |
+
async function initModel() {
|
| 260 |
+
const modelId = 'HuggingFaceTB/SmolVLM-500M-Instruct'; // or "HuggingFaceTB/SmolVLM-Instruct";
|
| 261 |
+
loadingOverlay.style.display = 'flex';
|
| 262 |
+
responseText.value = 'Loading processor...';
|
| 263 |
+
processor = await AutoProcessor.from_pretrained(modelId);
|
| 264 |
+
responseText.value = 'Processor loaded. Loading model...';
|
| 265 |
+
model = await AutoModelForVision2Seq.from_pretrained(modelId, {
|
| 266 |
+
dtype: {
|
| 267 |
+
embed_tokens: 'fp16',
|
| 268 |
+
vision_encoder: 'q4',
|
| 269 |
+
decoder_model_merged: 'q4',
|
| 270 |
+
},
|
| 271 |
+
device: 'webgpu',
|
| 272 |
+
});
|
| 273 |
+
responseText.value = 'Model loaded. Initializing camera...';
|
| 274 |
+
loadingOverlay.style.display = 'none';
|
| 275 |
+
}
|
| 276 |
+
async function initCamera() {
|
| 277 |
+
try {
|
| 278 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
| 279 |
+
video: true,
|
| 280 |
+
audio: false,
|
| 281 |
+
});
|
| 282 |
+
video.srcObject = stream;
|
| 283 |
+
responseText.value = 'Camera access granted. Ready to start.';
|
| 284 |
+
} catch (err) {
|
| 285 |
+
console.error('Error accessing camera:', err);
|
| 286 |
+
responseText.value = `Error accessing camera: ${err.name} - ${err.message}. Please ensure permissions are granted and you are on HTTPS or localhost.`;
|
| 287 |
+
alert(
|
| 288 |
+
`Error accessing camera: ${err.name}. Make sure you've granted permission and are on HTTPS or localhost.`
|
| 289 |
+
);
|
|
|
|
| 290 |
}
|
| 291 |
+
}
|
| 292 |
+
function captureImage() {
|
| 293 |
+
if (!stream || !video.videoWidth) {
|
| 294 |
+
console.warn('Video stream not ready for capture.');
|
| 295 |
+
return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
}
|
| 297 |
+
canvas.width = video.videoWidth;
|
| 298 |
+
canvas.height = video.videoHeight;
|
| 299 |
+
const context = canvas.getContext('2d', {
|
| 300 |
+
willReadFrequently: true,
|
| 301 |
+
});
|
| 302 |
+
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 303 |
+
const frame = context.getImageData(
|
| 304 |
+
0,
|
| 305 |
+
0,
|
| 306 |
+
canvas.width,
|
| 307 |
+
canvas.height
|
| 308 |
+
);
|
| 309 |
+
return new RawImage(frame.data, frame.width, frame.height, 4);
|
| 310 |
+
}
|
| 311 |
+
async function runLocalVisionInference(imgElement, instruction) {
|
| 312 |
+
const messages = [
|
| 313 |
+
{
|
| 314 |
+
role: 'user',
|
| 315 |
+
content: [{ type: 'image' }, { type: 'text', text: instruction }],
|
| 316 |
+
},
|
| 317 |
+
];
|
| 318 |
+
|
| 319 |
+
const text = processor.apply_chat_template(messages, {
|
| 320 |
+
add_generation_prompt: true,
|
| 321 |
+
});
|
| 322 |
+
|
| 323 |
+
const inputs = await processor(text, [imgElement], {
|
| 324 |
+
do_image_splitting: false,
|
| 325 |
+
});
|
| 326 |
+
|
| 327 |
+
const generatedIds = await model.generate({
|
| 328 |
+
...inputs,
|
| 329 |
+
max_new_tokens: 100,
|
| 330 |
+
});
|
| 331 |
+
|
| 332 |
+
const output = processor.batch_decode(
|
| 333 |
+
generatedIds.slice(null, [inputs.input_ids.dims.at(-1), null]),
|
| 334 |
+
{ skip_special_tokens: true }
|
| 335 |
+
);
|
| 336 |
+
return output[0].trim();
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
async function callExternalLLmAPI(text) {
|
| 340 |
+
let response = await fetch(
|
| 341 |
+
'https://openrouter.ai/api/v1/chat/completions',
|
| 342 |
+
{
|
| 343 |
+
method: 'POST',
|
| 344 |
+
headers: {
|
| 345 |
+
Authorization:
|
| 346 |
+
'Bearer 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',
|
| 347 |
+
'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openrouter.ai.
|
| 348 |
+
'X-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openrouter.ai.
|
| 349 |
+
'Content-Type': 'application/json',
|
| 350 |
+
},
|
| 351 |
+
body: JSON.stringify({
|
| 352 |
+
model: 'qwen/qwen-2.5-72b-instruct:free',
|
| 353 |
+
messages: [
|
| 354 |
{
|
| 355 |
+
role: 'system',
|
| 356 |
+
content: CONTEXT,
|
| 357 |
},
|
| 358 |
+
{
|
| 359 |
+
role: 'user',
|
| 360 |
+
content: text,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
},
|
| 362 |
+
],
|
| 363 |
+
}),
|
| 364 |
+
}
|
| 365 |
+
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
+
if (!response.ok) {
|
| 368 |
+
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
}
|
| 370 |
|
| 371 |
+
const data = await response.json();
|
| 372 |
+
const generatedText = data.choices[0].message.content;
|
| 373 |
+
return generatedText;
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
async function callExternalLLmAPI2(text) {
|
| 377 |
+
const response = await openai.chat.completions.create({
|
| 378 |
+
messages: [
|
| 379 |
+
{ role: 'system', content: CONTEXT },
|
| 380 |
+
{ role: 'user', content: text },
|
| 381 |
+
],
|
| 382 |
+
model: 'gpt-4o',
|
| 383 |
+
});
|
| 384 |
+
|
| 385 |
+
let generatedText = response.choices[0].message.content;
|
| 386 |
+
generatedText = generatedText.trim();
|
| 387 |
+
return generatedText;
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
async function sendData() {
|
| 391 |
+
if (!isProcessing) return;
|
| 392 |
+
const instruction = 'What do you see?';
|
| 393 |
+
const rawImg = captureImage();
|
| 394 |
+
if (!rawImg) {
|
| 395 |
+
responseText.value = 'Capture failed';
|
| 396 |
+
return;
|
| 397 |
}
|
| 398 |
+
try {
|
| 399 |
+
const reply = await runLocalVisionInference(rawImg, instruction);
|
| 400 |
+
const translatedReply = await callExternalLLmAPI2(reply);
|
| 401 |
+
responseText.value = translatedReply;
|
| 402 |
+
} catch (e) {
|
| 403 |
+
console.error(e);
|
| 404 |
+
responseText.value = `Error: ${e.message}`;
|
| 405 |
}
|
| 406 |
+
}
|
| 407 |
+
function sleep(ms) {
|
| 408 |
+
return new Promise(resolve => setTimeout(resolve, ms));
|
| 409 |
+
}
|
| 410 |
+
async function processingLoop() {
|
| 411 |
+
const intervalMs = parseInt(intervalSelect.value, 10);
|
| 412 |
+
while (isProcessing) {
|
| 413 |
+
await sendData();
|
| 414 |
+
if (!isProcessing) break;
|
| 415 |
+
await sleep(intervalMs);
|
| 416 |
}
|
| 417 |
+
}
|
| 418 |
+
function handleStart() {
|
| 419 |
+
if (!stream) {
|
| 420 |
+
responseText.value = 'Camera not available. Cannot start.';
|
| 421 |
+
alert('Camera not available. Please grant permission first.');
|
| 422 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
}
|
| 424 |
+
isProcessing = true;
|
| 425 |
+
startButton.textContent = 'توقف';
|
| 426 |
+
startButton.classList.add('running');
|
| 427 |
+
startButton.classList.remove('primary');
|
| 428 |
+
intervalSelect.disabled = true;
|
| 429 |
+
responseText.value = 'Processing started...';
|
| 430 |
+
processingLoop();
|
| 431 |
+
}
|
| 432 |
+
function handleStop() {
|
| 433 |
+
isProcessing = false;
|
| 434 |
+
startButton.textContent = 'شروع';
|
| 435 |
+
startButton.classList.remove('running');
|
| 436 |
+
startButton.classList.add('primary');
|
| 437 |
+
intervalSelect.disabled = false;
|
| 438 |
+
if (responseText.value.startsWith('Processing started...')) {
|
| 439 |
+
responseText.value = 'Processing stopped.';
|
| 440 |
}
|
| 441 |
+
}
|
| 442 |
+
startButton.addEventListener('click', () => {
|
| 443 |
+
if (isProcessing) {
|
| 444 |
+
handleStop();
|
| 445 |
+
} else {
|
| 446 |
+
handleStart();
|
| 447 |
+
}
|
| 448 |
+
});
|
| 449 |
+
window.addEventListener('DOMContentLoaded', async () => {
|
| 450 |
+
if (!navigator.gpu) {
|
| 451 |
+
const videoElement = document.getElementById('videoFeed');
|
| 452 |
+
const warningElement = document.createElement('p');
|
| 453 |
+
warningElement.textContent = 'WebGPU is not available in this browser.';
|
| 454 |
+
warningElement.style.color = 'red';
|
| 455 |
+
warningElement.style.textAlign = 'center';
|
| 456 |
+
videoElement.parentNode.insertBefore(
|
| 457 |
+
warningElement,
|
| 458 |
+
videoElement.nextSibling
|
| 459 |
+
);
|
| 460 |
+
}
|
| 461 |
+
await initModel();
|
| 462 |
+
await initCamera();
|
| 463 |
+
responseText.placeholder = 'پاسخ سرور اینجا نمایش داده میشود...';
|
| 464 |
+
startButton.textContent = isProcessing ? 'توقف' : 'شروع';
|
| 465 |
+
});
|
| 466 |
+
window.addEventListener('beforeunload', () => {
|
| 467 |
+
if (stream) {
|
| 468 |
+
stream.getTracks().forEach(track => track.stop());
|
| 469 |
+
}
|
| 470 |
+
});
|
|
|
|
|
|
|
|
|
|
| 471 |
</script>
|
| 472 |
</body>
|
| 473 |
</html>
|