Spaces:
Running
Running
Upload 18 files
Browse files- App.tsx +994 -0
- Dockerfile.txt +22 -0
- README.md +122 -5
- components/FileUploader.tsx +81 -0
- components/Icons.tsx +212 -0
- components/MediaItem.tsx +233 -0
- constants/defaultWorkflow.ts +155 -0
- index.html +28 -0
- index.tsx +16 -0
- metadata.json +7 -0
- package.json +24 -0
- services/comfyService.ts +169 -0
- services/geminiService.ts +165 -0
- services/grokService.ts +242 -0
- services/qwenService.ts +212 -0
- tsconfig.json +21 -0
- types.ts +24 -0
- vite.config.ts +87 -0
App.tsx
ADDED
|
@@ -0,0 +1,994 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import React, { useState, useCallback, useMemo, useEffect, useRef } from 'react';
|
| 3 |
+
import type { MediaFile } from './types';
|
| 4 |
+
import { GenerationStatus } from './types';
|
| 5 |
+
import FileUploader from './components/FileUploader';
|
| 6 |
+
import MediaItem from './components/MediaItem';
|
| 7 |
+
import { generateCaption, refineCaption, checkCaptionQuality } from './services/geminiService';
|
| 8 |
+
import { generateCaptionQwen, refineCaptionQwen, checkQualityQwen } from './services/qwenService';
|
| 9 |
+
import { generateCaptionGrok, refineCaptionGrok, checkQualityGrok } from './services/grokService';
|
| 10 |
+
import { sendComfyPrompt } from './services/comfyService';
|
| 11 |
+
import { DownloadIcon, SparklesIcon, WandIcon, LoaderIcon, CopyIcon, UploadCloudIcon, XIcon, CheckCircleIcon, AlertTriangleIcon, StopIcon, TrashIcon } from './components/Icons';
|
| 12 |
+
import { DEFAULT_COMFY_WORKFLOW } from './constants/defaultWorkflow';
|
| 13 |
+
|
| 14 |
+
declare const process: {
|
| 15 |
+
env: { API_KEY?: string; [key: string]: string | undefined; }
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
declare global {
|
| 19 |
+
interface AIStudio {
|
| 20 |
+
hasSelectedApiKey: () => Promise<boolean>;
|
| 21 |
+
openSelectKey: () => Promise<void>;
|
| 22 |
+
}
|
| 23 |
+
interface Window { JSZip: any; aistudio?: AIStudio; }
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
type ApiProvider = 'gemini' | 'qwen' | 'grok';
|
| 27 |
+
type OSType = 'windows' | 'linux';
|
| 28 |
+
|
| 29 |
+
const GEMINI_MODELS = [
|
| 30 |
+
{ id: 'gemini-3-pro-preview', name: 'Gemini 3 Pro (High Quality)' },
|
| 31 |
+
{ id: 'gemini-3-flash-preview', name: 'Gemini 3 Flash (Fast)' },
|
| 32 |
+
{ id: 'gemini-2.5-pro-preview-09-2025', name: 'Gemini 2.5 Pro (Multimodal)' },
|
| 33 |
+
{ id: 'gemini-2.5-flash-native-audio-preview-09-2025', name: 'Gemini 2.5 Flash (Multimedia Speed)' }
|
| 34 |
+
];
|
| 35 |
+
|
| 36 |
+
const QWEN_MODELS = [
|
| 37 |
+
{ id: 'thesby/Qwen3-VL-8B-NSFW-Caption-V4.5', name: 'Thesby Qwen 3 VL 8B NSFW Caption V4.5' },
|
| 38 |
+
{ id: 'huihui-ai/Huihui-Qwen3-VL-8B-Instruct-abliterated', name: 'Huihui Qwen 3 VL 8B Abliterated (Uncensored)' },
|
| 39 |
+
{ id: 'Qwen/Qwen3-VL-8B-Instruct-FP8', name: 'Qwen 3 VL 8B FP8' },
|
| 40 |
+
];
|
| 41 |
+
|
| 42 |
+
const GROK_MODELS = [
|
| 43 |
+
{ id: 'grok-2-vision-1212', name: 'Grok 2 Vision (12-12)' },
|
| 44 |
+
{ id: 'grok-vision-beta', name: 'Grok Vision Beta' }
|
| 45 |
+
];
|
| 46 |
+
|
| 47 |
+
const DEFAULT_BULK_INSTRUCTIONS = `Dont use ambiguous language "perhaps" for example. Describe EVERYTHING visible: characters, clothing, actions, background, objects, lighting, and camera angle. Refrain from using generic phrases like "character, male, figure of" and use specific terminology: "woman, girl, boy, man". Do not mention the art style.`;
|
| 48 |
+
const DEFAULT_REFINEMENT_INSTRUCTIONS = `Refine the caption to be more descriptive and cinematic. Ensure all colors and materials are mentioned.`;
|
| 49 |
+
|
| 50 |
+
const App: React.FC = () => {
|
| 51 |
+
// --- STATE ---
|
| 52 |
+
const [mediaFiles, setMediaFiles] = useState<MediaFile[]>([]);
|
| 53 |
+
const [triggerWord, setTriggerWord] = useState<string>('MyStyle');
|
| 54 |
+
const [apiProvider, setApiProvider] = useState<ApiProvider>('gemini');
|
| 55 |
+
const [geminiApiKey, setGeminiApiKey] = useState<string>(process.env.API_KEY || '');
|
| 56 |
+
const [geminiModel, setGeminiModel] = useState<string>(GEMINI_MODELS[0].id);
|
| 57 |
+
const [hasSelectedKey, setHasSelectedKey] = useState<boolean>(false);
|
| 58 |
+
|
| 59 |
+
// xAI Grok Options
|
| 60 |
+
const [grokApiKey, setGrokApiKey] = useState<string>('');
|
| 61 |
+
const [grokModel, setGrokModel] = useState<string>(GROK_MODELS[0].id);
|
| 62 |
+
|
| 63 |
+
// Qwen Options
|
| 64 |
+
const [qwenEndpoint, setQwenEndpoint] = useState<string>('');
|
| 65 |
+
const [useCustomQwenModel, setUseCustomQwenModel] = useState<boolean>(false);
|
| 66 |
+
const [customQwenModelId, setCustomQwenModelId] = useState<string>('');
|
| 67 |
+
const [qwenModel, setQwenModel] = useState<string>(QWEN_MODELS[0].id);
|
| 68 |
+
const [qwenOsType, setQwenOsType] = useState<OSType>(() => navigator.userAgent.includes("Windows") ? 'windows' : 'linux');
|
| 69 |
+
const [qwenInstallDir, setQwenInstallDir] = useState<string>(() => navigator.userAgent.includes("Windows") ? 'C:\\AI\\qwen_local' : '/home/user/ai/qwen_local');
|
| 70 |
+
const [qwenMaxTokens, setQwenMaxTokens] = useState<number>(8192);
|
| 71 |
+
const [qwen8Bit, setQwen8Bit] = useState<boolean>(false);
|
| 72 |
+
const [qwenEager, setQwenEager] = useState<boolean>(false);
|
| 73 |
+
const [qwenVideoFrameCount, setQwenVideoFrameCount] = useState<number>(8);
|
| 74 |
+
|
| 75 |
+
// Offline Local Snapshot Options
|
| 76 |
+
const [useOfflineSnapshot, setUseOfflineSnapshot] = useState<boolean>(false);
|
| 77 |
+
const [snapshotPath, setSnapshotPath] = useState<string>('');
|
| 78 |
+
const [virtualModelName, setVirtualModelName] = useState<string>('thesby/Qwen3-VL-8B-NSFW-Caption-V4.5');
|
| 79 |
+
|
| 80 |
+
// ComfyUI Options
|
| 81 |
+
const [isComfyEnabled, setIsComfyEnabled] = useState<boolean>(false);
|
| 82 |
+
const [comfyUrl, setComfyUrl] = useState<string>('http://localhost:5000');
|
| 83 |
+
const [comfyWorkflow, setComfyWorkflow] = useState<any>(DEFAULT_COMFY_WORKFLOW);
|
| 84 |
+
const [comfyWorkflowName, setComfyWorkflowName] = useState<string>('Default Workflow');
|
| 85 |
+
const [comfySeed, setComfySeed] = useState<number>(-1);
|
| 86 |
+
const [comfySteps, setComfySteps] = useState<number>(4);
|
| 87 |
+
const [activePreviewId, setActivePreviewId] = useState<string | null>(null);
|
| 88 |
+
|
| 89 |
+
// Secure Bridge Options
|
| 90 |
+
const [useSecureBridge, setUseSecureBridge] = useState<boolean>(false);
|
| 91 |
+
const [isFirstTimeBridge, setIsFirstTimeBridge] = useState<boolean>(false);
|
| 92 |
+
const [bridgeOsType, setBridgeOsType] = useState<OSType>(() => navigator.userAgent.includes("Windows") ? 'windows' : 'linux');
|
| 93 |
+
const [bridgeInstallPath, setBridgeInstallPath] = useState<string>(() => navigator.userAgent.includes("Windows") ? 'C:\\AI\\bridge' : '/home/user/ai/bridge');
|
| 94 |
+
|
| 95 |
+
// Queue and Performance
|
| 96 |
+
const [useRequestQueue, setUseRequestQueue] = useState<boolean>(true);
|
| 97 |
+
const [concurrentTasks, setConcurrentTasks] = useState<number>(1);
|
| 98 |
+
const [isQueueRunning, setIsQueueRunning] = useState<boolean>(false);
|
| 99 |
+
|
| 100 |
+
// Dataset / Instructions
|
| 101 |
+
const [bulkGenerationInstructions, setBulkGenerationInstructions] = useState<string>(DEFAULT_BULK_INSTRUCTIONS);
|
| 102 |
+
const [bulkRefinementInstructions, setBulkRefinementInstructions] = useState<string>(DEFAULT_REFINEMENT_INSTRUCTIONS);
|
| 103 |
+
const [autofitTextareas, setAutofitTextareas] = useState<boolean>(false);
|
| 104 |
+
const [showSideBySidePreview, setShowSideBySidePreview] = useState<boolean>(false);
|
| 105 |
+
const [datasetPrefix, setDatasetPrefix] = useState<string>('item');
|
| 106 |
+
const [isCharacterTaggingEnabled, setIsCharacterTaggingEnabled] = useState<boolean>(false);
|
| 107 |
+
const [characterShowName, setCharacterShowName] = useState<string>('');
|
| 108 |
+
const [isExporting, setIsExporting] = useState<boolean>(false);
|
| 109 |
+
|
| 110 |
+
const abortControllerRef = useRef<AbortController>(new AbortController());
|
| 111 |
+
|
| 112 |
+
// --- EFFECTS ---
|
| 113 |
+
useEffect(() => {
|
| 114 |
+
if (window.aistudio) {
|
| 115 |
+
window.aistudio.hasSelectedApiKey().then(setHasSelectedKey);
|
| 116 |
+
}
|
| 117 |
+
const isHttps = window.location.protocol === 'https:';
|
| 118 |
+
if (!qwenEndpoint) {
|
| 119 |
+
setQwenEndpoint(isHttps ? '' : 'http://localhost:8000/v1');
|
| 120 |
+
}
|
| 121 |
+
}, [qwenEndpoint]);
|
| 122 |
+
|
| 123 |
+
// Handle Modal Keyboard Navigation
|
| 124 |
+
useEffect(() => {
|
| 125 |
+
const handleKeyDown = (e: KeyboardEvent) => {
|
| 126 |
+
if (!activePreviewId) return;
|
| 127 |
+
if (e.key === 'ArrowRight') handleNextPreview();
|
| 128 |
+
if (e.key === 'ArrowLeft') handlePrevPreview();
|
| 129 |
+
if (e.key === 'Escape') setActivePreviewId(null);
|
| 130 |
+
};
|
| 131 |
+
window.addEventListener('keydown', handleKeyDown);
|
| 132 |
+
return () => window.removeEventListener('keydown', handleKeyDown);
|
| 133 |
+
}, [activePreviewId, mediaFiles]);
|
| 134 |
+
|
| 135 |
+
// --- MEMOIZED VALUES ---
|
| 136 |
+
const hasValidConfig = useMemo(() => {
|
| 137 |
+
if (apiProvider === 'gemini') return !!geminiApiKey;
|
| 138 |
+
if (apiProvider === 'grok') return !!grokApiKey;
|
| 139 |
+
return qwenEndpoint !== '';
|
| 140 |
+
}, [apiProvider, geminiApiKey, grokApiKey, qwenEndpoint]);
|
| 141 |
+
|
| 142 |
+
const selectedFiles = useMemo(() => {
|
| 143 |
+
return (mediaFiles || []).filter(mf => mf.isSelected);
|
| 144 |
+
}, [mediaFiles]);
|
| 145 |
+
const currentPreviewItem = useMemo(() => (mediaFiles || []).find(m => m.id === activePreviewId), [mediaFiles, activePreviewId]);
|
| 146 |
+
|
| 147 |
+
const qwenEffectiveModel = useMemo(() => {
|
| 148 |
+
if (useOfflineSnapshot) return virtualModelName;
|
| 149 |
+
return useCustomQwenModel ? customQwenModelId : qwenModel;
|
| 150 |
+
}, [useOfflineSnapshot, virtualModelName, useCustomQwenModel, customQwenModelId, qwenModel]);
|
| 151 |
+
|
| 152 |
+
const qwenStartCommand = useMemo(() => {
|
| 153 |
+
const isWin = qwenOsType === 'windows';
|
| 154 |
+
const path = qwenInstallDir.replace(/[\\/]+$/, '');
|
| 155 |
+
|
| 156 |
+
// Model logic for command
|
| 157 |
+
const modelToLoad = useOfflineSnapshot ? snapshotPath : (useCustomQwenModel ? customQwenModelId : qwenModel);
|
| 158 |
+
|
| 159 |
+
const activate = isWin ? `venv\\Scripts\\activate` : `source venv/bin/activate`;
|
| 160 |
+
const python = isWin ? `python` : `python3`;
|
| 161 |
+
const offlineEnv = isWin ? `set HF_HUB_OFFLINE=1` : `export HF_HUB_OFFLINE=1`;
|
| 162 |
+
|
| 163 |
+
let args = `--model "${modelToLoad}" --max-model-len ${qwenMaxTokens}`;
|
| 164 |
+
if (useOfflineSnapshot) {
|
| 165 |
+
args += ` --served-model-name "${virtualModelName}"`;
|
| 166 |
+
}
|
| 167 |
+
if (qwen8Bit) args += ` --load-format bitsandbytes --quantization bitsandbytes`;
|
| 168 |
+
if (qwenEager) args += ` --enforce-eager`;
|
| 169 |
+
|
| 170 |
+
const baseCmd = isWin
|
| 171 |
+
? `cd /d "${path}" && ${useOfflineSnapshot ? `${offlineEnv} && ` : ''}${activate} && ${python} -m vllm.entrypoints.openai.api_server ${args}`
|
| 172 |
+
: `cd "${path}" && ${useOfflineSnapshot ? `${offlineEnv} && ` : ''}${activate} && ${python} -m vllm.entrypoints.openai.api_server ${args}`;
|
| 173 |
+
|
| 174 |
+
return baseCmd;
|
| 175 |
+
}, [qwenOsType, qwenInstallDir, useCustomQwenModel, customQwenModelId, qwenModel, qwenMaxTokens, qwen8Bit, qwenEager, useOfflineSnapshot, snapshotPath, virtualModelName]);
|
| 176 |
+
|
| 177 |
+
const bridgeStartCommand = useMemo(() => {
|
| 178 |
+
const isWindows = bridgeOsType === 'windows';
|
| 179 |
+
const path = bridgeInstallPath.replace(/[\\/]+$/, '');
|
| 180 |
+
const activateCmd = isWindows ? `call venv\\Scripts\\activate` : `source venv/bin/activate`;
|
| 181 |
+
const pipCmd = `pip install flask flask-cors requests`;
|
| 182 |
+
const setupCmd = isWindows
|
| 183 |
+
? `python -m venv venv && ${activateCmd} && ${pipCmd}`
|
| 184 |
+
: `python3 -m venv venv && ${activateCmd} && ${pipCmd}`;
|
| 185 |
+
return isWindows
|
| 186 |
+
? `cd /d "${path}" && ${isFirstTimeBridge ? `${setupCmd} && ` : ''}${activateCmd} && python bridge.py`
|
| 187 |
+
: `cd "${path}" && ${isFirstTimeBridge ? `${setupCmd} && ` : ''}${activateCmd} && python3 bridge.py`;
|
| 188 |
+
}, [bridgeInstallPath, bridgeOsType, isFirstTimeBridge]);
|
| 189 |
+
|
| 190 |
+
const isTunnelRequired = useMemo(() => {
|
| 191 |
+
return window.location.protocol === 'https:' && (qwenEndpoint.includes('localhost') || qwenEndpoint.includes('127.0.0.1'));
|
| 192 |
+
}, [qwenEndpoint]);
|
| 193 |
+
|
| 194 |
+
// --- HANDLERS ---
|
| 195 |
+
const handleSelectApiKey = async () => {
|
| 196 |
+
if (window.aistudio) {
|
| 197 |
+
await window.aistudio.openSelectKey();
|
| 198 |
+
setHasSelectedKey(true);
|
| 199 |
+
}
|
| 200 |
+
};
|
| 201 |
+
|
| 202 |
+
const updateFile = useCallback((id: string, updates: Partial<MediaFile>) => {
|
| 203 |
+
setMediaFiles(prev => (prev || []).map(mf => (mf.id === id ? { ...mf, ...updates } : mf)));
|
| 204 |
+
}, []);
|
| 205 |
+
|
| 206 |
+
const handleFilesAdded = useCallback(async (files: File[]) => {
|
| 207 |
+
const mediaFilesList = files.filter(file => file.type.startsWith('image/') || file.type.startsWith('video/'));
|
| 208 |
+
const textFilesList = files.filter(file => file.name.toLowerCase().endsWith('.txt'));
|
| 209 |
+
|
| 210 |
+
// Create a map of filename (no extension) to the text file object for quick lookup
|
| 211 |
+
const textFilesMap = new Map<string, File>();
|
| 212 |
+
textFilesList.forEach(f => {
|
| 213 |
+
const baseName = f.name.substring(0, f.name.lastIndexOf('.'));
|
| 214 |
+
textFilesMap.set(baseName.toLowerCase(), f);
|
| 215 |
+
});
|
| 216 |
+
|
| 217 |
+
const newMediaFiles = await Promise.all(mediaFilesList.map(async (file) => {
|
| 218 |
+
const baseName = file.name.substring(0, file.name.lastIndexOf('.'));
|
| 219 |
+
let initialCaption = '';
|
| 220 |
+
|
| 221 |
+
const matchedTxtFile = textFilesMap.get(baseName.toLowerCase());
|
| 222 |
+
if (matchedTxtFile) {
|
| 223 |
+
try {
|
| 224 |
+
initialCaption = await matchedTxtFile.text();
|
| 225 |
+
} catch (e) {
|
| 226 |
+
console.error(`Failed to read caption for ${file.name}`, e);
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
return {
|
| 231 |
+
id: `${file.name}-${Math.random()}`,
|
| 232 |
+
file,
|
| 233 |
+
previewUrl: URL.createObjectURL(file),
|
| 234 |
+
caption: initialCaption.trim(),
|
| 235 |
+
status: GenerationStatus.IDLE,
|
| 236 |
+
isSelected: false,
|
| 237 |
+
customInstructions: '',
|
| 238 |
+
comfyStatus: 'idle'
|
| 239 |
+
} as MediaFile;
|
| 240 |
+
}));
|
| 241 |
+
|
| 242 |
+
setMediaFiles(prev => [...(prev || []), ...newMediaFiles]);
|
| 243 |
+
}, []);
|
| 244 |
+
|
| 245 |
+
const handleCheckQuality = useCallback(async (id: string) => {
|
| 246 |
+
const fileToProcess = (mediaFiles || []).find(mf => mf.id === id);
|
| 247 |
+
if (!hasValidConfig || !fileToProcess || !fileToProcess.caption) return;
|
| 248 |
+
|
| 249 |
+
updateFile(id, { status: GenerationStatus.CHECKING, errorMessage: undefined });
|
| 250 |
+
|
| 251 |
+
try {
|
| 252 |
+
let score = 0;
|
| 253 |
+
if (apiProvider === 'gemini') {
|
| 254 |
+
score = await checkCaptionQuality(fileToProcess.file, fileToProcess.caption, abortControllerRef.current.signal, geminiApiKey, geminiModel);
|
| 255 |
+
} else if (apiProvider === 'grok') {
|
| 256 |
+
score = await checkQualityGrok(grokApiKey, grokModel, fileToProcess.file, fileToProcess.caption, qwenVideoFrameCount, abortControllerRef.current.signal);
|
| 257 |
+
} else {
|
| 258 |
+
score = await checkQualityQwen('', qwenEndpoint, qwenEffectiveModel, fileToProcess.file, fileToProcess.caption, qwenVideoFrameCount, abortControllerRef.current.signal);
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
updateFile(id, { qualityScore: score, status: GenerationStatus.SUCCESS });
|
| 262 |
+
} catch (err: any) {
|
| 263 |
+
if (err.name === 'AbortError' || err.message === 'AbortError') {
|
| 264 |
+
updateFile(id, { status: GenerationStatus.IDLE, errorMessage: "Stopped by user" });
|
| 265 |
+
} else {
|
| 266 |
+
updateFile(id, { status: GenerationStatus.ERROR, errorMessage: err.message });
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
}, [mediaFiles, apiProvider, qwenEndpoint, qwenEffectiveModel, qwenVideoFrameCount, grokApiKey, grokModel, hasValidConfig, updateFile, geminiApiKey, geminiModel]);
|
| 270 |
+
|
| 271 |
+
const handleGenerateCaption = useCallback(async (id: string, itemInstructions?: string) => {
|
| 272 |
+
const fileToProcess = (mediaFiles || []).find(mf => mf.id === id);
|
| 273 |
+
if (!hasValidConfig || !fileToProcess) return;
|
| 274 |
+
|
| 275 |
+
updateFile(id, { status: GenerationStatus.GENERATING, errorMessage: undefined, qualityScore: undefined });
|
| 276 |
+
|
| 277 |
+
const combinedInstructions = `${bulkGenerationInstructions}\n\n${itemInstructions || ''}`.trim();
|
| 278 |
+
|
| 279 |
+
try {
|
| 280 |
+
let caption = '';
|
| 281 |
+
if (apiProvider === 'gemini') {
|
| 282 |
+
caption = await generateCaption(fileToProcess.file, triggerWord, combinedInstructions, isCharacterTaggingEnabled, characterShowName, abortControllerRef.current.signal, geminiApiKey, geminiModel);
|
| 283 |
+
} else if (apiProvider === 'grok') {
|
| 284 |
+
caption = await generateCaptionGrok(grokApiKey, grokModel, fileToProcess.file, triggerWord, combinedInstructions, isCharacterTaggingEnabled, characterShowName, qwenVideoFrameCount, abortControllerRef.current.signal);
|
| 285 |
+
} else {
|
| 286 |
+
caption = await generateCaptionQwen('', qwenEndpoint, qwenEffectiveModel, fileToProcess.file, triggerWord, combinedInstructions, isCharacterTaggingEnabled, characterShowName, qwenVideoFrameCount, abortControllerRef.current.signal);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
updateFile(id, { caption, status: GenerationStatus.SUCCESS });
|
| 290 |
+
} catch (err: any) {
|
| 291 |
+
if (err.name === 'AbortError' || err.message === 'AbortError') {
|
| 292 |
+
updateFile(id, { status: GenerationStatus.IDLE, errorMessage: "Stopped by user" });
|
| 293 |
+
} else {
|
| 294 |
+
updateFile(id, { status: GenerationStatus.ERROR, errorMessage: err.message });
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
}, [mediaFiles, triggerWord, apiProvider, qwenEndpoint, qwenEffectiveModel, qwenVideoFrameCount, grokApiKey, grokModel, bulkGenerationInstructions, isCharacterTaggingEnabled, characterShowName, hasValidConfig, updateFile, geminiApiKey, geminiModel]);
|
| 298 |
+
|
| 299 |
+
const handleRefineCaptionItem = useCallback(async (id: string, itemInstructions?: string) => {
|
| 300 |
+
const fileToProcess = (mediaFiles || []).find(mf => mf.id === id);
|
| 301 |
+
if (!hasValidConfig || !fileToProcess || !fileToProcess.caption) return;
|
| 302 |
+
|
| 303 |
+
updateFile(id, { status: GenerationStatus.GENERATING, errorMessage: undefined });
|
| 304 |
+
|
| 305 |
+
const combinedInstructions = `${bulkRefinementInstructions}\n\n${itemInstructions || ''}`.trim();
|
| 306 |
+
|
| 307 |
+
try {
|
| 308 |
+
let caption = '';
|
| 309 |
+
if (apiProvider === 'gemini') {
|
| 310 |
+
caption = await refineCaption(fileToProcess.file, fileToProcess.caption, combinedInstructions, abortControllerRef.current.signal, geminiApiKey, geminiModel);
|
| 311 |
+
} else if (apiProvider === 'grok') {
|
| 312 |
+
caption = await refineCaptionGrok(grokApiKey, grokModel, fileToProcess.file, fileToProcess.caption, combinedInstructions, qwenVideoFrameCount, abortControllerRef.current.signal);
|
| 313 |
+
} else {
|
| 314 |
+
caption = await refineCaptionQwen('', qwenEndpoint, qwenEffectiveModel, fileToProcess.file, fileToProcess.caption, combinedInstructions, qwenVideoFrameCount, abortControllerRef.current.signal);
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
updateFile(id, { caption, status: GenerationStatus.SUCCESS });
|
| 318 |
+
} catch (err: any) {
|
| 319 |
+
if (err.name === 'AbortError' || err.message === 'AbortError') {
|
| 320 |
+
updateFile(id, { status: GenerationStatus.IDLE, errorMessage: "Stopped by user" });
|
| 321 |
+
} else {
|
| 322 |
+
updateFile(id, { status: GenerationStatus.ERROR, errorMessage: err.message });
|
| 323 |
+
}
|
| 324 |
+
}
|
| 325 |
+
}, [mediaFiles, apiProvider, qwenEndpoint, qwenEffectiveModel, qwenVideoFrameCount, grokApiKey, grokModel, bulkRefinementInstructions, hasValidConfig, updateFile, geminiApiKey, geminiModel]);
|
| 326 |
+
|
| 327 |
+
// --- QUEUE CONTROLLER ---
|
| 328 |
+
const runTasksInQueue = async (tasks: (() => Promise<void>)[]) => {
|
| 329 |
+
setIsQueueRunning(true);
|
| 330 |
+
const pool = new Set<Promise<void>>();
|
| 331 |
+
for (const task of tasks) {
|
| 332 |
+
if (abortControllerRef.current.signal.aborted) break;
|
| 333 |
+
const promise = task();
|
| 334 |
+
pool.add(promise);
|
| 335 |
+
promise.finally(() => pool.delete(promise));
|
| 336 |
+
if (pool.size >= concurrentTasks) {
|
| 337 |
+
await Promise.race(pool);
|
| 338 |
+
}
|
| 339 |
+
}
|
| 340 |
+
await Promise.all(pool);
|
| 341 |
+
setIsQueueRunning(false);
|
| 342 |
+
};
|
| 343 |
+
|
| 344 |
+
const handleBulkGenerate = () => {
|
| 345 |
+
const tasks = selectedFiles.map(file => () => handleGenerateCaption(file.id, file.customInstructions));
|
| 346 |
+
if (useRequestQueue) {
|
| 347 |
+
runTasksInQueue(tasks);
|
| 348 |
+
} else {
|
| 349 |
+
tasks.forEach(t => t());
|
| 350 |
+
}
|
| 351 |
+
};
|
| 352 |
+
|
| 353 |
+
const handleBulkRefine = () => {
|
| 354 |
+
const tasks = selectedFiles.map(file => () => handleRefineCaptionItem(file.id, file.customInstructions));
|
| 355 |
+
if (useRequestQueue) {
|
| 356 |
+
runTasksInQueue(tasks);
|
| 357 |
+
} else {
|
| 358 |
+
tasks.forEach(t => t());
|
| 359 |
+
}
|
| 360 |
+
};
|
| 361 |
+
|
| 362 |
+
const handleBulkQualityCheck = () => {
|
| 363 |
+
const tasks = selectedFiles.map(file => () => handleCheckQuality(file.id));
|
| 364 |
+
if (useRequestQueue) {
|
| 365 |
+
runTasksInQueue(tasks);
|
| 366 |
+
} else {
|
| 367 |
+
tasks.forEach(t => t());
|
| 368 |
+
}
|
| 369 |
+
};
|
| 370 |
+
|
| 371 |
+
const handleClearWorkflow = useCallback(() => {
|
| 372 |
+
setComfyWorkflow(DEFAULT_COMFY_WORKFLOW);
|
| 373 |
+
setComfyWorkflowName('Default Workflow');
|
| 374 |
+
}, []);
|
| 375 |
+
|
| 376 |
+
const handleComfyPreview = useCallback(async (id: string) => {
|
| 377 |
+
const item = (mediaFiles || []).find(m => m.id === id);
|
| 378 |
+
if (!item || !comfyWorkflow || !comfyUrl) return;
|
| 379 |
+
|
| 380 |
+
updateFile(id, { comfyStatus: 'generating', comfyErrorMessage: undefined });
|
| 381 |
+
try {
|
| 382 |
+
const previewUrl = await sendComfyPrompt(comfyUrl, comfyWorkflow, item.caption, comfySeed, comfySteps, useSecureBridge, abortControllerRef.current.signal);
|
| 383 |
+
updateFile(id, { comfyPreviewUrl: previewUrl, comfyStatus: 'success' });
|
| 384 |
+
} catch (err: any) {
|
| 385 |
+
if (err.name === 'AbortError' || err.message === 'Aborted') {
|
| 386 |
+
updateFile(id, { comfyStatus: 'idle', comfyErrorMessage: "Stopped" });
|
| 387 |
+
} else {
|
| 388 |
+
updateFile(id, { comfyStatus: 'error', comfyErrorMessage: err.message });
|
| 389 |
+
}
|
| 390 |
+
}
|
| 391 |
+
}, [mediaFiles, comfyWorkflow, comfyUrl, comfySeed, comfySteps, useSecureBridge, updateFile]);
|
| 392 |
+
|
| 393 |
+
const handleBulkPreview = () => {
|
| 394 |
+
selectedFiles.forEach(file => handleComfyPreview(file.id));
|
| 395 |
+
};
|
| 396 |
+
|
| 397 |
+
const handleDeleteSelected = useCallback(() => {
|
| 398 |
+
setMediaFiles(prev => {
|
| 399 |
+
const remaining = (prev || []).filter(mf => !mf.isSelected);
|
| 400 |
+
return remaining || [];
|
| 401 |
+
});
|
| 402 |
+
}, []);
|
| 403 |
+
|
| 404 |
+
const handleStopTasks = () => {
|
| 405 |
+
abortControllerRef.current.abort();
|
| 406 |
+
abortControllerRef.current = new AbortController();
|
| 407 |
+
setIsQueueRunning(false);
|
| 408 |
+
setMediaFiles(prev => (prev || []).map(mf => {
|
| 409 |
+
if (mf.status === GenerationStatus.GENERATING || mf.status === GenerationStatus.CHECKING) {
|
| 410 |
+
return { ...mf, status: GenerationStatus.IDLE, errorMessage: "Stopped by user" };
|
| 411 |
+
}
|
| 412 |
+
if (mf.comfyStatus === 'generating') {
|
| 413 |
+
return { ...mf, comfyStatus: 'idle', comfyErrorMessage: "Stopped" };
|
| 414 |
+
}
|
| 415 |
+
return mf;
|
| 416 |
+
}));
|
| 417 |
+
};
|
| 418 |
+
|
| 419 |
+
const handleExportDataset = useCallback(async () => {
|
| 420 |
+
if (selectedFiles.length === 0) return;
|
| 421 |
+
const JSZip = (window as any).JSZip;
|
| 422 |
+
if (!JSZip) return alert("JSZip not loaded.");
|
| 423 |
+
|
| 424 |
+
setIsExporting(true);
|
| 425 |
+
try {
|
| 426 |
+
const zip = new JSZip();
|
| 427 |
+
const prefix = datasetPrefix.trim() || 'item';
|
| 428 |
+
selectedFiles.forEach((mf, idx) => {
|
| 429 |
+
const fileExt = mf.file.name.split('.').pop() || 'dat';
|
| 430 |
+
const finalName = `${prefix}_${idx + 1}`;
|
| 431 |
+
zip.file(`${finalName}.${fileExt}`, mf.file);
|
| 432 |
+
zip.file(`${finalName}.txt`, mf.caption || "");
|
| 433 |
+
});
|
| 434 |
+
const content = await zip.generateAsync({ type: 'blob' });
|
| 435 |
+
const link = document.createElement('a');
|
| 436 |
+
link.href = URL.createObjectURL(content);
|
| 437 |
+
link.download = `lora_dataset_${new Date().getTime()}.zip`;
|
| 438 |
+
link.click();
|
| 439 |
+
} catch (err: any) {
|
| 440 |
+
alert("Export failed: " + err.message);
|
| 441 |
+
} finally { setIsExporting(false); }
|
| 442 |
+
}, [selectedFiles, datasetPrefix]);
|
| 443 |
+
|
| 444 |
+
const handleNextPreview = useCallback(() => {
|
| 445 |
+
if (!activePreviewId || (mediaFiles || []).length <= 1) return;
|
| 446 |
+
const currentIndex = mediaFiles.findIndex(m => m.id === activePreviewId);
|
| 447 |
+
const nextIndex = (currentIndex + 1) % mediaFiles.length;
|
| 448 |
+
setActivePreviewId(mediaFiles[nextIndex].id);
|
| 449 |
+
}, [activePreviewId, mediaFiles]);
|
| 450 |
+
|
| 451 |
+
const handlePrevPreview = useCallback(() => {
|
| 452 |
+
if (!activePreviewId || (mediaFiles || []).length <= 1) return;
|
| 453 |
+
const currentIndex = mediaFiles.findIndex(m => m.id === activePreviewId);
|
| 454 |
+
const prevIndex = (currentIndex - 1 + mediaFiles.length) % mediaFiles.length;
|
| 455 |
+
setActivePreviewId(mediaFiles[prevIndex].id);
|
| 456 |
+
}, [activePreviewId, mediaFiles]);
|
| 457 |
+
|
| 458 |
+
const downloadQwenSetupScript = () => {
|
| 459 |
+
const isWin = qwenOsType === 'windows';
|
| 460 |
+
const content = isWin
|
| 461 |
+
? `@echo off\nSETLOCAL EnableDelayedExpansion\necho [LoRA Caption Assistant] Starting Local Qwen Setup for Windows...\n\n:: Check for Python\npython --version >nul 2>&1\nif %errorlevel% neq 0 (\n echo [ERROR] Python not found! Please install Python 3.10+ from python.org\n pause\n exit /b\n)\n\necho [1/3] Creating Virtual Environment...\npython -m venv venv\nif %errorlevel% neq 0 (\n echo [ERROR] Failed to create venv.\n pause\n exit /b\n)\n\necho [2/3] Activating Environment and Upgrading Pip...\ncall venv\\Scripts\\activate\npython -m pip install --upgrade pip\n\necho [3/3] Installing vLLM and Dependencies...\necho vLLM natively on Windows is Experimental. Using WSL2 is highly recommended.\necho Attempting installation of bitsandbytes and requirements...\npip install bitsandbytes requests\n:: Note: Users often need specific wheels for vLLM on Windows or WSL2.\necho To run vLLM on Windows, please follow the official guide for WSL2.\necho This script sets up the local Python environment for bridging.\npause`
|
| 462 |
+
: `#!/bin/bash\npython3 -m venv venv\nsource venv/bin/activate\npip install vllm bitsandbytes\necho Setup Complete.`;
|
| 463 |
+
const filename = isWin ? 'setup_qwen.bat' : 'setup_qwen.sh';
|
| 464 |
+
const blob = new Blob([content], { type: 'text/plain' });
|
| 465 |
+
const url = URL.createObjectURL(blob);
|
| 466 |
+
const a = document.createElement('a');
|
| 467 |
+
a.href = url;
|
| 468 |
+
a.download = filename;
|
| 469 |
+
a.click();
|
| 470 |
+
URL.revokeObjectURL(url);
|
| 471 |
+
};
|
| 472 |
+
|
| 473 |
+
const downloadBridgeSetupScript = () => {
|
| 474 |
+
const isWin = bridgeOsType === 'windows';
|
| 475 |
+
const content = isWin
|
| 476 |
+
? `@echo off\nSETLOCAL EnableDelayedExpansion\necho [LoRA Caption Assistant] Starting Secure Bridge Setup for Windows...\n\n:: Check for Python\npython --version >nul 2>&1\nif %errorlevel% neq 0 (\n echo [ERROR] Python not found! Please install Python 3.10+ from python.org\n pause\n exit /b\n)\n\necho [1/3] Creating Virtual Environment...\npython -m venv venv\nif %errorlevel% neq 0 (\n echo [ERROR] Failed to create venv.\n pause\n exit /b\n)\n\necho [2/3] Activating Environment...\ncall venv\\Scripts\\activate\n\necho [3/3] Installing Bridge Dependencies...\npip install flask flask-cors requests\nif %errorlevel% neq 0 (\n echo [ERROR] Installation failed.\n pause\n exit /b\n)\n\necho Bridge Setup Complete. You can now download bridge.py and run it using the command shown in the app.\npause`
|
| 477 |
+
: `#!/bin/bash\npython3 -m venv venv\nsource venv/bin/activate\npip install flask flask-cors requests\necho Bridge Setup Complete.`;
|
| 478 |
+
const filename = isWin ? 'setup_bridge.bat' : 'setup_bridge.sh';
|
| 479 |
+
const blob = new Blob([content], { type: 'text/plain' });
|
| 480 |
+
const url = URL.createObjectURL(blob);
|
| 481 |
+
const a = document.createElement('a');
|
| 482 |
+
a.href = url;
|
| 483 |
+
a.download = filename;
|
| 484 |
+
a.click();
|
| 485 |
+
URL.revokeObjectURL(url);
|
| 486 |
+
};
|
| 487 |
+
|
| 488 |
+
const downloadBridgeScript = () => {
|
| 489 |
+
const code = `import requests\nfrom flask import Flask, request, Response\nfrom flask_cors import CORS\napp = Flask(__name__)\nCORS(app)\nTARGET = "http://127.0.0.1:8188"\n@app.route('/', defaults={'path': ''}, methods=['GET','POST','PUT','DELETE','PATCH','OPTIONS'])\n@app.route('/<path:path>', methods=['GET','POST','PUT','DELETE','PATCH','OPTIONS'])\ndef proxy(path):\n url = f"{TARGET}/{path}"\n headers = {k:v for k,v in request.headers.items() if k.lower() not in ['host', 'origin', 'referer']}\n resp = requests.request(method=request.method, url=url, headers=headers, data=request.get_data(), params=request.args, stream=True)\n return Response(resp.content, resp.status_code, [(n,v) for n,v in resp.headers.items() if n.lower() not in ['content-encoding','content-length','transfer-encoding','connection']])\nif __name__ == '__main__': app.run(port=5000, host='0.0.0.0')`;
|
| 490 |
+
const blob = new Blob([code], { type: 'text/x-python' });
|
| 491 |
+
const url = URL.createObjectURL(blob);
|
| 492 |
+
const a = document.createElement('a');
|
| 493 |
+
a.href = url;
|
| 494 |
+
a.download = 'bridge.py';
|
| 495 |
+
a.click();
|
| 496 |
+
URL.revokeObjectURL(url);
|
| 497 |
+
};
|
| 498 |
+
|
| 499 |
+
// --- RENDER ---
|
| 500 |
+
return (
|
| 501 |
+
<div className="min-h-screen bg-gray-950 text-gray-100 font-sans p-4 sm:p-8">
|
| 502 |
+
{/* PREVIEW MODAL */}
|
| 503 |
+
{activePreviewId && currentPreviewItem && (
|
| 504 |
+
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/95 backdrop-blur-sm animate-fade-in" onClick={() => setActivePreviewId(null)}>
|
| 505 |
+
<div className="bg-gray-900 w-full max-w-6xl rounded-2xl border border-gray-700 overflow-hidden flex flex-col max-h-[95vh] animate-scale-up shadow-2xl relative" onClick={(e) => e.stopPropagation()}>
|
| 506 |
+
<button onClick={handlePrevPreview} className="absolute left-4 top-1/2 -translate-y-1/2 z-10 p-4 bg-gray-800/80 hover:bg-indigo-600 rounded-full text-white shadow-2xl transition-all border border-white/5 active:scale-90">
|
| 507 |
+
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="3" d="M15 19l-7-7 7-7"/></svg>
|
| 508 |
+
</button>
|
| 509 |
+
<button onClick={handleNextPreview} className="absolute right-4 top-1/2 -translate-y-1/2 z-10 p-4 bg-gray-800/80 hover:bg-indigo-600 rounded-full text-white shadow-2xl transition-all border border-white/5 active:scale-90">
|
| 510 |
+
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="3" d="M9 5l7 7-7 7"/></svg>
|
| 511 |
+
</button>
|
| 512 |
+
<div className="px-6 py-4 border-b border-gray-800 flex justify-between items-center bg-gray-850">
|
| 513 |
+
<div className="flex items-center gap-4">
|
| 514 |
+
<SparklesIcon className="w-5 h-5 text-indigo-400" />
|
| 515 |
+
<div className="flex flex-col">
|
| 516 |
+
<h3 className="text-xs font-black uppercase tracking-widest text-gray-400">{(mediaFiles || []).findIndex(m => m.id === activePreviewId) + 1} of {mediaFiles.length}</h3>
|
| 517 |
+
<h3 className="text-[11px] font-bold truncate max-w-md text-gray-500">{currentPreviewItem.file.name}</h3>
|
| 518 |
+
</div>
|
| 519 |
+
</div>
|
| 520 |
+
<div className="flex items-center gap-2">
|
| 521 |
+
<button onClick={handlePrevPreview} className="px-4 py-2 bg-gray-800 hover:bg-gray-700 rounded-xl text-[10px] font-black uppercase transition-all">Prev</button>
|
| 522 |
+
<button onClick={handleNextPreview} className="px-4 py-2 bg-gray-800 hover:bg-gray-700 rounded-xl text-[10px] font-black uppercase transition-all">Next</button>
|
| 523 |
+
<button onClick={() => setActivePreviewId(null)} className="ml-4 p-2 hover:bg-red-600/20 rounded-full transition-colors text-gray-500 hover:text-red-400"><XIcon className="w-5 h-5" /></button>
|
| 524 |
+
</div>
|
| 525 |
+
</div>
|
| 526 |
+
<div className="flex-grow overflow-y-auto p-6 space-y-8 bg-black/40">
|
| 527 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-8 h-[450px]">
|
| 528 |
+
<div className="bg-black rounded-2xl border border-gray-800 flex items-center justify-center overflow-hidden relative shadow-inner">
|
| 529 |
+
{currentPreviewItem.file.type.startsWith('video/') ? <video src={currentPreviewItem.previewUrl} className="max-h-full" controls /> : <img src={currentPreviewItem.previewUrl} className="max-h-full object-contain" />}
|
| 530 |
+
<div className="absolute top-3 left-3 bg-black/70 backdrop-blur-md px-3 py-1 rounded-lg text-[10px] font-black uppercase text-white/80 border border-white/5">Original Data</div>
|
| 531 |
+
</div>
|
| 532 |
+
<div className="bg-black rounded-2xl border border-gray-800 flex items-center justify-center relative overflow-hidden shadow-inner">
|
| 533 |
+
{currentPreviewItem.comfyPreviewUrl ? <img src={currentPreviewItem.comfyPreviewUrl} className="max-h-full object-contain" /> : <div className="text-xs uppercase text-gray-700 tracking-widest font-black">No Preview Rendered</div>}
|
| 534 |
+
{currentPreviewItem.comfyStatus === 'generating' && <div className="absolute inset-0 bg-black/60 flex flex-col items-center justify-center gap-3"><LoaderIcon className="w-10 h-10 animate-spin text-orange-500" /><span className="text-xs font-black uppercase text-orange-400 tracking-widest">Rendering via ComfyUI...</span></div>}
|
| 535 |
+
<div className="absolute top-3 left-3 bg-orange-600/70 backdrop-blur-md px-3 py-1 rounded-lg text-[10px] font-black uppercase text-white/90 border border-white/5">ComfyUI Render</div>
|
| 536 |
+
</div>
|
| 537 |
+
</div>
|
| 538 |
+
<div className="space-y-6">
|
| 539 |
+
<textarea value={currentPreviewItem.caption} onChange={(e) => updateFile(currentPreviewItem.id, { caption: e.target.value })} className="w-full bg-gray-950 border border-gray-700 rounded-2xl p-6 text-sm h-40 outline-none focus:ring-2 focus:ring-indigo-500 transition-all shadow-inner leading-relaxed" />
|
| 540 |
+
<div className="flex gap-4">
|
| 541 |
+
<input type="text" value={currentPreviewItem.customInstructions} onChange={(e) => updateFile(currentPreviewItem.id, { customInstructions: e.target.value })} placeholder="Refine caption instructions..." className="flex-grow bg-gray-800 border border-gray-700 rounded-xl px-5 py-3 text-sm outline-none focus:ring-1 focus:ring-indigo-500 shadow-sm" />
|
| 542 |
+
<button onClick={() => handleGenerateCaption(currentPreviewItem.id, currentPreviewItem.customInstructions)} className="px-8 py-3 bg-green-600 hover:bg-green-500 text-white rounded-xl text-xs font-black uppercase transition-all shadow-xl active:scale-95">Re-Generate</button>
|
| 543 |
+
<button onClick={() => handleRefineCaptionItem(currentPreviewItem.id, currentPreviewItem.customInstructions)} className="px-8 py-3 bg-indigo-600 hover:bg-indigo-500 text-white rounded-xl text-xs font-black uppercase transition-all shadow-xl active:scale-95">Refine</button>
|
| 544 |
+
<button onClick={() => handleCheckQuality(currentPreviewItem.id)} className="px-8 py-3 bg-blue-600 hover:bg-blue-500 text-white rounded-xl text-xs font-black uppercase transition-all shadow-xl active:scale-95">Check Quality</button>
|
| 545 |
+
<button onClick={() => handleComfyPreview(currentPreviewItem.id)} className="px-8 py-3 bg-orange-600 hover:bg-orange-500 text-white rounded-xl text-xs font-black uppercase transition-all shadow-xl active:scale-95">Preview</button>
|
| 546 |
+
</div>
|
| 547 |
+
</div>
|
| 548 |
+
</div>
|
| 549 |
+
</div>
|
| 550 |
+
</div>
|
| 551 |
+
)}
|
| 552 |
+
|
| 553 |
+
<main className="max-w-6xl mx-auto space-y-8 animate-fade-in">
|
| 554 |
+
<section className="bg-gray-900 border border-gray-800 p-8 rounded-3xl shadow-2xl space-y-12">
|
| 555 |
+
<h2 className="text-3xl font-black flex items-center gap-4 uppercase tracking-tighter text-white">1. Global Settings & Actions</h2>
|
| 556 |
+
|
| 557 |
+
<div className="grid grid-cols-1 lg:grid-cols-2 gap-16">
|
| 558 |
+
<div className="space-y-10">
|
| 559 |
+
<div>
|
| 560 |
+
<label className="text-xs font-black text-gray-500 uppercase tracking-widest block mb-4">AI Provider</label>
|
| 561 |
+
<div className="flex p-1.5 bg-black rounded-2xl border border-gray-800 shadow-inner">
|
| 562 |
+
<button onClick={() => setApiProvider('gemini')} className={`flex-1 py-3 text-[10px] font-black uppercase rounded-xl transition-all ${apiProvider === 'gemini' ? 'bg-indigo-600 text-white shadow-lg' : 'text-gray-600 hover:text-gray-400'}`}>Google Gemini</button>
|
| 563 |
+
<button onClick={() => setApiProvider('grok')} className={`flex-1 py-3 text-[10px] font-black uppercase rounded-xl transition-all ${apiProvider === 'grok' ? 'bg-indigo-600 text-white shadow-lg' : 'text-gray-600 hover:text-gray-400'}`}>xAI Grok</button>
|
| 564 |
+
<button onClick={() => setApiProvider('qwen')} className={`flex-1 py-3 text-[10px] font-black uppercase rounded-xl transition-all ${apiProvider === 'qwen' ? 'bg-indigo-600 text-white shadow-lg' : 'text-gray-600 hover:text-gray-400'}`}>Local Qwen</button>
|
| 565 |
+
</div>
|
| 566 |
+
</div>
|
| 567 |
+
|
| 568 |
+
{apiProvider === 'gemini' && (
|
| 569 |
+
<div className="bg-indigo-500/5 border border-indigo-500/20 p-6 rounded-3xl space-y-6 animate-slide-down shadow-xl">
|
| 570 |
+
<div className="space-y-4">
|
| 571 |
+
<div className="flex justify-between items-center">
|
| 572 |
+
<label className="text-[10px] font-black text-indigo-400 uppercase tracking-widest">Gemini Model Version</label>
|
| 573 |
+
</div>
|
| 574 |
+
<select
|
| 575 |
+
value={geminiModel}
|
| 576 |
+
onChange={(e) => setGeminiModel(e.target.value)}
|
| 577 |
+
className="w-full p-3 bg-black border border-indigo-500/30 rounded-xl text-xs font-bold text-gray-300 shadow-inner focus:ring-1 focus:ring-indigo-500 outline-none"
|
| 578 |
+
>
|
| 579 |
+
{GEMINI_MODELS.map(m => <option key={m.id} value={m.id}>{m.name}</option>)}
|
| 580 |
+
</select>
|
| 581 |
+
</div>
|
| 582 |
+
|
| 583 |
+
<div className="space-y-4">
|
| 584 |
+
<div className="flex justify-between items-center">
|
| 585 |
+
<label className="text-[10px] font-black text-indigo-400 uppercase tracking-widest">Gemini API Key</label>
|
| 586 |
+
{geminiApiKey && <span className="flex items-center gap-1.5 text-[9px] font-black uppercase text-green-400 bg-green-400/10 px-2 py-0.5 rounded-full"><CheckCircleIcon className="w-3 h-3"/> Configured</span>}
|
| 587 |
+
</div>
|
| 588 |
+
<div className="relative group">
|
| 589 |
+
<input
|
| 590 |
+
type="password"
|
| 591 |
+
value={geminiApiKey}
|
| 592 |
+
onChange={(e) => setGeminiApiKey(e.target.value)}
|
| 593 |
+
placeholder="Enter your Gemini API key here..."
|
| 594 |
+
className="w-full py-4 px-5 bg-black border border-indigo-500/30 rounded-2xl text-xs font-mono shadow-inner focus:ring-1 focus:ring-indigo-500 outline-none hover:border-indigo-500/60 transition-all"
|
| 595 |
+
/>
|
| 596 |
+
<div className="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-indigo-400/50 group-hover:text-indigo-400 transition-colors">
|
| 597 |
+
<SparklesIcon className="w-5 h-5" />
|
| 598 |
+
</div>
|
| 599 |
+
</div>
|
| 600 |
+
</div>
|
| 601 |
+
<p className="text-[10px] text-gray-500 flex items-center gap-1.5 px-1">
|
| 602 |
+
<AlertTriangleIcon className="w-3 h-3 text-indigo-400" />
|
| 603 |
+
Get an API key from
|
| 604 |
+
<a href="https://aistudio.google.com/app/apikey" target="_blank" rel="noopener noreferrer" className="text-indigo-400 hover:underline font-bold">Google AI Studio</a>
|
| 605 |
+
</p>
|
| 606 |
+
</div>
|
| 607 |
+
)}
|
| 608 |
+
|
| 609 |
+
{apiProvider === 'grok' && (
|
| 610 |
+
<div className="bg-orange-500/5 border border-orange-500/20 p-6 rounded-3xl space-y-6 animate-slide-down shadow-xl">
|
| 611 |
+
<div className="space-y-4">
|
| 612 |
+
<div className="flex justify-between items-center">
|
| 613 |
+
<label className="text-[10px] font-black text-orange-400 uppercase tracking-widest">Grok Model Version</label>
|
| 614 |
+
</div>
|
| 615 |
+
<select
|
| 616 |
+
value={grokModel}
|
| 617 |
+
onChange={(e) => setGrokModel(e.target.value)}
|
| 618 |
+
className="w-full p-3 bg-black border border-orange-500/30 rounded-xl text-xs font-bold text-gray-300 shadow-inner focus:ring-1 focus:ring-orange-500 outline-none"
|
| 619 |
+
>
|
| 620 |
+
{GROK_MODELS.map(m => <option key={m.id} value={m.id}>{m.name}</option>)}
|
| 621 |
+
</select>
|
| 622 |
+
</div>
|
| 623 |
+
|
| 624 |
+
<div className="space-y-4">
|
| 625 |
+
<div className="flex justify-between items-center">
|
| 626 |
+
<label className="text-[10px] font-black text-orange-400 uppercase tracking-widest">xAI API Key</label>
|
| 627 |
+
{grokApiKey && <span className="flex items-center gap-1.5 text-[9px] font-black uppercase text-green-400 bg-green-400/10 px-2 py-0.5 rounded-full"><CheckCircleIcon className="w-3 h-3"/> Configured</span>}
|
| 628 |
+
</div>
|
| 629 |
+
<div className="relative group">
|
| 630 |
+
<input
|
| 631 |
+
type="password"
|
| 632 |
+
value={grokApiKey}
|
| 633 |
+
onChange={(e) => setGrokApiKey(e.target.value)}
|
| 634 |
+
placeholder="Enter your xAI Grok API key here..."
|
| 635 |
+
className="w-full py-4 px-5 bg-black border border-orange-500/30 rounded-2xl text-xs font-mono shadow-inner focus:ring-1 focus:ring-orange-500 outline-none hover:border-orange-500/60 transition-all"
|
| 636 |
+
/>
|
| 637 |
+
<div className="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-orange-400/50 group-hover:text-orange-400 transition-colors">
|
| 638 |
+
<SparklesIcon className="w-5 h-5" />
|
| 639 |
+
</div>
|
| 640 |
+
</div>
|
| 641 |
+
</div>
|
| 642 |
+
<p className="text-[10px] text-gray-500 flex items-center gap-1.5 px-1">
|
| 643 |
+
<AlertTriangleIcon className="w-3 h-3 text-orange-400" />
|
| 644 |
+
Get an API key from
|
| 645 |
+
<a href="https://console.x.ai/" target="_blank" rel="noopener noreferrer" className="text-orange-400 hover:underline font-bold">xAI Console</a>
|
| 646 |
+
</p>
|
| 647 |
+
</div>
|
| 648 |
+
)}
|
| 649 |
+
|
| 650 |
+
{apiProvider === 'qwen' && (
|
| 651 |
+
<div className="bg-gray-950 p-6 rounded-3xl border border-gray-800 space-y-6 animate-slide-down shadow-xl">
|
| 652 |
+
<div className="flex justify-between items-center mb-2">
|
| 653 |
+
<label className="text-[10px] font-black text-indigo-400 uppercase tracking-widest">Local Model Configuration</label>
|
| 654 |
+
<div className="flex items-center gap-4">
|
| 655 |
+
<label className="flex items-center gap-2 cursor-pointer group">
|
| 656 |
+
<input type="checkbox" checked={useOfflineSnapshot} onChange={e => setUseOfflineSnapshot(e.target.checked)} className="h-4 w-4 rounded bg-gray-800 border-gray-700 text-indigo-600" />
|
| 657 |
+
<span className="text-[10px] font-bold text-orange-400 group-hover:text-orange-300">Use Offline Local Snapshot</span>
|
| 658 |
+
</label>
|
| 659 |
+
{!useOfflineSnapshot && (
|
| 660 |
+
<label className="flex items-center gap-2 cursor-pointer group">
|
| 661 |
+
<input type="checkbox" checked={useCustomQwenModel} onChange={e => setUseCustomQwenModel(e.target.checked)} className="h-4 w-4 rounded bg-gray-800 border-gray-700 text-indigo-600" />
|
| 662 |
+
<span className="text-[10px] font-bold text-gray-500 group-hover:text-gray-300">Custom Model ID</span>
|
| 663 |
+
</label>
|
| 664 |
+
)}
|
| 665 |
+
</div>
|
| 666 |
+
</div>
|
| 667 |
+
|
| 668 |
+
{useOfflineSnapshot ? (
|
| 669 |
+
<div className="space-y-4 animate-slide-down">
|
| 670 |
+
<div className="space-y-1">
|
| 671 |
+
<label className="text-[9px] font-black text-gray-700 uppercase">Snapshot Directory Path</label>
|
| 672 |
+
<input type="text" value={snapshotPath} onChange={e => setSnapshotPath(e.target.value)} placeholder="/path/to/hf_cache/.../snapshots/hash..." className="w-full p-2.5 bg-black border border-gray-800 rounded-xl text-xs font-mono shadow-inner" />
|
| 673 |
+
</div>
|
| 674 |
+
<div className="space-y-1">
|
| 675 |
+
<label className="text-[9px] font-black text-gray-700 uppercase">Virtual Model Name (Served Name)</label>
|
| 676 |
+
<input type="text" value={virtualModelName} onChange={e => setVirtualModelName(e.target.value)} placeholder="org/model-id..." className="w-full p-2.5 bg-black border border-gray-800 rounded-xl text-xs font-mono shadow-inner" />
|
| 677 |
+
</div>
|
| 678 |
+
</div>
|
| 679 |
+
) : useCustomQwenModel ? (
|
| 680 |
+
<input type="text" value={customQwenModelId} onChange={e => setCustomQwenModelId(e.target.value)} placeholder="org/model-id..." className="w-full p-3 bg-black border border-gray-800 rounded-xl text-xs font-mono shadow-inner" />
|
| 681 |
+
) : (
|
| 682 |
+
<select value={qwenModel} onChange={e => setQwenModel(e.target.value)} className="w-full p-3 bg-black border border-gray-800 rounded-xl text-xs font-bold text-gray-300 shadow-inner">
|
| 683 |
+
{QWEN_MODELS.map(m => <option key={m.id} value={m.id}>{m.name}</option>)}
|
| 684 |
+
</select>
|
| 685 |
+
)}
|
| 686 |
+
|
| 687 |
+
<div className="pt-4 border-t border-gray-800 space-y-4">
|
| 688 |
+
<div className="flex justify-between items-center">
|
| 689 |
+
<span className="text-[10px] font-black text-gray-600 uppercase">OS Type:</span>
|
| 690 |
+
<div className="flex gap-2">
|
| 691 |
+
<button onClick={() => setQwenOsType('windows')} className={`px-3 py-1 text-[9px] font-black uppercase rounded-lg transition-all ${qwenOsType === 'windows' ? 'bg-indigo-600 text-white' : 'text-gray-600 hover:text-gray-400'}`}>Windows</button>
|
| 692 |
+
<button onClick={() => setQwenOsType('linux')} className={`px-3 py-1 text-[9px] font-black uppercase rounded-lg transition-all ${qwenOsType === 'linux' ? 'bg-indigo-600 text-white' : 'text-gray-600 hover:text-gray-400'}`}>Linux</button>
|
| 693 |
+
</div>
|
| 694 |
+
</div>
|
| 695 |
+
|
| 696 |
+
<div className="grid grid-cols-4 gap-4">
|
| 697 |
+
<div className="col-span-3 space-y-1">
|
| 698 |
+
<label className="text-[9px] font-black text-gray-700 uppercase">Install Path</label>
|
| 699 |
+
<input type="text" value={qwenInstallDir} onChange={e => setQwenInstallDir(e.target.value)} className="w-full p-2.5 bg-black border border-gray-800 rounded-xl text-xs font-mono" />
|
| 700 |
+
</div>
|
| 701 |
+
<div className="space-y-1">
|
| 702 |
+
<label className="text-[9px] font-black text-gray-700 uppercase">Max Tokens</label>
|
| 703 |
+
<input type="number" value={qwenMaxTokens} onChange={e => setQwenMaxTokens(Number(e.target.value))} className="w-full p-2.5 bg-black border border-gray-800 rounded-xl text-xs text-center" />
|
| 704 |
+
</div>
|
| 705 |
+
</div>
|
| 706 |
+
|
| 707 |
+
<div className="flex flex-col gap-2">
|
| 708 |
+
<label className="flex items-center gap-2 cursor-pointer group">
|
| 709 |
+
<input type="checkbox" checked={qwen8Bit} onChange={e => setQwen8Bit(e.target.checked)} className="h-4 w-4 rounded bg-gray-800 text-indigo-600" />
|
| 710 |
+
<span className="text-[10px] font-bold text-gray-500 group-hover:text-gray-300">Enable 8-bit Quantization (bitsandbytes)</span>
|
| 711 |
+
</label>
|
| 712 |
+
<label className="flex items-center gap-2 cursor-pointer group">
|
| 713 |
+
<input type="checkbox" checked={qwenEager} onChange={e => setQwenEager(e.target.checked)} className="h-4 w-4 rounded bg-gray-950 text-indigo-600" />
|
| 714 |
+
<span className="text-[10px] font-bold text-gray-500 group-hover:text-gray-300">Enforce Eager Mode</span>
|
| 715 |
+
</label>
|
| 716 |
+
</div>
|
| 717 |
+
|
| 718 |
+
<button onClick={downloadQwenSetupScript} className="w-full py-3 bg-green-700 hover:bg-green-600 text-white text-[10px] font-black uppercase rounded-xl transition-all shadow-lg">Download {qwenOsType === 'windows' ? 'setup_qwen.bat' : 'setup_qwen.sh'}</button>
|
| 719 |
+
|
| 720 |
+
<div className="space-y-2">
|
| 721 |
+
<label className="text-[9px] font-black text-gray-700 uppercase">Local Start Command:</label>
|
| 722 |
+
<div className="relative group">
|
| 723 |
+
<div className="p-3 bg-black rounded-xl border border-gray-900 font-mono text-[10px] text-green-500/80 break-all leading-relaxed max-h-24 overflow-y-auto shadow-inner">
|
| 724 |
+
{qwenStartCommand}
|
| 725 |
+
</div>
|
| 726 |
+
<button onClick={() => navigator.clipboard.writeText(qwenStartCommand)} className="absolute top-2 right-2 p-1.5 bg-gray-800 hover:bg-gray-700 text-gray-400 rounded-lg opacity-0 group-hover:opacity-100 transition-all"><CopyIcon className="w-3.5 h-3.5"/></button>
|
| 727 |
+
</div>
|
| 728 |
+
</div>
|
| 729 |
+
|
| 730 |
+
<div className="space-y-1">
|
| 731 |
+
<label className="text-[10px] font-black text-indigo-400 uppercase tracking-widest">Endpoint URL (Tunnel or Local)</label>
|
| 732 |
+
<input type="text" value={qwenEndpoint} onChange={e => setQwenEndpoint(e.target.value)} placeholder="http://localhost:8000/v1" className="w-full p-3 bg-black border border-gray-800 rounded-xl text-xs font-mono shadow-inner focus:ring-1 focus:ring-indigo-500 outline-none" />
|
| 733 |
+
</div>
|
| 734 |
+
</div>
|
| 735 |
+
</div>
|
| 736 |
+
)}
|
| 737 |
+
|
| 738 |
+
<div className="space-y-6">
|
| 739 |
+
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
| 740 |
+
<div className="space-y-2">
|
| 741 |
+
<label className="text-[10px] font-black text-gray-500 uppercase tracking-widest">Trigger Word</label>
|
| 742 |
+
<input type="text" value={triggerWord} onChange={e => setTriggerWord(e.target.value)} className="w-full p-3 bg-gray-950 border border-gray-800 rounded-2xl text-sm font-bold shadow-inner" placeholder="MyStyle" />
|
| 743 |
+
</div>
|
| 744 |
+
<div className="space-y-2">
|
| 745 |
+
<label className="text-[10px] font-black text-gray-500 uppercase tracking-widest">File Prefix</label>
|
| 746 |
+
<input type="text" value={datasetPrefix} onChange={e => setDatasetPrefix(e.target.value)} className="w-full p-3 bg-gray-950 border border-gray-800 rounded-2xl text-sm font-bold shadow-inner" placeholder="item" />
|
| 747 |
+
</div>
|
| 748 |
+
</div>
|
| 749 |
+
<div className="bg-gray-800/40 p-5 rounded-3xl border border-gray-800 space-y-4 shadow-xl">
|
| 750 |
+
<label className="flex items-center gap-3 cursor-pointer group">
|
| 751 |
+
<input type="checkbox" checked={isCharacterTaggingEnabled} onChange={(e) => setIsCharacterTaggingEnabled(e.target.checked)} className="h-6 w-6 rounded-lg bg-gray-900 border-gray-700 text-indigo-600 transition-all shadow-sm" />
|
| 752 |
+
<span className="text-xs font-black text-gray-500 uppercase tracking-wider group-hover:text-gray-300 transition-colors">Character Tagging</span>
|
| 753 |
+
</label>
|
| 754 |
+
{isCharacterTaggingEnabled && (
|
| 755 |
+
<div className="animate-slide-down">
|
| 756 |
+
<input type="text" value={characterShowName} onChange={(e) => setCharacterShowName(e.target.value)} placeholder="Enter show/series name..." className="w-full p-3 bg-gray-950 border border-gray-700 rounded-xl text-xs font-medium focus:ring-1 focus:ring-indigo-500 outline-none transition-all shadow-inner" />
|
| 757 |
+
</div>
|
| 758 |
+
)}
|
| 759 |
+
</div>
|
| 760 |
+
</div>
|
| 761 |
+
</div>
|
| 762 |
+
|
| 763 |
+
<div className="space-y-10">
|
| 764 |
+
<div className="space-y-8">
|
| 765 |
+
<div className="space-y-3">
|
| 766 |
+
<label className="text-xs font-black text-gray-500 uppercase tracking-widest block">System Instructions & Prompting</label>
|
| 767 |
+
<textarea value={bulkGenerationInstructions} onChange={(e) => setBulkGenerationInstructions(e.target.value)} className="w-full p-5 bg-gray-950 border border-gray-800 rounded-3xl text-[13px] h-40 leading-relaxed resize-none outline-none focus:ring-2 focus:ring-indigo-500 shadow-inner" placeholder="Enter global captioning rules..." />
|
| 768 |
+
</div>
|
| 769 |
+
<div className="space-y-3">
|
| 770 |
+
<label className="text-xs font-black text-indigo-400 uppercase tracking-widest block">Refinement Instructions</label>
|
| 771 |
+
<textarea value={bulkRefinementInstructions} onChange={(e) => setBulkRefinementInstructions(e.target.value)} className="w-full p-5 bg-gray-950 border border-indigo-500/20 rounded-3xl text-[13px] h-40 leading-relaxed resize-none outline-none focus:ring-2 focus:ring-indigo-500 shadow-inner" placeholder="Enter instructions for refining existing captions..." />
|
| 772 |
+
</div>
|
| 773 |
+
</div>
|
| 774 |
+
|
| 775 |
+
<div className="flex flex-col gap-6 pt-4 border-t border-gray-800">
|
| 776 |
+
<div className="flex flex-wrap gap-x-8 gap-y-4">
|
| 777 |
+
<label className="flex items-center gap-3 cursor-pointer group">
|
| 778 |
+
<input type="checkbox" checked={autofitTextareas} onChange={(e) => setAutofitTextareas(e.target.checked)} className="h-5 w-5 rounded-md bg-gray-900 border-gray-700 text-indigo-500 shadow-inner" />
|
| 779 |
+
<span className="text-xs font-bold text-gray-500 uppercase group-hover:text-gray-300 transition-colors">Autofit Textboxes</span>
|
| 780 |
+
</label>
|
| 781 |
+
<label className="flex items-center gap-3 cursor-pointer group">
|
| 782 |
+
<input type="checkbox" checked={showSideBySidePreview} onChange={(e) => setShowSideBySidePreview(e.target.checked)} className="h-5 w-5 rounded-md bg-gray-900 border-gray-700 text-indigo-500 shadow-inner" />
|
| 783 |
+
<span className="text-xs font-bold text-gray-500 uppercase group-hover:text-gray-300 transition-colors">Side-by-Side Comparison</span>
|
| 784 |
+
</label>
|
| 785 |
+
<label className="flex items-center gap-3 cursor-pointer group">
|
| 786 |
+
<input type="checkbox" checked={isComfyEnabled} onChange={(e) => setIsComfyEnabled(e.target.checked)} className="h-5 w-5 rounded-md bg-gray-900 border-gray-700 text-orange-500 shadow-inner" />
|
| 787 |
+
<span className="text-xs font-black text-orange-500 uppercase tracking-widest group-hover:text-orange-400 transition-colors">Enable ComfyUI Previews</span>
|
| 788 |
+
</label>
|
| 789 |
+
</div>
|
| 790 |
+
|
| 791 |
+
<div className="bg-indigo-600/5 border border-indigo-600/20 p-6 rounded-3xl space-y-4">
|
| 792 |
+
<div className="flex justify-between items-center">
|
| 793 |
+
<label className="flex items-center gap-3 cursor-pointer group">
|
| 794 |
+
<input type="checkbox" checked={useRequestQueue} onChange={(e) => setUseRequestQueue(e.target.checked)} className="h-5 w-5 rounded bg-gray-900 border-gray-700 text-indigo-500" />
|
| 795 |
+
<span className="text-xs font-black text-indigo-400 uppercase tracking-widest group-hover:text-indigo-300 transition-colors">Enable Request Queue</span>
|
| 796 |
+
</label>
|
| 797 |
+
{useRequestQueue && (
|
| 798 |
+
<div className="flex items-center gap-3">
|
| 799 |
+
<label className="text-[10px] font-black text-gray-600 uppercase">Concurrent Tasks</label>
|
| 800 |
+
<input type="number" min="1" max="10" value={concurrentTasks} onChange={(e) => setConcurrentTasks(Number(e.target.value))} className="w-16 p-1 bg-black border border-gray-800 rounded text-center text-xs font-bold" />
|
| 801 |
+
</div>
|
| 802 |
+
)}
|
| 803 |
+
</div>
|
| 804 |
+
<p className="text-[10px] text-gray-600 italic">Recommended for Gemini Free Tier or Local GPU to prevent rate limits or OOM errors.</p>
|
| 805 |
+
</div>
|
| 806 |
+
|
| 807 |
+
{isComfyEnabled && (
|
| 808 |
+
<div className="bg-orange-600/5 border border-orange-600/20 p-6 rounded-3xl space-y-6 animate-slide-down shadow-xl">
|
| 809 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
| 810 |
+
<div className="space-y-2">
|
| 811 |
+
<label className="text-[10px] font-black text-gray-600 uppercase">Endpoint</label>
|
| 812 |
+
<input type="text" value={comfyUrl} onChange={(e) => setComfyUrl(e.target.value)} placeholder="http://127.0.0.1:8188" className="w-full p-3 bg-black border border-gray-800 rounded-xl text-xs font-mono shadow-inner" />
|
| 813 |
+
</div>
|
| 814 |
+
<div className="space-y-2">
|
| 815 |
+
<label className="text-[10px] font-black text-gray-600 uppercase">Workflow ({comfyWorkflowName})</label>
|
| 816 |
+
<div className="flex gap-2">
|
| 817 |
+
<button onClick={() => document.getElementById('wf-up')?.click()} className="flex-1 py-2.5 bg-orange-600 hover:bg-orange-500 text-white rounded-xl shadow-lg transition-all active:scale-95 text-[10px] font-black uppercase tracking-widest">Load JSON</button>
|
| 818 |
+
<button onClick={handleClearWorkflow} className="px-4 bg-gray-800 hover:bg-gray-700 text-gray-400 rounded-xl transition-all active:scale-95"><TrashIcon className="w-4 h-4"/></button>
|
| 819 |
+
<input id="wf-up" type="file" accept=".json" onChange={(e) => {
|
| 820 |
+
const f = e.target.files?.[0];
|
| 821 |
+
if (f) {
|
| 822 |
+
const r = new FileReader();
|
| 823 |
+
r.onload = (ev) => {
|
| 824 |
+
try {
|
| 825 |
+
setComfyWorkflow(JSON.parse(ev.target?.result as string));
|
| 826 |
+
setComfyWorkflowName(f.name);
|
| 827 |
+
} catch { alert("Invalid Workflow JSON"); }
|
| 828 |
+
};
|
| 829 |
+
r.readAsText(f);
|
| 830 |
+
}
|
| 831 |
+
}} className="hidden" />
|
| 832 |
+
</div>
|
| 833 |
+
</div>
|
| 834 |
+
<div className="space-y-2">
|
| 835 |
+
<label className="text-[10px] font-black text-gray-600 uppercase">Default Seed (-1 for random)</label>
|
| 836 |
+
<input type="number" value={comfySeed} onChange={(e) => setComfySeed(Number(e.target.value))} className="w-full p-3 bg-black border border-gray-800 rounded-xl text-xs shadow-inner" />
|
| 837 |
+
</div>
|
| 838 |
+
<div className="space-y-2">
|
| 839 |
+
<label className="text-[10px] font-black text-gray-600 uppercase">Steps</label>
|
| 840 |
+
<input type="number" value={comfySteps} onChange={(e) => setComfySteps(Number(e.target.value))} className="w-full p-3 bg-black border border-gray-800 rounded-xl text-xs shadow-inner" />
|
| 841 |
+
</div>
|
| 842 |
+
</div>
|
| 843 |
+
|
| 844 |
+
{/* Secure Bridge Sub-section */}
|
| 845 |
+
<div className="pt-6 border-t border-orange-600/10 space-y-6">
|
| 846 |
+
<div className="flex justify-between items-center">
|
| 847 |
+
<h3 className="text-[11px] font-black text-orange-400 uppercase tracking-widest">Secure Bridge (for HTTPS/Remote access)</h3>
|
| 848 |
+
<label className="flex items-center gap-3 cursor-pointer group">
|
| 849 |
+
<input type="checkbox" checked={useSecureBridge} onChange={(e) => setUseSecureBridge(e.target.checked)} className="h-5 w-5 rounded bg-gray-900 border-gray-700 text-orange-500" />
|
| 850 |
+
<span className="text-[10px] font-bold text-gray-500 group-hover:text-gray-300 transition-colors">Enable Bridge Proxy</span>
|
| 851 |
+
</label>
|
| 852 |
+
</div>
|
| 853 |
+
|
| 854 |
+
{useSecureBridge && (
|
| 855 |
+
<div className="space-y-6 animate-slide-down">
|
| 856 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
| 857 |
+
<div className="space-y-2">
|
| 858 |
+
<label className="text-[9px] font-black text-gray-600 uppercase">Bridge OS</label>
|
| 859 |
+
<div className="flex gap-2">
|
| 860 |
+
<button onClick={() => setBridgeOsType('windows')} className={`flex-1 py-2 text-[10px] font-black uppercase rounded-lg transition-all ${bridgeOsType === 'windows' ? 'bg-orange-600 text-white' : 'bg-gray-800 text-gray-500'}`}>Windows</button>
|
| 861 |
+
<button onClick={() => setBridgeOsType('linux')} className={`flex-1 py-2 text-[10px] font-black uppercase rounded-lg transition-all ${bridgeOsType === 'linux' ? 'bg-orange-600 text-white' : 'bg-gray-800 text-gray-500'}`}>Linux</button>
|
| 862 |
+
</div>
|
| 863 |
+
</div>
|
| 864 |
+
<div className="space-y-2">
|
| 865 |
+
<label className="text-[9px] font-black text-gray-600 uppercase">Install Path</label>
|
| 866 |
+
<input type="text" value={bridgeInstallPath} onChange={(e) => setBridgeInstallPath(e.target.value)} className="w-full p-3 bg-black border border-gray-800 rounded-xl text-xs font-mono shadow-inner" />
|
| 867 |
+
</div>
|
| 868 |
+
</div>
|
| 869 |
+
|
| 870 |
+
<div className="space-y-4">
|
| 871 |
+
<label className="flex items-center gap-3 cursor-pointer group">
|
| 872 |
+
<input type="checkbox" checked={isFirstTimeBridge} onChange={(e) => setIsFirstTimeBridge(e.target.checked)} className="h-4 w-4 rounded bg-gray-950 border-gray-800 text-orange-500" />
|
| 873 |
+
<span className="text-[10px] font-bold text-gray-500 group-hover:text-gray-300">First-time Setup (Include VENV & Pip Install)</span>
|
| 874 |
+
</label>
|
| 875 |
+
<div className="flex gap-4">
|
| 876 |
+
<button onClick={downloadBridgeSetupScript} className="flex-1 py-3 bg-indigo-700 hover:bg-indigo-600 text-white text-[10px] font-black uppercase rounded-xl transition-all shadow-lg">Download {bridgeOsType === 'windows' ? 'setup_bridge.bat' : 'setup_bridge.sh'}</button>
|
| 877 |
+
<button onClick={downloadBridgeScript} className="flex-1 py-3 bg-orange-700 hover:bg-orange-600 text-white text-[10px] font-black uppercase rounded-xl transition-all shadow-lg">Download Bridge.py</button>
|
| 878 |
+
</div>
|
| 879 |
+
</div>
|
| 880 |
+
|
| 881 |
+
<div className="space-y-2">
|
| 882 |
+
<label className="text-[9px] font-black text-gray-700 uppercase tracking-widest">Start Command:</label>
|
| 883 |
+
<div className="relative group">
|
| 884 |
+
<div className="p-3 bg-black rounded-xl border border-gray-900 font-mono text-[10px] text-green-500/80 break-all leading-relaxed shadow-inner">
|
| 885 |
+
{bridgeStartCommand}
|
| 886 |
+
</div>
|
| 887 |
+
<button onClick={() => navigator.clipboard.writeText(bridgeStartCommand)} className="absolute top-2 right-2 p-1.5 bg-gray-800 hover:bg-gray-700 text-gray-400 rounded-lg opacity-0 group-hover:opacity-100 transition-all"><CopyIcon className="w-3.5 h-3.5"/></button>
|
| 888 |
+
</div>
|
| 889 |
+
<p className="text-[9px] text-gray-600 italic">The bridge will proxy requests from this HTTPS app to your local HTTP ComfyUI server.</p>
|
| 890 |
+
</div>
|
| 891 |
+
</div>
|
| 892 |
+
)}
|
| 893 |
+
</div>
|
| 894 |
+
</div>
|
| 895 |
+
)}
|
| 896 |
+
</div>
|
| 897 |
+
</div>
|
| 898 |
+
</div>
|
| 899 |
+
|
| 900 |
+
<div className="border-t border-gray-800 pt-10 flex flex-col gap-6">
|
| 901 |
+
<div className="flex flex-wrap gap-4 justify-end">
|
| 902 |
+
<button
|
| 903 |
+
onClick={handleDeleteSelected}
|
| 904 |
+
disabled={selectedFiles.length === 0}
|
| 905 |
+
className="px-6 py-4 bg-red-600/20 hover:bg-red-600/30 border border-red-600/30 rounded-2xl text-[11px] font-black uppercase text-red-400 flex items-center gap-3 transition-all active:scale-95 shadow-lg disabled:opacity-20 disabled:grayscale"
|
| 906 |
+
>
|
| 907 |
+
<TrashIcon className="w-5 h-5"/> Delete Selected ({selectedFiles.length})
|
| 908 |
+
</button>
|
| 909 |
+
<button onClick={handleStopTasks} className="px-6 py-4 bg-orange-600/20 hover:bg-orange-600/40 border border-orange-600/30 rounded-2xl text-[11px] font-black uppercase text-orange-400 flex items-center gap-3 transition-all active:scale-95 shadow-lg"><StopIcon className="w-5 h-5"/> Stop Tasks</button>
|
| 910 |
+
|
| 911 |
+
<button onClick={handleBulkQualityCheck} disabled={selectedFiles.length === 0 || !hasValidConfig || isQueueRunning} className="px-6 py-4 bg-blue-600 hover:bg-blue-500 text-white rounded-2xl text-[11px] font-black uppercase flex items-center gap-4 transition-all shadow-xl active:scale-95 disabled:opacity-40">
|
| 912 |
+
<CheckCircleIcon className="w-5 h-5" /> Check Quality Selected ({selectedFiles.length})
|
| 913 |
+
</button>
|
| 914 |
+
|
| 915 |
+
<button onClick={handleBulkGenerate} disabled={selectedFiles.length === 0 || !hasValidConfig || isQueueRunning} className="px-10 py-4 bg-green-600 hover:bg-green-500 text-white rounded-2xl text-xs font-black uppercase flex items-center gap-4 transition-all shadow-2xl shadow-green-900/30 active:scale-95 disabled:opacity-40">
|
| 916 |
+
<SparklesIcon className="w-6 h-6" /> Generate Selected ({selectedFiles.length})
|
| 917 |
+
</button>
|
| 918 |
+
|
| 919 |
+
<button onClick={handleBulkRefine} disabled={selectedFiles.length === 0 || !hasValidConfig || isQueueRunning} className="px-10 py-4 bg-indigo-600 hover:bg-indigo-500 text-white rounded-2xl text-xs font-black uppercase flex items-center gap-4 transition-all shadow-xl active:scale-95 disabled:opacity-40">
|
| 920 |
+
<WandIcon className="w-6 h-6" /> Refine Selected ({selectedFiles.length})
|
| 921 |
+
</button>
|
| 922 |
+
</div>
|
| 923 |
+
<div className="flex flex-wrap gap-4 justify-end">
|
| 924 |
+
{isComfyEnabled && (
|
| 925 |
+
<button onClick={handleBulkPreview} disabled={selectedFiles.length === 0} className="px-10 py-4 bg-orange-600 hover:bg-orange-500 text-white rounded-2xl text-xs font-black uppercase flex items-center gap-4 transition-all shadow-xl shadow-orange-900/20 active:scale-95 disabled:opacity-40">
|
| 926 |
+
<WandIcon className="w-6 h-6" /> Preview Selected ({selectedFiles.length})
|
| 927 |
+
</button>
|
| 928 |
+
)}
|
| 929 |
+
<button onClick={handleExportDataset} disabled={selectedFiles.length === 0 || isExporting} className="w-full sm:w-auto px-16 py-5 bg-indigo-700 hover:bg-indigo-600 text-white rounded-2xl text-xs font-black uppercase flex items-center justify-center gap-4 transition-all shadow-2xl active:scale-95 disabled:opacity-40">
|
| 930 |
+
{isExporting ? <LoaderIcon className="w-6 h-6 animate-spin" /> : <DownloadIcon className="w-6 h-6" />}
|
| 931 |
+
{isExporting ? 'Packaging ZIP...' : 'Download Finished Dataset'}
|
| 932 |
+
</button>
|
| 933 |
+
</div>
|
| 934 |
+
</div>
|
| 935 |
+
</section>
|
| 936 |
+
|
| 937 |
+
<section className="bg-gray-900 border border-gray-800 p-8 rounded-3xl shadow-xl overflow-hidden relative">
|
| 938 |
+
<div className="absolute top-0 right-0 p-8 opacity-10 pointer-events-none"><UploadCloudIcon className="w-32 h-32" /></div>
|
| 939 |
+
<h2 className="text-xl font-black mb-6 uppercase tracking-widest text-gray-400">2. Upload Source Media</h2>
|
| 940 |
+
<FileUploader onFilesAdded={handleFilesAdded} />
|
| 941 |
+
</section>
|
| 942 |
+
|
| 943 |
+
<section className="space-y-8 animate-slide-up min-h-[400px]">
|
| 944 |
+
{mediaFiles && mediaFiles.length > 0 ? (
|
| 945 |
+
<>
|
| 946 |
+
<div className="flex justify-between items-center bg-gray-900/80 backdrop-blur-2xl p-6 rounded-3xl border border-gray-800 sticky top-4 z-40 shadow-[0_20px_50px_-10px_rgba(0,0,0,0.5)]">
|
| 947 |
+
<div className="flex items-center gap-4">
|
| 948 |
+
<div className="h-10 w-1.5 bg-indigo-500 rounded-full shadow-[0_0_15px_rgba(99,102,241,0.5)]"></div>
|
| 949 |
+
<div className="flex flex-col">
|
| 950 |
+
<h2 className="text-2xl font-black text-white uppercase tracking-tighter leading-none">3. Data Curation Workspace</h2>
|
| 951 |
+
<p className="text-[10px] font-black text-gray-600 uppercase tracking-widest mt-1">Ready for Parallel Processing ({mediaFiles.length} Loaded)</p>
|
| 952 |
+
</div>
|
| 953 |
+
</div>
|
| 954 |
+
<div className="flex items-center gap-6">
|
| 955 |
+
<div className="flex items-center gap-3 bg-black px-6 py-3 rounded-2xl border border-gray-800 shadow-inner group active:scale-95 transition-all">
|
| 956 |
+
<input type="checkbox" id="sel-all" className="h-6 w-6 rounded-lg bg-gray-900 border-gray-700 text-indigo-600 transition-all cursor-pointer shadow-sm" checked={mediaFiles.length > 0 && mediaFiles.every(f => f.isSelected)} onChange={(e) => setMediaFiles(prev => (prev || []).map(mf => ({ ...mf, isSelected: e.target.checked })))} />
|
| 957 |
+
<label htmlFor="sel-all" className="text-xs font-black text-gray-500 cursor-pointer group-hover:text-gray-300 transition-colors uppercase tracking-widest">Select All Items</label>
|
| 958 |
+
</div>
|
| 959 |
+
</div>
|
| 960 |
+
</div>
|
| 961 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-12">
|
| 962 |
+
{mediaFiles.map(item => (
|
| 963 |
+
<MediaItem
|
| 964 |
+
key={item.id}
|
| 965 |
+
item={item}
|
| 966 |
+
autofit={autofitTextareas}
|
| 967 |
+
isApiKeySet={hasValidConfig}
|
| 968 |
+
isComfyEnabled={isComfyEnabled}
|
| 969 |
+
showSideBySidePreview={showSideBySidePreview}
|
| 970 |
+
onGenerate={handleGenerateCaption}
|
| 971 |
+
onCheckQuality={handleCheckQuality}
|
| 972 |
+
onPreview={handleComfyPreview}
|
| 973 |
+
onCaptionChange={(id, cap) => updateFile(id, { caption: cap })}
|
| 974 |
+
onCustomInstructionsChange={(id, ins) => updateFile(id, { customInstructions: ins })}
|
| 975 |
+
onSelectionChange={(id, sel) => updateFile(id, { isSelected: sel })}
|
| 976 |
+
onOpenPreviewModal={setActivePreviewId}
|
| 977 |
+
/>
|
| 978 |
+
))}
|
| 979 |
+
</div>
|
| 980 |
+
</>
|
| 981 |
+
) : (
|
| 982 |
+
<div className="flex flex-col items-center justify-center py-32 bg-gray-900/50 rounded-3xl border-2 border-dashed border-gray-800 text-gray-500 animate-pulse">
|
| 983 |
+
<UploadCloudIcon className="w-16 h-16 mb-6 opacity-20" />
|
| 984 |
+
<h3 className="text-lg font-black uppercase tracking-widest text-gray-700">No items uploaded yet</h3>
|
| 985 |
+
<p className="text-xs mt-2 uppercase tracking-tight text-gray-600">Start by dropping files into the upload zone above</p>
|
| 986 |
+
</div>
|
| 987 |
+
)}
|
| 988 |
+
</section>
|
| 989 |
+
</main>
|
| 990 |
+
</div>
|
| 991 |
+
);
|
| 992 |
+
};
|
| 993 |
+
|
| 994 |
+
export default App;
|
Dockerfile.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:18-alpine
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy package.json first to ensure it exists for npm install
|
| 6 |
+
COPY package.json ./
|
| 7 |
+
|
| 8 |
+
# Install dependencies
|
| 9 |
+
RUN npm install
|
| 10 |
+
|
| 11 |
+
# Copy the rest of the application code
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Build the React application
|
| 15 |
+
# This will use the API_KEY environment variable if provided during build
|
| 16 |
+
RUN npm run build
|
| 17 |
+
|
| 18 |
+
# Expose port 7860 (Required by Hugging Face Spaces)
|
| 19 |
+
EXPOSE 7860
|
| 20 |
+
|
| 21 |
+
# Start the Vite preview server
|
| 22 |
+
CMD ["npm", "run", "preview"]
|
README.md
CHANGED
|
@@ -1,10 +1,127 @@
|
|
|
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: docker
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
---
|
| 3 |
+
title: LoRA Caption Assistant
|
| 4 |
+
emoji: 🖼️
|
| 5 |
+
colorFrom: gray
|
| 6 |
colorTo: indigo
|
| 7 |
sdk: docker
|
| 8 |
+
app_port: 7860
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# LoRA Caption Assistant
|
| 12 |
+
|
| 13 |
+
An AI-powered web application designed to assist in generating high-quality, detailed captions for image and video datasets. This tool is specifically tailored for training LoRA (Low-Rank Adaptation) models, utilizing Google's Gemini API or a Local Qwen Model (via vLLM) to automate the captioning process.
|
| 14 |
+
|
| 15 |
+
## Features
|
| 16 |
+
|
| 17 |
+
* **Automated Captioning**: Generates detailed, objective descriptions using Gemini 2.5 Pro or local Qwen-VL.
|
| 18 |
+
* **LoRA Optimized**: Automatic trigger word insertion and style-agnostic descriptions.
|
| 19 |
+
* **Multi-Modal**: Supports both image and video inputs.
|
| 20 |
+
* **Character Tagging**: Optional automatic identification and tagging of specific characters.
|
| 21 |
+
* **Quality Assurance**: AI-powered scoring system to evaluate caption quality (1-5 scale).
|
| 22 |
+
* **Batch Processing**: Robust queue system with rate limiting (RPM) and batch sizes.
|
| 23 |
+
* **Export**: Downloads the dataset (media + text files) as a ZIP file.
|
| 24 |
+
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
## 🚀 Deployment on Hugging Face Spaces
|
| 28 |
+
|
| 29 |
+
This is the recommended way to run the application if you don't have a GPU.
|
| 30 |
+
|
| 31 |
+
### Step 1: Create a Space
|
| 32 |
+
1. Go to [Hugging Face Spaces](https://huggingface.co/spaces).
|
| 33 |
+
2. Click **Create new Space**.
|
| 34 |
+
3. Enter a name (e.g., `lora-caption-assistant`).
|
| 35 |
+
4. Select **Docker** as the SDK.
|
| 36 |
+
5. Choose "Blank" or "Public" template.
|
| 37 |
+
6. Click **Create Space**.
|
| 38 |
+
|
| 39 |
+
### Step 2: Upload Files
|
| 40 |
+
Upload the contents of this repository to your Space. Ensure the following files are in the **root** directory:
|
| 41 |
+
* `Dockerfile` (Critical: The app will fail without this)
|
| 42 |
+
* `package.json`
|
| 43 |
+
* `vite.config.ts`
|
| 44 |
+
* `index.html`
|
| 45 |
+
* `src/` folder (containing `App.tsx`, etc.)
|
| 46 |
+
|
| 47 |
+
### Step 3: Configure API Key (For Gemini)
|
| 48 |
+
1. In your Space, go to **Settings**.
|
| 49 |
+
2. Scroll to **Variables and secrets**.
|
| 50 |
+
3. Click **New secret**.
|
| 51 |
+
4. **Name**: `API_KEY`
|
| 52 |
+
5. **Value**: Your Google Gemini API Key.
|
| 53 |
+
|
| 54 |
---
|
| 55 |
|
| 56 |
+
## 🤖 Local Qwen Setup Guide
|
| 57 |
+
|
| 58 |
+
If you have a powerful NVIDIA GPU (12GB+ VRAM recommended), you can run the captioning model **locally for free** and connect this web app to it.
|
| 59 |
+
|
| 60 |
+
### Prerequisites
|
| 61 |
+
* **OS**: Windows or Linux
|
| 62 |
+
* **GPU**: NVIDIA GPU (CUDA support)
|
| 63 |
+
* **Software**: Python 3.10+ and CUDA Toolkit installed.
|
| 64 |
+
|
| 65 |
+
### Step 1: Get the Script
|
| 66 |
+
1. Open the LoRA Caption Assistant Web App.
|
| 67 |
+
2. Under **AI Provider**, select **Local Qwen (GPU)**.
|
| 68 |
+
3. Select your desired model (e.g., `Qwen 2.5 VL 7B`).
|
| 69 |
+
4. Set your desired install folder path.
|
| 70 |
+
5. Click **Download Setup Script**.
|
| 71 |
+
|
| 72 |
+
### Step 2: Run the Server
|
| 73 |
+
1. Locate the downloaded `.bat` (Windows) or `.sh` (Linux) file.
|
| 74 |
+
2. Run it.
|
| 75 |
+
3. The script will:
|
| 76 |
+
* Create a Python virtual environment.
|
| 77 |
+
* Install `vllm`.
|
| 78 |
+
* Download the selected Qwen model from Hugging Face.
|
| 79 |
+
* Start an OpenAI-compatible API server on port 8000.
|
| 80 |
+
|
| 81 |
+
### Step 3: Connect to the App
|
| 82 |
+
|
| 83 |
+
**Scenario A: Running App Locally (localhost)**
|
| 84 |
+
* If you are running this web app on your own computer (`npm run dev`), simply set the Endpoint in the app to: `http://localhost:8000/v1`
|
| 85 |
+
|
| 86 |
+
**Scenario B: Running App on Hugging Face (HTTPS)**
|
| 87 |
+
* If you are accessing the web app via Hugging Face Spaces, you **cannot** connect to `localhost` directly due to browser security (Mixed Content Blocking).
|
| 88 |
+
* You must create a secure tunnel.
|
| 89 |
+
|
| 90 |
+
**How to Tunnel:**
|
| 91 |
+
1. **Cloudflare Tunnel (Easiest)**:
|
| 92 |
+
* Download `cloudflared`.
|
| 93 |
+
* Run: `cloudflared tunnel --url http://localhost:8000`
|
| 94 |
+
* Copy the URL ending in `.trycloudflare.com`.
|
| 95 |
+
2. **Paste the URL**:
|
| 96 |
+
* Paste this secure URL into the **Local Endpoint** field in the Web App.
|
| 97 |
+
* Add `/v1` to the end (e.g., `https://example.trycloudflare.com/v1`).
|
| 98 |
+
|
| 99 |
+
---
|
| 100 |
+
|
| 101 |
+
## 💻 Local Development (Web App)
|
| 102 |
+
|
| 103 |
+
### Prerequisites
|
| 104 |
+
* Node.js (v18+)
|
| 105 |
+
* npm
|
| 106 |
+
|
| 107 |
+
### Installation
|
| 108 |
+
1. Clone the repo:
|
| 109 |
+
```bash
|
| 110 |
+
git clone <your-repo-url>
|
| 111 |
+
cd lora-caption-assistant
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
2. Install dependencies:
|
| 115 |
+
```bash
|
| 116 |
+
npm install
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
3. Run the app:
|
| 120 |
+
```bash
|
| 121 |
+
npm run dev
|
| 122 |
+
```
|
| 123 |
+
Open `http://localhost:5173` in your browser.
|
| 124 |
+
|
| 125 |
+
## License
|
| 126 |
+
|
| 127 |
+
MIT
|
components/FileUploader.tsx
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import React, { useState, useCallback, useRef } from 'react';
|
| 3 |
+
import { UploadCloudIcon } from './Icons';
|
| 4 |
+
|
| 5 |
+
interface FileUploaderProps {
|
| 6 |
+
onFilesAdded: (files: File[]) => void;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
const FileUploader: React.FC<FileUploaderProps> = ({ onFilesAdded }) => {
|
| 10 |
+
const [isDragging, setIsDragging] = useState(false);
|
| 11 |
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
| 12 |
+
|
| 13 |
+
const handleDragEnter = useCallback((e: React.DragEvent<HTMLDivElement>) => {
|
| 14 |
+
e.preventDefault();
|
| 15 |
+
e.stopPropagation();
|
| 16 |
+
setIsDragging(true);
|
| 17 |
+
}, []);
|
| 18 |
+
|
| 19 |
+
const handleDragLeave = useCallback((e: React.DragEvent<HTMLDivElement>) => {
|
| 20 |
+
e.preventDefault();
|
| 21 |
+
e.stopPropagation();
|
| 22 |
+
setIsDragging(false);
|
| 23 |
+
}, []);
|
| 24 |
+
|
| 25 |
+
const handleDragOver = useCallback((e: React.DragEvent<HTMLDivElement>) => {
|
| 26 |
+
e.preventDefault();
|
| 27 |
+
e.stopPropagation();
|
| 28 |
+
}, []);
|
| 29 |
+
|
| 30 |
+
const handleDrop = useCallback((e: React.DragEvent<HTMLDivElement>) => {
|
| 31 |
+
e.preventDefault();
|
| 32 |
+
e.stopPropagation();
|
| 33 |
+
setIsDragging(false);
|
| 34 |
+
const files = Array.from(e.dataTransfer.files);
|
| 35 |
+
if (files && files.length > 0) {
|
| 36 |
+
onFilesAdded(files);
|
| 37 |
+
}
|
| 38 |
+
}, [onFilesAdded]);
|
| 39 |
+
|
| 40 |
+
const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
|
| 41 |
+
const files = Array.from(e.target.files || []);
|
| 42 |
+
if (files && files.length > 0) {
|
| 43 |
+
onFilesAdded(files);
|
| 44 |
+
}
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
const openFileDialog = () => {
|
| 48 |
+
fileInputRef.current?.click();
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
return (
|
| 52 |
+
<div
|
| 53 |
+
className={`relative border-2 border-dashed rounded-lg p-12 text-center transition-colors duration-200 ease-in-out ${
|
| 54 |
+
isDragging ? 'border-indigo-400 bg-gray-800' : 'border-gray-600 hover:border-indigo-500'
|
| 55 |
+
}`}
|
| 56 |
+
onDragEnter={handleDragEnter}
|
| 57 |
+
onDragLeave={handleDragLeave}
|
| 58 |
+
onDragOver={handleDragOver}
|
| 59 |
+
onDrop={handleDrop}
|
| 60 |
+
onClick={openFileDialog}
|
| 61 |
+
>
|
| 62 |
+
<input
|
| 63 |
+
ref={fileInputRef}
|
| 64 |
+
type="file"
|
| 65 |
+
multiple
|
| 66 |
+
accept="image/*,video/*,.txt"
|
| 67 |
+
className="hidden"
|
| 68 |
+
onChange={handleFileSelect}
|
| 69 |
+
/>
|
| 70 |
+
<div className="flex flex-col items-center justify-center space-y-4">
|
| 71 |
+
<UploadCloudIcon className="w-12 h-12 text-gray-400" />
|
| 72 |
+
<p className="text-gray-400">
|
| 73 |
+
<span className="font-semibold text-indigo-400">Click to upload</span> or drag and drop
|
| 74 |
+
</p>
|
| 75 |
+
<p className="text-xs text-gray-500">Upload media and optional .txt captions (must have matching filenames)</p>
|
| 76 |
+
</div>
|
| 77 |
+
</div>
|
| 78 |
+
);
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
export default FileUploader;
|
components/Icons.tsx
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import React from 'react';
|
| 3 |
+
|
| 4 |
+
export const UploadCloudIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 5 |
+
<svg
|
| 6 |
+
{...props}
|
| 7 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 8 |
+
width="24"
|
| 9 |
+
height="24"
|
| 10 |
+
viewBox="0 0 24 24"
|
| 11 |
+
fill="none"
|
| 12 |
+
stroke="currentColor"
|
| 13 |
+
strokeWidth="2"
|
| 14 |
+
strokeLinecap="round"
|
| 15 |
+
strokeLinejoin="round"
|
| 16 |
+
>
|
| 17 |
+
<path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242" />
|
| 18 |
+
<path d="M12 12v9" />
|
| 19 |
+
<path d="m16 16-4-4-4 4" />
|
| 20 |
+
</svg>
|
| 21 |
+
);
|
| 22 |
+
|
| 23 |
+
export const SparklesIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 24 |
+
<svg
|
| 25 |
+
{...props}
|
| 26 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 27 |
+
width="24"
|
| 28 |
+
height="24"
|
| 29 |
+
viewBox="0 0 24 24"
|
| 30 |
+
fill="none"
|
| 31 |
+
stroke="currentColor"
|
| 32 |
+
strokeWidth="2"
|
| 33 |
+
strokeLinecap="round"
|
| 34 |
+
strokeLinejoin="round"
|
| 35 |
+
>
|
| 36 |
+
<path d="m12 3-1.912 5.813a2 2 0 0 1-1.275 1.275L3 12l5.813 1.912a2 2 0 0 1 1.275 1.275L12 21l1.912-5.813a2 2 0 0 1 1.275-1.275L21 12l-5.813-1.912a2 2 0 0 1-1.275-1.275L12 3Z" />
|
| 37 |
+
<path d="M5 3v4" />
|
| 38 |
+
<path d="M19 17v4" />
|
| 39 |
+
<path d="M3 5h4" />
|
| 40 |
+
<path d="M17 19h4" />
|
| 41 |
+
</svg>
|
| 42 |
+
);
|
| 43 |
+
|
| 44 |
+
export const DownloadIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 45 |
+
<svg
|
| 46 |
+
{...props}
|
| 47 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 48 |
+
width="24"
|
| 49 |
+
height="24"
|
| 50 |
+
viewBox="0 0 24 24"
|
| 51 |
+
fill="none"
|
| 52 |
+
stroke="currentColor"
|
| 53 |
+
strokeWidth="2"
|
| 54 |
+
strokeLinecap="round"
|
| 55 |
+
strokeLinejoin="round"
|
| 56 |
+
>
|
| 57 |
+
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
|
| 58 |
+
<polyline points="7 10 12 15 17 10" />
|
| 59 |
+
<line x1="12" x2="12" y1="15" y2="3" />
|
| 60 |
+
</svg>
|
| 61 |
+
);
|
| 62 |
+
|
| 63 |
+
export const LoaderIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 64 |
+
<svg
|
| 65 |
+
{...props}
|
| 66 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 67 |
+
width="24"
|
| 68 |
+
height="24"
|
| 69 |
+
viewBox="0 0 24 24"
|
| 70 |
+
fill="none"
|
| 71 |
+
stroke="currentColor"
|
| 72 |
+
strokeWidth="2"
|
| 73 |
+
strokeLinecap="round"
|
| 74 |
+
strokeLinejoin="round"
|
| 75 |
+
>
|
| 76 |
+
<line x1="12" y1="2" x2="12" y2="6" />
|
| 77 |
+
<line x1="12" y1="18" x2="12" y2="22" />
|
| 78 |
+
<line x1="4.93" y1="4.93" x2="7.76" y2="7.76" />
|
| 79 |
+
<line x1="16.24" y1="16.24" x2="19.07" y2="19.07" />
|
| 80 |
+
<line x1="2" y1="12" x2="6" y2="12" />
|
| 81 |
+
<line x1="18" y1="12" x2="22" y2="12" />
|
| 82 |
+
<line x1="4.93" y1="19.07" x2="7.76" y2="16.24" />
|
| 83 |
+
<line x1="16.24" y1="7.76" x2="19.07" y2="4.93" />
|
| 84 |
+
</svg>
|
| 85 |
+
);
|
| 86 |
+
|
| 87 |
+
export const WandIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 88 |
+
<svg
|
| 89 |
+
{...props}
|
| 90 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 91 |
+
width="24"
|
| 92 |
+
height="24"
|
| 93 |
+
viewBox="0 0 24 24"
|
| 94 |
+
fill="none"
|
| 95 |
+
stroke="currentColor"
|
| 96 |
+
strokeWidth="2"
|
| 97 |
+
strokeLinecap="round"
|
| 98 |
+
strokeLinejoin="round">
|
| 99 |
+
<path d="M15 4V2"/><path d="M15 16v-2"/><path d="M8 9h2"/>
|
| 100 |
+
<path d="M20 9h2"/><path d="M17.8 11.8 19 13"/>
|
| 101 |
+
<path d="M15 9h0"/><path d="M17.8 6.2 19 5"/>
|
| 102 |
+
<path d="m3 21 9-9"/><path d="M12.2 6.2 11 5"/>
|
| 103 |
+
</svg>
|
| 104 |
+
);
|
| 105 |
+
|
| 106 |
+
export const CheckCircleIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 107 |
+
<svg
|
| 108 |
+
{...props}
|
| 109 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 110 |
+
width="24"
|
| 111 |
+
height="24"
|
| 112 |
+
viewBox="0 0 24 24"
|
| 113 |
+
fill="none"
|
| 114 |
+
stroke="currentColor"
|
| 115 |
+
strokeWidth="2"
|
| 116 |
+
strokeLinecap="round"
|
| 117 |
+
strokeLinejoin="round"
|
| 118 |
+
>
|
| 119 |
+
<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
|
| 120 |
+
<polyline points="22 4 12 14.01 9 11.01" />
|
| 121 |
+
</svg>
|
| 122 |
+
);
|
| 123 |
+
|
| 124 |
+
export const TrashIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 125 |
+
<svg
|
| 126 |
+
{...props}
|
| 127 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 128 |
+
width="24"
|
| 129 |
+
height="24"
|
| 130 |
+
viewBox="0 0 24 24"
|
| 131 |
+
fill="none"
|
| 132 |
+
stroke="currentColor"
|
| 133 |
+
strokeWidth="2"
|
| 134 |
+
strokeLinecap="round"
|
| 135 |
+
strokeLinejoin="round"
|
| 136 |
+
>
|
| 137 |
+
<path d="M3 6h18" />
|
| 138 |
+
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
|
| 139 |
+
</svg>
|
| 140 |
+
);
|
| 141 |
+
|
| 142 |
+
export const XIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 143 |
+
<svg
|
| 144 |
+
{...props}
|
| 145 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 146 |
+
width="24"
|
| 147 |
+
height="24"
|
| 148 |
+
viewBox="0 0 24 24"
|
| 149 |
+
fill="none"
|
| 150 |
+
stroke="currentColor"
|
| 151 |
+
strokeWidth="2"
|
| 152 |
+
strokeLinecap="round"
|
| 153 |
+
strokeLinejoin="round"
|
| 154 |
+
>
|
| 155 |
+
<path d="M18 6L6 18" />
|
| 156 |
+
<path d="M6 6l12 12" />
|
| 157 |
+
</svg>
|
| 158 |
+
);
|
| 159 |
+
|
| 160 |
+
export const AlertTriangleIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 161 |
+
<svg
|
| 162 |
+
{...props}
|
| 163 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 164 |
+
width="24"
|
| 165 |
+
height="24"
|
| 166 |
+
viewBox="0 0 24 24"
|
| 167 |
+
fill="none"
|
| 168 |
+
stroke="currentColor"
|
| 169 |
+
strokeWidth="2"
|
| 170 |
+
strokeLinecap="round"
|
| 171 |
+
strokeLinejoin="round"
|
| 172 |
+
>
|
| 173 |
+
<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z" />
|
| 174 |
+
<path d="M12 9v4" />
|
| 175 |
+
<path d="M12 17h.01" />
|
| 176 |
+
</svg>
|
| 177 |
+
);
|
| 178 |
+
|
| 179 |
+
export const CopyIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 180 |
+
<svg
|
| 181 |
+
{...props}
|
| 182 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 183 |
+
width="24"
|
| 184 |
+
height="24"
|
| 185 |
+
viewBox="0 0 24 24"
|
| 186 |
+
fill="none"
|
| 187 |
+
stroke="currentColor"
|
| 188 |
+
strokeWidth="2"
|
| 189 |
+
strokeLinecap="round"
|
| 190 |
+
strokeLinejoin="round"
|
| 191 |
+
>
|
| 192 |
+
<rect width="14" height="14" x="8" y="8" rx="2" ry="2" />
|
| 193 |
+
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
|
| 194 |
+
</svg>
|
| 195 |
+
);
|
| 196 |
+
|
| 197 |
+
export const StopIcon = (props: React.SVGProps<SVGSVGElement>) => (
|
| 198 |
+
<svg
|
| 199 |
+
{...props}
|
| 200 |
+
xmlns="http://www.w3.org/2000/svg"
|
| 201 |
+
width="24"
|
| 202 |
+
height="24"
|
| 203 |
+
viewBox="0 0 24 24"
|
| 204 |
+
fill="none"
|
| 205 |
+
stroke="currentColor"
|
| 206 |
+
strokeWidth="2"
|
| 207 |
+
strokeLinecap="round"
|
| 208 |
+
strokeLinejoin="round"
|
| 209 |
+
>
|
| 210 |
+
<rect x="6" y="6" width="12" height="12" />
|
| 211 |
+
</svg>
|
| 212 |
+
);
|
components/MediaItem.tsx
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React from 'react';
|
| 2 |
+
import type { MediaFile } from '../types';
|
| 3 |
+
import { GenerationStatus } from '../types';
|
| 4 |
+
import { SparklesIcon, LoaderIcon, WandIcon, CheckCircleIcon } from './Icons';
|
| 5 |
+
|
| 6 |
+
interface MediaItemProps {
|
| 7 |
+
item: MediaFile;
|
| 8 |
+
autofit: boolean;
|
| 9 |
+
isApiKeySet: boolean;
|
| 10 |
+
isComfyEnabled: boolean;
|
| 11 |
+
showSideBySidePreview: boolean;
|
| 12 |
+
onGenerate: (id: string, customInstructions?: string) => void;
|
| 13 |
+
onCheckQuality: (id: string) => void;
|
| 14 |
+
onPreview: (id: string) => void;
|
| 15 |
+
onCaptionChange: (id:string, caption: string) => void;
|
| 16 |
+
onCustomInstructionsChange: (id: string, instructions: string) => void;
|
| 17 |
+
onSelectionChange: (id: string, isSelected: boolean) => void;
|
| 18 |
+
onOpenPreviewModal: (id: string) => void;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
const getScoreColor = (score?: number) => {
|
| 22 |
+
if (score === undefined) return 'text-gray-500';
|
| 23 |
+
if (score >= 4) return 'text-green-400';
|
| 24 |
+
if (score >= 3) return 'text-yellow-400';
|
| 25 |
+
return 'text-red-400';
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
const MediaItem: React.FC<MediaItemProps> = ({
|
| 30 |
+
item,
|
| 31 |
+
autofit,
|
| 32 |
+
isApiKeySet,
|
| 33 |
+
isComfyEnabled,
|
| 34 |
+
showSideBySidePreview,
|
| 35 |
+
onGenerate,
|
| 36 |
+
onCheckQuality,
|
| 37 |
+
onPreview,
|
| 38 |
+
onCaptionChange,
|
| 39 |
+
onCustomInstructionsChange,
|
| 40 |
+
onSelectionChange,
|
| 41 |
+
onOpenPreviewModal
|
| 42 |
+
}) => {
|
| 43 |
+
const isVideo = item.file.type.startsWith('video/');
|
| 44 |
+
const textareaRef = React.useRef<HTMLTextAreaElement>(null);
|
| 45 |
+
|
| 46 |
+
React.useEffect(() => {
|
| 47 |
+
if (textareaRef.current && autofit) {
|
| 48 |
+
textareaRef.current.style.height = 'auto'; // Reset height
|
| 49 |
+
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
|
| 50 |
+
} else if (textareaRef.current) {
|
| 51 |
+
textareaRef.current.style.height = ''; // Revert to CSS-defined height
|
| 52 |
+
}
|
| 53 |
+
}, [item.caption, autofit]);
|
| 54 |
+
|
| 55 |
+
const getStatusColor = () => {
|
| 56 |
+
switch(item.status) {
|
| 57 |
+
case GenerationStatus.SUCCESS: return 'border-green-500';
|
| 58 |
+
case GenerationStatus.ERROR: return 'border-red-500';
|
| 59 |
+
case GenerationStatus.GENERATING: return 'border-indigo-500';
|
| 60 |
+
case GenerationStatus.CHECKING: return 'border-yellow-500';
|
| 61 |
+
default: return 'border-gray-700';
|
| 62 |
+
}
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
const isProcessing = item.status === GenerationStatus.GENERATING || item.status === GenerationStatus.CHECKING;
|
| 66 |
+
const isPreviewing = item.comfyStatus === 'generating';
|
| 67 |
+
const hasPreview = !!item.comfyPreviewUrl;
|
| 68 |
+
|
| 69 |
+
const renderMedia = (url: string, isOriginal: boolean) => {
|
| 70 |
+
const isVideoFile = isOriginal && isVideo;
|
| 71 |
+
return (
|
| 72 |
+
<div className="relative flex-1 bg-gray-900 rounded-md overflow-hidden flex flex-col group/media shadow-inner cursor-pointer" onClick={() => onOpenPreviewModal(item.id)}>
|
| 73 |
+
<div className="flex-grow flex items-center justify-center min-h-[160px] h-full">
|
| 74 |
+
{isVideoFile ? (
|
| 75 |
+
<video src={url} className="max-w-full max-h-full object-contain" />
|
| 76 |
+
) : (
|
| 77 |
+
<img src={url} alt={item.file.name} className="max-w-full max-h-full object-contain" />
|
| 78 |
+
)}
|
| 79 |
+
</div>
|
| 80 |
+
<div className="absolute bottom-1 left-1 px-1.5 py-0.5 bg-black/60 backdrop-blur-sm rounded text-[9px] font-black uppercase tracking-widest text-white/90 border border-white/10 opacity-80 pointer-events-none">
|
| 81 |
+
{isOriginal ? 'Original' : 'Preview'}
|
| 82 |
+
</div>
|
| 83 |
+
</div>
|
| 84 |
+
);
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
return (
|
| 88 |
+
<div className={`bg-gray-800 rounded-lg overflow-hidden border-2 transition-all ${getStatusColor()}`}>
|
| 89 |
+
<div className="relative p-2 space-y-2">
|
| 90 |
+
<input
|
| 91 |
+
type="checkbox"
|
| 92 |
+
checked={item.isSelected}
|
| 93 |
+
onChange={(e) => onSelectionChange(item.id, e.target.checked)}
|
| 94 |
+
className="absolute top-4 left-4 h-6 w-6 bg-gray-900/80 backdrop-blur-sm border-gray-600 text-indigo-500 rounded focus:ring-indigo-600 z-10 cursor-pointer shadow-lg"
|
| 95 |
+
/>
|
| 96 |
+
{item.qualityScore !== undefined && (
|
| 97 |
+
<div className="absolute top-4 right-4 bg-gray-900/70 backdrop-blur-sm px-3 py-1 rounded-full text-sm font-semibold flex items-center gap-1.5 z-10 shadow-sm border border-white/5">
|
| 98 |
+
<span className={`tracking-widest ${getScoreColor(item.qualityScore)}`}>
|
| 99 |
+
{'★'.repeat(item.qualityScore)}{'☆'.repeat(5 - item.qualityScore)}
|
| 100 |
+
</span>
|
| 101 |
+
<span className="text-gray-300 text-[10px]">{item.qualityScore}/5</span>
|
| 102 |
+
</div>
|
| 103 |
+
)}
|
| 104 |
+
|
| 105 |
+
{hasPreview && !showSideBySidePreview && (
|
| 106 |
+
<button
|
| 107 |
+
onClick={() => onOpenPreviewModal(item.id)}
|
| 108 |
+
className="absolute bottom-4 right-4 bg-orange-600 hover:bg-orange-500 text-white px-3 py-1.5 rounded-md text-[10px] font-black uppercase tracking-wider z-10 shadow-xl transition-all hover:scale-105 active:scale-95 border border-white/10"
|
| 109 |
+
>
|
| 110 |
+
View Comparison
|
| 111 |
+
</button>
|
| 112 |
+
)}
|
| 113 |
+
|
| 114 |
+
<div className={`h-64 flex gap-2 ${showSideBySidePreview && hasPreview ? 'flex-row' : 'flex-col'}`}>
|
| 115 |
+
{showSideBySidePreview && hasPreview ? (
|
| 116 |
+
<>
|
| 117 |
+
{renderMedia(item.previewUrl, true)}
|
| 118 |
+
{renderMedia(item.comfyPreviewUrl!, false)}
|
| 119 |
+
</>
|
| 120 |
+
) : (
|
| 121 |
+
<div
|
| 122 |
+
className="flex-grow flex items-center justify-center bg-gray-900 rounded-md overflow-hidden relative group/single cursor-pointer"
|
| 123 |
+
onClick={() => onOpenPreviewModal(item.id)}
|
| 124 |
+
>
|
| 125 |
+
{isVideo ? (
|
| 126 |
+
<video src={item.previewUrl} className="max-w-full max-h-full object-contain" />
|
| 127 |
+
) : (
|
| 128 |
+
<img src={item.previewUrl} alt={item.file.name} className="max-w-full max-h-full object-contain" />
|
| 129 |
+
)}
|
| 130 |
+
<div className="absolute inset-0 bg-black/0 group-hover/single:bg-black/20 transition-colors flex items-center justify-center">
|
| 131 |
+
<SparklesIcon className="w-8 h-8 text-white opacity-0 group-hover/single:opacity-100 transition-all scale-75 group-hover/single:scale-100" />
|
| 132 |
+
</div>
|
| 133 |
+
</div>
|
| 134 |
+
)}
|
| 135 |
+
</div>
|
| 136 |
+
</div>
|
| 137 |
+
|
| 138 |
+
<div className="p-4 space-y-4">
|
| 139 |
+
<div className="flex justify-between items-start gap-2">
|
| 140 |
+
<p className="text-sm text-gray-400 truncate flex-grow font-mono" title={item.file.name}>{item.file.name}</p>
|
| 141 |
+
<span className="text-[10px] text-gray-600 font-bold uppercase tracking-widest whitespace-nowrap">
|
| 142 |
+
{isVideo ? 'Video' : 'Image'}
|
| 143 |
+
</span>
|
| 144 |
+
</div>
|
| 145 |
+
|
| 146 |
+
<textarea
|
| 147 |
+
ref={textareaRef}
|
| 148 |
+
value={item.caption}
|
| 149 |
+
onChange={(e) => onCaptionChange(item.id, e.target.value)}
|
| 150 |
+
placeholder="Generated caption will appear here..."
|
| 151 |
+
rows={!autofit ? 6 : 1}
|
| 152 |
+
className={`w-full p-2.5 bg-gray-900 border border-gray-700 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-all resize-none overflow-hidden text-[13px] leading-relaxed text-gray-200 ${!autofit ? 'h-32' : ''}`}
|
| 153 |
+
/>
|
| 154 |
+
|
| 155 |
+
<div className="flex flex-col gap-2">
|
| 156 |
+
<input
|
| 157 |
+
type="text"
|
| 158 |
+
placeholder="Custom instructions for refinement..."
|
| 159 |
+
value={item.customInstructions}
|
| 160 |
+
onChange={(e) => onCustomInstructionsChange(item.id, e.target.value)}
|
| 161 |
+
className="w-full p-2.5 bg-gray-700 border border-gray-600 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition-all text-xs"
|
| 162 |
+
/>
|
| 163 |
+
<div className="flex flex-wrap gap-2">
|
| 164 |
+
<button
|
| 165 |
+
onClick={() => onGenerate(item.id, item.customInstructions)}
|
| 166 |
+
disabled={isProcessing || !isApiKeySet}
|
| 167 |
+
className="flex-1 flex items-center justify-center px-3 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 disabled:bg-gray-700 disabled:cursor-not-allowed transition-all text-[11px] font-black uppercase tracking-wider shadow-lg shadow-green-900/10"
|
| 168 |
+
>
|
| 169 |
+
{isProcessing ? (
|
| 170 |
+
<LoaderIcon className="w-4 h-4 animate-spin mr-2" />
|
| 171 |
+
) : (
|
| 172 |
+
item.customInstructions ? <WandIcon className="w-4 h-4 mr-2" /> : <SparklesIcon className="w-4 h-4 mr-2" />
|
| 173 |
+
)}
|
| 174 |
+
<span>
|
| 175 |
+
{item.status === GenerationStatus.GENERATING ? 'Working...' :
|
| 176 |
+
item.status === GenerationStatus.CHECKING ? 'Checking...' :
|
| 177 |
+
item.customInstructions ? 'Refine' : 'Generate'}
|
| 178 |
+
</span>
|
| 179 |
+
</button>
|
| 180 |
+
|
| 181 |
+
<button
|
| 182 |
+
onClick={() => onCheckQuality(item.id)}
|
| 183 |
+
disabled={isProcessing || !isApiKeySet || !item.caption}
|
| 184 |
+
className="flex-1 flex items-center justify-center px-3 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:bg-gray-700 disabled:cursor-not-allowed transition-all text-[11px] font-black uppercase tracking-wider shadow-lg shadow-blue-900/10"
|
| 185 |
+
>
|
| 186 |
+
<CheckCircleIcon className="w-4 h-4 mr-2" />
|
| 187 |
+
<span>Check Quality</span>
|
| 188 |
+
</button>
|
| 189 |
+
|
| 190 |
+
{isComfyEnabled && (
|
| 191 |
+
<button
|
| 192 |
+
onClick={() => onPreview(item.id)}
|
| 193 |
+
disabled={isPreviewing || !item.caption}
|
| 194 |
+
className={`flex-shrink-0 flex items-center justify-center px-4 py-2 text-white rounded-md transition-all text-[11px] font-black uppercase tracking-wider shadow-lg ${item.comfyStatus === 'error' ? 'bg-red-600 hover:bg-red-700 shadow-red-900/10' : 'bg-orange-600 hover:bg-orange-700 shadow-orange-900/10'} disabled:bg-gray-500 disabled:cursor-not-allowed`}
|
| 195 |
+
title={item.comfyErrorMessage || "Generate preview with ComfyUI"}
|
| 196 |
+
>
|
| 197 |
+
{isPreviewing ? (
|
| 198 |
+
<LoaderIcon className="w-4 h-4 animate-spin mr-2" />
|
| 199 |
+
) : (
|
| 200 |
+
<SparklesIcon className="w-4 h-4 mr-2" />
|
| 201 |
+
)}
|
| 202 |
+
<span>Preview</span>
|
| 203 |
+
</button>
|
| 204 |
+
)}
|
| 205 |
+
</div>
|
| 206 |
+
|
| 207 |
+
{isComfyEnabled && item.comfyStatus === 'error' && (
|
| 208 |
+
<div className="bg-red-900/20 p-2 rounded border border-red-500/30">
|
| 209 |
+
<p className="text-[10px] text-red-400 leading-tight">
|
| 210 |
+
<span className="font-bold uppercase tracking-tighter">Bridge/Server Error:</span> {item.comfyErrorMessage}
|
| 211 |
+
</p>
|
| 212 |
+
</div>
|
| 213 |
+
)}
|
| 214 |
+
{isComfyEnabled && item.comfyStatus === 'generating' && (
|
| 215 |
+
<div className="bg-orange-900/20 p-2 rounded border border-orange-500/30 animate-pulse">
|
| 216 |
+
<p className="text-[10px] text-orange-400 font-bold uppercase text-center tracking-widest">
|
| 217 |
+
Queueing in ComfyUI...
|
| 218 |
+
</p>
|
| 219 |
+
</div>
|
| 220 |
+
)}
|
| 221 |
+
</div>
|
| 222 |
+
|
| 223 |
+
{item.status === GenerationStatus.ERROR && (
|
| 224 |
+
<p className="text-[11px] text-red-400 mt-1 italic font-medium leading-tight">
|
| 225 |
+
<span className="font-black uppercase tracking-tighter mr-1">Error:</span> {item.errorMessage}
|
| 226 |
+
</p>
|
| 227 |
+
)}
|
| 228 |
+
</div>
|
| 229 |
+
</div>
|
| 230 |
+
);
|
| 231 |
+
};
|
| 232 |
+
|
| 233 |
+
export default MediaItem;
|
constants/defaultWorkflow.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const DEFAULT_COMFY_WORKFLOW = {
|
| 2 |
+
"3": {
|
| 3 |
+
"inputs": {
|
| 4 |
+
"seed": 912870720080529,
|
| 5 |
+
"steps": 8,
|
| 6 |
+
"cfg": 1,
|
| 7 |
+
"sampler_name": "euler",
|
| 8 |
+
"scheduler": "simple",
|
| 9 |
+
"denoise": 1,
|
| 10 |
+
"model": [
|
| 11 |
+
"66",
|
| 12 |
+
0
|
| 13 |
+
],
|
| 14 |
+
"positive": [
|
| 15 |
+
"6",
|
| 16 |
+
0
|
| 17 |
+
],
|
| 18 |
+
"negative": [
|
| 19 |
+
"7",
|
| 20 |
+
0
|
| 21 |
+
],
|
| 22 |
+
"latent_image": [
|
| 23 |
+
"58",
|
| 24 |
+
0
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
"class_type": "KSampler",
|
| 28 |
+
"_meta": {
|
| 29 |
+
"title": "KSampler"
|
| 30 |
+
}
|
| 31 |
+
},
|
| 32 |
+
"6": {
|
| 33 |
+
"inputs": {
|
| 34 |
+
"text": "",
|
| 35 |
+
"clip": [
|
| 36 |
+
"38",
|
| 37 |
+
0
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
"class_type": "CLIPTextEncode",
|
| 41 |
+
"_meta": {
|
| 42 |
+
"title": "CLIP Text Encode (Positive Prompt)"
|
| 43 |
+
}
|
| 44 |
+
},
|
| 45 |
+
"7": {
|
| 46 |
+
"inputs": {
|
| 47 |
+
"text": "",
|
| 48 |
+
"clip": [
|
| 49 |
+
"38",
|
| 50 |
+
0
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
"class_type": "CLIPTextEncode",
|
| 54 |
+
"_meta": {
|
| 55 |
+
"title": "CLIP Text Encode (Negative Prompt)"
|
| 56 |
+
}
|
| 57 |
+
},
|
| 58 |
+
"8": {
|
| 59 |
+
"inputs": {
|
| 60 |
+
"samples": [
|
| 61 |
+
"3",
|
| 62 |
+
0
|
| 63 |
+
],
|
| 64 |
+
"vae": [
|
| 65 |
+
"39",
|
| 66 |
+
0
|
| 67 |
+
]
|
| 68 |
+
},
|
| 69 |
+
"class_type": "VAEDecode",
|
| 70 |
+
"_meta": {
|
| 71 |
+
"title": "VAE Decode"
|
| 72 |
+
}
|
| 73 |
+
},
|
| 74 |
+
"37": {
|
| 75 |
+
"inputs": {
|
| 76 |
+
"unet_name": "qwen_image_fp8_e4m3fn.safetensors",
|
| 77 |
+
"weight_dtype": "default"
|
| 78 |
+
},
|
| 79 |
+
"class_type": "UNETLoader",
|
| 80 |
+
"_meta": {
|
| 81 |
+
"title": "Load Diffusion Model"
|
| 82 |
+
}
|
| 83 |
+
},
|
| 84 |
+
"38": {
|
| 85 |
+
"inputs": {
|
| 86 |
+
"clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
|
| 87 |
+
"type": "qwen_image",
|
| 88 |
+
"device": "default"
|
| 89 |
+
},
|
| 90 |
+
"class_type": "CLIPLoader",
|
| 91 |
+
"_meta": {
|
| 92 |
+
"title": "Load CLIP"
|
| 93 |
+
}
|
| 94 |
+
},
|
| 95 |
+
"39": {
|
| 96 |
+
"inputs": {
|
| 97 |
+
"vae_name": "qwen_image_vae.safetensors"
|
| 98 |
+
},
|
| 99 |
+
"class_type": "VAELoader",
|
| 100 |
+
"_meta": {
|
| 101 |
+
"title": "Load VAE"
|
| 102 |
+
}
|
| 103 |
+
},
|
| 104 |
+
"58": {
|
| 105 |
+
"inputs": {
|
| 106 |
+
"width": 1024,
|
| 107 |
+
"height": 1024,
|
| 108 |
+
"batch_size": 1
|
| 109 |
+
},
|
| 110 |
+
"class_type": "EmptySD3LatentImage",
|
| 111 |
+
"_meta": {
|
| 112 |
+
"title": "EmptySD3LatentImage"
|
| 113 |
+
}
|
| 114 |
+
},
|
| 115 |
+
"60": {
|
| 116 |
+
"inputs": {
|
| 117 |
+
"filename_prefix": "ComfyUI",
|
| 118 |
+
"images": [
|
| 119 |
+
"8",
|
| 120 |
+
0
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
"class_type": "SaveImage",
|
| 124 |
+
"_meta": {
|
| 125 |
+
"title": "Save Image"
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
"66": {
|
| 129 |
+
"inputs": {
|
| 130 |
+
"shift": 3,
|
| 131 |
+
"model": [
|
| 132 |
+
"73",
|
| 133 |
+
0
|
| 134 |
+
]
|
| 135 |
+
},
|
| 136 |
+
"class_type": "ModelSamplingAuraFlow",
|
| 137 |
+
"_meta": {
|
| 138 |
+
"title": "ModelSamplingAuraFlow"
|
| 139 |
+
}
|
| 140 |
+
},
|
| 141 |
+
"73": {
|
| 142 |
+
"inputs": {
|
| 143 |
+
"lora_name": "Qwen-Image-Lightning-8steps-V2.0.safetensors",
|
| 144 |
+
"strength_model": 1,
|
| 145 |
+
"model": [
|
| 146 |
+
"37",
|
| 147 |
+
0
|
| 148 |
+
]
|
| 149 |
+
},
|
| 150 |
+
"class_type": "LoraLoaderModelOnly",
|
| 151 |
+
"_meta": {
|
| 152 |
+
"title": "LoraLoaderModelOnly"
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
};
|
index.html
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 7 |
+
<title>LoRA Caption Assistant</title>
|
| 8 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 9 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
|
| 10 |
+
<script type="importmap">
|
| 11 |
+
{
|
| 12 |
+
"imports": {
|
| 13 |
+
"react": "https://aistudiocdn.com/react@^19.2.0",
|
| 14 |
+
"react-dom/": "https://aistudiocdn.com/react-dom@^19.2.0/",
|
| 15 |
+
"react/": "https://aistudiocdn.com/react@^19.2.0/",
|
| 16 |
+
"@google/genai": "https://aistudiocdn.com/@google/genai@^1.30.0",
|
| 17 |
+
"@vitejs/plugin-react": "https://aistudiocdn.com/@vitejs/plugin-react@^5.1.1",
|
| 18 |
+
"vite": "https://aistudiocdn.com/vite@^7.2.2"
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
</script>
|
| 22 |
+
<link rel="stylesheet" href="/index.css">
|
| 23 |
+
</head>
|
| 24 |
+
<body class="bg-gray-900 text-gray-100">
|
| 25 |
+
<div id="root"></div>
|
| 26 |
+
<script type="module" src="/index.tsx"></script>
|
| 27 |
+
</body>
|
| 28 |
+
</html>
|
index.tsx
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import React from 'react';
|
| 3 |
+
import ReactDOM from 'react-dom/client';
|
| 4 |
+
import App from './App';
|
| 5 |
+
|
| 6 |
+
const rootElement = document.getElementById('root');
|
| 7 |
+
if (!rootElement) {
|
| 8 |
+
throw new Error("Could not find root element to mount to");
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
const root = ReactDOM.createRoot(rootElement);
|
| 12 |
+
root.render(
|
| 13 |
+
<React.StrictMode>
|
| 14 |
+
<App />
|
| 15 |
+
</React.StrictMode>
|
| 16 |
+
);
|
metadata.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "LoRA Caption Assistant",
|
| 3 |
+
"description": "High-quality image/video captioning for Wan 2.2 LoRAs using Gemini 3 Pro.",
|
| 4 |
+
"requestFramePermissions": [
|
| 5 |
+
"camera"
|
| 6 |
+
]
|
| 7 |
+
}
|
package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
{
|
| 3 |
+
"name": "lora-caption-assistant",
|
| 4 |
+
"version": "1.0.0",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"dev": "vite",
|
| 8 |
+
"build": "vite build",
|
| 9 |
+
"preview": "vite preview"
|
| 10 |
+
},
|
| 11 |
+
"dependencies": {
|
| 12 |
+
"@google/genai": "^1.30.0",
|
| 13 |
+
"react": "^19.2.0",
|
| 14 |
+
"react-dom": "^19.2.0"
|
| 15 |
+
},
|
| 16 |
+
"devDependencies": {
|
| 17 |
+
"@types/node": "^20.11.0",
|
| 18 |
+
"@types/react": "^19.2.0",
|
| 19 |
+
"@types/react-dom": "^19.2.0",
|
| 20 |
+
"@vitejs/plugin-react": "^4.3.1",
|
| 21 |
+
"typescript": "^5.5.3",
|
| 22 |
+
"vite": "^5.4.1"
|
| 23 |
+
}
|
| 24 |
+
}
|
services/comfyService.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Service for interacting with local ComfyUI instances
|
| 3 |
+
*/
|
| 4 |
+
|
| 5 |
+
/**
|
| 6 |
+
* Converts a standard ComfyUI UI workflow (the one with 'nodes' array)
|
| 7 |
+
* to the API format that the /prompt endpoint expects.
|
| 8 |
+
*/
|
| 9 |
+
const convertUiToApi = (uiWorkflow: any): any => {
|
| 10 |
+
const apiPrompt: any = {};
|
| 11 |
+
|
| 12 |
+
if (!uiWorkflow.nodes || !Array.isArray(uiWorkflow.nodes)) {
|
| 13 |
+
return uiWorkflow; // Already in API format or unknown
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
uiWorkflow.nodes.forEach((node: any) => {
|
| 17 |
+
const nodeId = node.id.toString();
|
| 18 |
+
const inputs: any = {};
|
| 19 |
+
|
| 20 |
+
// Map inputs based on links
|
| 21 |
+
if (uiWorkflow.links && node.inputs) {
|
| 22 |
+
node.inputs.forEach((input: any) => { // Removed unused 'index' parameter
|
| 23 |
+
const linkId = input.link;
|
| 24 |
+
if (linkId) {
|
| 25 |
+
const link = uiWorkflow.links.find((l: any) => l[0] === linkId);
|
| 26 |
+
if (link) {
|
| 27 |
+
// link format: [id, origin_id, origin_slot, target_id, target_slot, type]
|
| 28 |
+
inputs[input.name] = [link[1].toString(), link[2]];
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
});
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
// Map widgets to inputs
|
| 35 |
+
// This is a simplified mapping; standard nodes usually have widgets in a specific order
|
| 36 |
+
if (node.widgets_values && Array.isArray(node.widgets_values)) {
|
| 37 |
+
// Note: This mapping is brittle as it depends on node implementation,
|
| 38 |
+
// but for standard nodes it often works.
|
| 39 |
+
// We'll primarily rely on the explicit injection logic later.
|
| 40 |
+
node.widgets_values.forEach((val: any, idx: number) => {
|
| 41 |
+
// We don't strictly know the keys here without the node definition,
|
| 42 |
+
// but we store them to be safe.
|
| 43 |
+
inputs[`_widget_${idx}`] = val;
|
| 44 |
+
});
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
apiPrompt[nodeId] = {
|
| 48 |
+
class_type: node.type,
|
| 49 |
+
inputs: inputs,
|
| 50 |
+
_meta: { title: node.title || node.type }
|
| 51 |
+
};
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
return apiPrompt;
|
| 55 |
+
};
|
| 56 |
+
|
| 57 |
+
export const sendComfyPrompt = async (
|
| 58 |
+
serverUrl: string,
|
| 59 |
+
workflow: any,
|
| 60 |
+
promptText: string,
|
| 61 |
+
seed: number,
|
| 62 |
+
steps: number,
|
| 63 |
+
useSecureBridge: boolean = false,
|
| 64 |
+
signal?: AbortSignal
|
| 65 |
+
): Promise<string> => {
|
| 66 |
+
const baseUrl = serverUrl.replace(/\/+$/, '');
|
| 67 |
+
console.log(`[ComfyUI] Starting preview. Bridge: ${useSecureBridge}, Target: ${baseUrl}`);
|
| 68 |
+
|
| 69 |
+
// 1. Prepare Workflow
|
| 70 |
+
let apiPrompt: any = {};
|
| 71 |
+
const isUiFormat = workflow.nodes && Array.isArray(workflow.nodes);
|
| 72 |
+
|
| 73 |
+
if (isUiFormat) {
|
| 74 |
+
console.log("[ComfyUI] Standard UI format detected. Attempting internal mapping...");
|
| 75 |
+
// For standard UI format, we'll try to find the nodes by type and title
|
| 76 |
+
apiPrompt = JSON.parse(JSON.stringify(workflow)); // Work on a copy
|
| 77 |
+
|
| 78 |
+
let promptNode = apiPrompt.nodes.find((n: any) =>
|
| 79 |
+
n.type === 'CLIPTextEncode' &&
|
| 80 |
+
((n.title || "").toLowerCase().includes("positive") || !(n.title || "").toLowerCase().includes("negative"))
|
| 81 |
+
);
|
| 82 |
+
let samplerNode = apiPrompt.nodes.find((n: any) => n.type === 'KSampler' || n.type === 'KSamplerAdvanced');
|
| 83 |
+
|
| 84 |
+
if (promptNode) {
|
| 85 |
+
// In UI format, prompt is usually the first widget
|
| 86 |
+
if (promptNode.widgets_values) promptNode.widgets_values[0] = promptText;
|
| 87 |
+
}
|
| 88 |
+
if (samplerNode && samplerNode.widgets_values) {
|
| 89 |
+
if (seed !== -1) samplerNode.widgets_values[0] = seed;
|
| 90 |
+
if (steps !== -1) samplerNode.widgets_values[2] = steps;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// IMPORTANT: The /prompt endpoint REQUIRES API format.
|
| 94 |
+
// If we have UI format, we MUST convert it or it will fail.
|
| 95 |
+
apiPrompt = convertUiToApi(apiPrompt);
|
| 96 |
+
} else {
|
| 97 |
+
apiPrompt = JSON.parse(JSON.stringify(workflow));
|
| 98 |
+
// Identify nodes in API format
|
| 99 |
+
let promptNodeId = '';
|
| 100 |
+
let samplerNodeId = '';
|
| 101 |
+
for (const id in apiPrompt) {
|
| 102 |
+
const node = apiPrompt[id];
|
| 103 |
+
const type = node.class_type;
|
| 104 |
+
const title = (node._meta?.title || "").toLowerCase();
|
| 105 |
+
if (!promptNodeId && type === 'CLIPTextEncode' && (title.includes('positive') || !title.includes('negative'))) promptNodeId = id;
|
| 106 |
+
if (!samplerNodeId && (type === 'KSampler' || type === 'KSamplerAdvanced')) samplerNodeId = id;
|
| 107 |
+
}
|
| 108 |
+
if (promptNodeId) apiPrompt[promptNodeId].inputs.text = promptText;
|
| 109 |
+
if (samplerNodeId) {
|
| 110 |
+
if (seed !== -1) apiPrompt[samplerNodeId].inputs.seed = seed;
|
| 111 |
+
if (steps !== -1) apiPrompt[samplerNodeId].inputs.steps = steps;
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
// 2. Determine Endpoint
|
| 116 |
+
let fetchUrl = `${baseUrl}/prompt`;
|
| 117 |
+
let fetchHeaders: Record<string, string> = { 'Content-Type': 'application/json' };
|
| 118 |
+
|
| 119 |
+
if (useSecureBridge) {
|
| 120 |
+
fetchUrl = `${window.location.origin}/comfy-bridge/prompt`;
|
| 121 |
+
fetchHeaders['x-bridge-target'] = baseUrl;
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
// 3. Send Request
|
| 125 |
+
const response = await fetch(fetchUrl, {
|
| 126 |
+
method: 'POST',
|
| 127 |
+
headers: fetchHeaders,
|
| 128 |
+
body: JSON.stringify({ prompt: apiPrompt }),
|
| 129 |
+
signal
|
| 130 |
+
}).catch(err => {
|
| 131 |
+
if (err.name === 'AbortError') throw err;
|
| 132 |
+
throw new Error(`Connection failed: ${err.message}. Ensure your ComfyUI server or Bridge is reachable.`);
|
| 133 |
+
});
|
| 134 |
+
|
| 135 |
+
if (!response.ok) {
|
| 136 |
+
const errText = await response.text();
|
| 137 |
+
throw new Error(`Server Error (${response.status}): ${errText.substring(0, 100)}...`);
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
const { prompt_id } = await response.json();
|
| 141 |
+
|
| 142 |
+
// 4. Poll
|
| 143 |
+
const pollUrl = useSecureBridge ? `${window.location.origin}/comfy-bridge/history/${prompt_id}` : `${baseUrl}/history/${prompt_id}`;
|
| 144 |
+
const pollHeaders: HeadersInit = useSecureBridge ? { 'x-bridge-target': baseUrl } : {}; // Added explicit HeadersInit type
|
| 145 |
+
|
| 146 |
+
for (let i = 0; i < 60; i++) {
|
| 147 |
+
if (signal?.aborted) throw new Error("Aborted");
|
| 148 |
+
|
| 149 |
+
const hRes = await fetch(pollUrl, { headers: pollHeaders, signal });
|
| 150 |
+
if (hRes.ok) {
|
| 151 |
+
const history = await hRes.json();
|
| 152 |
+
if (history[prompt_id]) {
|
| 153 |
+
const outputs = history[prompt_id].outputs;
|
| 154 |
+
for (const nodeId in outputs) {
|
| 155 |
+
if (outputs[nodeId].images?.length > 0) {
|
| 156 |
+
const img = outputs[nodeId].images[0];
|
| 157 |
+
let finalUrl = useSecureBridge
|
| 158 |
+
? `${window.location.origin}/comfy-bridge/view?filename=${img.filename}&subfolder=${img.subfolder}&type=${img.type}&target_base=${encodeURIComponent(baseUrl)}`
|
| 159 |
+
: `${baseUrl}/view?filename=${img.filename}&subfolder=${img.subfolder}&type=${img.type}`;
|
| 160 |
+
return finalUrl;
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
await new Promise(r => setTimeout(r, 3000));
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
throw new Error("Preview generation timed out.");
|
| 169 |
+
};
|
services/geminiService.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { GoogleGenAI, GenerateContentResponse } from "@google/genai";
|
| 2 |
+
|
| 3 |
+
const withRetry = async <T>(
|
| 4 |
+
apiCall: () => Promise<T>,
|
| 5 |
+
maxRetries: number = 3,
|
| 6 |
+
initialDelay: number = 1000
|
| 7 |
+
): Promise<T> => {
|
| 8 |
+
let attempt = 0;
|
| 9 |
+
while (true) {
|
| 10 |
+
try {
|
| 11 |
+
return await apiCall();
|
| 12 |
+
} catch (error) {
|
| 13 |
+
attempt++;
|
| 14 |
+
if (
|
| 15 |
+
error instanceof Error &&
|
| 16 |
+
(error.message.includes("503") || error.message.toLowerCase().includes("overloaded")) &&
|
| 17 |
+
attempt < maxRetries
|
| 18 |
+
) {
|
| 19 |
+
const delay = initialDelay * Math.pow(2, attempt - 1) + Math.random() * 500;
|
| 20 |
+
console.warn(`Attempt ${attempt} failed. Retrying in ${delay.toFixed(0)}ms...`);
|
| 21 |
+
await new Promise(resolve => setTimeout(resolve, delay));
|
| 22 |
+
} else {
|
| 23 |
+
throw error;
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
const fileToGenerativePart = async (file: File) => {
|
| 30 |
+
const base64EncodedDataPromise = new Promise<string>((resolve) => {
|
| 31 |
+
const reader = new FileReader();
|
| 32 |
+
reader.onloadend = () => {
|
| 33 |
+
if (typeof reader.result === 'string') {
|
| 34 |
+
resolve(reader.result.split(',')[1]);
|
| 35 |
+
}
|
| 36 |
+
};
|
| 37 |
+
reader.readAsDataURL(file);
|
| 38 |
+
});
|
| 39 |
+
return {
|
| 40 |
+
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
|
| 41 |
+
};
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
const constructPrompt = (
|
| 45 |
+
triggerWord: string,
|
| 46 |
+
customInstructions?: string,
|
| 47 |
+
isCharacterTaggingEnabled?: boolean,
|
| 48 |
+
characterShowName?: string
|
| 49 |
+
): string => {
|
| 50 |
+
let basePrompt = `You are an expert captioner for AI model training data. Your task is to describe the provided image/video in detail for a style LoRA. Follow these rules strictly:
|
| 51 |
+
1. Start the caption with the trigger word: "${triggerWord}".
|
| 52 |
+
2. Describe EVERYTHING visible: characters, clothing, actions, background, objects, lighting, and camera angle.
|
| 53 |
+
3. Be objective and factual.
|
| 54 |
+
4. DO NOT mention art styles or generic animation terms like "anime" or "cartoon".
|
| 55 |
+
5. Write as a single, continuous paragraph.`;
|
| 56 |
+
|
| 57 |
+
if (isCharacterTaggingEnabled && characterShowName) {
|
| 58 |
+
basePrompt += `\n6. Identify characters from the show/series "${characterShowName}" and append tags at the end of the caption, separated by commas. The format for each tag must be "char_[charactername]" (e.g., ", char_simon, char_kamina"). If no characters are recognized, do not add tags.`;
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
if (customInstructions) {
|
| 62 |
+
return `${basePrompt}\n\nAdditional instructions: ${customInstructions}`;
|
| 63 |
+
}
|
| 64 |
+
return basePrompt;
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
export const generateCaption = async (
|
| 68 |
+
file: File,
|
| 69 |
+
triggerWord: string,
|
| 70 |
+
customInstructions?: string,
|
| 71 |
+
isCharacterTaggingEnabled?: boolean,
|
| 72 |
+
characterShowName?: string,
|
| 73 |
+
signal?: AbortSignal,
|
| 74 |
+
apiKeyOverride?: string,
|
| 75 |
+
model: string = 'gemini-3-pro-preview'
|
| 76 |
+
): Promise<string> => {
|
| 77 |
+
const apiKey = apiKeyOverride || process.env.API_KEY;
|
| 78 |
+
if (!apiKey) throw new Error("API Key is missing. Please enter your Gemini API key in the Global Settings.");
|
| 79 |
+
|
| 80 |
+
const ai = new GoogleGenAI({ apiKey });
|
| 81 |
+
const imagePart = await fileToGenerativePart(file);
|
| 82 |
+
const prompt = constructPrompt(triggerWord, customInstructions, isCharacterTaggingEnabled, characterShowName);
|
| 83 |
+
|
| 84 |
+
const apiCall = () => ai.models.generateContent({
|
| 85 |
+
model: model,
|
| 86 |
+
contents: { parts: [imagePart, { text: prompt }] },
|
| 87 |
+
config: { signal } as any
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
const response: GenerateContentResponse = await withRetry(apiCall);
|
| 91 |
+
|
| 92 |
+
if (signal?.aborted) throw new Error("AbortError");
|
| 93 |
+
|
| 94 |
+
if (response.text) {
|
| 95 |
+
return response.text.trim();
|
| 96 |
+
}
|
| 97 |
+
throw new Error("No caption text returned from Gemini.");
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
export const refineCaption = async (
|
| 101 |
+
file: File,
|
| 102 |
+
currentCaption: string,
|
| 103 |
+
refinementInstructions: string,
|
| 104 |
+
signal?: AbortSignal,
|
| 105 |
+
apiKeyOverride?: string,
|
| 106 |
+
model: string = 'gemini-3-pro-preview'
|
| 107 |
+
): Promise<string> => {
|
| 108 |
+
const apiKey = apiKeyOverride || process.env.API_KEY;
|
| 109 |
+
if (!apiKey) throw new Error("API Key is missing.");
|
| 110 |
+
|
| 111 |
+
const ai = new GoogleGenAI({ apiKey });
|
| 112 |
+
const imagePart = await fileToGenerativePart(file);
|
| 113 |
+
const prompt = `You are an expert editor for LoRA training data.
|
| 114 |
+
Refine the provided caption based on the visual information and the user's refinement instructions.
|
| 115 |
+
Maintain the continuous paragraph format and ensure the trigger word is preserved.
|
| 116 |
+
|
| 117 |
+
CURRENT CAPTION: "${currentCaption}"
|
| 118 |
+
REFINEMENT INSTRUCTIONS: "${refinementInstructions}"
|
| 119 |
+
|
| 120 |
+
Output only the refined caption.`;
|
| 121 |
+
|
| 122 |
+
const apiCall = () => ai.models.generateContent({
|
| 123 |
+
model: model,
|
| 124 |
+
contents: { parts: [imagePart, { text: prompt }] },
|
| 125 |
+
config: { signal } as any
|
| 126 |
+
});
|
| 127 |
+
|
| 128 |
+
const response: GenerateContentResponse = await withRetry(apiCall);
|
| 129 |
+
if (signal?.aborted) throw new Error("AbortError");
|
| 130 |
+
|
| 131 |
+
if (response.text) {
|
| 132 |
+
return response.text.trim();
|
| 133 |
+
}
|
| 134 |
+
throw new Error("No refined text returned.");
|
| 135 |
+
};
|
| 136 |
+
|
| 137 |
+
export const checkCaptionQuality = async (
|
| 138 |
+
file: File,
|
| 139 |
+
caption: string,
|
| 140 |
+
signal?: AbortSignal,
|
| 141 |
+
apiKeyOverride?: string,
|
| 142 |
+
model: string = 'gemini-3-pro-preview'
|
| 143 |
+
): Promise<number> => {
|
| 144 |
+
const apiKey = apiKeyOverride || process.env.API_KEY;
|
| 145 |
+
if (!apiKey) throw new Error("API Key is missing.");
|
| 146 |
+
|
| 147 |
+
const ai = new GoogleGenAI({ apiKey });
|
| 148 |
+
const imagePart = await fileToGenerativePart(file);
|
| 149 |
+
const prompt = `Evaluate the following caption for accuracy and detail based on the image. Respond with ONLY an integer from 1 to 5.\nCaption: "${caption}"`;
|
| 150 |
+
|
| 151 |
+
try {
|
| 152 |
+
const apiCall = () => ai.models.generateContent({
|
| 153 |
+
model: model,
|
| 154 |
+
contents: { parts: [imagePart, { text: prompt }] },
|
| 155 |
+
config: { signal } as any
|
| 156 |
+
});
|
| 157 |
+
const response: GenerateContentResponse = await withRetry(apiCall);
|
| 158 |
+
const scoreText = response.text?.trim() || '0';
|
| 159 |
+
const score = parseInt(scoreText.match(/\d+/)?.[0] || '0', 10);
|
| 160 |
+
return score;
|
| 161 |
+
} catch (error) {
|
| 162 |
+
console.error("Quality check failed:", error);
|
| 163 |
+
return 0;
|
| 164 |
+
}
|
| 165 |
+
};
|
services/grokService.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
/**
|
| 3 |
+
* Service for interacting with xAI Grok via OpenAI-compatible vision endpoints.
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
const fileToBase64 = (file: File): Promise<string> => {
|
| 7 |
+
return new Promise((resolve, reject) => {
|
| 8 |
+
const reader = new FileReader();
|
| 9 |
+
reader.readAsDataURL(file);
|
| 10 |
+
reader.onload = () => {
|
| 11 |
+
if (typeof reader.result === 'string') {
|
| 12 |
+
resolve(reader.result);
|
| 13 |
+
} else {
|
| 14 |
+
reject(new Error('Failed to convert file to base64'));
|
| 15 |
+
}
|
| 16 |
+
};
|
| 17 |
+
reader.onerror = error => reject(error);
|
| 18 |
+
});
|
| 19 |
+
};
|
| 20 |
+
|
| 21 |
+
const extractFramesFromVideo = async (videoFile: File, numberOfFrames: number): Promise<string[]> => {
|
| 22 |
+
return new Promise((resolve, reject) => {
|
| 23 |
+
const video = document.createElement('video');
|
| 24 |
+
video.preload = 'metadata';
|
| 25 |
+
video.muted = true;
|
| 26 |
+
video.playsInline = true;
|
| 27 |
+
const url = URL.createObjectURL(videoFile);
|
| 28 |
+
const frames: string[] = [];
|
| 29 |
+
const timeout = setTimeout(() => {
|
| 30 |
+
URL.revokeObjectURL(url);
|
| 31 |
+
video.src = "";
|
| 32 |
+
reject(new Error("Video processing timed out"));
|
| 33 |
+
}, 60000);
|
| 34 |
+
|
| 35 |
+
video.onloadeddata = async () => {
|
| 36 |
+
const duration = video.duration;
|
| 37 |
+
const canvas = document.createElement('canvas');
|
| 38 |
+
const ctx = canvas.getContext('2d');
|
| 39 |
+
if (!ctx) {
|
| 40 |
+
clearTimeout(timeout);
|
| 41 |
+
URL.revokeObjectURL(url);
|
| 42 |
+
reject(new Error("Could not create canvas context"));
|
| 43 |
+
return;
|
| 44 |
+
}
|
| 45 |
+
canvas.width = video.videoWidth;
|
| 46 |
+
canvas.height = video.videoHeight;
|
| 47 |
+
const step = duration / numberOfFrames;
|
| 48 |
+
try {
|
| 49 |
+
for (let i = 0; i < numberOfFrames; i++) {
|
| 50 |
+
const time = (step * i) + (step / 2);
|
| 51 |
+
await new Promise<void>((frameResolve) => {
|
| 52 |
+
const onSeeked = () => {
|
| 53 |
+
video.removeEventListener('seeked', onSeeked);
|
| 54 |
+
frameResolve();
|
| 55 |
+
};
|
| 56 |
+
video.addEventListener('seeked', onSeeked);
|
| 57 |
+
video.currentTime = Math.min(time, duration - 0.1);
|
| 58 |
+
});
|
| 59 |
+
ctx.drawImage(video, 0, 0);
|
| 60 |
+
frames.push(canvas.toDataURL('image/jpeg', 0.8));
|
| 61 |
+
}
|
| 62 |
+
clearTimeout(timeout);
|
| 63 |
+
URL.revokeObjectURL(url);
|
| 64 |
+
video.src = "";
|
| 65 |
+
resolve(frames);
|
| 66 |
+
} catch (e) {
|
| 67 |
+
clearTimeout(timeout);
|
| 68 |
+
URL.revokeObjectURL(url);
|
| 69 |
+
reject(e);
|
| 70 |
+
}
|
| 71 |
+
};
|
| 72 |
+
video.onerror = () => {
|
| 73 |
+
clearTimeout(timeout);
|
| 74 |
+
URL.revokeObjectURL(url);
|
| 75 |
+
reject(new Error("Failed to load video file"));
|
| 76 |
+
};
|
| 77 |
+
video.src = url;
|
| 78 |
+
});
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
const constructPrompt = (
|
| 82 |
+
triggerWord: string,
|
| 83 |
+
customInstructions?: string,
|
| 84 |
+
isCharacterTaggingEnabled?: boolean,
|
| 85 |
+
characterShowName?: string
|
| 86 |
+
): string => {
|
| 87 |
+
let basePrompt = `You are an expert captioner for AI model training data. Your task is to describe the provided image/video in detail for a style LoRA. Follow these rules strictly:
|
| 88 |
+
1. Start the caption with the trigger word: "${triggerWord}".
|
| 89 |
+
2. Describe EVERYTHING visible: characters, clothing, actions, background, objects, lighting, and camera angle.
|
| 90 |
+
3. Be objective and factual.
|
| 91 |
+
4. DO NOT mention the art style, "anime", "cartoon", "illustration", "2d", or "animation".
|
| 92 |
+
5. Write the description as a single, continuous paragraph.`;
|
| 93 |
+
|
| 94 |
+
if (isCharacterTaggingEnabled && characterShowName && characterShowName.trim() !== '') {
|
| 95 |
+
basePrompt += `\n6. After the description, identify any characters from the show "${characterShowName}" and append their tags to the very end of the caption, separated by commas. The format for each tag must be "char_[charactername]" (e.g., ", char_simon, char_kamina"). If no characters are recognized, add no tags.`;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (customInstructions) {
|
| 99 |
+
return `${basePrompt}\n\nIMPORTANT USER INSTRUCTIONS:\n${customInstructions}`;
|
| 100 |
+
}
|
| 101 |
+
return basePrompt;
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
+
export const generateCaptionGrok = async (
|
| 105 |
+
apiKey: string,
|
| 106 |
+
model: string,
|
| 107 |
+
file: File,
|
| 108 |
+
triggerWord: string,
|
| 109 |
+
customInstructions?: string,
|
| 110 |
+
isCharacterTaggingEnabled?: boolean,
|
| 111 |
+
characterShowName?: string,
|
| 112 |
+
videoFrameCount: number = 8,
|
| 113 |
+
signal?: AbortSignal
|
| 114 |
+
): Promise<string> => {
|
| 115 |
+
if (!apiKey) throw new Error("xAI API Key is required for Grok.");
|
| 116 |
+
const endpoint = 'https://api.x.ai/v1/chat/completions';
|
| 117 |
+
const prompt = constructPrompt(triggerWord, customInstructions, isCharacterTaggingEnabled, characterShowName);
|
| 118 |
+
|
| 119 |
+
let contentParts: any[] = [{ type: "text", text: prompt }];
|
| 120 |
+
if (file.type.startsWith('video/')) {
|
| 121 |
+
const frames = await extractFramesFromVideo(file, videoFrameCount);
|
| 122 |
+
frames.forEach(frame => contentParts.push({ type: "image_url", image_url: { url: frame } }));
|
| 123 |
+
} else {
|
| 124 |
+
const base64Image = await fileToBase64(file);
|
| 125 |
+
contentParts.push({ type: "image_url", image_url: { url: base64Image } });
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
const payload = {
|
| 129 |
+
model: model || 'grok-2-vision-1212',
|
| 130 |
+
messages: [{ role: "user", content: contentParts }],
|
| 131 |
+
max_tokens: 1000,
|
| 132 |
+
temperature: 0.2
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
const response = await fetch(endpoint, {
|
| 136 |
+
method: "POST",
|
| 137 |
+
headers: {
|
| 138 |
+
"Content-Type": "application/json",
|
| 139 |
+
"Authorization": `Bearer ${apiKey}`
|
| 140 |
+
},
|
| 141 |
+
body: JSON.stringify(payload),
|
| 142 |
+
signal
|
| 143 |
+
});
|
| 144 |
+
|
| 145 |
+
if (!response.ok) {
|
| 146 |
+
const errData = await response.json().catch(() => ({}));
|
| 147 |
+
throw new Error(`Grok API Error (${response.status}): ${errData.error?.message || response.statusText}`);
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
const data = await response.json();
|
| 151 |
+
return data.choices?.[0]?.message?.content?.trim() || "";
|
| 152 |
+
};
|
| 153 |
+
|
| 154 |
+
export const refineCaptionGrok = async (
|
| 155 |
+
apiKey: string,
|
| 156 |
+
model: string,
|
| 157 |
+
file: File,
|
| 158 |
+
currentCaption: string,
|
| 159 |
+
refinementInstructions: string,
|
| 160 |
+
videoFrameCount: number = 4,
|
| 161 |
+
signal?: AbortSignal
|
| 162 |
+
): Promise<string> => {
|
| 163 |
+
if (!apiKey) throw new Error("xAI API Key is required for Grok.");
|
| 164 |
+
const endpoint = 'https://api.x.ai/v1/chat/completions';
|
| 165 |
+
const prompt = `Refine the following caption based on the visual information and the instructions. Output ONLY the refined text.
|
| 166 |
+
CURRENT CAPTION: "${currentCaption}"
|
| 167 |
+
INSTRUCTIONS: "${refinementInstructions}"`;
|
| 168 |
+
|
| 169 |
+
let contentParts: any[] = [{ type: "text", text: prompt }];
|
| 170 |
+
if (file.type.startsWith('video/')) {
|
| 171 |
+
const frames = await extractFramesFromVideo(file, videoFrameCount);
|
| 172 |
+
frames.forEach(frame => contentParts.push({ type: "image_url", image_url: { url: frame } }));
|
| 173 |
+
} else {
|
| 174 |
+
const base64Image = await fileToBase64(file);
|
| 175 |
+
contentParts.push({ type: "image_url", image_url: { url: base64Image } });
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
const payload = {
|
| 179 |
+
model: model || 'grok-2-vision-1212',
|
| 180 |
+
messages: [{ role: "user", content: contentParts }],
|
| 181 |
+
max_tokens: 1000,
|
| 182 |
+
temperature: 0.2
|
| 183 |
+
};
|
| 184 |
+
|
| 185 |
+
const response = await fetch(endpoint, {
|
| 186 |
+
method: "POST",
|
| 187 |
+
headers: {
|
| 188 |
+
"Content-Type": "application/json",
|
| 189 |
+
"Authorization": `Bearer ${apiKey}`
|
| 190 |
+
},
|
| 191 |
+
body: JSON.stringify(payload),
|
| 192 |
+
signal
|
| 193 |
+
});
|
| 194 |
+
|
| 195 |
+
if (!response.ok) throw new Error(`Grok API Error: ${response.status}`);
|
| 196 |
+
const data = await response.json();
|
| 197 |
+
return data.choices?.[0]?.message?.content?.trim() || "";
|
| 198 |
+
};
|
| 199 |
+
|
| 200 |
+
export const checkQualityGrok = async (
|
| 201 |
+
apiKey: string,
|
| 202 |
+
model: string,
|
| 203 |
+
file: File,
|
| 204 |
+
caption: string,
|
| 205 |
+
videoFrameCount: number = 4,
|
| 206 |
+
signal?: AbortSignal
|
| 207 |
+
): Promise<number> => {
|
| 208 |
+
if (!apiKey) throw new Error("xAI API Key is required for Grok.");
|
| 209 |
+
const endpoint = 'https://api.x.ai/v1/chat/completions';
|
| 210 |
+
const prompt = `Evaluate the caption quality. Respond with ONLY an integer from 1 to 5.\nCaption: "${caption}"`;
|
| 211 |
+
|
| 212 |
+
let contentParts: any[] = [{ type: "text", text: prompt }];
|
| 213 |
+
if (file.type.startsWith('video/')) {
|
| 214 |
+
const frames = await extractFramesFromVideo(file, videoFrameCount);
|
| 215 |
+
frames.forEach(frame => contentParts.push({ type: "image_url", image_url: { url: frame } }));
|
| 216 |
+
} else {
|
| 217 |
+
const base64Image = await fileToBase64(file);
|
| 218 |
+
contentParts.push({ type: "image_url", image_url: { url: base64Image } });
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
const payload = {
|
| 222 |
+
model: model || 'grok-2-vision-1212',
|
| 223 |
+
messages: [{ role: "user", content: contentParts }],
|
| 224 |
+
max_tokens: 10,
|
| 225 |
+
temperature: 0.1
|
| 226 |
+
};
|
| 227 |
+
|
| 228 |
+
const response = await fetch(endpoint, {
|
| 229 |
+
method: "POST",
|
| 230 |
+
headers: {
|
| 231 |
+
"Content-Type": "application/json",
|
| 232 |
+
"Authorization": `Bearer ${apiKey}`
|
| 233 |
+
},
|
| 234 |
+
body: JSON.stringify(payload),
|
| 235 |
+
signal
|
| 236 |
+
});
|
| 237 |
+
|
| 238 |
+
if (!response.ok) throw new Error(`Grok API Error: ${response.status}`);
|
| 239 |
+
const data = await response.json();
|
| 240 |
+
const text = data.choices?.[0]?.message?.content?.trim();
|
| 241 |
+
return parseInt(text?.match(/\d+/)?.[0] || '0', 10);
|
| 242 |
+
};
|
services/qwenService.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Service for interacting with Qwen-VL via OpenAI-compatible endpoints.
|
| 3 |
+
* This supports OpenRouter, Ollama, vLLM, etc.
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
const fileToBase64 = (file: File): Promise<string> => {
|
| 7 |
+
return new Promise((resolve, reject) => {
|
| 8 |
+
const reader = new FileReader();
|
| 9 |
+
reader.readAsDataURL(file);
|
| 10 |
+
reader.onload = () => {
|
| 11 |
+
if (typeof reader.result === 'string') {
|
| 12 |
+
resolve(reader.result); // This includes the data:image/png;base64, prefix
|
| 13 |
+
} else {
|
| 14 |
+
reject(new Error('Failed to convert file to base64'));
|
| 15 |
+
}
|
| 16 |
+
};
|
| 17 |
+
reader.onerror = error => reject(error);
|
| 18 |
+
});
|
| 19 |
+
};
|
| 20 |
+
|
| 21 |
+
const extractFramesFromVideo = async (videoFile: File, numberOfFrames: number): Promise<string[]> => {
|
| 22 |
+
return new Promise((resolve, reject) => {
|
| 23 |
+
const video = document.createElement('video');
|
| 24 |
+
video.preload = 'metadata';
|
| 25 |
+
video.muted = true;
|
| 26 |
+
video.playsInline = true;
|
| 27 |
+
const url = URL.createObjectURL(videoFile);
|
| 28 |
+
const frames: string[] = [];
|
| 29 |
+
const timeout = setTimeout(() => {
|
| 30 |
+
URL.revokeObjectURL(url);
|
| 31 |
+
video.src = "";
|
| 32 |
+
reject(new Error("Video processing timed out"));
|
| 33 |
+
}, 60000);
|
| 34 |
+
|
| 35 |
+
video.onloadeddata = async () => {
|
| 36 |
+
const duration = video.duration;
|
| 37 |
+
const canvas = document.createElement('canvas');
|
| 38 |
+
const ctx = canvas.getContext('2d');
|
| 39 |
+
if (!ctx) {
|
| 40 |
+
clearTimeout(timeout);
|
| 41 |
+
URL.revokeObjectURL(url);
|
| 42 |
+
reject(new Error("Could not create canvas context"));
|
| 43 |
+
return;
|
| 44 |
+
}
|
| 45 |
+
canvas.width = video.videoWidth;
|
| 46 |
+
canvas.height = video.videoHeight;
|
| 47 |
+
const step = duration / numberOfFrames;
|
| 48 |
+
try {
|
| 49 |
+
for (let i = 0; i < numberOfFrames; i++) {
|
| 50 |
+
const time = (step * i) + (step / 2);
|
| 51 |
+
await new Promise<void>((frameResolve) => {
|
| 52 |
+
const onSeeked = () => {
|
| 53 |
+
video.removeEventListener('seeked', onSeeked);
|
| 54 |
+
frameResolve();
|
| 55 |
+
};
|
| 56 |
+
video.addEventListener('seeked', onSeeked);
|
| 57 |
+
video.currentTime = time;
|
| 58 |
+
});
|
| 59 |
+
ctx.drawImage(video, 0, 0);
|
| 60 |
+
frames.push(canvas.toDataURL('image/jpeg', 0.8));
|
| 61 |
+
}
|
| 62 |
+
clearTimeout(timeout);
|
| 63 |
+
URL.revokeObjectURL(url);
|
| 64 |
+
video.src = "";
|
| 65 |
+
resolve(frames);
|
| 66 |
+
} catch (e) {
|
| 67 |
+
clearTimeout(timeout);
|
| 68 |
+
URL.revokeObjectURL(url);
|
| 69 |
+
reject(e);
|
| 70 |
+
}
|
| 71 |
+
};
|
| 72 |
+
video.onerror = () => {
|
| 73 |
+
clearTimeout(timeout);
|
| 74 |
+
URL.revokeObjectURL(url);
|
| 75 |
+
reject(new Error("Failed to load video file"));
|
| 76 |
+
};
|
| 77 |
+
video.src = url;
|
| 78 |
+
});
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
const constructPrompt = (
|
| 82 |
+
triggerWord: string,
|
| 83 |
+
customInstructions?: string,
|
| 84 |
+
isCharacterTaggingEnabled?: boolean,
|
| 85 |
+
characterShowName?: string
|
| 86 |
+
): string => {
|
| 87 |
+
let basePrompt = `You are an expert captioner for AI model training data. Your task is to describe the provided image/video in detail for a style LoRA. Follow these rules strictly:
|
| 88 |
+
1. Start the caption with the trigger word: "${triggerWord}".
|
| 89 |
+
2. Describe EVERYTHING visible: characters, clothing, actions, background, objects, lighting, and camera angle.
|
| 90 |
+
3. Be objective and factual.
|
| 91 |
+
4. DO NOT mention the art style, "anime", "cartoon", "illustration", "2d", or "animation".
|
| 92 |
+
5. Write the description as a single, continuous paragraph.`;
|
| 93 |
+
|
| 94 |
+
if (isCharacterTaggingEnabled && characterShowName && characterShowName.trim() !== '') {
|
| 95 |
+
basePrompt += `\n6. After the description, identify any characters from the show "${characterShowName}" and append their tags to the very end of the caption, separated by commas. The format for each tag must be "char_[charactername]" (e.g., ", char_simon, char_kamina"). If no characters are recognized, add no tags.`;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (customInstructions) {
|
| 99 |
+
return `${basePrompt}\n\nIMPORTANT USER INSTRUCTIONS:\n${customInstructions}`;
|
| 100 |
+
}
|
| 101 |
+
return basePrompt;
|
| 102 |
+
};
|
| 103 |
+
|
| 104 |
+
export const generateCaptionQwen = async (
|
| 105 |
+
apiKey: string,
|
| 106 |
+
baseUrl: string,
|
| 107 |
+
model: string,
|
| 108 |
+
file: File,
|
| 109 |
+
triggerWord: string,
|
| 110 |
+
customInstructions?: string,
|
| 111 |
+
isCharacterTaggingEnabled?: boolean,
|
| 112 |
+
characterShowName?: string,
|
| 113 |
+
videoFrameCount: number = 8,
|
| 114 |
+
signal?: AbortSignal
|
| 115 |
+
): Promise<string> => {
|
| 116 |
+
if (!baseUrl) throw new Error("Local Endpoint URL is required for Qwen.");
|
| 117 |
+
let endpoint = baseUrl;
|
| 118 |
+
if (!endpoint.includes('/chat/completions')) {
|
| 119 |
+
endpoint = endpoint.replace(/\/+$/, '') + '/chat/completions';
|
| 120 |
+
}
|
| 121 |
+
const prompt = constructPrompt(triggerWord, customInstructions, isCharacterTaggingEnabled, characterShowName);
|
| 122 |
+
let contentParts: any[] = [{ type: "text", text: prompt }];
|
| 123 |
+
if (file.type.startsWith('video/')) {
|
| 124 |
+
const frames = await extractFramesFromVideo(file, videoFrameCount);
|
| 125 |
+
frames.forEach(frame => contentParts.push({ type: "image_url", image_url: { url: frame } }));
|
| 126 |
+
} else {
|
| 127 |
+
const base64Image = await fileToBase64(file);
|
| 128 |
+
contentParts.push({ type: "image_url", image_url: { url: base64Image } });
|
| 129 |
+
}
|
| 130 |
+
const payload = {
|
| 131 |
+
model: model || 'Qwen/Qwen2.5-VL-7B-Instruct',
|
| 132 |
+
messages: [{ role: "user", content: contentParts }],
|
| 133 |
+
max_tokens: 1000,
|
| 134 |
+
temperature: 0.2
|
| 135 |
+
};
|
| 136 |
+
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
| 137 |
+
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
| 138 |
+
const response = await fetch(endpoint, { method: "POST", headers, body: JSON.stringify(payload), signal });
|
| 139 |
+
if (!response.ok) throw new Error(`API Error: ${response.status}`);
|
| 140 |
+
const data = await response.json();
|
| 141 |
+
return data.choices?.[0]?.message?.content?.trim() || "";
|
| 142 |
+
};
|
| 143 |
+
|
| 144 |
+
export const refineCaptionQwen = async (
|
| 145 |
+
apiKey: string,
|
| 146 |
+
baseUrl: string,
|
| 147 |
+
model: string,
|
| 148 |
+
file: File,
|
| 149 |
+
currentCaption: string,
|
| 150 |
+
refinementInstructions: string,
|
| 151 |
+
videoFrameCount: number = 8,
|
| 152 |
+
signal?: AbortSignal
|
| 153 |
+
): Promise<string> => {
|
| 154 |
+
let endpoint = baseUrl;
|
| 155 |
+
if (!endpoint.includes('/chat/completions')) endpoint = endpoint.replace(/\/+$/, '') + '/chat/completions';
|
| 156 |
+
const prompt = `Refine the following caption based on the visual information and the instructions. Output ONLY the refined text.
|
| 157 |
+
CURRENT CAPTION: "${currentCaption}"
|
| 158 |
+
INSTRUCTIONS: "${refinementInstructions}"`;
|
| 159 |
+
let contentParts: any[] = [{ type: "text", text: prompt }];
|
| 160 |
+
if (file.type.startsWith('video/')) {
|
| 161 |
+
const frames = await extractFramesFromVideo(file, 4);
|
| 162 |
+
frames.forEach(frame => contentParts.push({ type: "image_url", image_url: { url: frame } }));
|
| 163 |
+
} else {
|
| 164 |
+
const base64Image = await fileToBase64(file);
|
| 165 |
+
contentParts.push({ type: "image_url", image_url: { url: base64Image } });
|
| 166 |
+
}
|
| 167 |
+
const payload = {
|
| 168 |
+
model: model || 'Qwen/Qwen2.5-VL-7B-Instruct',
|
| 169 |
+
messages: [{ role: "user", content: contentParts }],
|
| 170 |
+
max_tokens: 1000,
|
| 171 |
+
temperature: 0.2
|
| 172 |
+
};
|
| 173 |
+
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
| 174 |
+
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
| 175 |
+
const response = await fetch(endpoint, { method: "POST", headers, body: JSON.stringify(payload), signal });
|
| 176 |
+
const data = await response.json();
|
| 177 |
+
return data.choices?.[0]?.message?.content?.trim() || "";
|
| 178 |
+
};
|
| 179 |
+
|
| 180 |
+
export const checkQualityQwen = async (
|
| 181 |
+
apiKey: string,
|
| 182 |
+
baseUrl: string,
|
| 183 |
+
model: string,
|
| 184 |
+
file: File,
|
| 185 |
+
caption: string,
|
| 186 |
+
videoFrameCount: number = 8,
|
| 187 |
+
signal?: AbortSignal
|
| 188 |
+
): Promise<number> => {
|
| 189 |
+
let endpoint = baseUrl;
|
| 190 |
+
if (!endpoint.includes('/chat/completions')) endpoint = endpoint.replace(/\/+$/, '') + '/chat/completions';
|
| 191 |
+
const prompt = `Evaluate the caption quality. Respond with ONLY an integer from 1 to 5.\nCaption: "${caption}"`;
|
| 192 |
+
let contentParts: any[] = [{ type: "text", text: prompt }];
|
| 193 |
+
if (file.type.startsWith('video/')) {
|
| 194 |
+
const frames = await extractFramesFromVideo(file, 4);
|
| 195 |
+
frames.forEach(frame => contentParts.push({ type: "image_url", image_url: { url: frame } }));
|
| 196 |
+
} else {
|
| 197 |
+
const base64Image = await fileToBase64(file);
|
| 198 |
+
contentParts.push({ type: "image_url", image_url: { url: base64Image } });
|
| 199 |
+
}
|
| 200 |
+
const payload = {
|
| 201 |
+
model: model || 'Qwen/Qwen2.5-VL-7B-Instruct',
|
| 202 |
+
messages: [{ role: "user", content: contentParts }],
|
| 203 |
+
max_tokens: 10,
|
| 204 |
+
temperature: 0.1
|
| 205 |
+
};
|
| 206 |
+
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
| 207 |
+
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
| 208 |
+
const response = await fetch(endpoint, { method: "POST", headers, body: JSON.stringify(payload), signal });
|
| 209 |
+
const data = await response.json();
|
| 210 |
+
const text = data.choices?.[0]?.message?.content?.trim();
|
| 211 |
+
return parseInt(text?.match(/\d+/)?.[0] || '0', 10);
|
| 212 |
+
};
|
tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"compilerOptions": {
|
| 3 |
+
"target": "ES2020",
|
| 4 |
+
"useDefineForClassFields": true,
|
| 5 |
+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
| 6 |
+
"module": "ESNext",
|
| 7 |
+
"skipLibCheck": true,
|
| 8 |
+
"moduleResolution": "bundler",
|
| 9 |
+
"allowImportingTsExtensions": true,
|
| 10 |
+
"resolveJsonModule": true,
|
| 11 |
+
"isolatedModules": true,
|
| 12 |
+
"noEmit": true,
|
| 13 |
+
"jsx": "react-jsx",
|
| 14 |
+
"strict": true,
|
| 15 |
+
"noUnusedLocals": true,
|
| 16 |
+
"noUnusedParameters": true,
|
| 17 |
+
"noFallthroughCasesInSwitch": true,
|
| 18 |
+
"types": ["vite/client", "node"]
|
| 19 |
+
},
|
| 20 |
+
"include": ["**/*.ts", "**/*.tsx"]
|
| 21 |
+
}
|
types.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
export enum GenerationStatus {
|
| 3 |
+
IDLE = 'idle',
|
| 4 |
+
GENERATING = 'generating',
|
| 5 |
+
CHECKING = 'checking',
|
| 6 |
+
SUCCESS = 'success',
|
| 7 |
+
ERROR = 'error',
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
export interface MediaFile {
|
| 11 |
+
id: string;
|
| 12 |
+
file: File;
|
| 13 |
+
previewUrl: string;
|
| 14 |
+
caption: string;
|
| 15 |
+
status: GenerationStatus;
|
| 16 |
+
errorMessage?: string;
|
| 17 |
+
isSelected: boolean;
|
| 18 |
+
customInstructions: string;
|
| 19 |
+
qualityScore?: number;
|
| 20 |
+
// ComfyUI Preview fields
|
| 21 |
+
comfyPreviewUrl?: string;
|
| 22 |
+
comfyStatus?: 'idle' | 'generating' | 'success' | 'error';
|
| 23 |
+
comfyErrorMessage?: string;
|
| 24 |
+
}
|
vite.config.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { defineConfig, loadEnv } from 'vite';
|
| 2 |
+
import react from '@vitejs/plugin-react';
|
| 3 |
+
|
| 4 |
+
export default defineConfig(({ mode }) => {
|
| 5 |
+
// Load env file based on `mode` in the current working directory.
|
| 6 |
+
const env = loadEnv(mode, '.', '');
|
| 7 |
+
|
| 8 |
+
return {
|
| 9 |
+
plugins: [react()],
|
| 10 |
+
define: {
|
| 11 |
+
// Expose the API_KEY from the environment to the client-side code.
|
| 12 |
+
'process.env.API_KEY': JSON.stringify(env.API_KEY),
|
| 13 |
+
'process.env': {}
|
| 14 |
+
},
|
| 15 |
+
server: {
|
| 16 |
+
port: 7860,
|
| 17 |
+
host: true,
|
| 18 |
+
proxy: {
|
| 19 |
+
// Secure Bridge Proxy Implementation
|
| 20 |
+
'/comfy-bridge': {
|
| 21 |
+
target: 'http://localhost:8188', // Fallback target
|
| 22 |
+
changeOrigin: true,
|
| 23 |
+
secure: false,
|
| 24 |
+
ws: true,
|
| 25 |
+
router: (req: any) => { // Added explicit 'any' type
|
| 26 |
+
// Dynamically determine target from header or query param
|
| 27 |
+
const target = req.headers['x-bridge-target'] as string;
|
| 28 |
+
if (target) return target;
|
| 29 |
+
|
| 30 |
+
// For GET requests like images, target might be in query
|
| 31 |
+
const url = new URL(req.url!, 'http://localhost');
|
| 32 |
+
const queryTarget = url.searchParams.get('target_base');
|
| 33 |
+
if (queryTarget) return queryTarget;
|
| 34 |
+
|
| 35 |
+
return 'http://localhost:8188';
|
| 36 |
+
},
|
| 37 |
+
rewrite: (path: string) => path.replace(/^\/comfy-bridge/, ''),
|
| 38 |
+
onProxyReq: (proxyReq: any) => { // Fixed implicit any and removed unused params
|
| 39 |
+
// CRITICAL: Strip security headers that cause 403 on ComfyUI
|
| 40 |
+
proxyReq.removeHeader('origin');
|
| 41 |
+
proxyReq.removeHeader('referer');
|
| 42 |
+
proxyReq.removeHeader('x-bridge-target'); // Clean up internal header
|
| 43 |
+
|
| 44 |
+
// Also clean up query params used for routing
|
| 45 |
+
if (proxyReq.path.includes('target_base=')) {
|
| 46 |
+
proxyReq.path = proxyReq.path.replace(/[&?]target_base=[^&]*/, '');
|
| 47 |
+
if (proxyReq.path.endsWith('?') || proxyReq.path.endsWith('&')) {
|
| 48 |
+
proxyReq.path = proxyReq.path.slice(0, -1);
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
},
|
| 52 |
+
onProxyRes: (proxyRes: any) => { // Fixed implicit any and removed unused params
|
| 53 |
+
// Ensure CORS is handled by the proxy
|
| 54 |
+
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
},
|
| 59 |
+
preview: {
|
| 60 |
+
port: 7860,
|
| 61 |
+
host: true,
|
| 62 |
+
allowedHosts: true,
|
| 63 |
+
proxy: {
|
| 64 |
+
// Implementation duplicated for preview mode (HF Spaces production)
|
| 65 |
+
'/comfy-bridge': {
|
| 66 |
+
target: 'http://localhost:8188',
|
| 67 |
+
changeOrigin: true,
|
| 68 |
+
secure: false,
|
| 69 |
+
ws: true,
|
| 70 |
+
router: (req: any) => { // Added explicit 'any' type
|
| 71 |
+
const target = req.headers['x-bridge-target'] as string;
|
| 72 |
+
if (target) return target;
|
| 73 |
+
const url = new URL(req.url!, 'http://localhost');
|
| 74 |
+
const queryTarget = url.searchParams.get('target_base');
|
| 75 |
+
return queryTarget || 'http://localhost:8188';
|
| 76 |
+
},
|
| 77 |
+
rewrite: (path: string) => path.replace(/^\/comfy-bridge/, ''),
|
| 78 |
+
onProxyReq: (proxyReq: any) => { // Fixed implicit any
|
| 79 |
+
proxyReq.removeHeader('origin');
|
| 80 |
+
proxyReq.removeHeader('referer');
|
| 81 |
+
proxyReq.removeHeader('x-bridge-target');
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
};
|
| 87 |
+
});
|