-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (102 loc) · 3.46 KB
/
Copy pathscript.js
File metadata and controls
122 lines (102 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const API_URL = "https://arungurajapu-sentiment-analysis.hf.space";
const textModeBtn = document.getElementById("modeTextBtn");
const fileModeBtn = document.getElementById("modeFileBtn");
const textBlock = document.getElementById("textModeBlock");
const fileBlock = document.getElementById("fileModeBlock");
const analyzeBtn = document.getElementById("analyzeBtn");
const statusBox = document.getElementById("status");
const resultsBox = document.getElementById("results");
// -------- Mode Switching ----------
textModeBtn.addEventListener("click", () => {
textModeBtn.classList.add("active");
fileModeBtn.classList.remove("active");
textBlock.classList.remove("hidden");
fileBlock.classList.add("hidden");
});
fileModeBtn.addEventListener("click", () => {
fileModeBtn.classList.add("active");
textModeBtn.classList.remove("active");
fileBlock.classList.remove("hidden");
textBlock.classList.add("hidden");
});
// -------- Analyze Button ----------
analyzeBtn.addEventListener("click", async () => {
resultsBox.classList.remove("hidden");
resultsBox.innerHTML = "";
statusBox.innerHTML = "⏳ Processing...";
// If TEXT MODE
if (textModeBtn.classList.contains("active")) {
await predictText();
}
// If FILE MODE
else {
await predictFile();
}
});
// -------- TEXT MODE ----------
async function predictText() {
const textarea = document.getElementById("inputText");
const texts = textarea.value
.split("\n")
.map(t => t.trim())
.filter(t => t.length > 0);
if (texts.length === 0) {
statusBox.innerHTML = "⚠️ Please enter some text!";
return;
}
try {
const res = await fetch(`${API_URL}/predict_text`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ texts })
});
const data = await res.json();
statusBox.innerHTML = "✅ Completed";
showResults(data.results);
} catch (err) {
statusBox.innerHTML = "❌ Error connecting to API";
}
}
// -------- FILE MODE ----------
async function predictFile() {
const file = document.getElementById("datasetFile").files[0];
const column = document.getElementById("textColumnName").value.trim();
if (!file) {
statusBox.innerHTML = "⚠️ Please upload a file!";
return;
}
if (!column) {
statusBox.innerHTML = "⚠️ Enter text column name!";
return;
}
const form = new FormData();
form.append("file", file);
form.append("text_column", column);
try {
const res = await fetch(`${API_URL}/predict_file`, {
method: "POST",
body: form
});
const data = await res.json();
statusBox.innerHTML = `✅ Processed ${data.row_count} rows`;
showResults(data.predictions);
} catch (err) {
statusBox.innerHTML = "❌ Error processing file";
}
}
// -------- Display Results ----------
function showResults(items) {
resultsBox.innerHTML = items
.map(r => `
<div class="result-item ${r.label === "LABEL_1" ? "positive" : "negative"}">
<div class="result-header">
<span class="label ${r.label === "LABEL_1" ? "positive" : "negative"}">
${r.label === "LABEL_1" ? "Positive 😊" : "Negative 😡"}
</span>
<span class="confidence">${(r.score * 100).toFixed(1)}% confidence</span>
</div>
<div class="result-text">${r.text}</div>
</div>
`)
.join("");
}