thejagstudio commited on
Commit
bf8ef86
1 Parent(s): caf9f43

Upload 3 files

Browse files
Files changed (3) hide show
  1. gemma-2b-it-gpu-int4.bin +3 -0
  2. index.html +81 -29
  3. 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
- <!DOCTYPE html>
2
- <html lang="en">
3
-
4
- <head>
5
- <meta charset="UTF-8" />
6
- <link rel="stylesheet" href="style.css" />
7
-
8
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
9
- <title>Transformers.js - Object Detection</title>
10
- </head>
11
-
12
- <body>
13
- <h1>Object Detection w/ 🤗 Transformers.js</h1>
14
- <label id="container" for="upload">
15
- <svg width="25" height="25" viewBox="0 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg">
16
- <path fill="#000"
17
- d="M3.5 24.3a3 3 0 0 1-1.9-.8c-.5-.5-.8-1.2-.8-1.9V2.9c0-.7.3-1.3.8-1.9.6-.5 1.2-.7 2-.7h18.6c.7 0 1.3.2 1.9.7.5.6.7 1.2.7 2v18.6c0 .7-.2 1.4-.7 1.9a3 3 0 0 1-2 .8H3.6Zm0-2.7h18.7V2.9H3.5v18.7Zm2.7-2.7h13.3c.3 0 .5 0 .6-.3v-.7l-3.7-5a.6.6 0 0 0-.6-.2c-.2 0-.4 0-.5.3l-3.5 4.6-2.4-3.3a.6.6 0 0 0-.6-.3c-.2 0-.4.1-.5.3l-2.7 3.6c-.1.2-.2.4 0 .7.1.2.3.3.6.3Z">
18
- </path>
19
- </svg>
20
- Click to upload image
21
- <label id="example">(or try example)</label>
22
- </label>
23
- <label id="status">Loading model...</label>
24
- <input id="upload" type="file" accept="image/*" />
25
-
26
- <script src="index.js" type="module"></script>
27
- </body>
28
-
29
- </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>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 { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.10.1';
2
-
3
- // Since we will download the model from the Hugging Face Hub, we can skip the local model check
4
- env.allowLocalModels = false;
5
-
6
- // Reference the elements that we will need
7
- const status = document.getElementById('status');
8
- const fileUpload = document.getElementById('upload');
9
- const imageContainer = document.getElementById('container');
10
- const example = document.getElementById('example');
11
-
12
- const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
13
-
14
- // Create a new object detection pipeline
15
- status.textContent = 'Loading model...';
16
- const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50');
17
- status.textContent = 'Ready';
18
-
19
- example.addEventListener('click', (e) => {
20
- e.preventDefault();
21
- detect(EXAMPLE_URL);
22
- });
23
-
24
- fileUpload.addEventListener('change', function (e) {
25
- const file = e.target.files[0];
26
- if (!file) {
27
- return;
28
- }
29
-
30
- const reader = new FileReader();
31
-
32
- // Set up a callback when the file is loaded
33
- reader.onload = e2 => detect(e2.target.result);
34
-
35
- reader.readAsDataURL(file);
36
- });
37
-
38
-
39
- // Detect objects in the image
40
- async function detect(img) {
41
- imageContainer.innerHTML = '';
42
- imageContainer.style.backgroundImage = `url(${img})`;
43
-
44
- status.textContent = 'Analysing...';
45
- const output = await detector(img, {
46
- threshold: 0.5,
47
- percentage: true,
48
- });
49
- status.textContent = '';
50
- output.forEach(renderBox);
51
- }
52
-
53
- // Render a bounding box and label on the image
54
- function renderBox({ box, label }) {
55
- const { xmax, xmin, ymax, ymin } = box;
56
-
57
- // Generate a random color for the box
58
- const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
59
-
60
- // Draw the box
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();