AEUPH commited on
Commit
4f23333
·
verified ·
1 Parent(s): d4050fa

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +117 -18
index.html CHANGED
@@ -3,27 +3,126 @@
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>
 
3
 
4
  <head>
5
  <meta charset="UTF-8" />
 
 
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Babylon.js 3D VR Game with Transformers.js Chat</title>
8
+ <style>
9
+ body, html { margin: 0; padding: 0; overflow: hidden; font-family: 'Trebuchet MS', serif; }
10
+ canvas { width: 100%; height: 100%; display: block; }
11
+ #chatUI { position: absolute; top: 10px; right: 10px; width: 300px; }
12
+ #chatLog { width: 100%; height: 400px; background-color: rgba(0, 0, 0, 0.7); padding: 10px; color: #fff; overflow-y: auto; border: 2px solid #4e342e; box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.9); }
13
+ #chatInput { width: 100%; height: 40px; margin-top: 10px; background-color: rgba(255, 255, 255, 0.4); border: 2px solid #4e342e; }
14
+ #sendButton { width: 100%; height: 40px; margin-top: 10px; background: linear-gradient(to bottom, #957d5f, #6c543e); color: #e0d7c5; border: none; cursor: pointer; }
15
+ </style>
16
  </head>
17
 
18
  <body>
19
+ <canvas id="renderCanvas"></canvas>
20
+ <div id="chatUI">
21
+ <div id="chatLog"></div>
22
+ <input type="text" id="chatInput" placeholder="Type your message here..." />
23
+ <button id="sendButton">Send</button>
24
+ </div>
25
+
26
+ <script src="https://cdn.babylonjs.com/babylon.js"></script>
27
+ <script src="https://cdn.babylonjs.com/loaders/babylon.glTF2FileLoader.min.js"></script>
28
+ <script src="https://unpkg.com/@huggingface/transformers"></script>
29
+
30
+ <script>
31
+ // Setup Babylon.js
32
+ const canvas = document.getElementById("renderCanvas");
33
+ const engine = new BABYLON.Engine(canvas, true);
34
+
35
+ const createScene = function() {
36
+ const scene = new BABYLON.Scene(engine);
37
+
38
+ // Camera
39
+ const camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 10, BABYLON.Vector3.Zero(), scene);
40
+ camera.attachControl(canvas, true);
41
+
42
+ // Light
43
+ const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
44
+
45
+ // Ground
46
+ const ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 10, height: 10 }, scene);
47
+
48
+ // Player and NPCs
49
+ const player = BABYLON.MeshBuilder.CreateBox("player", { size: 1 }, scene);
50
+ player.position.y = 0.5;
51
+
52
+ const npc1 = BABYLON.MeshBuilder.CreateSphere("npc1", { diameter: 1 }, scene);
53
+ npc1.position.x = 3;
54
+ npc1.position.y = 0.5;
55
+
56
+ // Basic movement (Q-learning is complex in a static file, so using basic movement for now)
57
+ window.addEventListener("keydown", (event) => {
58
+ switch (event.key) {
59
+ case "ArrowUp":
60
+ player.position.z -= 0.1;
61
+ break;
62
+ case "ArrowDown":
63
+ player.position.z += 0.1;
64
+ break;
65
+ case "ArrowLeft":
66
+ player.position.x -= 0.1;
67
+ break;
68
+ case "ArrowRight":
69
+ player.position.x += 0.1;
70
+ break;
71
+ }
72
+ checkCollision(player, npc1);
73
+ });
74
+
75
+ function checkCollision(mesh1, mesh2) {
76
+ if (mesh1.intersectsMesh(mesh2)) {
77
+ openChat("npc1", "NPC-1", "Hello! How can I assist you today?");
78
+ }
79
+ }
80
+
81
+ return scene;
82
+ };
83
+
84
+ const scene = createScene();
85
+
86
+ engine.runRenderLoop(function() {
87
+ scene.render();
88
+ });
89
+
90
+ window.addEventListener("resize", function() {
91
+ engine.resize();
92
+ });
93
+
94
+ // Chat System with Transformers.js
95
+ async function setupChat() {
96
+ const model = await transformers.AutoModelForCausalLM.from_pretrained('mistralai/Mistral');
97
+ const tokenizer = await transformers.AutoTokenizer.from_pretrained('mistralai/Mistral');
98
+ return { model, tokenizer };
99
+ }
100
+
101
+ let chatSetup = setupChat();
102
+
103
+ document.getElementById("sendButton").addEventListener("click", async () => {
104
+ const inputField = document.getElementById("chatInput");
105
+ const chatLog = document.getElementById("chatLog");
106
+ const message = inputField.value;
107
+ if (message.trim() === "") return;
108
+
109
+ chatLog.innerHTML += `<p>Player: ${message}</p>`;
110
+ inputField.value = "";
111
+
112
+ const { model, tokenizer } = await chatSetup;
113
+ const inputs = tokenizer(message, { return_tensors: "pt" });
114
+ const response = await model.generate(inputs.input_ids, { max_length: 50 });
115
+ const output = tokenizer.decode(response[0], { skip_special_tokens: true });
116
+
117
+ chatLog.innerHTML += `<p>NPC: ${output}</p>`;
118
+ chatLog.scrollTop = chatLog.scrollHeight;
119
+ });
120
+
121
+ function openChat(npcId, npcName, greeting) {
122
+ const chatLog = document.getElementById("chatLog");
123
+ chatLog.innerHTML += `<p>${npcName}: ${greeting}</p>`;
124
+ }
125
+ </script>
126
  </body>
127
 
128
+ </html>