TDN-M commited on
Commit
5f44c5d
·
verified ·
1 Parent(s): 71988fe

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +173 -0
script.js ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const PRODUCT_GROUPS = {
2
+ Standard: {
3
+ "Standard": {
4
+ "C1012 Glacier White": "817687427545199895",
5
+ "C1026 Polar": "819910519797326073",
6
+ "C3269 Ash Grey": "821839484099264081",
7
+ "C3168 Silver Wave": "821849044696643212",
8
+ "C1005 Milky White": "821948258441171133",
9
+ },
10
+ "Deluxe": {
11
+ "C2103 Onyx Carrara": "827090618489513527",
12
+ "C2104 Massa": "822075428127644644",
13
+ "C3105 Casla Cloudy": "828912225788997963",
14
+ "C3146 Casla Nova": "828013009961087650",
15
+ "C2240 Marquin": "828085015087780649",
16
+ "C2262 Concrete (Honed)": "822211862058871636",
17
+ "C3311 Calacatta Sky": "829984593223502930",
18
+ "C3346 Massimo": "827938741386607132",
19
+ },
20
+ "Luxury": {
21
+ "C4143 Mario": "829984593223502930",
22
+ "C4145 Marina": "828132560375742058",
23
+ "C4202 Calacatta Gold": "828167757632695310",
24
+ "C1205 Casla Everest": "828296778450463190",
25
+ "C4211 Calacatta Supreme": "828436321937882328",
26
+ "C4204 Calacatta Classic": "828422973179466146",
27
+ "C5240 Spring": "is coming",
28
+ "C1102 Super White": "828545723344775887",
29
+ "C4246 Casla Mystery": "828544778451950698",
30
+ "C4345 Oro": "828891068780182635",
31
+ "C4346 Luxe": "829436426547535131",
32
+ "C4342 Casla Eternal": "829190256201829181",
33
+ "C4221 Athena": "829644354504131520",
34
+ "C4222 Lagoon": "is coming",
35
+ "C5225 Amber": "is coming",
36
+ },
37
+ "Super Luxury": {
38
+ "C4255 Calacatta Extra": "829659013227537217",
39
+ },
40
+ }
41
+
42
+ function switchTab(tab) {
43
+ document.querySelectorAll('.tab-content').forEach(el => el.style.display = 'none');
44
+ document.querySelectorAll('.tabs button').forEach(el => el.classList.remove('active'));
45
+ document.getElementById(tab).style.display = 'block';
46
+ document.querySelector(`button[onclick="switchTab('${tab}')"]`).classList.add('active');
47
+ }
48
+
49
+ function renderProductGroups(containerId) {
50
+ const container = document.getElementById(containerId);
51
+ for (const [group, products] of Object.entries(PRODUCT_GROUPS)) {
52
+ const div = document.createElement('div');
53
+ div.className = 'product-group';
54
+ div.style.backgroundColor = GROUP_COLORS[group];
55
+ div.innerHTML = `<h3>${group}</h3>`;
56
+ for (const code in products) {
57
+ if (products[code] !== "is coming" && products[code] !== "is loading") {
58
+ div.innerHTML += `
59
+ <label>
60
+ <input type="checkbox" name="${group}" value="${code}">
61
+ ${code}
62
+ </label>
63
+ `;
64
+ }
65
+ }
66
+ container.appendChild(div);
67
+ }
68
+ }
69
+
70
+ async function generateText2Img() {
71
+ const prompt = document.getElementById('prompt').value;
72
+ const size = document.getElementById('size').value;
73
+ const customSize = document.getElementById('custom-size').value;
74
+ const productCheckboxes = document.querySelectorAll('#product-groups input:checked');
75
+ const productCodes = Array.from(productCheckboxes).map(cb => cb.value);
76
+
77
+ if (!prompt || !productCodes.length) {
78
+ showError('Please enter a prompt and select at least one product.');
79
+ return;
80
+ }
81
+
82
+ showProgress(true);
83
+ try {
84
+ const response = await fetch('https://YOUR_USERNAME-caslaquartz-backend.hf.space/api/text2img', {
85
+ method: 'POST',
86
+ headers: { 'Content-Type': 'application/json' },
87
+ body: JSON.stringify({
88
+ prompt,
89
+ size,
90
+ custom_size: size === 'Custom size' ? customSize : null,
91
+ product_codes: productCodes
92
+ })
93
+ });
94
+ const data = await response.json();
95
+ if (data.error) throw new Error(data.error);
96
+ document.getElementById('output-image').src = data.image_url;
97
+ document.getElementById('output-image').style.display = 'block';
98
+ } catch (err) {
99
+ showError(err.message);
100
+ } finally {
101
+ showProgress(false);
102
+ }
103
+ }
104
+
105
+ async function generateImg2Img() {
106
+ const fileInput = document.getElementById('image-upload');
107
+ const position = document.getElementById('position').value;
108
+ const size = document.getElementById('size-img2img').value;
109
+ const customSize = document.getElementById('custom-size-img2img').value;
110
+ const productCheckboxes = document.querySelectorAll('#product-groups-img2img input:checked');
111
+ const productCodes = Array.from(productCheckboxes).map(cb => cb.value);
112
+
113
+ if (!fileInput.files[0] || !productCodes.length) {
114
+ showError('Please upload an image and select at least one product.');
115
+ return;
116
+ }
117
+
118
+ showProgress(true);
119
+ const formData = new FormData();
120
+ formData.append('file', fileInput.files[0]);
121
+ formData.append('position', position);
122
+ formData.append('size', size);
123
+ if (size === 'Custom size') formData.append('custom_size', customSize);
124
+ productCodes.forEach(code => formData.append('product_codes', code));
125
+
126
+ try {
127
+ const response = await fetch('https://YOUR_USERNAME-caslaquartz-backend.hf.space/api/img2img', {
128
+ method: 'POST',
129
+ body: formData
130
+ });
131
+ const data = await response.json();
132
+ if (data.error) throw new Error(data.error);
133
+ document.getElementById('output-image').src = data.image_url;
134
+ document.getElementById('output-image').style.display = 'block';
135
+ } catch (err) {
136
+ showError(err.message);
137
+ } finally {
138
+ showProgress(false);
139
+ }
140
+ }
141
+
142
+ function showProgress(show) {
143
+ const progress = document.getElementById('progress');
144
+ const bar = progress.querySelector('.progress-bar');
145
+ progress.style.display = show ? 'block' : 'none';
146
+ if (show) {
147
+ let width = 0;
148
+ const interval = setInterval(() => {
149
+ if (width >= 100) clearInterval(interval);
150
+ width += 10;
151
+ bar.style.width = `${width}%`;
152
+ }, 500);
153
+ } else {
154
+ bar.style.width = '0%';
155
+ }
156
+ }
157
+
158
+ function showError(message) {
159
+ const error = document.getElementById('error');
160
+ error.textContent = message;
161
+ error.style.display = 'block';
162
+ setTimeout(() => error.style.display = 'none', 5000);
163
+ }
164
+
165
+ // Khởi tạo
166
+ document.getElementById('size').addEventListener('change', (e) => {
167
+ document.getElementById('custom-size').style.display = e.target.value === 'Custom size' ? 'block' : 'none';
168
+ });
169
+ document.getElementById('size-img2img').addEventListener('change', (e) => {
170
+ document.getElementById('custom-size-img2img').style.display = e.target.value === 'Custom size' ? 'block' : 'none';
171
+ });
172
+ renderProductGroups('product-groups');
173
+ renderProductGroups('product-groups-img2img');