HarshvardhanCn01 commited on
Commit
42e5d6f
·
verified ·
1 Parent(s): 672ae31

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. Logo_Final.jpg +0 -0
  2. README.md +3 -3
  3. index.css +271 -0
  4. index.html +38 -19
  5. index.js +270 -0
Logo_Final.jpg ADDED
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: New Space
3
- emoji: 🐠
4
  colorFrom: purple
5
- colorTo: purple
6
  sdk: static
7
  pinned: false
8
  ---
 
1
  ---
2
+ title: Trial
3
+ emoji: 📊
4
  colorFrom: purple
5
+ colorTo: yellow
6
  sdk: static
7
  pinned: false
8
  ---
index.css ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap');
2
+
3
+ :root {
4
+ --primary-color: #6a11cb;
5
+ --secondary-color: #2575fc;
6
+ --text-color: #333;
7
+ --bg-color: #f0f2f5;
8
+ --chat-bg: #ffffff;
9
+ --user-message-bg: #2575fc;
10
+ --ai-message-bg: #f1f3f5;
11
+ --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
12
+ --gradient: linear-gradient(to right, var(--primary-color), var(--secondary-color));
13
+ }
14
+
15
+ body {
16
+ font-family: 'Poppins', sans-serif;
17
+ line-height: 1.6;
18
+ color: var(--text-color);
19
+ background-color: var(--bg-color);
20
+ margin: 0;
21
+ padding: 0;
22
+ display: flex;
23
+ justify-content: center;
24
+ align-items: center;
25
+ min-height: 100vh;
26
+ background-image: var(--gradient);
27
+ background-attachment: fixed;
28
+ }
29
+
30
+ .chat-container {
31
+ background-color: var(--chat-bg);
32
+ border-radius: 20px;
33
+ box-shadow: var(--shadow);
34
+ width: 90%;
35
+ max-width: 800px;
36
+ height: 80vh;
37
+ display: flex;
38
+ flex-direction: column;
39
+ overflow: hidden;
40
+ }
41
+
42
+ .chat-header {
43
+ background-image: var(--gradient);
44
+ color: white;
45
+ padding: 20px;
46
+ text-align: center;
47
+ }
48
+
49
+ .chat-header h1 {
50
+ margin: 0;
51
+ font-size: 24px;
52
+ font-weight: 600;
53
+ }
54
+
55
+ .model-selector {
56
+ display: flex;
57
+ justify-content: center;
58
+ gap: 10px;
59
+ margin-top: 10px;
60
+ }
61
+
62
+ .select, .button {
63
+ font-family: 'Poppins', sans-serif;
64
+ font-size: 14px;
65
+ padding: 8px 12px;
66
+ border-radius: 20px;
67
+ border: none;
68
+ outline: none;
69
+ transition: all 0.3s ease;
70
+ }
71
+
72
+ .select {
73
+ background-color: white;
74
+ color: var(--text-color);
75
+ }
76
+
77
+ .button {
78
+ background-color: white;
79
+ color: var(--primary-color);
80
+ cursor: pointer;
81
+ }
82
+
83
+ .button:hover:not(:disabled) {
84
+ background-color: var(--secondary-color);
85
+ color: white;
86
+ }
87
+
88
+ .button:disabled {
89
+ opacity: 0.5;
90
+ cursor: not-allowed;
91
+ }
92
+
93
+ .progress-container {
94
+ width: 100%;
95
+ background-color: #e0e0e0;
96
+ padding: 3px;
97
+ border-radius: 3px;
98
+ box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
99
+ }
100
+
101
+ .progress-bar {
102
+ display: block;
103
+ height: 10px;
104
+ background: var(--gradient);
105
+ border-radius: 3px;
106
+ transition: width 0.3s ease-in-out;
107
+ }
108
+
109
+ .status {
110
+ padding: 10px;
111
+ text-align: center;
112
+ font-size: 14px;
113
+ color: var(--primary-color);
114
+ background-color: var(--bg-color);
115
+ }
116
+
117
+ .chat-box {
118
+ flex-grow: 1;
119
+ padding: 20px;
120
+ overflow-y: auto;
121
+ background-color: var(--chat-bg);
122
+ }
123
+
124
+ .welcome-message {
125
+ text-align: center;
126
+ color: var(--text-color);
127
+ padding: 20px;
128
+ background-color: rgba(255, 255, 255, 0.8);
129
+ border-radius: 10px;
130
+ margin-bottom: 20px;
131
+ }
132
+
133
+ .message {
134
+ max-width: 80%;
135
+ margin-bottom: 15px;
136
+ padding: 12px 16px;
137
+ border-radius: 20px;
138
+ font-size: 14px;
139
+ line-height: 1.4;
140
+ position: relative;
141
+ }
142
+
143
+ .user-message {
144
+ background-color: var(--user-message-bg);
145
+ color: white;
146
+ align-self: flex-end;
147
+ margin-left: auto;
148
+ border-bottom-right-radius: 0;
149
+ }
150
+
151
+ .assistant-message {
152
+ background-color: var(--ai-message-bg);
153
+ color: var(--text-color);
154
+ align-self: flex-start;
155
+ border-bottom-left-radius: 0;
156
+ box-shadow: var(--shadow);
157
+ }
158
+
159
+ .chat-input-container {
160
+ display: flex;
161
+ padding: 20px;
162
+ background-color: var(--chat-bg);
163
+ border-top: 1px solid var(--bg-color);
164
+ }
165
+
166
+ .input {
167
+ flex-grow: 1;
168
+ font-family: 'Poppins', sans-serif;
169
+ font-size: 14px;
170
+ padding: 12px 20px;
171
+ border: none;
172
+ border-radius: 25px;
173
+ background-color: var(--bg-color);
174
+ color: var(--text-color);
175
+ outline: none;
176
+ }
177
+
178
+ .send-button {
179
+ background-image: var(--gradient);
180
+ color: white;
181
+ border-radius: 50%;
182
+ width: 50px;
183
+ height: 50px;
184
+ display: flex;
185
+ justify-content: center;
186
+ align-items: center;
187
+ margin-left: 10px;
188
+ transition: all 0.3s ease;
189
+ }
190
+
191
+ .send-button:hover:not(:disabled) {
192
+ transform: scale(1.1);
193
+ }
194
+
195
+ .chat-stats {
196
+ font-size: 12px;
197
+ color: var(--text-color);
198
+ text-align: center;
199
+ padding: 5px;
200
+ background-color: var(--bg-color);
201
+ }
202
+
203
+ .hidden {
204
+ display: none;
205
+ }
206
+
207
+ /* Scrollbar Styles */
208
+ ::-webkit-scrollbar {
209
+ width: 8px;
210
+ }
211
+
212
+ ::-webkit-scrollbar-track {
213
+ background: var(--chat-bg);
214
+ }
215
+
216
+ ::-webkit-scrollbar-thumb {
217
+ background-image: var(--gradient);
218
+ border-radius: 20px;
219
+ }
220
+
221
+ /* Animations */
222
+ @keyframes fadeIn {
223
+ from { opacity: 0; transform: translateY(20px); }
224
+ to { opacity: 1; transform: translateY(0); }
225
+ }
226
+
227
+ .message {
228
+ animation: fadeIn 0.3s ease-out;
229
+ }
230
+
231
+ @keyframes typing {
232
+ 0% { content: ''; }
233
+ 25% { content: '.'; }
234
+ 50% { content: '..'; }
235
+ 75% { content: '...'; }
236
+ 100% { content: ''; }
237
+ }
238
+
239
+ .typing::after {
240
+ content: '';
241
+ animation: typing 1s infinite;
242
+ }
243
+
244
+ .message p {
245
+ margin: 0 0 10px 0;
246
+ }
247
+
248
+ .message ul, .message ol {
249
+ margin: 0 0 10px 0;
250
+ padding-left: 20px;
251
+ }
252
+
253
+ .message pre {
254
+ background-color: #f4f4f4;
255
+ padding: 10px;
256
+ border-radius: 5px;
257
+ overflow-x: auto;
258
+ }
259
+
260
+ .message code {
261
+ background-color: #f4f4f4;
262
+ padding: 2px 4px;
263
+ border-radius: 3px;
264
+ }
265
+
266
+ .message blockquote {
267
+ border-left: 4px solid #ccc;
268
+ margin: 0 0 10px 0;
269
+ padding-left: 10px;
270
+ color: #666;
271
+ }
index.html CHANGED
@@ -1,19 +1,38 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Kanha AI</title>
7
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
8
+ <link rel="stylesheet" href="index.css">
9
+ <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
10
+ </head>
11
+
12
+ <body>
13
+ <div class="chat-container">
14
+ <div class="chat-header">
15
+ <h1><img src="./Logo_Final.jpg" alt="Custom Icon" style="width: 30px; height: 30px; margin-right: 10px; vertical-align: middle;">Kanha</h1>
16
+ </div>
17
+ <div id="progress-container" class="progress-container hidden">
18
+ <div id="progress-bar" class="progress-bar"></div>
19
+ </div>
20
+ <div id="download-status" class="status hidden"></div>
21
+ <div id="chat-box" class="chat-box">
22
+ <div class="welcome-message">
23
+ <h2>Hello, I am Kanha!</h2>
24
+ <p>Type your message and press enter or click the send button to start chatting. The model will initialize on your first message.</p>
25
+ </div>
26
+ </div>
27
+ <div id="chat-stats" class="chat-stats hidden"></div>
28
+ <div class="chat-input-container">
29
+ <input type="text" id="user-input" class="input" placeholder="Type your message here..." required>
30
+ <button id="send" class="button send-button">
31
+ <i class="fas fa-paper-plane"></i>
32
+ </button>
33
+ </div>
34
+ </div>
35
+ <script src="https://esm.run/@mlc-ai/web-llm" type="module"></script>
36
+ <script src="index.js" type="module"></script>
37
+ </body>
38
+ </html>
index.js ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import * as webllm from "https://esm.run/@mlc-ai/web-llm";
2
+
3
+ const messages = [
4
+ {
5
+ content: "You are a helpful AI agent assisting users. And your name is 'Kanha'",
6
+ role: "system",
7
+ },
8
+ ];
9
+
10
+ const modelLibURLPrefix = "https://huggingface.co/HarshvardhanCn01/";
11
+ const modelVersion = "llama-3.2-1b-test_10steps_bs1_r0_lr2e-06_nq-q4f16_1-MLC";
12
+
13
+ const appConfig = {
14
+ model_list: [
15
+ {
16
+ model: "https://huggingface.co/HarshvardhanCn01/llama-3.2-1b-test_10steps_bs1_r0_lr2e-06_nq-q4f16_1-MLC",
17
+ model_id: "llama-3.2-1b-test_10steps_bs1_r0_lr2e-06_nq-q4f16_1-MLC",
18
+ model_lib:
19
+ webllm.modelLibURLPrefix + webllm.modelVersion + "/Llama-3.2-1B-Instruct-q4f16_1-ctx4k_cs1k-webgpu.wasm",
20
+ vram_required_MB: 3672.07,
21
+ low_resource_required: false,
22
+ overrides: {
23
+ context_window_size: 4096,
24
+ },
25
+ },
26
+ ],
27
+ };
28
+
29
+ let selectedModel = "llama-3.2-1b-test_10steps_bs1_r0_lr2e-06_nq-q4f16_1-MLC";
30
+ let engine = null;
31
+ let isInitializing = false;
32
+
33
+ async function createEngine() {
34
+ if (engine) return engine;
35
+
36
+ engine = await webllm.CreateMLCEngine(selectedModel, { appConfig: appConfig });
37
+ engine.setInitProgressCallback(updateEngineInitProgressCallback);
38
+ return engine;
39
+ }
40
+
41
+ function updateEngineInitProgressCallback(report) {
42
+ console.log("initialize", report.progress);
43
+ const statusElement = document.getElementById("download-status");
44
+ statusElement.textContent = report.text;
45
+ statusElement.classList.remove("hidden");
46
+
47
+ const progressBar = document.getElementById("progress-bar");
48
+ progressBar.style.width = `${report.progress * 100}%`;
49
+ }
50
+
51
+ function getPlatform() {
52
+ const userAgent = navigator.userAgent || navigator.vendor || window.opera;
53
+ if (/android/i.test(userAgent)) {
54
+ return "Android";
55
+ }
56
+ if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
57
+ return "iOS";
58
+ }
59
+ return "Other";
60
+ }
61
+
62
+ function getWebGPUInstructions(platform) {
63
+ switch (platform) {
64
+ case "Android":
65
+ return "To enable WebGPU on Android:\n1. Go to chrome://flags\n2. Enable 'WebGPU Developer Features' and 'Unsafe WebGPU Support'\n3. Restart your browser";
66
+ case "iOS":
67
+ return "To enable WebGPU on iOS:\n1. Open Settings\n2. Tap Safari\n3. Tap Advanced\n4. Tap Feature Flags\n5. Turn on WebGPU";
68
+ default:
69
+ return "WebGPU might not be supported on your device. Please check if your browser is up to date.";
70
+ }
71
+ }
72
+
73
+ async function checkWebGPUSupport() {
74
+ if (!navigator.gpu) {
75
+ const platform = getPlatform();
76
+ const instructions = getWebGPUInstructions(platform);
77
+ throw new Error(`WebGPU is not supported in this browser. ${instructions}`);
78
+ }
79
+ const adapter = await navigator.gpu.requestAdapter();
80
+ if (!adapter) {
81
+ throw new Error("Couldn't request WebGPU adapter. Please make sure WebGPU is enabled on your device.");
82
+ }
83
+ const device = await adapter.requestDevice();
84
+ if (!device) {
85
+ throw new Error("Couldn't request WebGPU device. Please make sure WebGPU is enabled on your device.");
86
+ }
87
+ return true;
88
+ }
89
+
90
+ async function checkRAM() {
91
+ if (!navigator.deviceMemory) {
92
+ console.warn("Device memory information is not available.");
93
+ return true; // Assume it's okay if we can't check
94
+ }
95
+ const ramGB = navigator.deviceMemory;
96
+ if (ramGB < 3) {
97
+ throw new Error(`Insufficient RAM. Required: 2GB Free RAM, Available: ${ramGB}GB`);
98
+ }
99
+ return true;
100
+ }
101
+
102
+ async function initializeWebLLMEngine() {
103
+ if (isInitializing) return;
104
+ isInitializing = true;
105
+
106
+ const progressContainer = document.getElementById("progress-container");
107
+ const statusElement = document.getElementById("download-status");
108
+
109
+ try {
110
+ // Check system requirements
111
+ await checkRAM();
112
+ await checkWebGPUSupport();
113
+
114
+ progressContainer.classList.remove("hidden");
115
+ statusElement.classList.remove("hidden");
116
+
117
+ selectedModel = "llama-3.2-1b-test_10steps_bs1_r0_lr2e-06_nq-q4f16_1-MLC"; // Using the default model
118
+ const config = {
119
+ temperature: 1.0,
120
+ top_p: 1,
121
+ };
122
+
123
+ const engine = await createEngine();
124
+ await engine.reload(selectedModel, config);
125
+
126
+ statusElement.textContent = "Model initialized successfully!";
127
+ } catch (error) {
128
+ console.error("Error initializing WebLLM engine:", error);
129
+ statusElement.textContent = `Error initializing: ${error.message}\n\nFor more information and troubleshooting, please visit kanha.ai/faq`;
130
+ statusElement.classList.remove("hidden");
131
+ throw error; // Re-throw the error to be caught in onMessageSend
132
+ } finally {
133
+ progressContainer.classList.add("hidden");
134
+ isInitializing = false;
135
+ }
136
+ }
137
+
138
+ async function streamingGenerating(messages, onUpdate, onFinish, onError) {
139
+ try {
140
+ let curMessage = "";
141
+ let usage;
142
+ const engine = await createEngine();
143
+ const completion = await engine.chat.completions.create({
144
+ stream: true,
145
+ messages,
146
+ stream_options: { include_usage: true },
147
+ });
148
+ for await (const chunk of completion) {
149
+ const curDelta = chunk.choices[0]?.delta.content;
150
+ if (curDelta) {
151
+ curMessage += curDelta;
152
+ }
153
+ if (chunk.usage) {
154
+ usage = chunk.usage;
155
+ }
156
+ onUpdate(curMessage);
157
+ }
158
+ const finalMessage = await engine.getMessage();
159
+ onFinish(finalMessage, usage);
160
+ } catch (err) {
161
+ onError(err);
162
+ }
163
+ }
164
+
165
+ async function onMessageSend() {
166
+ const input = document.getElementById("user-input");
167
+ const sendButton = document.getElementById("send");
168
+ const message = {
169
+ content: input.value.trim(),
170
+ role: "user",
171
+ };
172
+ if (message.content.length === 0) {
173
+ return;
174
+ }
175
+ sendButton.disabled = true;
176
+ sendButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i>';
177
+
178
+ messages.push(message);
179
+ appendMessage(message);
180
+
181
+ input.value = "";
182
+ input.setAttribute("placeholder", "AI is thinking...");
183
+
184
+ const aiMessage = {
185
+ content: "typing...",
186
+ role: "assistant",
187
+ };
188
+ appendMessage(aiMessage);
189
+
190
+ try {
191
+ if (!engine) {
192
+ await initializeWebLLMEngine();
193
+ }
194
+
195
+ const onFinishGenerating = (finalMessage, usage) => {
196
+ updateLastMessage(finalMessage);
197
+ sendButton.disabled = false;
198
+ sendButton.innerHTML = '<i class="fas fa-paper-plane"></i>';
199
+ input.setAttribute("placeholder", "Type your message here...");
200
+ if (usage) {
201
+ const usageText =
202
+ `Prompt tokens: ${usage.prompt_tokens}, ` +
203
+ `Completion tokens: ${usage.completion_tokens}, ` +
204
+ `Prefill: ${usage.extra.prefill_tokens_per_s.toFixed(2)} tokens/sec, ` +
205
+ `Decoding: ${usage.extra.decode_tokens_per_s.toFixed(2)} tokens/sec`;
206
+ document.getElementById("chat-stats").classList.remove("hidden");
207
+ document.getElementById("chat-stats").textContent = usageText;
208
+ }
209
+ };
210
+
211
+ await streamingGenerating(
212
+ messages,
213
+ updateLastMessage,
214
+ onFinishGenerating,
215
+ onError
216
+ );
217
+ } catch (error) {
218
+ onError(error);
219
+ // Update the AI message to show the error
220
+ updateLastMessage("I'm sorry, but I encountered an error: " + error.message);
221
+ }
222
+ }
223
+
224
+ function appendMessage(message) {
225
+ const chatBox = document.getElementById("chat-box");
226
+ const messageElement = document.createElement("div");
227
+ messageElement.classList.add("message");
228
+
229
+ if (message.role === "user") {
230
+ messageElement.classList.add("user-message");
231
+ messageElement.textContent = message.content;
232
+ } else {
233
+ messageElement.classList.add("assistant-message");
234
+ if (message.content === "typing...") {
235
+ messageElement.classList.add("typing");
236
+ messageElement.textContent = message.content;
237
+ } else {
238
+ messageElement.innerHTML = marked.parse(message.content);
239
+ }
240
+ }
241
+
242
+ chatBox.appendChild(messageElement);
243
+ chatBox.scrollTop = chatBox.scrollHeight;
244
+ }
245
+
246
+ function updateLastMessage(content) {
247
+ const chatBox = document.getElementById("chat-box");
248
+ const messages = chatBox.getElementsByClassName("message");
249
+ const lastMessage = messages[messages.length - 1];
250
+ lastMessage.innerHTML = marked.parse(content);
251
+ lastMessage.classList.remove("typing");
252
+ }
253
+
254
+ function onError(err) {
255
+ console.error(err);
256
+ const statusElement = document.getElementById("download-status");
257
+ statusElement.textContent = `Error: ${err.message}\n\nFor more information and troubleshooting, please visit kanha.ai/faq`;
258
+ statusElement.classList.remove("hidden");
259
+ document.getElementById("send").disabled = false;
260
+ document.getElementById("send").innerHTML = '<i class="fas fa-paper-plane"></i>';
261
+ }
262
+
263
+ // UI binding
264
+ document.getElementById("send").addEventListener("click", onMessageSend);
265
+ document.getElementById("user-input").addEventListener("keypress", function(event) {
266
+ if (event.key === "Enter") {
267
+ event.preventDefault();
268
+ onMessageSend();
269
+ }
270
+ });