Spaces:
Running
Running
Update index.html
Browse files- index.html +29 -109
index.html
CHANGED
@@ -3,135 +3,55 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>
|
7 |
-
|
8 |
-
<!-- Import the pipeline from transformers.js -->
|
9 |
<script type="module">
|
10 |
-
import { pipeline } from '
|
11 |
-
window.pipeline = pipeline;
|
12 |
-
</script>
|
13 |
-
|
14 |
-
<!-- Bootstrap CSS -->
|
15 |
-
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
16 |
-
</head>
|
17 |
-
|
18 |
-
<body style="font-family: Arial, sans-serif;">
|
19 |
-
<div class="container mt-5" style="max-width: 800px; margin: 0 auto;">
|
20 |
-
<h1 class="text-center mb-4" style="font-weight: bold; color: #333;">AI Chatbot</h1>
|
21 |
|
22 |
-
|
23 |
-
<div id="loading-status" class="alert alert-warning text-center">β³ Please wait, model is loading...</div>
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
</div>
|
29 |
|
30 |
-
<!-- Input and Button Section -->
|
31 |
-
<div class="input-group mt-3">
|
32 |
-
<input id="user-input" type="text" class="form-control" placeholder="Type your message..." disabled />
|
33 |
-
<button id="send-button" class="btn btn-primary" onclick="handleUserInput()" disabled>Send</button>
|
34 |
-
</div>
|
35 |
-
</div>
|
36 |
-
|
37 |
-
<script>
|
38 |
-
let generator;
|
39 |
-
|
40 |
-
// Initialize the text-to-text model
|
41 |
-
async function initializeModel() {
|
42 |
try {
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
// Update UI when model is loaded
|
48 |
-
document.getElementById('loading-status').className = "alert alert-success text-center";
|
49 |
-
document.getElementById('loading-status').innerHTML = "β
Model Loaded! You can start chatting.";
|
50 |
-
|
51 |
-
// Enable input fields
|
52 |
-
document.getElementById('user-input').disabled = false;
|
53 |
-
document.getElementById('send-button').disabled = false;
|
54 |
} catch (error) {
|
|
|
55 |
console.error("Error loading model:", error);
|
56 |
-
document.getElementById('loading-status').className = "alert alert-danger text-center";
|
57 |
-
document.getElementById('loading-status').innerHTML = "β Error loading model. Please refresh.";
|
58 |
}
|
59 |
}
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
const
|
64 |
-
messageElement.className = className;
|
65 |
-
messageElement.innerText = message;
|
66 |
-
document.getElementById('chat-container').appendChild(messageElement);
|
67 |
-
document.getElementById('chat-container').scrollTop = document.getElementById('chat-container').scrollHeight;
|
68 |
-
}
|
69 |
-
|
70 |
-
// Handle user input
|
71 |
-
async function handleUserInput() {
|
72 |
-
const userInput = document.getElementById('user-input').value.trim();
|
73 |
-
if (!userInput) return;
|
74 |
-
|
75 |
-
displayMessage("You: " + userInput, 'user-message');
|
76 |
-
document.getElementById('user-input').value = "";
|
77 |
|
78 |
-
await generateResponse(userInput);
|
79 |
-
}
|
80 |
-
|
81 |
-
// Generate response
|
82 |
-
async function generateResponse(inputText) {
|
83 |
if (!generator) {
|
84 |
-
|
85 |
return;
|
86 |
}
|
87 |
|
|
|
88 |
try {
|
89 |
-
|
90 |
-
|
91 |
-
const generatedText = result[0]?.generated_text || "Sorry, I couldn't generate a response.";
|
92 |
-
displayMessage("Bot: " + generatedText, 'bot-message');
|
93 |
} catch (error) {
|
94 |
-
|
95 |
-
|
96 |
}
|
97 |
}
|
98 |
|
99 |
-
|
100 |
-
window.addEventListener("DOMContentLoaded", initializeModel);
|
101 |
</script>
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
background-color: #d1ecf1;
|
112 |
-
max-width: 75%;
|
113 |
-
margin-left: auto;
|
114 |
-
font-family: Arial, sans-serif;
|
115 |
-
}
|
116 |
-
.bot-message {
|
117 |
-
text-align: left;
|
118 |
-
color: #155724;
|
119 |
-
margin: 10px 0;
|
120 |
-
padding: 10px;
|
121 |
-
border-radius: 15px;
|
122 |
-
background-color: #d4edda;
|
123 |
-
max-width: 75%;
|
124 |
-
margin-right: auto;
|
125 |
-
font-family: Arial, sans-serif;
|
126 |
-
}
|
127 |
-
/* Scrollbar styling */
|
128 |
-
#chat-container::-webkit-scrollbar {
|
129 |
-
width: 6px;
|
130 |
-
}
|
131 |
-
#chat-container::-webkit-scrollbar-thumb {
|
132 |
-
background: #888;
|
133 |
-
border-radius: 10px;
|
134 |
-
}
|
135 |
-
</style>
|
136 |
</body>
|
137 |
</html>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Text-to-Text Generation</title>
|
|
|
|
|
7 |
<script type="module">
|
8 |
+
import { pipeline } from '@huggingface/transformers';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
let generator = null; // Global model reference
|
|
|
11 |
|
12 |
+
async function loadModel() {
|
13 |
+
const status = document.getElementById("status");
|
14 |
+
status.innerText = "π‘ Loading text generation model... Please wait.";
|
|
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
try {
|
17 |
+
generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M', { quantized: true });
|
18 |
+
status.innerText = "β
Model loaded! You can start the conversation.";
|
19 |
+
status.style.color = "green";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
} catch (error) {
|
21 |
+
status.innerText = "β Model failed to load. Check the console.";
|
22 |
console.error("Error loading model:", error);
|
|
|
|
|
23 |
}
|
24 |
}
|
25 |
|
26 |
+
async function generateText() {
|
27 |
+
const inputText = document.getElementById("userInput").value;
|
28 |
+
const outputDiv = document.getElementById("output");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
|
|
|
|
|
|
|
|
|
|
30 |
if (!generator) {
|
31 |
+
outputDiv.innerText = "β Model not loaded yet!";
|
32 |
return;
|
33 |
}
|
34 |
|
35 |
+
outputDiv.innerText = "β³ Generating response...";
|
36 |
try {
|
37 |
+
const response = await generator(inputText);
|
38 |
+
outputDiv.innerText = "π€ AI: " + response[0].generated_text;
|
|
|
|
|
39 |
} catch (error) {
|
40 |
+
outputDiv.innerText = "β Error in response generation.";
|
41 |
+
console.error("Generation error:", error);
|
42 |
}
|
43 |
}
|
44 |
|
45 |
+
window.onload = loadModel;
|
|
|
46 |
</script>
|
47 |
+
</head>
|
48 |
+
<body>
|
49 |
+
<h1>Text-to-Text Generation (Xenova/LaMini-Flan-T5-783M)</h1>
|
50 |
+
<p id="status" style="color: orange;">π‘ Initializing...</p>
|
51 |
+
|
52 |
+
<input type="text" id="userInput" placeholder="Type something..." />
|
53 |
+
<button onclick="generateText()">Generate</button>
|
54 |
+
|
55 |
+
<p id="output"></p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
</body>
|
57 |
</html>
|