giftjohnpaul27 commited on
Commit
f0df42e
ยท
verified ยท
1 Parent(s): 64a0e4b

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +82 -77
index.html CHANGED
@@ -1,100 +1,105 @@
 
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>Sentiment AI Analyzer</title>
7
- <script src="https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2"></script>
8
- <link rel="stylesheet" href="style.css">
9
  </head>
10
- <body>
11
 
12
- <div class="container">
 
13
  <h1>๐Ÿง  Sentiment AI Analyzer</h1>
14
- <p>Analyze the emotional tone of your text with AI.</p>
15
 
16
- <textarea id="textInput" placeholder="Enter text to analyze..."></textarea>
 
17
 
18
- <button id="analyzeBtn">Analyze Sentiment</button>
19
-
20
- <div id="status">Initializing AI model...</div>
21
 
 
22
  <div id="result"></div>
23
- </div>
 
 
 
 
 
 
 
 
24
 
25
- <script>
26
  let classifier = null;
27
 
28
  async function loadModel() {
29
- const status = document.getElementById("status");
30
-
31
- try {
32
- status.textContent = "Loading AI model...";
33
- classifier = await window.transformers.pipeline(
34
- "sentiment-analysis",
35
- "Xenova/distilbert-base-uncased-finetuned-sst-2-english"
36
- );
37
-
38
- status.textContent = "AI model ready!";
39
- } catch (error) {
40
- console.error(error);
41
- status.textContent = "Model loading failed. Please refresh.";
42
- }
 
 
43
  }
44
 
45
  async function analyzeText() {
46
- const input = document.getElementById("textInput").value.trim();
47
- const resultDiv = document.getElementById("result");
48
- const status = document.getElementById("status");
49
- const button = document.getElementById("analyzeBtn");
50
-
51
- if (!input) {
52
- resultDiv.innerHTML = "<p>Please enter text.</p>";
53
- return;
54
- }
55
-
56
- if (!classifier) {
57
- resultDiv.innerHTML = "<p>The AI model is still loading.</p>";
58
- return;
59
- }
60
-
61
- button.disabled = true;
62
- status.textContent = "Analyzing text...";
63
-
64
- try {
65
- const predictions = await classifier(input);
66
- const prediction = predictions[0];
67
-
68
- const label = prediction.label.toUpperCase();
69
- const score = (prediction.score * 100).toFixed(2);
70
-
71
- const emoji = label === "POSITIVE" ? "๐Ÿ˜Š" : "๐Ÿ˜ž";
72
-
73
- resultDiv.innerHTML = `
74
- <div class="result-card">
75
- <h2>${emoji} ${label}</h2>
76
- <p><strong>Confidence:</strong> ${score}%</p>
77
- <div class="progress">
78
- <div class="progress-bar" style="width:${score}%"></div>
79
- </div>
80
- </div>
81
- `;
82
-
83
- status.textContent = "Analysis complete!";
84
- } catch (error) {
85
- console.error(error);
86
- resultDiv.innerHTML = "<p>Analysis failed. Please try again.</p>";
87
- status.textContent = "Error.";
88
- }
89
 
90
- button.disabled = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  }
92
 
93
- document.getElementById("analyzeBtn")
94
- .addEventListener("click", analyzeText);
95
 
96
  loadModel();
97
- </script>
98
-
99
  </body>
100
- </html>
 
1
+
2
  <!DOCTYPE html>
3
  <html lang="en">
4
  <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Sentiment AI Analyzer</title>
8
+ <link rel="stylesheet" href="style.css">
 
9
  </head>
 
10
 
11
+ <body>
12
+ <main class="container">
13
  <h1>๐Ÿง  Sentiment AI Analyzer</h1>
14
+ <p>Analyze the emotional tone of your text using AI.</p>
15
 
16
+ <textarea id="textInput" rows="6"
17
+ placeholder="Enter a sentence, review or message..."></textarea>
18
 
19
+ <button id="analyzeBtn" disabled>Loading AI Model...</button>
 
 
20
 
21
+ <p id="status">Initializing AI...</p>
22
  <div id="result"></div>
23
+ </main>
24
+
25
+ <script type="module">
26
+ import { pipeline } from
27
+ "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.2";
28
+
29
+ const status = document.getElementById("status");
30
+ const button = document.getElementById("analyzeBtn");
31
+ const resultDiv = document.getElementById("result");
32
 
 
33
  let classifier = null;
34
 
35
  async function loadModel() {
36
+ try {
37
+ status.textContent = "Downloading AI model. Please wait...";
38
+
39
+ classifier = await pipeline(
40
+ "sentiment-analysis",
41
+ "Xenova/distilbert-base-uncased-finetuned-sst-2-english"
42
+ );
43
+
44
+ status.textContent = "AI model is ready!";
45
+ button.disabled = false;
46
+ button.textContent = "Analyze Sentiment";
47
+
48
+ } catch (error) {
49
+ console.error("Model loading error:", error);
50
+ status.textContent = "Model loading failed: " + error.message;
51
+ }
52
  }
53
 
54
  async function analyzeText() {
55
+ const text = document.getElementById("textInput").value.trim();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ if (!text) {
58
+ resultDiv.innerHTML = "<p>Please enter some text.</p>";
59
+ return;
60
+ }
61
+
62
+ if (!classifier) {
63
+ status.textContent = "The model is not ready yet.";
64
+ return;
65
+ }
66
+
67
+ button.disabled = true;
68
+ status.textContent = "Analyzing...";
69
+ resultDiv.innerHTML = "";
70
+
71
+ try {
72
+ const output = await classifier(text);
73
+ const prediction = output[0];
74
+
75
+ const label = prediction.label.toUpperCase();
76
+ const confidence = (prediction.score * 100).toFixed(2);
77
+
78
+ const emoji = label === "POSITIVE" ? "๐Ÿ˜Š" : "๐Ÿ˜ž";
79
+
80
+ resultDiv.innerHTML = `
81
+ <div class="result-card">
82
+ <h2>${emoji} ${label}</h2>
83
+ <p>Confidence: ${confidence}%</p>
84
+ <div class="progress">
85
+ <div class="progress-bar" style="width:${confidence}%"></div>
86
+ </div>
87
+ </div>
88
+ `;
89
+
90
+ status.textContent = "Analysis completed.";
91
+
92
+ } catch (error) {
93
+ console.error("Analysis error:", error);
94
+ status.textContent = "Analysis failed: " + error.message;
95
+ }
96
+
97
+ button.disabled = false;
98
  }
99
 
100
+ button.addEventListener("click", analyzeText);
 
101
 
102
  loadModel();
103
+ </script>
 
104
  </body>
105
+ </html>