File size: 15,443 Bytes
5435413 de7bb17 5435413 9c2938b 4164789 9c2938b 4164789 5435413 9c2938b 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 5435413 b195c80 5435413 de7bb17 5435413 de7bb17 5435413 9c2938b 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 5435413 de7bb17 b195c80 de7bb17 b195c80 de7bb17 b195c80 de7bb17 5435413 de7bb17 dd3fe0a b195c80 dd3fe0a de7bb17 dd3fe0a b195c80 de7bb17 b195c80 de7bb17 5435413 de7bb17 9c2938b de7bb17 5435413 de7bb17 5435413 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 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 448 449 450 451 452 453 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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
<script lang="ts">
import type { GradioClient } from '$lib/types';
import {
initializeTrainerScanProgress,
getNextPendingImage,
markImageProcessingCompleted,
markImageProcessingFailed,
getScanningStats
} from '$lib/db/trainerScanning';
import PicletGenerator from '$lib/components/PicletGenerator/PicletGenerator.svelte';
interface Props {
joyCaptionClient: GradioClient;
fluxClient: GradioClient;
commandClient: GradioClient;
// Unused clients (kept for future use)
// hunyuanClient: GradioClient;
// zephyrClient: GradioClient;
// qwenClient: GradioClient;
// dotsClient: GradioClient;
}
let { joyCaptionClient, fluxClient, commandClient }: Props = $props();
// Scanner state
let scanState = $state({
isScanning: false,
currentImage: null as string | null,
currentTrainer: null as string | null,
progress: {
total: 0,
completed: 0,
failed: 0,
pending: 0
},
error: null as string | null
});
// Promise tracking for current image processing
let currentImagePromise: {
resolve: (value: any) => void;
reject: (error: any) => void;
} | null = null;
let showDetails = $state(false);
let isInitializing = $state(false);
let shouldStop = $state(false);
// Reference to PicletGenerator component
let picletGenerator: any;
// Initialize trainer paths on component mount
$effect(() => {
if (joyCaptionClient && fluxClient && commandClient) {
loadInitialState();
}
});
async function loadInitialState() {
try {
isInitializing = true;
// Load trainer image paths and initialize database
const response = await fetch('/trainer_image_paths.txt');
if (!response.ok) {
throw new Error(`Failed to fetch trainer_image_paths.txt: ${response.statusText}`);
}
const content = await response.text();
const imagePaths = content.trim().split('\n')
.map(path => typeof path === 'string' ? path.trim() : '')
.filter(path => path.length > 0);
console.log(`Loaded ${imagePaths.length} trainer image paths`);
await initializeTrainerScanProgress(imagePaths);
await updateProgress();
} catch (error) {
console.error('Failed to initialize scanner:', error);
scanState.error = error instanceof Error ? error.message : 'Failed to initialize';
} finally {
isInitializing = false;
}
}
async function updateProgress() {
const stats = await getScanningStats();
scanState.progress = {
total: stats.total,
completed: stats.completed,
failed: stats.failed,
pending: stats.pending
};
}
async function startScanning() {
if (scanState.isScanning) return;
scanState.isScanning = true;
scanState.error = null;
shouldStop = false;
try {
await processTrainerImages();
} catch (error) {
console.error('Scanning error:', error);
scanState.error = error instanceof Error ? error.message : 'Unknown error';
} finally {
scanState.isScanning = false;
scanState.currentImage = null;
scanState.currentTrainer = null;
}
}
function stopScanning() {
shouldStop = true;
}
async function processTrainerImages() {
while (!shouldStop) {
const nextImage = await getNextPendingImage();
if (!nextImage) {
// No more pending images
break;
}
scanState.currentImage = nextImage.imagePath;
scanState.currentTrainer = nextImage.trainerName;
try {
// Fetch remote image
const imageFile = await fetchRemoteImage(nextImage.remoteUrl, nextImage.imagePath);
// Create a Promise that will be resolved by the completion callback
const imageProcessingPromise = new Promise<void>((resolve, reject) => {
currentImagePromise = { resolve, reject };
});
// Queue the image in PicletGenerator
if (picletGenerator) {
picletGenerator.queueTrainerImage(imageFile, nextImage.imagePath);
}
// Wait for this image to be processed before continuing
console.log(`🔧 DEBUG: Waiting for completion of ${nextImage.imagePath}...`);
await imageProcessingPromise;
console.log(`✅ DEBUG: Completed processing of ${nextImage.imagePath}, moving to next image`);
} catch (error) {
console.error(`Failed to process ${nextImage.imagePath}:`, error);
await markImageProcessingFailed(
nextImage.imagePath,
error instanceof Error ? error.message : 'Unknown error'
);
// Reject the current promise if it exists
if (currentImagePromise) {
currentImagePromise.reject(error);
currentImagePromise = null;
}
}
await updateProgress();
}
}
async function fetchRemoteImage(remoteUrl: string, originalPath: string): Promise<File> {
const response = await fetch(remoteUrl);
if (!response.ok) {
throw new Error(`Failed to fetch ${remoteUrl}: ${response.statusText}`);
}
const blob = await response.blob();
const fileName = originalPath.split('/').pop() || 'trainer_image.jpg';
return new File([blob], fileName, { type: blob.type });
}
async function onTrainerImageCompleted(imagePath: string, picletId: number) {
console.log(`✅ Trainer image completed: ${imagePath} -> Piclet ID: ${picletId}`);
try {
await markImageProcessingCompleted(imagePath, picletId);
} catch (error) {
console.error(`❌ Failed to mark ${imagePath} as completed:`, error);
}
await updateProgress();
// Resolve the current image processing promise
if (currentImagePromise) {
currentImagePromise.resolve(undefined);
currentImagePromise = null;
}
}
async function onTrainerImageFailed(imagePath: string, error: string) {
console.error(`❌ Trainer image failed: ${imagePath} -> ${error}`);
await markImageProcessingFailed(imagePath, error);
await updateProgress();
// Resolve the current image processing promise (failed images should still allow progression)
if (currentImagePromise) {
currentImagePromise.resolve(undefined);
currentImagePromise = null;
}
}
function formatImageName(imagePath: string | null): string {
if (!imagePath) return '';
const parts = imagePath.split('/');
return parts[parts.length - 1] || '';
}
function formatTrainerName(trainerName: string | null): string {
if (!trainerName) return '';
// Convert "001_Willow_Snap" to "Willow Snap"
return trainerName.split('_').slice(1).join(' ');
}
function getProgressPercent(): number {
const { total, completed } = scanState.progress;
return total > 0 ? Math.round((completed / total) * 100) : 0;
}
</script>
<div class="auto-trainer-scanner">
<div class="scanner-header">
<div class="title-section">
<h3>🤖 Auto Trainer Scanner</h3>
<button
class="details-toggle"
onclick={() => showDetails = !showDetails}
>
{showDetails ? '▼' : '▶'} Details
</button>
</div>
{#if scanState.progress.total > 0}
<div class="progress-summary">
<div class="progress-bar">
<div
class="progress-fill"
style="width: {getProgressPercent()}%"
></div>
</div>
<span class="progress-text">
{scanState.progress.completed} / {scanState.progress.total} ({getProgressPercent()}%)
</span>
</div>
{/if}
</div>
{#if showDetails}
<div class="scanner-details">
{#if isInitializing}
<div class="status-message">
<div class="spinner"></div>
<span>Initializing scanner...</span>
</div>
{:else if scanState.isScanning}
<div class="scanning-status">
<div class="current-processing">
<div class="spinner"></div>
<div class="processing-info">
<div class="current-trainer">
Processing: <strong>{formatTrainerName(scanState.currentTrainer)}</strong>
</div>
<div class="current-image">
{formatImageName(scanState.currentImage)}
</div>
</div>
</div>
<button class="stop-button" onclick={stopScanning}>
⏹️ Stop Scanning
</button>
</div>
{:else}
<div class="scanner-controls">
<button
class="start-button"
onclick={startScanning}
disabled={scanState.progress.pending === 0}
>
▶️ Start Auto Scan
</button>
</div>
{/if}
{#if scanState.progress.total > 0}
<div class="progress-details">
<div class="progress-stats">
<div class="stat">
<span class="stat-label">Total:</span>
<span class="stat-value">{scanState.progress.total}</span>
</div>
<div class="stat completed">
<span class="stat-label">Completed:</span>
<span class="stat-value">{scanState.progress.completed}</span>
</div>
<div class="stat pending">
<span class="stat-label">Pending:</span>
<span class="stat-value">{scanState.progress.pending}</span>
</div>
{#if scanState.progress.failed > 0}
<div class="stat failed">
<span class="stat-label">Failed:</span>
<span class="stat-value">{scanState.progress.failed}</span>
</div>
{/if}
</div>
</div>
{/if}
{#if scanState.error}
<div class="error-message">
<strong>Error:</strong> {scanState.error}
</div>
{/if}
<div class="scanner-info">
<p>
This will automatically process trainer images from the HuggingFace dataset,
converting them into unique Piclets. The scanner will resume from where it left off
if interrupted.
</p>
</div>
</div>
{/if}
</div>
<!-- Hidden PicletGenerator for trainer mode processing -->
<div style="display: none;">
<PicletGenerator
bind:this={picletGenerator}
{joyCaptionClient}
{fluxClient}
{commandClient}
isTrainerMode={true}
onTrainerImageCompleted={onTrainerImageCompleted}
onTrainerImageFailed={onTrainerImageFailed}
/>
</div>
<style>
.auto-trainer-scanner {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 12px;
padding: 1rem;
margin-bottom: 1rem;
color: white;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.scanner-header {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.title-section {
display: flex;
align-items: center;
justify-content: space-between;
}
.title-section h3 {
margin: 0;
font-size: 1.1rem;
}
.details-toggle {
background: rgba(255, 255, 255, 0.2);
border: none;
color: white;
padding: 0.3rem 0.6rem;
border-radius: 6px;
cursor: pointer;
font-size: 0.9rem;
transition: background-color 0.2s;
}
.details-toggle:hover {
background: rgba(255, 255, 255, 0.3);
}
.progress-summary {
display: flex;
align-items: center;
gap: 1rem;
}
.progress-bar {
flex: 1;
height: 8px;
background: rgba(255, 255, 255, 0.2);
border-radius: 4px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
transition: width 0.3s ease;
}
.progress-text {
font-size: 0.9rem;
font-weight: 500;
white-space: nowrap;
}
.scanner-details {
margin-top: 1rem;
padding-top: 1rem;
border-top: 1px solid rgba(255, 255, 255, 0.2);
}
.status-message {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.8rem;
background: rgba(255, 255, 255, 0.1);
border-radius: 8px;
margin-bottom: 1rem;
}
.scanning-status {
display: flex;
flex-direction: column;
gap: 1rem;
}
.current-processing {
display: flex;
align-items: center;
gap: 1rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.1);
border-radius: 8px;
}
.processing-info {
flex: 1;
}
.current-trainer {
font-size: 1rem;
margin-bottom: 0.3rem;
}
.current-image {
font-size: 0.9rem;
opacity: 0.8;
}
.scanner-controls {
display: flex;
gap: 0.8rem;
margin-bottom: 1rem;
}
.start-button, .stop-button {
padding: 0.8rem 1.2rem;
border: none;
border-radius: 8px;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
}
.start-button {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
color: white;
}
.start-button:hover:not(:disabled) {
transform: translateY(-1px);
box-shadow: 0 4px 8px rgba(79, 172, 254, 0.3);
}
.start-button:disabled {
background: rgba(255, 255, 255, 0.3);
cursor: not-allowed;
opacity: 0.6;
}
.stop-button {
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
color: white;
}
.stop-button:hover {
transform: translateY(-1px);
box-shadow: 0 4px 8px rgba(255, 107, 107, 0.3);
}
.progress-details {
margin-bottom: 1rem;
}
.progress-stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
gap: 0.8rem;
}
.stat {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.6rem;
background: rgba(255, 255, 255, 0.1);
border-radius: 6px;
border-left: 3px solid rgba(255, 255, 255, 0.5);
}
.stat.completed {
border-left-color: #4caf50;
}
.stat.pending {
border-left-color: #ff9800;
}
.stat.failed {
border-left-color: #f44336;
}
.stat-label {
font-size: 0.9rem;
opacity: 0.9;
}
.stat-value {
font-weight: 600;
font-size: 1rem;
}
.error-message {
background: rgba(244, 67, 54, 0.2);
border: 1px solid rgba(244, 67, 54, 0.4);
border-radius: 8px;
padding: 0.8rem;
margin-bottom: 1rem;
font-size: 0.9rem;
}
.scanner-info {
background: rgba(255, 255, 255, 0.1);
border-radius: 8px;
padding: 0.8rem;
font-size: 0.9rem;
line-height: 1.4;
}
.scanner-info p {
margin: 0;
opacity: 0.9;
}
.spinner {
width: 20px;
height: 20px;
border: 2px solid rgba(255, 255, 255, 0.3);
border-top: 2px solid white;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (max-width: 768px) {
.progress-summary {
flex-direction: column;
align-items: stretch;
gap: 0.5rem;
}
.current-processing {
flex-direction: column;
align-items: flex-start;
text-align: left;
}
.scanner-controls {
flex-direction: column;
}
.progress-stats {
grid-template-columns: 1fr;
}
}
</style> |