krishna195 commited on
Commit
3952763
Β·
verified Β·
1 Parent(s): 1e483c6

Update index.html

Browse files
Files changed (1) hide show
  1. 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>AI Chatbot</title>
7
-
8
- <!-- Import the pipeline from transformers.js -->
9
  <script type="module">
10
- import { pipeline } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.5.4';
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
- <!-- Model Loading Status -->
23
- <div id="loading-status" class="alert alert-warning text-center">⏳ Please wait, model is loading...</div>
24
 
25
- <!-- Chatbot Interface -->
26
- <div id="chat-container" class="p-3" style="height: 70vh; overflow-y: auto; background: #f8f9fa; border-radius: 10px; border: 1px solid #ddd;">
27
- <!-- Chat messages will appear here -->
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
- console.log("Loading text generation model...");
44
- generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M');
45
- console.log("Model loaded successfully!");
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
- // Display message in chat
62
- function displayMessage(message, className) {
63
- const messageElement = document.createElement('div');
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
- displayMessage("Model is still loading. Please wait...", 'bot-message');
85
  return;
86
  }
87
 
 
88
  try {
89
- console.log("Generating response...");
90
- const result = await generator(inputText, { max_new_tokens: 100 });
91
- const generatedText = result[0]?.generated_text || "Sorry, I couldn't generate a response.";
92
- displayMessage("Bot: " + generatedText, 'bot-message');
93
  } catch (error) {
94
- console.error("Error in response generation:", error);
95
- displayMessage("Error in response generation.", 'bot-message');
96
  }
97
  }
98
 
99
- // Load model on page load
100
- window.addEventListener("DOMContentLoaded", initializeModel);
101
  </script>
102
-
103
- <style>
104
- /* Chat message styling */
105
- .user-message {
106
- text-align: right;
107
- color: #004085;
108
- margin: 10px 0;
109
- padding: 10px;
110
- border-radius: 15px;
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>