Spaces:
Running
Running
thejagstudio
commited on
Commit
•
bf8ef86
1
Parent(s):
caf9f43
Upload 3 files
Browse files- gemma-2b-it-gpu-int4.bin +3 -0
- index.html +81 -29
- index.js +60 -79
gemma-2b-it-gpu-int4.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ef44d548e44a2a6f313c3f3e94a48e1de786871ad95f4cd81bfb35372032cdbd
|
3 |
+
size 1354301440
|
index.html
CHANGED
@@ -1,29 +1,81 @@
|
|
1 |
-
<!
|
2 |
-
<html lang="en">
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>On-device LLM Inference</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
display: flex;
|
11 |
+
flex-direction: column;
|
12 |
+
align-items: center;
|
13 |
+
justify-content: center;
|
14 |
+
margin: 0;
|
15 |
+
padding-top: 20px; /* Added padding at the top */
|
16 |
+
height: auto; /* Changed to auto for dynamic content sizing */
|
17 |
+
min-height: 100vh; /* Ensure it covers at least the full viewport height */
|
18 |
+
background-color: #f0f0f0;
|
19 |
+
}
|
20 |
+
.container {
|
21 |
+
width: 80%;
|
22 |
+
max-width: 640px; /* Adjusted for better control over max width */
|
23 |
+
text-align: center;
|
24 |
+
}
|
25 |
+
h1 {
|
26 |
+
color: #333;
|
27 |
+
margin-bottom: 20px;
|
28 |
+
}
|
29 |
+
textarea {
|
30 |
+
width: 100%; /* Full width of the container */
|
31 |
+
height: 200px; /* Height adjusted for paragraphs */
|
32 |
+
margin: 10px 0;
|
33 |
+
padding: 15px; /* More padding for better readability */
|
34 |
+
border: 1px solid #ccc;
|
35 |
+
border-radius: 8px;
|
36 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
37 |
+
font-family: inherit;
|
38 |
+
font-size: 16px;
|
39 |
+
resize: vertical; /* Users can adjust the vertical size */
|
40 |
+
}
|
41 |
+
input[type="button"] {
|
42 |
+
padding: 10px 20px;
|
43 |
+
font-size: 16px;
|
44 |
+
border: none;
|
45 |
+
border-radius: 5px;
|
46 |
+
background-color: #007bff;
|
47 |
+
color: white;
|
48 |
+
cursor: pointer;
|
49 |
+
transition: background-color 0.2s;
|
50 |
+
}
|
51 |
+
input[type="button"]:hover {
|
52 |
+
background-color: #0056b3;
|
53 |
+
}
|
54 |
+
input[type="button"]:disabled {
|
55 |
+
background-color: #ccc;
|
56 |
+
cursor: not-allowed;
|
57 |
+
}
|
58 |
+
</style>
|
59 |
+
</head>
|
60 |
+
<body>
|
61 |
+
<div class="container">
|
62 |
+
<h1>On-device LLM Inference</h1>
|
63 |
+
<label for="input">Input:</label>
|
64 |
+
<br>
|
65 |
+
<textarea id="input"></textarea>
|
66 |
+
<br>
|
67 |
+
<input
|
68 |
+
type="button"
|
69 |
+
id="submit"
|
70 |
+
value="Get Response"
|
71 |
+
disabled
|
72 |
+
>
|
73 |
+
<br>
|
74 |
+
<br>
|
75 |
+
<label for="output">Result:</label>
|
76 |
+
<br>
|
77 |
+
<textarea id="output"></textarea>
|
78 |
+
</div>
|
79 |
+
<script type="module" src="index.js"></script>
|
80 |
+
</body>
|
81 |
+
</html>
|
index.js
CHANGED
@@ -1,79 +1,60 @@
|
|
1 |
-
import {
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
const
|
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 |
-
const boxElement = document.createElement('div');
|
62 |
-
boxElement.className = 'bounding-box';
|
63 |
-
Object.assign(boxElement.style, {
|
64 |
-
borderColor: color,
|
65 |
-
left: 100 * xmin + '%',
|
66 |
-
top: 100 * ymin + '%',
|
67 |
-
width: 100 * (xmax - xmin) + '%',
|
68 |
-
height: 100 * (ymax - ymin) + '%',
|
69 |
-
})
|
70 |
-
|
71 |
-
// Draw label
|
72 |
-
const labelElement = document.createElement('span');
|
73 |
-
labelElement.textContent = label;
|
74 |
-
labelElement.className = 'bounding-box-label';
|
75 |
-
labelElement.style.backgroundColor = color;
|
76 |
-
|
77 |
-
boxElement.appendChild(labelElement);
|
78 |
-
imageContainer.appendChild(boxElement);
|
79 |
-
}
|
|
|
1 |
+
import { FilesetResolver, LlmInference } from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai";
|
2 |
+
|
3 |
+
const input = document.getElementById("input");
|
4 |
+
const output = document.getElementById("output");
|
5 |
+
const submit = document.getElementById("submit");
|
6 |
+
|
7 |
+
const modelFileName = "gemma-2b-it-gpu-int4.bin";
|
8 |
+
|
9 |
+
/**
|
10 |
+
* Display newly generated partial results to the output text box.
|
11 |
+
*/
|
12 |
+
function displayPartialResults(partialResults, complete) {
|
13 |
+
output.textContent += partialResults;
|
14 |
+
|
15 |
+
if (complete) {
|
16 |
+
if (!output.textContent) {
|
17 |
+
output.textContent = "Result is empty";
|
18 |
+
}
|
19 |
+
submit.disabled = false;
|
20 |
+
}
|
21 |
+
}
|
22 |
+
|
23 |
+
/**
|
24 |
+
* Main function to run LLM Inference.
|
25 |
+
*/
|
26 |
+
async function runDemo() {
|
27 |
+
const genaiFileset = await FilesetResolver.forGenAiTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai/wasm");
|
28 |
+
let llmInference;
|
29 |
+
|
30 |
+
submit.onclick = () => {
|
31 |
+
output.textContent = "";
|
32 |
+
submit.disabled = true;
|
33 |
+
llmInference.generateResponse(input.value, displayPartialResults);
|
34 |
+
};
|
35 |
+
|
36 |
+
submit.value = "Loading the model...";
|
37 |
+
LlmInference.createFromOptions(genaiFileset, {
|
38 |
+
baseOptions: { modelAssetPath: modelFileName },
|
39 |
+
// maxTokens: 512, // The maximum number of tokens (input tokens + output
|
40 |
+
// // tokens) the model handles.
|
41 |
+
// randomSeed: 1, // The random seed used during text generation.
|
42 |
+
// topK: 1, // The number of tokens the model considers at each step of
|
43 |
+
// // generation. Limits predictions to the top k most-probable
|
44 |
+
// // tokens. Setting randomSeed is required for this to make
|
45 |
+
// // effects.
|
46 |
+
// temperature:
|
47 |
+
// 1.0, // The amount of randomness introduced during generation.
|
48 |
+
// // Setting randomSeed is required for this to make effects.
|
49 |
+
})
|
50 |
+
.then((llm) => {
|
51 |
+
llmInference = llm;
|
52 |
+
submit.disabled = false;
|
53 |
+
submit.value = "Get Response";
|
54 |
+
})
|
55 |
+
.catch(() => {
|
56 |
+
alert("Failed to initialize the task.");
|
57 |
+
});
|
58 |
+
}
|
59 |
+
|
60 |
+
runDemo();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|