-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
485 lines (403 loc) · 17.7 KB
/
Copy pathmain.js
File metadata and controls
485 lines (403 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { GoogleGenAI } from '@google/genai';
class PromptGenerator {
constructor() {
this.ai = null;
this.initializeElements();
this.bindEvents();
}
initializeElements() {
// Input elements
this.apiKeyInput = document.getElementById('apiKey');
this.promptInput = document.getElementById('prompt');
this.artStyleSelect = document.getElementById('artStyle');
this.detailLevelSelect = document.getElementById('detailLevel');
this.aspectRatioSelect = document.getElementById('aspectRatio');
this.numberOfPromptsSelect = document.getElementById('numberOfPrompts');
this.negativePromptInput = document.getElementById('negativePrompt');
this.guidanceScaleInput = document.getElementById('guidanceScale');
this.guidanceValueSpan = document.getElementById('guidanceValue');
this.seedInput = document.getElementById('seed');
// Control elements
this.generateBtn = document.getElementById('generateBtn');
this.btnText = document.querySelector('.btn-text');
this.btnLoading = document.querySelector('.btn-loading');
// Output elements
this.errorMessage = document.getElementById('errorMessage');
this.loadingMessage = document.getElementById('loadingMessage');
this.promptResults = document.getElementById('promptResults');
}
bindEvents() {
this.generateBtn.addEventListener('click', () => this.generatePrompts());
this.guidanceScaleInput.addEventListener('input', (e) => {
this.guidanceValueSpan.textContent = e.target.value;
});
// Enter key support for prompt
this.promptInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && e.ctrlKey) {
this.generatePrompts();
}
});
// Load saved API key
this.loadSavedApiKey();
this.apiKeyInput.addEventListener('change', () => this.saveApiKey());
}
loadSavedApiKey() {
const savedApiKey = localStorage.getItem('gemini_api_key');
if (savedApiKey) {
this.apiKeyInput.value = savedApiKey;
}
}
saveApiKey() {
const apiKey = this.apiKeyInput.value.trim();
if (apiKey) {
localStorage.setItem('gemini_api_key', apiKey);
}
}
validateInputs() {
const apiKey = this.apiKeyInput.value.trim();
const prompt = this.promptInput.value.trim();
if (!apiKey) {
throw new Error('Vui lòng nhập API key Gemini');
}
if (!prompt) {
throw new Error('Vui lòng nhập ý tưởng hình ảnh');
}
if (prompt.length < 5) {
throw new Error('Ý tưởng quá ngắn. Vui lòng mô tả chi tiết hơn');
}
return { apiKey, prompt };
}
getPromptGenerationConfig() {
const artStyle = this.artStyleSelect.value;
const detailLevel = this.detailLevelSelect.value;
const aspectRatio = this.aspectRatioSelect.value;
const numberOfPrompts = parseInt(this.numberOfPromptsSelect.value);
return {
artStyle,
detailLevel,
aspectRatio,
numberOfPrompts
};
}
buildSystemPrompt() {
return `Bạn là một chuyên gia tạo prompt cho AI Art (Stable Diffusion, Midjourney, DALL-E).
Nhiệm vụ của bạn là chuyển đổi ý tưởng đơn giản thành các prompt chi tiết, chuyên nghiệp.
Quy tắc tạo prompt:
1. Bắt đầu với chủ thể chính
2. Thêm chi tiết về hành động, cảm xúc
3. Mô tả môi trường, bối cảnh
4. Chỉ định phong cách nghệ thuật
5. Thêm thông số kỹ thuật (lighting, composition, quality)
6. Kết thúc với negative prompt (những gì cần tránh)
Định dạng trả về:
**Prompt [số]:**
[Mô tả chi tiết bằng tiếng Anh]
**Mô tả tiếng Việt:**
[Giải thích prompt bằng tiếng Việt để người dùng hiểu]
**Negative Prompt:**
[Những gì cần tránh bằng tiếng Anh]
---`;
}
showError(message) {
this.errorMessage.textContent = message;
this.errorMessage.style.display = 'block';
this.loadingMessage.style.display = 'none';
}
hideError() {
this.errorMessage.style.display = 'none';
}
showLoading() {
this.loadingMessage.style.display = 'block';
this.generateBtn.disabled = true;
this.btnText.style.display = 'none';
this.btnLoading.style.display = 'flex';
}
hideLoading() {
this.loadingMessage.style.display = 'none';
this.generateBtn.disabled = false;
this.btnText.style.display = 'inline';
this.btnLoading.style.display = 'none';
}
async generatePrompts() {
try {
this.hideError();
// Validate inputs
const { apiKey, prompt } = this.validateInputs();
// Initialize AI client
this.ai = new GoogleGenAI({ apiKey });
// Show loading
this.showLoading();
// Get configuration
const config = this.getPromptGenerationConfig();
// Build the detailed prompt for Gemini
const systemPrompt = this.buildSystemPrompt();
const userPrompt = this.buildUserPrompt(prompt, config);
// Generate prompts using Gemini
const response = await this.ai.models.generateContent({
model: 'gemini-1.5-flash',
contents: [
{ role: 'user', parts: [{ text: systemPrompt + '\n\n' + userPrompt }] }
],
config: {
temperature: 0.8,
maxOutputTokens: 2048
}
});
// Process results
this.displayPromptResults(response, prompt, config);
} catch (error) {
console.error('Error generating prompts:', error);
this.showError(this.getErrorMessage(error));
} finally {
this.hideLoading();
}
}
buildUserPrompt(userIdea, config) {
const styleMap = {
'realistic': 'realistic, photorealistic',
'anime': 'anime style, manga style',
'cartoon': 'cartoon style, animated',
'oil-painting': 'oil painting, classical art',
'watercolor': 'watercolor painting, soft colors',
'digital-art': 'digital art, concept art',
'fantasy': 'fantasy art, magical',
'cyberpunk': 'cyberpunk style, neon, futuristic',
'minimalist': 'minimalist, simple, clean',
'vintage': 'vintage style, retro'
};
const detailMap = {
'basic': 'simple description',
'detailed': 'detailed description with lighting and composition',
'very-detailed': 'very detailed with specific camera angles, lighting, and artistic techniques',
'ultra-detailed': 'ultra detailed with professional photography terms, specific art techniques, and quality modifiers'
};
return `Ý tưởng gốc: "${userIdea}"
Phong cách: ${styleMap[config.artStyle]}
Mức độ chi tiết: ${detailMap[config.detailLevel]}
Tỷ lệ khung hình: ${config.aspectRatio}
Số lượng prompt cần tạo: ${config.numberOfPrompts}
Hãy tạo ${config.numberOfPrompts} prompt khác nhau cho ý tưởng này, mỗi prompt có góc nhìn và cách diễn đạt khác nhau nhưng cùng phong cách "${config.artStyle}".`;
}
getErrorMessage(error) {
if (error.message.includes('API key') || error.message.includes('401')) {
return 'API key không hợp lệ. Vui lòng kiểm tra lại API key của bạn.';
}
if (error.message.includes('quota') || error.message.includes('429')) {
return 'Bạn đã vượt quá giới hạn sử dụng API. Vui lòng thử lại sau.';
}
if (error.message.includes('safety')) {
return 'Nội dung của bạn vi phạm chính sách an toàn. Vui lòng thử mô tả khác.';
}
if (error.message.includes('network') || error.message.includes('fetch')) {
return 'Lỗi kết nối mạng. Vui lòng kiểm tra kết nối internet và thử lại.';
}
return error.message || 'Đã xảy ra lỗi không xác định. Vui lòng thử lại.';
}
displayPromptResults(response, originalIdea, config) {
// Clear previous results
this.promptResults.innerHTML = '';
if (!response.text) {
this.showError('Không thể tạo mô tả. Vui lòng thử lại.');
return;
}
// Parse the response to extract individual prompts
const generatedText = response.text;
const prompts = this.parseGeneratedPrompts(generatedText);
if (prompts.length === 0) {
this.showError('Không thể phân tích kết quả. Vui lòng thử lại.');
return;
}
prompts.forEach((promptData, index) => {
this.createPromptElement(promptData, index, originalIdea, config);
});
}
parseGeneratedPrompts(text) {
const prompts = [];
const sections = text.split('---').filter(section => section.trim());
sections.forEach((section, index) => {
const lines = section.trim().split('\n');
let promptText = '';
let vietnameseDescription = '';
let negativePrompt = '';
let currentSection = '';
lines.forEach(line => {
line = line.trim();
if (line.startsWith('**Prompt')) {
currentSection = 'prompt';
} else if (line.startsWith('**Mô tả tiếng Việt')) {
currentSection = 'vietnamese';
} else if (line.startsWith('**Negative Prompt')) {
currentSection = 'negative';
} else if (line && !line.startsWith('**')) {
if (currentSection === 'prompt') {
promptText += line + ' ';
} else if (currentSection === 'vietnamese') {
vietnameseDescription += line + ' ';
} else if (currentSection === 'negative') {
negativePrompt += line + ' ';
}
}
});
if (promptText.trim()) {
prompts.push({
prompt: promptText.trim(),
vietnameseDescription: vietnameseDescription.trim(),
negativePrompt: negativePrompt.trim(),
index: index + 1
});
}
});
return prompts;
}
createPromptElement(promptData, index, originalIdea, config) {
const promptItem = document.createElement('div');
promptItem.className = 'prompt-item';
const styleMap = {
'realistic': 'Thực tế',
'anime': 'Anime/Manga',
'cartoon': 'Cartoon',
'oil-painting': 'Sơn dầu',
'watercolor': 'Màu nước',
'digital-art': 'Digital Art',
'fantasy': 'Fantasy',
'cyberpunk': 'Cyberpunk',
'minimalist': 'Tối giản',
'vintage': 'Vintage'
};
promptItem.innerHTML = `
<div class="prompt-header">
<h3 class="prompt-title">Prompt ${promptData.index}</h3>
<span class="prompt-style">${styleMap[config.artStyle]}</span>
</div>
${promptData.vietnameseDescription ? `
<div class="vietnamese-description">
<h4>📝 Mô tả tiếng Việt:</h4>
<p>${promptData.vietnameseDescription}</p>
</div>
` : ''}
<div class="prompt-section">
<h4>🎨 Prompt (English):</h4>
<div class="prompt-content">${promptData.prompt}</div>
</div>
${promptData.negativePrompt ? `
<div class="prompt-section">
<h4>🚫 Negative Prompt:</h4>
<div class="prompt-content negative-prompt" style="border-color: #dc3545; background: #fff5f5;">
${promptData.negativePrompt}
</div>
</div>
` : ''}
<div class="prompt-meta">
<span>💡 Ý tưởng gốc: "${originalIdea}"</span>
<span>📐 Tỷ lệ: ${config.aspectRatio}</span>
<span>🎭 Phong cách: ${styleMap[config.artStyle]}</span>
</div>
<div class="prompt-actions">
<button class="copy-prompt-btn" data-prompt-index="${index}">
📋 Sao chép Prompt
</button>
${promptData.negativePrompt ? `
<button class="copy-negative-btn" data-prompt-index="${index}">
🚫 Sao chép Negative
</button>
` : ''}
<button class="copy-all-btn" data-prompt-index="${index}">
📄 Sao chép Tất cả
</button>
</div>
`;
this.promptResults.appendChild(promptItem);
// Add event listeners for this prompt item
const copyBtn = promptItem.querySelector('.copy-prompt-btn');
const copyNegativeBtn = promptItem.querySelector('.copy-negative-btn');
const copyAllBtn = promptItem.querySelector('.copy-all-btn');
copyBtn.addEventListener('click', () => this.copyPrompt(promptData.prompt));
if (copyNegativeBtn) {
copyNegativeBtn.addEventListener('click', () => this.copyPrompt(promptData.negativePrompt));
}
copyAllBtn.addEventListener('click', () => this.copyAllPrompts(promptData));
}
async copyPrompt(promptText) {
try {
if (!promptText || promptText.trim() === '') {
this.showError('Không có nội dung để sao chép.');
return;
}
await navigator.clipboard.writeText(promptText.trim());
this.showSuccessMessage('Đã sao chép vào clipboard! 📋');
} catch (error) {
console.error('Copy error:', error);
// Fallback method
this.fallbackCopy(promptText);
}
}
async copyAllPrompts(promptData) {
try {
let fullText = `Prompt:\n${promptData.prompt}`;
if (promptData.negativePrompt) {
fullText += `\n\nNegative Prompt:\n${promptData.negativePrompt}`;
}
await navigator.clipboard.writeText(fullText);
this.showSuccessMessage('Đã sao chép tất cả prompt vào clipboard! 📄');
} catch (error) {
console.error('Copy all error:', error);
this.fallbackCopy(fullText);
}
}
fallbackCopy(text) {
try {
// Create a temporary textarea
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
this.showSuccessMessage('Đã sao chép vào clipboard! 📋');
} catch (fallbackError) {
console.error('Fallback copy error:', fallbackError);
this.showError('Không thể sao chép. Vui lòng chọn và copy thủ công.');
}
}
showSuccessMessage(message) {
// Remove existing success messages
const existingSuccess = document.querySelector('.success-message');
if (existingSuccess) {
existingSuccess.remove();
}
const successDiv = document.createElement('div');
successDiv.className = 'success-message';
successDiv.textContent = message;
this.errorMessage.parentNode.insertBefore(successDiv, this.errorMessage);
// Auto hide after 3 seconds
setTimeout(() => {
if (successDiv.parentNode) {
successDiv.remove();
}
}, 3000);
}
}
// Initialize the application
window.promptApp = new PromptGenerator();
// Add some example prompts
const examplePrompts = [
"Một con mèo dễ thương đang ngồi trên cỏ xanh dưới ánh nắng mặt trời, phong cách anime",
"Phong cảnh núi non hùng vĩ với thác nước và cầu vồng, phong cách fantasy",
"Robot thân thiện đang cầm ván trượt màu đỏ, phong cách vector tối giản",
"Thành phố tương lai với xe bay và tòa nhà cao chọc trời, ban đêm với ánh đèn neon",
"Khu vườn hoa anh đào nở rộ với con đường đá nhỏ, phong cách Nhật Bản truyền thống"
];
// Add example prompt functionality
document.addEventListener('DOMContentLoaded', () => {
const promptInput = document.getElementById('prompt');
// Add placeholder with rotating examples
let exampleIndex = 0;
setInterval(() => {
if (!promptInput.value && document.activeElement !== promptInput) {
promptInput.placeholder = examplePrompts[exampleIndex];
exampleIndex = (exampleIndex + 1) % examplePrompts.length;
}
}, 3000);
});