-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
245 lines (212 loc) · 9.12 KB
/
Copy pathindex.html
File metadata and controls
245 lines (212 loc) · 9.12 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Model Compression Reasoning Engine</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body class="bg-gray-100 min-h-screen p-4">
<div class="max-w-4xl mx-auto bg-white rounded-lg shadow-lg flex flex-col h-screen max-h-screen">
<div class="border-b border-gray-200 p-4 flex justify-between items-center">
<h1 class="text-xl font-semibold text-gray-800">ModelShrink</h1>
<button
id="compressBtn"
disabled
class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors duration-200"
>
Compress Model
</button>
</div>
<div id="chatContainer" class="flex-1 overflow-y-auto p-4 space-y-4 min-h-0">
<div class="flex justify-center">
<div class="bg-gray-50 text-gray-600 text-sm px-4 py-2 rounded-full">
Welcome! Upload your model and ask me about compression.
</div>
</div>
</div>
<div class="border-t border-gray-200 p-4">
<div class="flex space-x-3 items-center">
<label class="cursor-pointer bg-gray-200 hover:bg-gray-300 rounded-lg flex items-center justify-center h-12 w-12">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-700" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
</svg>
<input type="file" id="uploadModel" class="hidden" />
</label>
<div class="flex-1 flex items-center">
<textarea
id="messageInput"
placeholder="Type your message... (Enter to send, Shift+Enter for new line)"
class="w-full px-4 py-2 bg-gray-50 rounded-lg resize-none max-h-24"
></textarea>
</div>
<button
id="sendBtn"
class="bg-blue-500 text-white h-12 w-12 rounded-lg hover:bg-blue-600 transition-colors duration-200 flex items-center justify-center"
>
<svg class="w-6 h-6 transform rotate-90" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
</svg>
</button>
</div>
</div>
</div>
<div id="typingIndicator" class="hidden flex items-start space-x-3 mb-4">
<div class="bg-gray-200 text-gray-700 p-3 rounded-2xl rounded-bl-md max-w-xs">
<div class="flex space-x-1">
<div class="w-2 h-2 bg-gray-500 rounded-full animate-bounce"></div>
<div class="w-2 h-2 bg-gray-500 rounded-full animate-bounce" style="animation-delay: 0.1s"></div>
<div class="w-2 h-2 bg-gray-500 rounded-full animate-bounce" style="animation-delay: 0.2s"></div>
</div>
</div>
</div>
<script>
const chatHistory = [];
let uploadedModelName = null;
const chatContainer = document.getElementById('chatContainer');
const messageInput = document.getElementById('messageInput');
const sendBtn = document.getElementById('sendBtn');
const compressBtn = document.getElementById('compressBtn');
const uploadModel = document.getElementById('uploadModel');
const typingIndicator = document.getElementById('typingIndicator');
uploadModel.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
const formData = new FormData();
formData.append("file", file);
try {
const response = await fetch("/upload", {
method: "POST",
body: formData
});
const result = await response.json();
if (result.error) {
renderMessage("⚠️ Upload failed: " + result.error, "system");
} else {
uploadedModelName = result.path;
renderMessage(`📂 Uploaded model: ${file.name}`, "system");
compressBtn.disabled = false;
}
} catch (err) {
renderMessage("⚠️ Upload error: " + err.message, "system");
}
});
sendBtn.addEventListener('click', sendMessage);
messageInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
async function sendMessage() {
const message = messageInput.value.trim();
if (!message) return;
renderMessage(message, 'user');
messageInput.value = '';
messageInput.style.height = 'auto';
showTypingIndicator();
try {
const response = await fetch("/predict", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ data: [message] })
});
const result = await response.json();
hideTypingIndicator();
if (result.error) {
renderMessage("⚠️ " + result.error, "assistant");
} else {
chatHistory.push({ role: 'user', content: message });
chatHistory.push({ role: 'assistant', content: result.data[0] });
renderMessage(result.data[0], 'assistant');
}
} catch (err) {
hideTypingIndicator();
renderMessage("⚠️ Network error: " + err.message, 'assistant');
}
}
compressBtn.addEventListener('click', compressModel);
async function compressModel() {
showTypingIndicator();
try {
const response = await fetch("/compress", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
dtype: "auto",
})
});
const result = await response.json();
hideTypingIndicator();
if (result.error) {
renderMessage("⚠️ " + result.error, "system");
} else {
renderMessage(JSON.stringify(result, null, 2), "json");
const compressedPath = result.compressed_model_path;
if (compressedPath) {
const messageDiv = document.createElement('div');
messageDiv.className = 'flex justify-start mb-4';
const messageBubble = document.createElement('div');
messageBubble.className = 'bg-green-100 text-green-800 px-4 py-3 rounded-2xl rounded-bl-md';
const downloadLink = document.createElement('a');
downloadLink.href = `/download/${compressedPath}`;
downloadLink.textContent = `⬇️ Download ${compressedPath}`;
downloadLink.className = "text-green-900 font-semibold hover:underline";
messageBubble.appendChild(downloadLink);
messageDiv.appendChild(messageBubble);
chatContainer.appendChild(messageDiv);
scrollToBottom();
}
}
} catch (err) {
hideTypingIndicator();
renderMessage("⚠️ Compression error: " + err.message, "system");
}
}
function renderMessage(content, role) {
const messageDiv = document.createElement('div');
messageDiv.className = `flex ${role === 'user' ? 'justify-end' : 'justify-start'} mb-4`;
const messageBubble = document.createElement('div');
if (role === 'json') {
messageBubble.className =
"bg-gray-900 text-green-400 font-mono text-sm px-4 py-3 rounded-lg whitespace-pre-wrap max-w-full overflow-x-auto";
messageBubble.innerHTML = `<pre><code>${content}</code></pre>`;
} else {
messageBubble.className = `max-w-lg lg:max-w-2xl px-4 py-3 rounded-2xl break-words ${
role === 'user'
? 'bg-blue-500 text-white rounded-br-md'
: role === 'assistant'
? 'bg-gray-200 text-gray-700 rounded-bl-md'
: 'bg-yellow-100 text-yellow-700'
}`;
messageBubble.innerHTML = marked.parse(content);
}
messageDiv.appendChild(messageBubble);
chatContainer.appendChild(messageDiv);
scrollToBottom();
}
function showTypingIndicator() {
const indicatorClone = typingIndicator.cloneNode(true);
indicatorClone.id = 'activeTypingIndicator';
indicatorClone.classList.remove('hidden');
chatContainer.appendChild(indicatorClone);
scrollToBottom();
}
function hideTypingIndicator() {
const activeIndicator = document.getElementById('activeTypingIndicator');
if (activeIndicator) activeIndicator.remove();
}
function scrollToBottom() {
chatContainer.scrollTop = chatContainer.scrollHeight;
}
window.addEventListener('load', () => {
messageInput.focus();
});
messageInput.addEventListener('input', () => {
messageInput.style.height = 'auto';
messageInput.style.height = `${messageInput.scrollHeight}px`;
});
</script>
</body>
</html>